Skip to content
Open

1 #942

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v2
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn --batch-mode --update-snapshots verify
95 changes: 47 additions & 48 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
package mate.academy;

import java.time.LocalDate;
import java.time.LocalDateTime;
import mate.academy.model.CinemaHall;
import mate.academy.model.Movie;
import mate.academy.model.MovieSession;
import mate.academy.service.CinemaHallService;
import mate.academy.service.MovieService;
import mate.academy.service.MovieSessionService;
import mate.academy.lib.Injector;
import mate.academy.model.User;
import mate.academy.service.AuthenticationService;
import mate.academy.service.UserService;

public class Main {
private static final Injector injector = Injector.getInstance("mate.academy");

public static void main(String[] args) {
MovieService movieService = null;

Movie fastAndFurious = new Movie("Fast and Furious");
fastAndFurious.setDescription("An action film about street racing, heists, and spies.");
movieService.add(fastAndFurious);
System.out.println(movieService.get(fastAndFurious.getId()));
movieService.getAll().forEach(System.out::println);

CinemaHall firstCinemaHall = new CinemaHall();
firstCinemaHall.setCapacity(100);
firstCinemaHall.setDescription("first hall with capacity 100");

CinemaHall secondCinemaHall = new CinemaHall();
secondCinemaHall.setCapacity(200);
secondCinemaHall.setDescription("second hall with capacity 200");

CinemaHallService cinemaHallService = null;
cinemaHallService.add(firstCinemaHall);
cinemaHallService.add(secondCinemaHall);

System.out.println(cinemaHallService.getAll());
System.out.println(cinemaHallService.get(firstCinemaHall.getId()));

MovieSession tomorrowMovieSession = new MovieSession();
tomorrowMovieSession.setCinemaHall(firstCinemaHall);
tomorrowMovieSession.setMovie(fastAndFurious);
tomorrowMovieSession.setShowTime(LocalDateTime.now().plusDays(1L));

MovieSession yesterdayMovieSession = new MovieSession();
yesterdayMovieSession.setCinemaHall(firstCinemaHall);
yesterdayMovieSession.setMovie(fastAndFurious);
yesterdayMovieSession.setShowTime(LocalDateTime.now().minusDays(1L));

MovieSessionService movieSessionService = null;
movieSessionService.add(tomorrowMovieSession);
movieSessionService.add(yesterdayMovieSession);

System.out.println(movieSessionService.get(yesterdayMovieSession.getId()));
System.out.println(movieSessionService.findAvailableSessions(
fastAndFurious.getId(), LocalDate.now()));

// 0. INJECTING
final var userService
= (UserService) injector.getInstance(UserService.class);
final var authenticationService
= (AuthenticationService) injector.getInstance(AuthenticationService.class);

// 1. CREATING
// User
User user1 = new User();
user1.setEmail("user1@gmail.com");
user1.setPassword("password");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Passwords are being set directly on the User objects without hashing or salting. According to the checklist, passwords must be hashed and salted using HashUtil before being stored. Please update this logic to ensure password security.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Passwords are being set directly on the User object without hashing or salting. According to the checklist, passwords must be hashed and salted before storage. Please use the HashUtil (or equivalent) to hash passwords before calling userService.add(user).


User user2 = new User();
user2.setEmail("user2@gmail.com");
user2.setPassword("password");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Passwords are being set directly on the User objects without hashing or salting. As per the checklist, use HashUtil to hash and salt passwords before storing them.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Passwords are being set directly on the User object without hashing or salting. Please use the password hashing utility before saving the user, as required by the checklist.


User user3 = new User();
user3.setEmail("user3@gmail.com");
user3.setPassword("password");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Passwords are being set directly on the User objects without hashing or salting. Please use HashUtil to hash and salt passwords before storing them, as required by the checklist.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Passwords are being set directly on the User object without hashing or salting. Please use the password hashing utility before saving the user, as required by the checklist.


// 2. ADDING
// User
userService.add(user1);
userService.add(user2);
userService.add(user3);

// 3. GETTING BY EMAIL
// User
System.out.println("\n *** CREATE AND READ FUNCTIONS TEST *** \n");
System.out.println(userService.findByEmail("user1@gmail.com"));
System.out.println(userService.findByEmail("user2@gmail.com"));
System.out.println(userService.findByEmail("user3@gmail.com"));

// 4. REGISTRATION
// Correct
authenticationService.registerUser("jan@gmail.com", "haslojana");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The registration step (authenticationService.registerUser) appears to store the password in plain text. According to typical security requirements and likely the checklist, passwords should be hashed and salted during registration. Please verify that your AuthenticationService implementation hashes and salts passwords before storing them, as this is critical for secure authentication.

System.out.println(userService.findByEmail("jan@gmail.com"));

// 5. LOGIN
System.out.println(authenticationService.login("jan@gmail.com", "haslojana"));
}
}
4 changes: 4 additions & 0 deletions src/main/java/mate/academy/dao/CinemaHallDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public interface CinemaHallDao {
Optional<CinemaHall> get(Long id);

List<CinemaHall> getAll();

boolean update(CinemaHall cinemaHall);

boolean delete(Long id);
}
4 changes: 4 additions & 0 deletions src/main/java/mate/academy/dao/MovieDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public interface MovieDao {
Optional<Movie> get(Long id);

List<Movie> getAll();

boolean update(Movie movie);

boolean delete(Long id);
}
8 changes: 6 additions & 2 deletions src/main/java/mate/academy/dao/MovieSessionDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import mate.academy.model.MovieSession;

