feat: add services for all models: training, examresults, exam for training, orderfix: repositories and models
This commit is contained in:
parent
1b17c56fa9
commit
a252b18bcf
@ -64,12 +64,37 @@ public class User {
|
|||||||
public void addAchievement(Achievement achievement){
|
public void addAchievement(Achievement achievement){
|
||||||
achievements.add(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(){
|
public void clearAchievements(){
|
||||||
achievements.clear();
|
achievements.clear();
|
||||||
}
|
}
|
||||||
public void addBenefit(Benefit benefit){
|
public void addBenefit(Benefit benefit){
|
||||||
benefits.add(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(){
|
public void clearBenefit(){
|
||||||
benefits.clear();
|
benefits.clear();
|
||||||
}
|
}
|
||||||
|
@ -15,4 +15,9 @@ public interface ExamRepository extends JpaRepository<Exam, Integer> {
|
|||||||
"where e.id not in (select er.exam_id from exam_results er " +
|
"where e.id not in (select er.exam_id from exam_results er " +
|
||||||
"where er.user_id = :id )", nativeQuery = true)
|
"where er.user_id = :id )", nativeQuery = true)
|
||||||
Optional<List<Exam>> findByUserIdNot(@Param("id") int id);
|
Optional<List<Exam>> 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<List<Exam>> findByTrainingIdNot(@Param("id") int id);
|
||||||
}
|
}
|
||||||
|
@ -10,11 +10,11 @@ import putBit.app.models.User;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface OrderRepository extends JpaRepository<Order, User> {
|
public interface OrderRepository extends JpaRepository<Order, Integer> {
|
||||||
@Query(value = "select o.* from orders where " +
|
@Query(value = "select o.* from orders where " +
|
||||||
"o.user_id = (select u.id from users u where " +
|
"o.user_id = (select u.id from users u where " +
|
||||||
"u.name LIKE %:name% or u.snils like %:snils%) " , nativeQuery = true)
|
"u.name LIKE %:value% or u.snils like %:value%) " , nativeQuery = true)
|
||||||
Optional<List<Order>> findByUserInfo(@Param("snils")String snils, @Param("name")String name);
|
Optional<List<Order>> findByUserInfo(@Param("value")String value);
|
||||||
|
|
||||||
|
|
||||||
Optional<List<Order>> findByBenefit(Boolean benefit);
|
Optional<List<Order>> findByBenefit(Boolean benefit);
|
||||||
|
@ -3,6 +3,7 @@ package putBit.app.repositories;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import putBit.app.models.Benefit;
|
import putBit.app.models.Benefit;
|
||||||
import putBit.app.models.Order;
|
import putBit.app.models.Order;
|
||||||
|
import putBit.app.models.Training;
|
||||||
import putBit.app.models.TrainingExam;
|
import putBit.app.models.TrainingExam;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -10,7 +11,6 @@ import java.util.Optional;
|
|||||||
|
|
||||||
public interface TrainingExamRepository extends JpaRepository<TrainingExam, Integer> {
|
public interface TrainingExamRepository extends JpaRepository<TrainingExam, Integer> {
|
||||||
|
|
||||||
Optional<List<TrainingExam>> findExamsByTrainingId(int userId);
|
Optional<List<TrainingExam>> findByTraining(Training training);
|
||||||
|
|
||||||
Optional<List<TrainingExam>> findExamsByTrainingIdNot(int userId);
|
|
||||||
}
|
}
|
||||||
|
@ -9,5 +9,5 @@ import java.util.Optional;
|
|||||||
|
|
||||||
public interface TrainingRepository extends JpaRepository<Training, Integer> {
|
public interface TrainingRepository extends JpaRepository<Training, Integer> {
|
||||||
|
|
||||||
Optional<List<Training>> findByNumContainingOrTitleContaining(String num, String title);
|
Optional<List<Training>> findByTitleContaining(String title);
|
||||||
}
|
}
|
||||||
|
53
app/src/main/java/putBit/app/services/ExamResultService.java
Normal file
53
app/src/main/java/putBit/app/services/ExamResultService.java
Normal file
@ -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<ExamResult> findByUser(User user){
|
||||||
|
User currentUser = userService.findById(user.getId());
|
||||||
|
return examResultRepository.findByUser(currentUser)
|
||||||
|
.orElseThrow(() -> new ExecutionException("Экзамен не найден"));
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.hibernate.sql.exec.ExecutionException;
|
import org.hibernate.sql.exec.ExecutionException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import putBit.app.models.Exam;
|
import putBit.app.models.Exam;
|
||||||
|
import putBit.app.models.Training;
|
||||||
import putBit.app.models.User;
|
import putBit.app.models.User;
|
||||||
import putBit.app.models.enums.Role;
|
import putBit.app.models.enums.Role;
|
||||||
import putBit.app.repositories.ExamRepository;
|
import putBit.app.repositories.ExamRepository;
|
||||||
@ -49,4 +50,9 @@ public class ExamService {
|
|||||||
.orElseThrow(() -> new ExecutionException("Экзамен не найден"));
|
.orElseThrow(() -> new ExecutionException("Экзамен не найден"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Exam> findNotTraining(Training training){
|
||||||
|
return examRepository.findByTrainingIdNot(training.getId())
|
||||||
|
.orElseThrow(() -> new ExecutionException("Экзамен не найден"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
91
app/src/main/java/putBit/app/services/OrderService.java
Normal file
91
app/src/main/java/putBit/app/services/OrderService.java
Normal file
@ -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<Order> findForBenefit(){
|
||||||
|
return orderRepository.findByBenefit(true)
|
||||||
|
.orElseThrow(() -> new ExecutionException("Заявка не найдена"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Order> findForBasic(){
|
||||||
|
return orderRepository.findByBenefit(false)
|
||||||
|
.orElseThrow(() -> new ExecutionException("Заявка не найдена"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Order> findByNameOrSnils(String text){
|
||||||
|
return orderRepository.findByUserInfo(text)
|
||||||
|
.orElseThrow(() -> new ExecutionException("Заявка не найдена"));
|
||||||
|
}
|
||||||
|
}
|
@ -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<TrainingExam> findByTraining(Training training){
|
||||||
|
Training currentTraining = trainingService.findById(training.getId());
|
||||||
|
return trainingExamRepository.findByTraining(currentTraining)
|
||||||
|
.orElseThrow(() -> new ExecutionException("Экзамены не найден"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
62
app/src/main/java/putBit/app/services/TrainingService.java
Normal file
62
app/src/main/java/putBit/app/services/TrainingService.java
Normal file
@ -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<Training> findAll(){
|
||||||
|
return trainingRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Training> findByTitle(String title){
|
||||||
|
return trainingRepository.findByTitleContaining(title)
|
||||||
|
.orElseThrow(() -> new ExecutionException("Направление не найдено"));
|
||||||
|
}
|
||||||
|
}
|
@ -54,28 +54,33 @@ public class UserService {
|
|||||||
currentUser.setPhone(user.getPhone());
|
currentUser.setPhone(user.getPhone());
|
||||||
if(!user.getSnils().isEmpty() && !currentUser.getSnils().equals(user.getSnils()))
|
if(!user.getSnils().isEmpty() && !currentUser.getSnils().equals(user.getSnils()))
|
||||||
currentUser.setSnils(user.getSnils());
|
currentUser.setSnils(user.getSnils());
|
||||||
if(!user.getAchievements().isEmpty())
|
|
||||||
{
|
return userRepository.save(currentUser);
|
||||||
for(Achievement ach:
|
}
|
||||||
user.getAchievements()
|
public User addAchievement(User user,Achievement achievement){
|
||||||
){
|
User currentUser = findById(user.getId());
|
||||||
Achievement newAch = achievementService.findById(ach.getId());
|
Achievement newAch = achievementService.findById(achievement.getId());
|
||||||
currentUser.addAchievement(newAch);
|
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);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
currentUser.clearAchievements();
|
public User removeAchievement(User user,Achievement achievement){
|
||||||
if(!user.getBenefits().isEmpty())
|
User currentUser = findById(user.getId());
|
||||||
{
|
Achievement newAch = achievementService.findById(achievement.getId());
|
||||||
for(Benefit benefit:
|
currentUser.deleteAchievement(newAch);
|
||||||
user.getBenefits()
|
return userRepository.save(currentUser);
|
||||||
){
|
|
||||||
Benefit newBenefit = benefitService.findById(benefit.getId());
|
|
||||||
currentUser.addBenefit(newBenefit);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
public User removeBenefit(User user,Benefit benefit){
|
||||||
currentUser.clearBenefit();
|
User currentUser = findById(user.getId());
|
||||||
|
Benefit newBen = benefitService.findById(benefit.getId());
|
||||||
|
currentUser.deleteBenefit(newBen);
|
||||||
return userRepository.save(currentUser);
|
return userRepository.save(currentUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user