From a252b18bcfe6c38d560593e15d49c8f4f37c9ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Sat, 7 Dec 2024 20:55:45 +0400 Subject: [PATCH] feat: add services for all models: training, examresults, exam for training, orderfix: repositories and models --- app/src/main/java/putBit/app/models/User.java | 25 +++++ .../app/repositories/ExamRepository.java | 5 + .../app/repositories/OrderRepository.java | 6 +- .../repositories/TrainingExamRepository.java | 4 +- .../app/repositories/TrainingRepository.java | 2 +- .../app/services/ExamResultService.java | 53 +++++++++++ .../java/putBit/app/services/ExamService.java | 6 ++ .../putBit/app/services/OrderService.java | 91 +++++++++++++++++++ .../app/services/TrainingExamService.java | 53 +++++++++++ .../putBit/app/services/TrainingService.java | 62 +++++++++++++ .../java/putBit/app/services/UserService.java | 49 +++++----- 11 files changed, 328 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/putBit/app/services/ExamResultService.java create mode 100644 app/src/main/java/putBit/app/services/OrderService.java create mode 100644 app/src/main/java/putBit/app/services/TrainingExamService.java create mode 100644 app/src/main/java/putBit/app/services/TrainingService.java diff --git a/app/src/main/java/putBit/app/models/User.java b/app/src/main/java/putBit/app/models/User.java index f9091a6..76e33e5 100644 --- a/app/src/main/java/putBit/app/models/User.java +++ b/app/src/main/java/putBit/app/models/User.java @@ -64,12 +64,37 @@ public class User { public void addAchievement(Achievement achievement){ achievements.add(achievement); } + public void deleteAchievement(Achievement achievement){ + for(Achievement ach: + achievements) + { + if (ach.getId() == achievement.getId()) { + achievements.remove(ach); + break; + } + + } + + } public void clearAchievements(){ achievements.clear(); } public void addBenefit(Benefit benefit){ benefits.add(benefit); } + public void deleteBenefit(Benefit benefit){ + for(Benefit ben: + benefits) + { + if (ben.getId() == benefit.getId()) { + benefits.remove(ben); + break; + } + + } + + } + public void clearBenefit(){ benefits.clear(); } diff --git a/app/src/main/java/putBit/app/repositories/ExamRepository.java b/app/src/main/java/putBit/app/repositories/ExamRepository.java index 590956b..91035e7 100644 --- a/app/src/main/java/putBit/app/repositories/ExamRepository.java +++ b/app/src/main/java/putBit/app/repositories/ExamRepository.java @@ -15,4 +15,9 @@ public interface ExamRepository extends JpaRepository { "where e.id not in (select er.exam_id from exam_results er " + "where er.user_id = :id )", nativeQuery = true) Optional> findByUserIdNot(@Param("id") int id); + + @Query(value = "select e.* from exams e " + + "where e.id not in (select te.exam_id from training_exams te " + + "where te.training_id = :id )", nativeQuery = true) + Optional> findByTrainingIdNot(@Param("id") int id); } diff --git a/app/src/main/java/putBit/app/repositories/OrderRepository.java b/app/src/main/java/putBit/app/repositories/OrderRepository.java index 80aaf6e..24c4a20 100644 --- a/app/src/main/java/putBit/app/repositories/OrderRepository.java +++ b/app/src/main/java/putBit/app/repositories/OrderRepository.java @@ -10,11 +10,11 @@ import putBit.app.models.User; import java.util.List; import java.util.Optional; -public interface OrderRepository extends JpaRepository { +public interface OrderRepository extends JpaRepository { @Query(value = "select o.* from orders where " + "o.user_id = (select u.id from users u where " + - "u.name LIKE %:name% or u.snils like %:snils%) " , nativeQuery = true) - Optional> findByUserInfo(@Param("snils")String snils, @Param("name")String name); + "u.name LIKE %:value% or u.snils like %:value%) " , nativeQuery = true) + Optional> findByUserInfo(@Param("value")String value); Optional> findByBenefit(Boolean benefit); diff --git a/app/src/main/java/putBit/app/repositories/TrainingExamRepository.java b/app/src/main/java/putBit/app/repositories/TrainingExamRepository.java index 3376040..e875ae6 100644 --- a/app/src/main/java/putBit/app/repositories/TrainingExamRepository.java +++ b/app/src/main/java/putBit/app/repositories/TrainingExamRepository.java @@ -3,6 +3,7 @@ package putBit.app.repositories; import org.springframework.data.jpa.repository.JpaRepository; import putBit.app.models.Benefit; import putBit.app.models.Order; +import putBit.app.models.Training; import putBit.app.models.TrainingExam; import java.util.List; @@ -10,7 +11,6 @@ import java.util.Optional; public interface TrainingExamRepository extends JpaRepository { - Optional> findExamsByTrainingId(int userId); + Optional> findByTraining(Training training); - Optional> findExamsByTrainingIdNot(int userId); } diff --git a/app/src/main/java/putBit/app/repositories/TrainingRepository.java b/app/src/main/java/putBit/app/repositories/TrainingRepository.java index 22edf33..6bc8800 100644 --- a/app/src/main/java/putBit/app/repositories/TrainingRepository.java +++ b/app/src/main/java/putBit/app/repositories/TrainingRepository.java @@ -9,5 +9,5 @@ import java.util.Optional; public interface TrainingRepository extends JpaRepository { - Optional> findByNumContainingOrTitleContaining(String num, String title); + Optional> findByTitleContaining(String title); } diff --git a/app/src/main/java/putBit/app/services/ExamResultService.java b/app/src/main/java/putBit/app/services/ExamResultService.java new file mode 100644 index 0000000..cb6d739 --- /dev/null +++ b/app/src/main/java/putBit/app/services/ExamResultService.java @@ -0,0 +1,53 @@ +package putBit.app.services; + +import lombok.RequiredArgsConstructor; +import org.hibernate.sql.exec.ExecutionException; +import org.springframework.stereotype.Service; +import putBit.app.models.Exam; +import putBit.app.models.ExamResult; +import putBit.app.models.User; +import putBit.app.repositories.ExamResultRepository; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class ExamResultService { + private final ExamResultRepository examResultRepository; + private final UserService userService; + private final ExamService examService; + public ExamResult create(ExamResult examResult){ + User user = userService.findById(examResult.getUser().getId()); + Exam exam = examService.findById(examResult.getExam().getId()); + ExamResult newExam = ExamResult.builder() + .user(user) + .exam(exam) + .points(examResult.getPoints()) + .build(); + return examResultRepository.save(newExam); + } + + public ExamResult findById(int id){ + return examResultRepository.findById(id) + .orElseThrow(() -> new ExecutionException("Экзамен не найден")); + } + + public ExamResult update(ExamResult examResult) + { + ExamResult currentExam = findById(examResult.getId()); + + currentExam.setPoints(examResult.getPoints()); + return examResultRepository.save(currentExam); + } + + public ExamResult delete(ExamResult exam){ + ExamResult currentExam = findById(exam.getId()); + examResultRepository.delete(currentExam); + return currentExam; + } + public List findByUser(User user){ + User currentUser = userService.findById(user.getId()); + return examResultRepository.findByUser(currentUser) + .orElseThrow(() -> new ExecutionException("Экзамен не найден")); + } +} diff --git a/app/src/main/java/putBit/app/services/ExamService.java b/app/src/main/java/putBit/app/services/ExamService.java index c6ae54f..6f06bad 100644 --- a/app/src/main/java/putBit/app/services/ExamService.java +++ b/app/src/main/java/putBit/app/services/ExamService.java @@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor; import org.hibernate.sql.exec.ExecutionException; import org.springframework.stereotype.Service; import putBit.app.models.Exam; +import putBit.app.models.Training; import putBit.app.models.User; import putBit.app.models.enums.Role; import putBit.app.repositories.ExamRepository; @@ -49,4 +50,9 @@ public class ExamService { .orElseThrow(() -> new ExecutionException("Экзамен не найден")); } + public List findNotTraining(Training training){ + return examRepository.findByTrainingIdNot(training.getId()) + .orElseThrow(() -> new ExecutionException("Экзамен не найден")); + } + } diff --git a/app/src/main/java/putBit/app/services/OrderService.java b/app/src/main/java/putBit/app/services/OrderService.java new file mode 100644 index 0000000..f3e733b --- /dev/null +++ b/app/src/main/java/putBit/app/services/OrderService.java @@ -0,0 +1,91 @@ +package putBit.app.services; + +import lombok.RequiredArgsConstructor; +import org.aspectj.weaver.ast.Or; +import org.hibernate.service.NullServiceException; +import org.hibernate.sql.exec.ExecutionException; +import org.springframework.stereotype.Service; +import putBit.app.models.*; +import putBit.app.repositories.OrderRepository; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class OrderService { + + private final UserService userService; + private final TrainingService trainingService; + private final OrderRepository orderRepository; + + public Order create(Order order){ + User user = userService.findById(order.getUser().getId()); + Training training = trainingService.findById(order.getTraining().getId()); + int limit = 0; + for(TrainingExam exam: + training.getExams()) + { + boolean check = false; + for(ExamResult result: + user.getExams()) + { + if(result.getExam().getId() == exam.getExam().getId()) + { + check = true; + break; + } + } + if(!check) + return null; + + } + for(TrainingExam exam: + training.getExams()) + { + limit+=exam.getPoints(); + } + int userPoints=0; + for(ExamResult result: + user.getExams()) + { + userPoints+= result.getPoints(); + } + if(limit>userPoints) + return null; + + Order newOrder = Order.builder() + .user(user) + .training(training) + .benefit(!user.getBenefits().isEmpty()) + .build(); + + return orderRepository.save(newOrder); + } + + public Order findById(Order order){ + return orderRepository.findById(order.getId()) + .orElseThrow(() -> new ExecutionException("Заявка не найдена")); + } + + public Order delete(Order order){ + Order currentOrder = findById(order); + orderRepository.delete(currentOrder); + return currentOrder; + + } + + public List findForBenefit(){ + return orderRepository.findByBenefit(true) + .orElseThrow(() -> new ExecutionException("Заявка не найдена")); + } + + public List findForBasic(){ + return orderRepository.findByBenefit(false) + .orElseThrow(() -> new ExecutionException("Заявка не найдена")); + } + + public List findByNameOrSnils(String text){ + return orderRepository.findByUserInfo(text) + .orElseThrow(() -> new ExecutionException("Заявка не найдена")); + } +} diff --git a/app/src/main/java/putBit/app/services/TrainingExamService.java b/app/src/main/java/putBit/app/services/TrainingExamService.java new file mode 100644 index 0000000..eba7656 --- /dev/null +++ b/app/src/main/java/putBit/app/services/TrainingExamService.java @@ -0,0 +1,53 @@ +package putBit.app.services; + +import lombok.RequiredArgsConstructor; +import org.hibernate.sql.exec.ExecutionException; +import org.springframework.stereotype.Service; +import putBit.app.models.*; +import putBit.app.repositories.TrainingExamRepository; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class TrainingExamService { + private final TrainingExamRepository trainingExamRepository; + private final ExamService examService; + private final TrainingService trainingService; + + public TrainingExam create(TrainingExam trainingExam){ + Training training = trainingService.findById(trainingExam.getTraining().getId()); + Exam exam = examService.findById(trainingExam.getExam().getId()); + TrainingExam newExam = TrainingExam.builder() + .training(training) + .exam(exam) + .points(trainingExam.getPoints()) + .build(); + return trainingExamRepository.save(newExam); + } + + public TrainingExam findById(int id){ + return trainingExamRepository.findById(id) + .orElseThrow(() -> new ExecutionException("Экзамен не найден")); + } + + public TrainingExam update(TrainingExam trainingExam) + { + TrainingExam currentExam = findById(trainingExam.getId()); + + currentExam.setPoints(trainingExam.getPoints()); + return trainingExamRepository.save(currentExam); + } + + public TrainingExam delete(TrainingExam exam){ + TrainingExam currentExam = findById(exam.getId()); + trainingExamRepository.delete(currentExam); + return currentExam; + } + public List findByTraining(Training training){ + Training currentTraining = trainingService.findById(training.getId()); + return trainingExamRepository.findByTraining(currentTraining) + .orElseThrow(() -> new ExecutionException("Экзамены не найден")); + } + +} diff --git a/app/src/main/java/putBit/app/services/TrainingService.java b/app/src/main/java/putBit/app/services/TrainingService.java new file mode 100644 index 0000000..504303f --- /dev/null +++ b/app/src/main/java/putBit/app/services/TrainingService.java @@ -0,0 +1,62 @@ +package putBit.app.services; + +import lombok.RequiredArgsConstructor; +import org.hibernate.sql.exec.ExecutionException; +import org.springframework.stereotype.Service; +import putBit.app.models.Exam; +import putBit.app.models.Training; +import putBit.app.models.User; +import putBit.app.repositories.TrainingRepository; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class TrainingService { + private final TrainingRepository trainingRepository; + + public Training create(Training training){ + Training newTraining = Training.builder() + .title(training.getTitle()) + .num(training.getNum()) + .desc(training.getDesc()) + .prof(training.getProf()) + .basic_places(training.getBasic_places()) + .benefit_places(training.getBenefit_places()) + .build(); + return trainingRepository.save(newTraining); + } + + public Training findById(int id){ + return trainingRepository.findById(id) + .orElseThrow(() -> new ExecutionException("Направление не найдено")); + } + + public Training update(Training training) + { + Training currentTraining = findById(training.getId()); + + currentTraining.setTitle(training.getTitle()); + currentTraining.setNum(training.getNum()); + currentTraining.setDesc(training.getDesc()); + currentTraining.setProf(training.getProf()); + currentTraining.setBasic_places(training.getBasic_places()); + currentTraining.setBenefit_places(training.getBenefit_places()); + return trainingRepository.save(currentTraining); + } + + public Training delete(Training training){ + Training currentTraining = findById(training.getId()); + trainingRepository.delete(currentTraining); + return currentTraining; + } + + public List findAll(){ + return trainingRepository.findAll(); + } + + public List findByTitle(String title){ + return trainingRepository.findByTitleContaining(title) + .orElseThrow(() -> new ExecutionException("Направление не найдено")); + } +} diff --git a/app/src/main/java/putBit/app/services/UserService.java b/app/src/main/java/putBit/app/services/UserService.java index eff6782..dd85f10 100644 --- a/app/src/main/java/putBit/app/services/UserService.java +++ b/app/src/main/java/putBit/app/services/UserService.java @@ -54,28 +54,33 @@ public class UserService { currentUser.setPhone(user.getPhone()); if(!user.getSnils().isEmpty() && !currentUser.getSnils().equals(user.getSnils())) currentUser.setSnils(user.getSnils()); - if(!user.getAchievements().isEmpty()) - { - for(Achievement ach: - user.getAchievements() - ){ - Achievement newAch = achievementService.findById(ach.getId()); - currentUser.addAchievement(newAch); - } - } - else - currentUser.clearAchievements(); - if(!user.getBenefits().isEmpty()) - { - for(Benefit benefit: - user.getBenefits() - ){ - Benefit newBenefit = benefitService.findById(benefit.getId()); - currentUser.addBenefit(newBenefit); - } - } - else - currentUser.clearBenefit(); + + return userRepository.save(currentUser); + } + public User addAchievement(User user,Achievement achievement){ + User currentUser = findById(user.getId()); + Achievement newAch = achievementService.findById(achievement.getId()); + currentUser.addAchievement(newAch); + return userRepository.save(currentUser); + } + public User addBenefit(User user,Benefit benefit){ + User currentUser = findById(user.getId()); + Benefit newBen = benefitService.findById(benefit.getId()); + currentUser.addBenefit(newBen); + return userRepository.save(currentUser); + } + + public User removeAchievement(User user,Achievement achievement){ + User currentUser = findById(user.getId()); + Achievement newAch = achievementService.findById(achievement.getId()); + currentUser.deleteAchievement(newAch); + return userRepository.save(currentUser); + } + + public User removeBenefit(User user,Benefit benefit){ + User currentUser = findById(user.getId()); + Benefit newBen = benefitService.findById(benefit.getId()); + currentUser.deleteBenefit(newBen); return userRepository.save(currentUser); } }