public interface MovieSessionDao {
List<MovieSession> findAvailableSessions(Long movieId, LocalDate date);
MovieSession add(MovieSession movieSession);

Optional<MovieSession> get(Long id);

MovieSession add(MovieSession session);
List<MovieSession> findAvailableSessions(Long movieId, LocalDate date);

boolean update(MovieSession movieSession);

boolean delete(Long id);
}
14 changes: 14 additions & 0 deletions src/main/java/mate/academy/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mate.academy.dao;

import java.util.Optional;
import mate.academy.model.User;

public interface UserDao {
User add(User user);

Optional<User> get(String email);

User update(User user);

boolean delete(String email);
}
63 changes: 48 additions & 15 deletions src/main/java/mate/academy/dao/impl/CinemaHallDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mate.academy.dao.impl;

import jakarta.persistence.criteria.CriteriaQuery;
import java.util.List;
import java.util.Optional;
import mate.academy.dao.CinemaHallDao;
Expand All @@ -9,16 +8,15 @@
import mate.academy.model.CinemaHall;
import mate.academy.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

@Dao
public class CinemaHallDaoImpl implements CinemaHallDao {
@Override
public CinemaHall add(CinemaHall cinemaHall) {
Session session = null;
Transaction transaction = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
transaction = session.beginTransaction();
session.persist(cinemaHall);
transaction.commit();
Expand All @@ -27,11 +25,7 @@ public CinemaHall add(CinemaHall cinemaHall) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Can't insert cinema hall: " + cinemaHall, e);
} finally {
if (session != null) {
session.close();
}
throw new DataProcessingException("Can't insert cinemaHall " + cinemaHall, e);
}
}

Expand All @@ -40,19 +34,58 @@ public Optional<CinemaHall> get(Long id) {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
return Optional.ofNullable(session.get(CinemaHall.class, id));
} catch (Exception e) {
throw new DataProcessingException("Can't get a cinema hall by id: " + id, e);
throw new DataProcessingException("Can't get a cinemaHall by id: " + id, e);
}
}

@Override
public List<CinemaHall> getAll() {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
CriteriaQuery<CinemaHall> criteriaQuery = session.getCriteriaBuilder()
.createQuery(CinemaHall.class);
criteriaQuery.from(CinemaHall.class);
return session.createQuery(criteriaQuery).getResultList();
return session.createQuery("from CinemaHall").list();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The query in getAll() should specify the result type for type safety. Consider using: session.createQuery("from CinemaHall", CinemaHall.class).list();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The query in getAll() should specify the result type to avoid unchecked cast warnings. Consider using: session.createQuery("from CinemaHall", CinemaHall.class).list();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor issue: The result of session.createQuery("from CinemaHall").list(); is not type-safe and may produce unchecked warnings. Consider using a typed query: session.createQuery("from CinemaHall", CinemaHall.class).getResultList(); for better type safety. This is not critical but improves code quality.

}
}

