-
Notifications
You must be signed in to change notification settings - Fork 907
1 #942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
1 #942
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: The registration step ( |
||
System.out.println(userService.findByEmail("jan@gmail.com")); | ||
|
||
// 5. LOGIN | ||
System.out.println(authenticationService.login("jan@gmail.com", "haslojana")); | ||
} | ||
} |
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); | ||
} |
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; | ||
|
@@ -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(); | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor issue: The result of |
||
} | ||
} | ||
|
||
@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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
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; | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor issue: The result of |
||
} | ||
} | ||
|
||
@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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
There was a problem hiding this comment.
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.