@Override
public boolean update(CinemaHall cinemaHall) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Transaction transaction = null;

try (Session session = sessionFactory.openSession()) {
transaction = session.beginTransaction();
session.update(cinemaHall);
transaction.commit();
return true;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Update of cinema hall " + cinemaHall + " failed", e);
}
}

@Override
public boolean delete(Long id) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = null;
Transaction transaction = null;

try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.delete(get(id)
.orElseThrow(() -> new RuntimeException("no object with id " + id)));
Comment on lines +75 to +76

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: In the delete() method, throwing a generic RuntimeException when the entity is not found may not provide enough context. Consider using a more specific exception or handling this case more gracefully if required by your checklist or standards.

transaction.commit();
} catch (Exception e) {
throw new DataProcessingException("Can't get all cinema halls", e);
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("removing movie with id: "
+ id + " from database failed", e);
} finally {
if (session != null) {
session.close();
}
}
return get(id).isEmpty();
}
}
53 changes: 40 additions & 13 deletions src/main/java/mate/academy/dao/impl/MovieDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mate.academy.dao.impl;

import jakarta.persistence.criteria.CriteriaQuery;
import java.util.List;
import java.util.Optional;
import mate.academy.dao.MovieDao;
Expand All @@ -9,16 +8,15 @@
import mate.academy.model.Movie;
import mate.academy.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

@Dao
public class MovieDaoImpl implements MovieDao {
@Override
public Movie add(Movie movie) {
Transaction transaction = null;
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
transaction = session.beginTransaction();
session.persist(movie);
transaction.commit();
Expand All @@ -28,10 +26,6 @@ public Movie add(Movie movie) {
transaction.rollback();
}
throw new DataProcessingException("Can't insert movie " + movie, e);
} finally {
if (session != null) {
session.close();
}
}
}

Expand All @@ -47,12 +41,45 @@ public Optional<Movie> get(Long id) {
@Override
public List<Movie> getAll() {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
CriteriaQuery<Movie> criteriaQuery = session.getCriteriaBuilder()
.createQuery(Movie.class);
criteriaQuery.from(Movie.class);
return session.createQuery(criteriaQuery).getResultList();
return session.createQuery("from Movie").list();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The query in getAll() should specify the result type for type safety. Consider using: session.createQuery("from Movie", Movie.class).list();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The query in getAll() should specify the result type to avoid unchecked cast warnings. Consider using: session.createQuery("from Movie", Movie.class).list();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor issue: The result of session.createQuery("from Movie").list(); is not type-safe and may produce unchecked warnings. Consider using a typed query: session.createQuery("from Movie", Movie.class).getResultList(); for better type safety. This is not critical but improves code quality.

}
}

@Override
public boolean update(Movie movie) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Transaction transaction = null;

try (Session session = sessionFactory.openSession()) {
transaction = session.beginTransaction();
session.update(movie);
transaction.commit();
return true;
} catch (Exception e) {
throw new DataProcessingException("Can't get all movies", e);
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Updating " + movie + " failed", e);
}
}

@Override
public boolean delete(Long id) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

Transaction transaction = null;
try (Session session = sessionFactory.openSession()) {
transaction = session.beginTransaction();
session.delete(get(id)
.orElseThrow(() -> new RuntimeException("no object with id " + id)));
Comment on lines +73 to +74

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: In the delete() method, throwing a generic RuntimeException when the entity is not found may not provide enough context. Consider using a more specific exception or handling this case more gracefully if required by your checklist or standards.

transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("removing movie with id: "
+ id + " from database failed", e);
}
return get(id).isEmpty();
}
}
Loading