feat: add classes for controllers of models, add class for send email. fix: services

This commit is contained in:
Володя 2024-12-10 00:11:12 +04:00
parent a2ff138e7f
commit e2b185804e
37 changed files with 2346 additions and 467 deletions

@ -61,6 +61,13 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>

@ -1,11 +1,15 @@
package putBit.app;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import putBit.app.Timer.myTimer;
import putBit.app.services.OrderService;
@SpringBootApplication
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}

@ -0,0 +1,35 @@
package putBit.app.Timer;
import jakarta.mail.MessagingException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import putBit.app.models.Order;
import putBit.app.services.OrderService;
import java.util.TimerTask;
@Data
@AllArgsConstructor
@ComponentScan
public class Task extends TimerTask {
@Autowired
private OrderService orderService;
@Override
public void run() {
try {
System.out.println("START");
orderService.workWithStudents();
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}

@ -0,0 +1,47 @@
package putBit.app.Timer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import putBit.app.services.OrderService;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.util.Date;
import java.util.Timer;
@Data
public final class myTimer {
private static myTimer INSTANCE;
private boolean closed = false;
private LocalDateTime date = LocalDateTime.of(2024, Month.DECEMBER,11, 0,0,0);
private OrderService orderService;
private myTimer(OrderService orderService) {
this.orderService = orderService;
System.out.println("create timer");
LocalDateTime later = LocalDateTime.now().plusDays(10);
Date LaterAsDate = Date.from(later.atZone(ZoneId.systemDefault()).toInstant());
new Timer().schedule(new Task(orderService), LaterAsDate);
}
public static myTimer getInstance(OrderService orderService) {
if(INSTANCE == null) {
INSTANCE = new myTimer(orderService);
}
return INSTANCE;
}
public static myTimer getInstance() {
return INSTANCE;
}
}

@ -0,0 +1,20 @@
package putBit.app;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import putBit.app.Timer.Task;
import putBit.app.Timer.myTimer;
import putBit.app.services.OrderService;
@Configuration
@ComponentScan("putBit.app.services")
class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
}
}

@ -0,0 +1,167 @@
package putBit.app.controllers;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import putBit.app.controllers.Dto.AchievementDto;
import putBit.app.controllers.Dto.ExamDto;
import putBit.app.services.AchievementService;
import putBit.app.services.exceptions.ValidationException;
import java.util.List;
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/achievement")
public class AchievementsController {
private final AchievementService achievementService;
@PostMapping("/")
public ResponseEntity<AchievementDto> create(@RequestBody AchievementDto examDto) {
try
{
return new ResponseEntity<>
(new AchievementDto(achievementService.create(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/")
public ResponseEntity<AchievementDto> update(@RequestBody AchievementDto examDto) {
try
{
return new ResponseEntity<>
(new AchievementDto(achievementService.update(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/{id}")
public ResponseEntity<AchievementDto> delete(@PathVariable int id) {
try
{
return new ResponseEntity<>
(new AchievementDto(achievementService.delete(id)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/")
public ResponseEntity<List<AchievementDto>> getAll() {
try
{
return new ResponseEntity<>
(achievementService.findAll().stream().map(AchievementDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/{id}")
public ResponseEntity<AchievementDto> getById(@PathVariable int id) {
try
{
return new ResponseEntity<>
(new AchievementDto(achievementService.findById(id)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/forUser/{id}")
public ResponseEntity<List<AchievementDto>> getForUser(@PathVariable int id) {
try
{
return new ResponseEntity<>
(achievementService.findByUser(id).stream().map(AchievementDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/forUserNot/{id}")
public ResponseEntity<List<AchievementDto>> getForUserNot(@PathVariable int id) {
try
{
return new ResponseEntity<>
(achievementService.findByUserNot(id).stream().map(AchievementDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -0,0 +1,129 @@
package putBit.app.controllers;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import putBit.app.controllers.Dto.AchievementDto;
import putBit.app.controllers.Dto.BenefitDto;
import putBit.app.services.BenefitService;
import putBit.app.services.exceptions.ValidationException;
import java.util.List;
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/benefit")
public class BenefitController {
private final BenefitService benefitService;
@PostMapping("/")
public ResponseEntity<BenefitDto> create(@RequestBody BenefitDto examDto) {
try {
return new ResponseEntity<>
(new BenefitDto(benefitService.create(examDto)), HttpStatus.OK);
} catch (ValidationException ex) {
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
} catch (Exception ex) {
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/")
public ResponseEntity<BenefitDto> update(@RequestBody BenefitDto examDto) {
try {
return new ResponseEntity<>
(new BenefitDto(benefitService.update(examDto)), HttpStatus.OK);
} catch (ValidationException ex) {
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
} catch (Exception ex) {
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/{id}")
public ResponseEntity<BenefitDto> delete(@PathVariable int id) {
try {
return new ResponseEntity<>
(new BenefitDto(benefitService.delete(id)), HttpStatus.OK);
} catch (ValidationException ex) {
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
} catch (Exception ex) {
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/")
public ResponseEntity<List<BenefitDto>> getAll() {
try {
return new ResponseEntity<>
(benefitService.findAll().stream().map(BenefitDto::new).toList(), HttpStatus.OK);
} catch (ValidationException ex) {
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
} catch (Exception ex) {
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/{id}")
public ResponseEntity<BenefitDto> getById(@PathVariable int id) {
try {
return new ResponseEntity<>
(new BenefitDto(benefitService.findById(id)), HttpStatus.OK);
} catch (ValidationException ex) {
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
} catch (Exception ex) {
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/forUser/{id}")
public ResponseEntity<List<BenefitDto>> getForUser(@PathVariable int id) {
try {
return new ResponseEntity<>
(benefitService.findByUser(id).stream().map(BenefitDto::new).toList(), HttpStatus.OK);
} catch (ValidationException ex) {
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
} catch (Exception ex) {
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/forUserNot/{id}")
public ResponseEntity<List<BenefitDto>> getForUserNot(@PathVariable int id) {
try {
return new ResponseEntity<>
(benefitService.findByUserNot(id).stream().map(BenefitDto::new).toList(), HttpStatus.OK);
} catch (ValidationException ex) {
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
} catch (Exception ex) {
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -0,0 +1,41 @@
package putBit.app.controllers;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import putBit.app.Timer.myTimer;
import putBit.app.controllers.Dto.UserDto;
import putBit.app.services.exceptions.ValidationException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/date_close")
public class DateController {
@PostMapping("/")
public ResponseEntity<Long> createUser() {
try
{
LocalDateTime date = myTimer.getInstance().getDate();
ZonedDateTime zdt = ZonedDateTime.of(date, ZoneId.systemDefault());
return new ResponseEntity<Long>(zdt.toInstant().toEpochMilli(), HttpStatus.OK);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -0,0 +1,26 @@
package putBit.app.controllers.Dto;
import lombok.*;
import putBit.app.models.Achievement;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AchievementDto {
private int id;
@NonNull
private String title ;
private int points;
public AchievementDto(Achievement achievement){
this.id = achievement.getId();
this.title = achievement.getTitle();
this.points = achievement.getPoints();
}
}

@ -0,0 +1,26 @@
package putBit.app.controllers.Dto;
import lombok.*;
import putBit.app.models.Achievement;
import putBit.app.models.Benefit;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BenefitDto {
private int id;
@NonNull
private String title ;
public BenefitDto(Benefit benefit){
this.id = benefit.getId();
this.title = benefit.getTitle();
}
}

@ -0,0 +1,28 @@
package putBit.app.controllers.Dto;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.*;
import putBit.app.models.Exam;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExamDto {
private int id;
@NonNull
private String title ;
public ExamDto(Exam exam){
this.id = exam.getId();
this.title = exam.getTitle();
}
}

@ -0,0 +1,39 @@
package putBit.app.controllers.Dto;
import jakarta.persistence.Column;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import putBit.app.models.Exam;
import putBit.app.models.ExamResult;
import putBit.app.models.User;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExamResultDto {
private int user ;
private int exam ;
private String title;
private int points ;
public ExamResultDto(ExamResult examResult){
this.user = examResult.getUser().getId();
this.exam = examResult.getExam().getId();
this.title = examResult.getExam().getTitle();
this.points = examResult.getPoints();
}
}

@ -0,0 +1,58 @@
package putBit.app.controllers.Dto;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import putBit.app.models.Benefit;
import putBit.app.models.Order;
import putBit.app.models.Training;
import putBit.app.models.User;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OrderDto {
private int user;
private int training;
private String name = null;
private String snils = null;
private int points = 0;
private Boolean confirm ;
private Boolean benefit ;
public OrderDto(Order order){
this.user = order.getUser().getId();
this.training = order.getTraining().getId();
this.name = order.getUser().getName();
this.snils = order.getUser().getSnils();
int sum = 0;
for(var exam: order.getUser().getExams())
{
for(var examT: order.getTraining().getExams())
{
if(examT.getExam().getId() == exam.getExam().getId())
sum+= exam.getPoints();
}
}
for(var ach : order.getUser().getAchievements())
{
sum+= ach.getPoints();
}
this.points = sum;
this.confirm = order.getConfirm();
this.benefit = order.getBenefit();
}
}

@ -0,0 +1,53 @@
package putBit.app.controllers.Dto;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import putBit.app.models.Training;
import java.util.ArrayList;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TrainingDto {
private int id;
private String num ;
private String title ;
private String desc ;
private String prof ;
private List<String> exams = new ArrayList<>();
private int basic_places ;
private int benefit_places ;
public TrainingDto(Training training){
this.id = training.getId();
this.title = training.getTitle();
this.num = training.getNum();
this.desc = training.getDesc();
for(var exam: training.getExams())
{
this.exams.add(exam.getExam().getTitle() + " (" + exam.getPoints() + "б.)");
}
this.prof = training.getProf();
this.basic_places = training.getBasic_places();
this.benefit_places = training.getBenefit_places();
}
}

@ -0,0 +1,31 @@
package putBit.app.controllers.Dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import putBit.app.models.TrainingExam;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TrainingExamDto {
private int training ;
private int exam ;
private String title;
private int points ;
public TrainingExamDto(TrainingExam trainingExam){
this.training = trainingExam.getTraining().getId();
this.exam = trainingExam.getExam().getId();
this.title = trainingExam.getExam().getTitle();
this.points = trainingExam.getPoints();
}
}

@ -0,0 +1,44 @@
package putBit.app.controllers.Dto;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import putBit.app.models.User;
import putBit.app.models.enums.Role;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserDto {
private int id;
private String role = null;
private String email = null;
private String password = null;
private String AgainPassword = null;
private String phone = null;
private String name = null;
private String snils = null;
public UserDto(User user){
this.id = user.getId();
this.role = user.getRole().name();
this.email = user.getEmail();
this.password = user.getPassword();
this.phone = user.getPhone();
this.name = user.getName();
this.snils = user.getSnils();
}
}

@ -0,0 +1,165 @@
package putBit.app.controllers;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import putBit.app.controllers.Dto.ExamDto;
import putBit.app.controllers.Dto.UserDto;
import putBit.app.services.ExamService;
import putBit.app.services.exceptions.ValidationException;
import java.util.List;
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/exam")
public class ExamController {
private final ExamService examService;
@PostMapping("/")
public ResponseEntity<ExamDto> create(@RequestBody ExamDto examDto) {
try
{
return new ResponseEntity<>
(new ExamDto(examService.create(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/")
public ResponseEntity<ExamDto> update(@RequestBody ExamDto examDto) {
try
{
return new ResponseEntity<>
(new ExamDto(examService.update(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/{id}")
public ResponseEntity<ExamDto> delete(@PathVariable int id) {
try
{
return new ResponseEntity<>
(new ExamDto(examService.delete(id)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/")
public ResponseEntity<List<ExamDto>> getAll() {
try
{
return new ResponseEntity<>
(examService.findAll().stream().map(ExamDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/{id}")
public ResponseEntity<ExamDto> getById(@PathVariable int id) {
try
{
return new ResponseEntity<>
(new ExamDto(examService.findById(id)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/forTraining/{id}")
public ResponseEntity<List<ExamDto>> getForTraining(@PathVariable int id) {
try
{
return new ResponseEntity<>
(examService.findNotTraining(id).stream().map(ExamDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/forUser/{id}")
public ResponseEntity<List<ExamDto>> getForUser(@PathVariable int id) {
try
{
return new ResponseEntity<>
(examService.findNotUser(id).stream().map(ExamDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -0,0 +1,125 @@
package putBit.app.controllers;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import putBit.app.controllers.Dto.ExamDto;
import putBit.app.controllers.Dto.ExamResultDto;
import putBit.app.models.ExamResult;
import putBit.app.services.ExamResultService;
import putBit.app.services.exceptions.ValidationException;
import java.util.List;
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/exam_result")
public class ExamResultController {
private final ExamResultService examResultService;
@PostMapping("/")
public ResponseEntity<ExamResultDto> create(@RequestBody ExamResultDto examDto) {
try
{
return new ResponseEntity<>
(new ExamResultDto(examResultService.create(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/")
public ResponseEntity<ExamResultDto> update(@RequestBody ExamResultDto examDto) {
try
{
return new ResponseEntity<>
(new ExamResultDto(examResultService.update(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/{userId}/{examId}")
public ResponseEntity<ExamResultDto> delete(@PathVariable int userId, @PathVariable int examId) {
try
{
return new ResponseEntity<>
(new ExamResultDto(examResultService.delete(userId,examId)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/{userId}/{examId}")
public ResponseEntity<ExamResultDto> getById(@PathVariable int userId, @PathVariable int examId) {
try
{
return new ResponseEntity<>
(new ExamResultDto(examResultService.findById(userId, examId)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/forUser/{id}")
public ResponseEntity<List<ExamResultDto>> getForUser(@PathVariable int id) {
try
{
return new ResponseEntity<>
(examResultService.findByUser(id).stream().map(ExamResultDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -0,0 +1,141 @@
package putBit.app.controllers;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import putBit.app.controllers.Dto.ExamDto;
import putBit.app.controllers.Dto.OrderDto;
import putBit.app.services.OrderService;
import putBit.app.services.exceptions.ValidationException;
import java.util.List;
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/order")
public class OrderController {
private final OrderService orderService;
@PostMapping("/")
public ResponseEntity<OrderDto> create(@RequestBody OrderDto examDto) {
try
{
return new ResponseEntity<>
(new OrderDto(orderService.create(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/{userId}/{trainingId}")
public ResponseEntity<OrderDto> delete(@PathVariable int userId, @PathVariable int trainingId) {
try
{
return new ResponseEntity<>
(new OrderDto(orderService.delete(userId, trainingId)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/benefit/{id}")
public ResponseEntity<List<OrderDto>> getBenefit(@PathVariable int id) {
try
{
return new ResponseEntity<>
(orderService.findForBenefit(id).stream().map(OrderDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/basic/{id}")
public ResponseEntity<List<OrderDto>> getBasic(@PathVariable int id) {
try
{
return new ResponseEntity<>
(orderService.findForBasic(id).stream().map(OrderDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/search/{id}/{value}")
public ResponseEntity<List<OrderDto>> getSearch(@PathVariable int id,@PathVariable String value) {
try
{
return new ResponseEntity<>
(orderService.findByNameOrSnils(id,value).stream().map(OrderDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/forUser/{id}")
public ResponseEntity<OrderDto> getByUser(@PathVariable int id) {
try
{
return new ResponseEntity<>
(new OrderDto(orderService.findByUser(id)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -0,0 +1,126 @@
package putBit.app.controllers;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import putBit.app.controllers.Dto.ExamDto;
import putBit.app.controllers.Dto.TrainingDto;
import putBit.app.services.TrainingService;
import putBit.app.services.exceptions.ValidationException;
import java.util.List;
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/training")
public class TrainingController {
private final TrainingService trainingService;
@PostMapping("/")
public ResponseEntity<TrainingDto> create(@RequestBody TrainingDto examDto) {
try
{
return new ResponseEntity<>
(new TrainingDto(trainingService.create(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/")
public ResponseEntity<TrainingDto> update(@RequestBody TrainingDto examDto) {
try
{
return new ResponseEntity<>
(new TrainingDto(trainingService.update(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/{id}")
public ResponseEntity<TrainingDto> delete(@PathVariable int id) {
try
{
return new ResponseEntity<>
(new TrainingDto(trainingService.delete(id)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/")
public ResponseEntity<List<TrainingDto>> getAll() {
try
{
return new ResponseEntity<>
(trainingService.findAll().stream().map(TrainingDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/{id}")
public ResponseEntity<TrainingDto> getById(@PathVariable int id) {
try
{
return new ResponseEntity<>
(new TrainingDto(trainingService.findById(id)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -0,0 +1,126 @@
package putBit.app.controllers;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import putBit.app.controllers.Dto.ExamResultDto;
import putBit.app.controllers.Dto.TrainingExamDto;
import putBit.app.services.TrainingExamService;
import putBit.app.services.TrainingService;
import putBit.app.services.exceptions.ValidationException;
import java.util.List;
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/training_exam")
public class TrainingExamController {
private final TrainingExamService trainingExamService;
@PostMapping("/")
public ResponseEntity<TrainingExamDto> create(@RequestBody TrainingExamDto examDto) {
try
{
return new ResponseEntity<>
(new TrainingExamDto(trainingExamService.create(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/")
public ResponseEntity<TrainingExamDto> update(@RequestBody TrainingExamDto examDto) {
try
{
return new ResponseEntity<>
(new TrainingExamDto(trainingExamService.update(examDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/{trainingId}/{examId}")
public ResponseEntity<TrainingExamDto> delete(@PathVariable int trainingId, @PathVariable int examId) {
try
{
return new ResponseEntity<>
(new TrainingExamDto(trainingExamService.delete(trainingId,examId)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/{trainingId}/{examId}")
public ResponseEntity<TrainingExamDto> getById(@PathVariable int trainingId, @PathVariable int examId) {
try
{
return new ResponseEntity<>
(new TrainingExamDto(trainingExamService.findById(trainingId, examId)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/forTraining/{id}")
public ResponseEntity<List<TrainingExamDto>> getForUser(@PathVariable int id) {
try
{
return new ResponseEntity<>
(trainingExamService.findByTraining(id).stream().map(TrainingExamDto::new).toList(), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -0,0 +1,114 @@
package putBit.app.controllers;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import putBit.app.controllers.Dto.ExamResultDto;
import putBit.app.controllers.Dto.UserDto;
import putBit.app.services.*;
import putBit.app.services.exceptions.EntityNotFoundException;
import putBit.app.services.exceptions.ValidationException;
import java.util.List;
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/user")
public class UserController {
private final UserService userService;
@PostMapping("/")
public ResponseEntity<UserDto> createUser(@RequestBody UserDto userDto) {
try
{
return new ResponseEntity<>
(new UserDto(userService.create(userDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/auth")
public ResponseEntity<UserDto> auth(@RequestBody UserDto userDto) {
try
{
return new ResponseEntity<>
(new UserDto(userService.auth(userDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (EntityNotFoundException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/")
public ResponseEntity<UserDto> update(@RequestBody UserDto userDto) {
try
{
return new ResponseEntity<>
(new UserDto(userService.update(userDto)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/{id}")
public ResponseEntity<UserDto> getById(@PathVariable int id) {
try
{
return new ResponseEntity<>
(new UserDto(userService.findById(id)), HttpStatus.OK);
}
catch (ValidationException ex)
{
return new ResponseEntity<>
(null, HttpStatus.BAD_REQUEST);
}
catch (Exception ex)
{
return new ResponseEntity<>
(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -41,7 +41,7 @@ public class Training {
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.REMOVE, mappedBy = "training")
List<TrainingExam> exams;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, mappedBy = "training")
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST, mappedBy = "training")
List<Order> orders;
}

@ -60,7 +60,7 @@ public class User {
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.REMOVE, mappedBy = "user")
List<ExamResult> exams;
@OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.REMOVE, mappedBy = "user" )
@OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.PERSIST, mappedBy = "user" )
Order order;
public void addAchievement(Achievement achievement){

@ -1,6 +1,7 @@
package putBit.app.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import putBit.app.models.Order;
@ -12,11 +13,19 @@ import java.util.List;
import java.util.Optional;
public interface OrderRepository extends JpaRepository<Order, OrderPK> {
@Query(value = "select o.* from orders where " +
"o.user_id = (select u.id from users u where " +
"u.name LIKE %:value% or u.snils like %:value%) " , nativeQuery = true)
List<Order> findByUserInfo(@Param("value")String value);
@Query(value = "select o.* from orders o " +
" where o.training_id = :id and " +
" o.user_id in (select u.id from users u where " +
" LOWER(u.name) LIKE CONCAT('%',LOWER(:value),'%') or LOWER(u.snils) like CONCAT('%',LOWER(:value),'%'))" , nativeQuery = true)
List<Order> findByUserInfo(@Param("value") String value, @Param("id") int id);
List<Order> findAllByBenefit(Boolean benefit);
List<Order> findAllByBenefitAndTraining(Boolean benefit, Training training);
Optional<Order> findByUser(User user);
@Modifying
@Query(value = "delete from orders o where " +
"o.user_id = :user" , nativeQuery = true)
void deleteByUser(@Param("user") int userId);
}

@ -9,5 +9,5 @@ import java.util.Optional;
public interface TrainingRepository extends JpaRepository<Training, Integer> {
List<Training> findAllByTitleContaining(String title);
List<Training> findAllByTitleContainingIgnoreCase(String title);
}

@ -3,6 +3,8 @@ package putBit.app.services;
import lombok.RequiredArgsConstructor;
import org.hibernate.sql.exec.ExecutionException;
import org.springframework.stereotype.Service;
import putBit.app.controllers.Dto.AchievementDto;
import putBit.app.controllers.Dto.UserDto;
import putBit.app.models.Achievement;
import putBit.app.models.User;
import putBit.app.repositories.AchievementRepository;
@ -20,7 +22,7 @@ public class AchievementService {
private final UserRepository userRepository;
public Achievement create(Achievement achievement){
public Achievement create(AchievementDto achievement){
if(achievement.getTitle().isEmpty() || (achievement.getTitle().length() < 3 || achievement.getTitle().length() > 20 ) )
throw new ValidationException("Неверные данные");
@ -39,7 +41,7 @@ public class AchievementService {
.orElseThrow(() -> new EntityNotFoundException("Достижение не найдено"));
}
public Achievement update(Achievement achievement)
public Achievement update(AchievementDto achievement)
{
if(achievement.getTitle().isEmpty() || (achievement.getTitle().length() < 3 || achievement.getTitle().length() > 20 ) )
throw new ValidationException("Неверные данные");
@ -53,9 +55,9 @@ public class AchievementService {
return achievementRepository.save(currentAchievement);
}
public Achievement delete(Achievement achievement){
Achievement currentAchievement = findById(achievement.getId());
List<User> users = userRepository.findUsersByAchievementsId(achievement.getId());
public Achievement delete(int id){
Achievement currentAchievement = findById(id);
List<User> users = userRepository.findUsersByAchievementsId(id);
for(User user:
users){
user.deleteAchievement(currentAchievement);
@ -69,11 +71,11 @@ public class AchievementService {
return achievementRepository.findAll();
}
public List<Achievement> findByUser(User user){
return achievementRepository.findAchievementsByUsersId(user.getId());
public List<Achievement> findByUser(int id){
return achievementRepository.findAchievementsByUsersId(id);
}
public List<Achievement> findByUserNot(User user){
return achievementRepository.findAchievementsByUsersIdNot(user.getId());
public List<Achievement> findByUserNot(int id){
return achievementRepository.findAchievementsByUsersIdNot(id);
}
}

@ -3,6 +3,8 @@ package putBit.app.services;
import lombok.RequiredArgsConstructor;
import org.hibernate.sql.exec.ExecutionException;
import org.springframework.stereotype.Service;
import putBit.app.controllers.Dto.BenefitDto;
import putBit.app.controllers.Dto.UserDto;
import putBit.app.models.Achievement;
import putBit.app.models.Benefit;
import putBit.app.models.User;
@ -21,7 +23,7 @@ public class BenefitService {
private final UserRepository userRepository;
public Benefit create(Benefit benefit){
public Benefit create(BenefitDto benefit){
if(benefit.getTitle().isEmpty() || (benefit.getTitle().length() < 3 || benefit.getTitle().length() > 20 ) )
throw new ValidationException("Неверные данные");
@ -37,7 +39,7 @@ public class BenefitService {
.orElseThrow(() -> new EntityNotFoundException("Льгота не найдена"));
}
public Benefit update(Benefit benefit)
public Benefit update(BenefitDto benefit)
{
if(benefit.getTitle().isEmpty() || (benefit.getTitle().length() < 3 || benefit.getTitle().length() > 20 ) )
throw new ValidationException("Неверные данные");
@ -47,9 +49,9 @@ public class BenefitService {
return benefitRepository.save(currentBenefit);
}
public Benefit delete(Benefit benefit){
Benefit currentBenefit = findById(benefit.getId());
List<User> users = userRepository.findUsersByBenefitsId(benefit.getId());
public Benefit delete(int id ){
Benefit currentBenefit = findById(id);
List<User> users = userRepository.findUsersByBenefitsId(id);
for(User user:
users){
user.deleteBenefit(currentBenefit);
@ -63,12 +65,12 @@ public class BenefitService {
return benefitRepository.findAll();
}
public List<Benefit> findByUser(User user){
return benefitRepository.findBenefitsByUsersId(user.getId());
public List<Benefit> findByUser(int id){
return benefitRepository.findBenefitsByUsersId(id);
}
public List<Benefit> findByUserNot(User user){
return benefitRepository.findBenefitsByUsersIdNot(user.getId());
public List<Benefit> findByUserNot(int id){
return benefitRepository.findBenefitsByUsersIdNot(id);
}
}

@ -3,6 +3,8 @@ package putBit.app.services;
import lombok.RequiredArgsConstructor;
import org.hibernate.sql.exec.ExecutionException;
import org.springframework.stereotype.Service;
import putBit.app.controllers.Dto.ExamResultDto;
import putBit.app.controllers.Dto.UserDto;
import putBit.app.models.Exam;
import putBit.app.models.ExamResult;
import putBit.app.models.PrimaryKey.ExamResultPK;
@ -21,17 +23,15 @@ public class ExamResultService {
private final UserService userService;
private final ExamService examService;
public ExamResult create(ExamResult examResult){
public ExamResult create(ExamResultDto examResult){
if(examResult.getPoints() > 100 || examResult.getPoints() < 0 )
throw new ValidationException("Неверные данные");
User user = userService.findById(examResult.getUser().getId());
Exam exam = examService.findById(examResult.getExam().getId());
User user = userService.findById(examResult.getUser());
Exam exam = examService.findById(examResult.getExam());
ExamResult newExam = ExamResult.builder()
.user(user)
.exam(exam)
.points(examResult.getPoints())
.points(0)
.build();
return examResultRepository.save(newExam);
}
@ -44,12 +44,12 @@ public class ExamResultService {
.orElseThrow(() -> new EntityNotFoundException("Экзамен не найден"));
}
public ExamResult update(ExamResult examResult)
public ExamResult update(ExamResultDto examResult)
{
if(examResult.getPoints() > 100 || examResult.getPoints() < 0 )
throw new ValidationException("Неверные данные");
ExamResult currentExam = findById(examResult.getUser().getId(),examResult.getExam().getId());
ExamResult currentExam = findById(examResult.getUser(),examResult.getExam());
currentExam.setPoints(examResult.getPoints());
return examResultRepository.save(currentExam);
@ -60,8 +60,8 @@ public class ExamResultService {
examResultRepository.delete(currentExam);
return currentExam;
}
public List<ExamResult> findByUser(User user){
User currentUser = userService.findById(user.getId());
public List<ExamResult> findByUser(int id){
User currentUser = userService.findById(id);
return examResultRepository.findAllByUser(currentUser);
}
}

@ -3,6 +3,9 @@ package putBit.app.services;
import lombok.RequiredArgsConstructor;
import org.hibernate.sql.exec.ExecutionException;
import org.springframework.stereotype.Service;
import putBit.app.controllers.Dto.ExamDto;
import putBit.app.controllers.Dto.TrainingDto;
import putBit.app.controllers.Dto.UserDto;
import putBit.app.models.Exam;
import putBit.app.models.Training;
import putBit.app.models.User;
@ -18,7 +21,7 @@ import java.util.List;
public class ExamService {
private final ExamRepository examRepository;
public Exam create(Exam exam){
public Exam create(ExamDto exam){
if(exam.getTitle().isEmpty() || (exam.getTitle().length() < 3 || exam.getTitle().length() > 20 ) )
throw new ValidationException("Неверные данные");
@ -34,7 +37,7 @@ public class ExamService {
.orElseThrow(() -> new EntityNotFoundException("Экзамен не найден"));
}
public Exam update(Exam exam)
public Exam update(ExamDto exam)
{
if(exam.getTitle().isEmpty() || (exam.getTitle().length() < 3 || exam.getTitle().length() > 20 ) )
throw new ValidationException("Неверные данные");
@ -45,8 +48,8 @@ public class ExamService {
return examRepository.save(currentExam);
}
public Exam delete(Exam exam){
Exam currentExam = findById(exam.getId());
public Exam delete(int id){
Exam currentExam = findById(id);
examRepository.delete(currentExam);
return currentExam;
}
@ -54,12 +57,13 @@ public class ExamService {
public List<Exam> findAll(){
return examRepository.findAll();
}
public List<Exam> findNotUser(User user){
return examRepository.findByUserIdNot(user.getId());
public List<Exam> findNotUser(int id){
return examRepository.findByUserIdNot(id);
}
public List<Exam> findNotTraining(Training training){
return examRepository.findByTrainingIdNot(training.getId());
public List<Exam> findNotTraining(int id){
return examRepository.findByTrainingIdNot(id);
}
}

@ -1,28 +1,54 @@
package putBit.app.services;
import jakarta.mail.MessagingException;
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 org.springframework.transaction.annotation.Transactional;
import putBit.app.Timer.myTimer;
import putBit.app.controllers.Dto.OrderDto;
import putBit.app.models.*;
import putBit.app.models.PrimaryKey.OrderPK;
import putBit.app.repositories.OrderRepository;
import putBit.app.repositories.TrainingRepository;
import putBit.app.repositories.UserRepository;
import putBit.app.services.WorkEmail.EmailWork;
import putBit.app.services.exceptions.EntityNotFoundException;
import putBit.app.services.help.OrderPoints;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
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());
private final EmailWork emailWork;
public OrderService(
UserService userService,
TrainingService trainingService,
OrderRepository orderRepository){
this.userService=userService;
this.trainingService = trainingService;
this.orderRepository = orderRepository;
this.emailWork = new EmailWork();
myTimer timer = myTimer.getInstance(this);
}
@Transactional
public Order create(OrderDto order){
User user = userService.findById(order.getUser());
Training training = trainingService.findById(order.getTraining());
int limit = 0;
for(TrainingExam exam:
training.getExams())
@ -70,23 +96,87 @@ public class OrderService {
return orderRepository.findById(pk)
.orElseThrow(() -> new EntityNotFoundException("Заявка не найдена"));
}
@Transactional
public Order delete(int userId, int trainId){
Order currentOrder = findById(userId,trainId);
orderRepository.delete(currentOrder);
orderRepository.deleteByUser(userId);
return currentOrder;
}
public List<Order> findForBenefit(){
return orderRepository.findAllByBenefit(true);
public List<Order> findForBenefit(int id){
Training training = trainingService.findById(id);
return orderRepository.findAllByBenefitAndTraining(true, training);
}
public List<Order> findForBasic(){
return orderRepository.findAllByBenefit(false);
public List<Order> findForBasic(int id){
Training training = trainingService.findById(id);
return orderRepository.findAllByBenefitAndTraining(false, training);
}
public List<Order> findByNameOrSnils(String text){
return orderRepository.findByUserInfo(text);
public List<Order> findByNameOrSnils(int id, String text){
return orderRepository.findByUserInfo(text, id);
}
public Order findByUser(int id){
User user = userService.findById(id);
return orderRepository.findByUser(user)
.orElseThrow(() -> new EntityNotFoundException("У этого челика нет заявления"));
}
public void emailSend() throws MessagingException {
List<Order> orders = orderRepository.findAll();
for(var order: orders){
String email = order.getUser().getEmail();
if(order.getConfirm())
emailWork.sendMessage(email, "Поздравляем, вы поступили");
else
emailWork.sendMessage(email, "Сожалеем, вы не поступили");
}
}
@Transactional
public void workWithStudents() throws MessagingException {
List<Training> trainings = trainingService.findAll();
for(Training training: trainings)
{
System.out.println(training.getId() + " or:" + training.getOrders().size());
if(training.getOrders().isEmpty())
continue;
List<OrderPoints> orders_benefits = new ArrayList<>(findForBenefit(training.getId()).stream().map(or -> new OrderPoints(or)).toList());
List<OrderPoints> orders_basic = new ArrayList<>(findForBasic(training.getId()).stream().map(or -> new OrderPoints(or)).toList());
orders_benefits.sort(Collections.reverseOrder());
orders_basic.sort(Collections.reverseOrder());
List<Order> orderToUpdate = new ArrayList<>();
System.out.println(orders_basic.size());
System.out.println(orders_benefits.size());
int i = 0;
for(var order: orders_benefits)
{
if(i == training.getBenefit_places())
break;
order.getOrder().setConfirm(true);
orderToUpdate.add(order.getOrder());
i++;
}
i = 0;
for(var order: orders_basic)
{
if(i == training.getBasic_places())
break;
order.getOrder().setConfirm(true);
orderToUpdate.add(order.getOrder());
i++;
}
orderRepository.saveAll(orderToUpdate);
}
//emailSend();
}
}

@ -3,6 +3,8 @@ package putBit.app.services;
import lombok.RequiredArgsConstructor;
import org.hibernate.sql.exec.ExecutionException;
import org.springframework.stereotype.Service;
import putBit.app.controllers.Dto.TrainingDto;
import putBit.app.controllers.Dto.TrainingExamDto;
import putBit.app.models.*;
import putBit.app.models.PrimaryKey.TrainingExamPK;
import putBit.app.repositories.TrainingExamRepository;
@ -18,19 +20,16 @@ public class TrainingExamService {
private final ExamService examService;
private final TrainingService trainingService;
public TrainingExam create(TrainingExam trainingExam){
if(trainingExam.getPoints() > 100 || trainingExam.getPoints() < 0 )
throw new ValidationException("Неверные данные");
public TrainingExam create(TrainingExamDto trainingExam){
Training training = trainingService.findById(trainingExam.getTraining().getId());
Exam exam = examService.findById(trainingExam.getExam().getId());
Training training = trainingService.findById(trainingExam.getTraining());
Exam exam = examService.findById(trainingExam.getExam());
TrainingExam newExam = TrainingExam.builder()
.training(training)
.exam(exam)
.points(trainingExam.getPoints())
.points(0)
.build();
return trainingExamRepository.save(newExam);
}
@ -41,12 +40,12 @@ public class TrainingExamService {
.orElseThrow(() -> new EntityNotFoundException("Экзамен не найден"));
}
public TrainingExam update(TrainingExam trainingExam)
public TrainingExam update(TrainingExamDto trainingExam)
{
if(trainingExam.getPoints() > 100 || trainingExam.getPoints() < 0 )
throw new ValidationException("Неверные данные");
TrainingExam currentExam = findById(trainingExam.getTraining().getId(),trainingExam.getExam().getId());
TrainingExam currentExam = findById(trainingExam.getTraining(),trainingExam.getExam());
currentExam.setPoints(trainingExam.getPoints());
return trainingExamRepository.save(currentExam);
@ -57,8 +56,8 @@ public class TrainingExamService {
trainingExamRepository.delete(currentExam);
return currentExam;
}
public List<TrainingExam> findByTraining(Training training){
Training currentTraining = trainingService.findById(training.getId());
public List<TrainingExam> findByTraining(int id){
Training currentTraining = trainingService.findById(id);
return trainingExamRepository.findAllByTraining(currentTraining);
}

@ -3,6 +3,7 @@ package putBit.app.services;
import lombok.RequiredArgsConstructor;
import org.hibernate.sql.exec.ExecutionException;
import org.springframework.stereotype.Service;
import putBit.app.controllers.Dto.TrainingDto;
import putBit.app.models.Exam;
import putBit.app.models.Training;
import putBit.app.models.User;
@ -17,7 +18,7 @@ import java.util.List;
public class TrainingService {
private final TrainingRepository trainingRepository;
public Training create(Training training){
public Training create(TrainingDto training){
if(training.getTitle().isEmpty() || (training.getTitle().length() < 2 || training.getTitle().length() > 50 ) )
throw new ValidationException("Неверное название");
@ -49,7 +50,7 @@ public class TrainingService {
.orElseThrow(() -> new EntityNotFoundException("Направление не найдено"));
}
public Training update(Training training)
public Training update(TrainingDto training)
{
if(training.getTitle().isEmpty() || (training.getTitle().length() < 2 || training.getTitle().length() > 50 ) )
@ -76,8 +77,8 @@ public class TrainingService {
return trainingRepository.save(currentTraining);
}
public Training delete(Training training){
Training currentTraining = findById(training.getId());
public Training delete(int id){
Training currentTraining = findById(id);
trainingRepository.delete(currentTraining);
return currentTraining;
}
@ -87,6 +88,6 @@ public class TrainingService {
}
public List<Training> findByTitle(String title){
return trainingRepository.findAllByTitleContaining(title);
return trainingRepository.findAllByTitleContainingIgnoreCase(title);
}
}

@ -3,6 +3,9 @@ package putBit.app.services;
import lombok.RequiredArgsConstructor;
import org.hibernate.sql.exec.ExecutionException;
import org.springframework.stereotype.Service;
import putBit.app.controllers.Dto.AchievementDto;
import putBit.app.controllers.Dto.BenefitDto;
import putBit.app.controllers.Dto.UserDto;
import putBit.app.models.Achievement;
import putBit.app.models.Benefit;
import putBit.app.models.User;
@ -11,6 +14,8 @@ import putBit.app.repositories.UserRepository;
import putBit.app.services.exceptions.EntityNotFoundException;
import putBit.app.services.exceptions.ValidationException;
import java.util.regex.Pattern;
@Service
@RequiredArgsConstructor
public class UserService {
@ -19,9 +24,17 @@ public class UserService {
private final BenefitService benefitService;
private final AchievementService achievementService;
public User create(User user){
if(user.getEmail().isEmpty() )
public static boolean patternMatches(String emailAddress) {
return Pattern
.compile("^(?=.{1,64}@)[A-Za-z0-9_-]+(\\\\.[A-Za-z0-9_-]+)*@[^-][A-Za-z0-9-]+(\\\\.[A-Za-z0-9-]+)*(\\\\.[A-Za-z]{2,})$")
.matcher(emailAddress)
.matches();
}
public User create(UserDto user){
if(patternMatches(user.getEmail()) )
throw new ValidationException("Неверная почта");
if(user.getName().isEmpty() || (user.getName().length() < 3 || user.getName().length() > 80 ) )
throw new ValidationException("Неверное ФИО");
@ -49,13 +62,13 @@ public class UserService {
.orElseThrow(() -> new EntityNotFoundException("Пользователь не найден"));
}
public User auth(User user)
public User auth(UserDto user)
{
return userRepository.findByEmailAndPassword(user.getEmail(), user.getPassword())
.orElseThrow(() -> new EntityNotFoundException("Пользователь не найден"));
}
public User update(User user)
public User update(UserDto user)
{
if(user.getEmail().isEmpty() )
throw new ValidationException("Неверная почта");
@ -84,20 +97,20 @@ public class UserService {
return userRepository.save(currentUser);
}
public User addAchievement(User user,Achievement achievement){
public User addAchievement(UserDto user, AchievementDto 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){
public User addBenefit(UserDto user, BenefitDto 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){
public User removeAchievement(UserDto user,AchievementDto achievement){
User currentUser = findById(user.getId());
Achievement newAch = achievementService.findById(achievement.getId());
if (!currentUser.deleteAchievement(newAch))
@ -105,7 +118,7 @@ public class UserService {
return userRepository.save(currentUser);
}
public User removeBenefit(User user,Benefit benefit){
public User removeBenefit(UserDto user,BenefitDto benefit){
User currentUser = findById(user.getId());
Benefit newBen = benefitService.findById(benefit.getId());
if (!currentUser.deleteBenefit(newBen))

@ -0,0 +1,59 @@
package putBit.app.services.WorkEmail;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import java.util.Properties;
public class EmailWork {
private Properties prop;
private final String username = "labrpp89@gmail.com";
private final String password = "wfhb szlu vibt jkae";
public EmailWork(){
prop = new Properties();
prop.put("mail.smtp.auth", true);
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.ssl.trust", "smtp.gmail.com");
}
public void sendMessage(String emailTo, String text) throws MessagingException {
Session session = Session.getInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@gmail.com"));
message.setRecipients(
Message.RecipientType.TO, InternetAddress.parse(emailTo));
message.setSubject("Ваш любимый вуз");
String msg = text;
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(msg, "text/html; charset=utf-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
Transport.send(message);
}
}

@ -0,0 +1,40 @@
package putBit.app.services.help;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import putBit.app.models.Order;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OrderPoints implements Comparable<OrderPoints>{
private Order order;
private int points;
public OrderPoints(Order order){
this.order = order;
int sum = 0;
for(var exam: order.getUser().getExams())
{
for(var examT: order.getTraining().getExams())
{
if(examT.getExam().getId() == exam.getExam().getId())
sum+= exam.getPoints();
}
}
for(var ach : order.getUser().getAchievements())
{
sum+= ach.getPoints();
}
this.points = sum;
}
@Override
public int compareTo(OrderPoints o) {
return Integer.compare(points,o.points);
}
}

@ -37,392 +37,500 @@ class AppApplicationTests {
@Autowired
OrderService orderService;
@Test
void createUser() {
User us1 = User.builder()
.email("email1")
.password("12345678")
.phone("88005553535")
.role(Role.USER)
.name("Vova Morozov")
.snils("33322233344")
.build();
User us2 = User.builder()
.email("email2")
.password("12345678")
.phone("88005553535")
.role(Role.USER)
.name("Dima Redov")
.snils("11122233344")
.build();
User res1 = userService.create(us1);
User res2 = userService.create(us2);
Assertions.assertNotNull(res1);
Assertions.assertNotNull(res2);
}
@Test
void CreateExam(){
Exam ex1 = Exam.builder()
.title("Математика")
.build();
Exam ex2 = Exam.builder()
.title("Русский язык")
.build();
Exam ex3 = Exam.builder()
.title("Физика")
.build();
Exam ex4 = Exam.builder()
.title("Информатика")
.build();
Exam res1 = examService.create(ex1);
Exam res2 = examService.create(ex2);
Exam res3 = examService.create(ex3);
Exam res4 = examService.create(ex4);
Assertions.assertNotNull(res1);
Assertions.assertNotNull(res2);
Assertions.assertNotNull(res3);
Assertions.assertNotNull(res4);
}
@Test
void CreateBenefit(){
Benefit bf1 = Benefit.builder()
.title("Инвалидность I")
.build();
Benefit bf2 = Benefit.builder()
.title("Инвалидность II")
.build();
Benefit bf3 = Benefit.builder()
.title("Инвалидность III")
.build();
Benefit res1 = benefitService.create(bf1);
Benefit res2 = benefitService.create(bf2);
Benefit res3 = benefitService.create(bf3);
Assertions.assertNotNull(res1);
Assertions.assertNotNull(res2);
Assertions.assertNotNull(res3);
}
@Test
void CreateAchievements(){
Achievement ac1 = Achievement.builder()
.title("ГТО 1 степени")
.points(5)
.build();
Achievement ac2 = Achievement.builder()
.title("ГТО 2 степени")
.points(3)
.build();
Achievement ac3 = Achievement.builder()
.title("Красный диплом")
.points(7)
.build();
Achievement res1 = achievementService.create(ac1);
Achievement res2 = achievementService.create(ac2);
Achievement res3 = achievementService.create(ac3);
Assertions.assertNotNull(res1);
Assertions.assertNotNull(res2);
Assertions.assertNotNull(res3);
}
@Test
void CreateExamResult(){
User us1 = userService.findById(1);
User us2 = userService.findById(52);
Exam ex1 = examService.findById(1);
Exam ex2 = examService.findById(2);
Exam ex3 = examService.findById(3);
ExamResult er1 = ExamResult.builder()
.user(us1)
.exam(ex1)
.points(30)
.build();
ExamResult er2 = ExamResult.builder()
.user(us1)
.exam(ex2)
.points(30)
.build();
ExamResult er3 = ExamResult.builder()
.user(us2)
.exam(ex2)
.points(50)
.build();
ExamResult er4 = ExamResult.builder()
.user(us2)
.exam(ex3)
.points(60)
.build();
var res1 = examResultService.create(er1);
var res2 = examResultService.create(er2);
var res3 = examResultService.create(er3);
var res4 = examResultService.create(er4);
Assertions.assertNotNull(res1);
Assertions.assertNotNull(res2);
Assertions.assertNotNull(res3);
Assertions.assertNotNull(res4);
}
@Test
void CreateTraining() {
Training tr1 = Training.builder()
.title("ПИбд")
.num("09.09.01.01")
.desc("Типо программисты")
.prof("бэкендер, фронтендер, аналитикffff")
.basic_places(100)
.benefit_places(10)
.build();
Training tr2 = Training.builder()
.title("ИВТ")
.num("09.09.02")
.desc("Типо программисты-инженеры")
.prof("на заваод")
.basic_places(120)
.benefit_places(12)
.build();
Training tr3 = Training.builder()
.title("ИСЭ")
.num("09.09.03")
.desc("Типо программисты-инженеры")
.prof("цифровой экономист")
.basic_places(120)
.benefit_places(12)
.build();
var res1 = trainingService.create(tr1);
var res2 = trainingService.create(tr2);
var res3 = trainingService.create(tr3);
Assertions.assertNotNull(res1);
Assertions.assertNotNull(res2);
Assertions.assertNotNull(res3);
}
@Test
void CreateTrainingExam(){
Training tr1 = trainingService.findById(1);
Training tr2 = trainingService.findById(2);
Exam ex1 = examService.findById(1);
Exam ex2 = examService.findById(2);
Exam ex3 = examService.findById(4);
TrainingExam te1 = TrainingExam.builder()
.training(tr1)
.exam(ex1)
.points(30)
.build();
TrainingExam te2 = TrainingExam.builder()
.training(tr1)
.exam(ex2)
.points(30)
.build();
TrainingExam te3 = TrainingExam.builder()
.training(tr2)
.exam(ex2)
.points(5)
.build();
TrainingExam te4 = TrainingExam.builder()
.training(tr2)
.exam(ex3)
.points(66)
.build();
var res1 = trainingExamService.create(te1);
var res2 = trainingExamService.create(te2);
var res3 = trainingExamService.create(te3);
var res4 = trainingExamService.create(te4);
Assertions.assertNotNull(res1);
Assertions.assertNotNull(res2);
Assertions.assertNotNull(res3);
Assertions.assertNotNull(res4);
}
@Test
void CreateOrder(){
Training tr1 = trainingService.findById(1);
Training tr2 = trainingService.findById(3);
User us1 = userService.findById(2);
User us2 = userService.findById(2);
Order o1 = Order.builder()
.user(us1)
.training(tr1)
.build();
Order o2 = Order.builder()
.user(us2)
.training(tr2)
.build();
var res1 = orderService.create(o1);
var res2 = orderService.create(o2);
Assertions.assertNotNull(res1);
Assertions.assertNotNull(res2);
}
@Test
void updateUser(){
User us1 = userService.findById(1);
String oldName = us1.getName();
us1.setName(oldName + "Upd");
var res = userService.update(us1);
Assertions.assertNotNull(res);
}
@Test
void updateUserAdd(){
User us1 = userService.findById(1);
Achievement ach1 = achievementService.findById(1);
Achievement ach2 = achievementService.findById(2);
List<User> res = new ArrayList<>();
res.add(userService.addAchievement(us1, ach2));
res.add(userService.addAchievement(us1, ach1));
User us2 = userService.findById(52);
Benefit b1 = benefitService.findById(2);
Benefit b2 = benefitService.findById(3);
res.add(userService.addAchievement(us2, ach2));
res.add(userService.addBenefit(us2, b2));
res.add(userService.addBenefit(us1,b2));
Assertions.assertEquals(res.size(),4);
}
@Test
void updateUserRemove(){
User us1 = userService.findById(52);
Benefit b1 = benefitService.findById(3);
int countB = us1.getBenefits().size();
us1 = userService.removeBenefit(us1,b1);
Assertions.assertEquals(countB-1,us1.getBenefits().size());
}
@Test
void updateExam(){
Exam ex = examService.findById(1);
String oldName = ex.getTitle();
ex.setTitle(ex.getTitle() + "UPD");
var res = examService.update(ex);
Assertions.assertEquals(oldName + "UPD", res.getTitle());
}
@Test
void updateExamResult(){
ExamResult er = examResultService.findById(1,1);
er.setPoints(100);
var res = examResultService.update(er);
Assertions.assertEquals(100,res.getPoints());
}
@Test
void updateBenefit(){
Benefit b = benefitService.findById(1);
String oldName = b.getTitle();
b.setTitle(b.getTitle() + "UPD");
var res = benefitService.update(b);
Assertions.assertEquals(oldName + "UPD", res.getTitle());
}
@Test
void updateAchievement(){
Achievement b = achievementService.findById(1);
String oldName = b.getTitle();
b.setTitle(b.getTitle() + "UPD");
var res = achievementService.update(b);
Assertions.assertEquals(oldName + "UPD", res.getTitle());
}
@Test
void updateTraining(){
Training b = trainingService.findById(1);
String oldName = b.getTitle();
b.setTitle(b.getTitle() + "UPD");
var res = trainingService.update(b);
Assertions.assertEquals(oldName + "UPD", res.getTitle());
}
@Test
void updateTrainigExam(){
TrainingExam er = trainingExamService.findById(1,1);
er.setPoints(90);
var res = trainingExamService.update(er);
Assertions.assertEquals(110,res.getPoints());
}
@Test
void deleteExam(){
int countBefore = examService.findAll().size();
Exam ex = examService.findById(3);
examService.delete(ex);
int countAfter = examService.findAll().size();
Assertions.assertEquals(countBefore-1,countAfter);
}
@Test
void deleteBenefit(){
int countBefore = benefitService.findAll().size();
Benefit ex = benefitService.findById(3);
benefitService.delete(ex);
int countAfter = benefitService.findAll().size();
Assertions.assertEquals(countBefore-1,countAfter);
}
@Test
void deleteAchievement(){
int countBefore = achievementService.findAll().size();
Achievement ex = achievementService.findById(2);
achievementService.delete(ex);
int countAfter = achievementService.findAll().size();
Assertions.assertEquals(countBefore-1,countAfter);
}
@Test
void deleteExamResult(){
var res = examResultService.delete(1,1);
Assertions.assertEquals(1,res.getUser().getId());
}
@Test
void deleteTraining(){
int countBefore = trainingService.findAll().size();
Training ex = trainingService.findById(2);
trainingService.delete(ex);
int countAfter = trainingService.findAll().size();
Assertions.assertEquals(countBefore-1,countAfter);
}
@Test
void deleteTrainingExam(){
var res = trainingExamService.delete(1,1);
Assertions.assertEquals(1,res.getTraining().getId());
}
@Test
void deleteOrder(){
var res = orderService.delete(2,3);
System.out.println(res.getUser().getName());
}
// @Test
// void createUser() {
//
// User us1 = User.builder()
// .email("email1")
// .password("12345678")
// .phone("88005553535")
// .role(Role.USER)
// .name("Vova Morozov")
// .snils("33322233344")
// .build();
// User us2 = User.builder()
// .email("email2")
// .password("12345678")
// .phone("88005553535")
// .role(Role.USER)
// .name("Dima Redov")
// .snils("11122233344")
// .build();
// User res1 = userService.create(us1);
// User res2 = userService.create(us2);
// Assertions.assertNotNull(res1);
// Assertions.assertNotNull(res2);
// }
//
// @Test
// void CreateExam(){
// Exam ex1 = Exam.builder()
// .title("Математика")
// .build();
// Exam ex2 = Exam.builder()
// .title("Русский язык")
// .build();
// Exam ex3 = Exam.builder()
// .title("Физика")
// .build();
// Exam ex4 = Exam.builder()
// .title("Информатика")
// .build();
//
// Exam res1 = examService.create(ex1);
// Exam res2 = examService.create(ex2);
// Exam res3 = examService.create(ex3);
// Exam res4 = examService.create(ex4);
//
// Assertions.assertNotNull(res1);
// Assertions.assertNotNull(res2);
// Assertions.assertNotNull(res3);
// Assertions.assertNotNull(res4);
// }
//
// @Test
// void CreateBenefit(){
// Benefit bf1 = Benefit.builder()
// .title("Инвалидность I")
// .build();
// Benefit bf2 = Benefit.builder()
// .title("Инвалидность II")
// .build();
// Benefit bf3 = Benefit.builder()
// .title("Инвалидность III")
// .build();
//
// Benefit res1 = benefitService.create(bf1);
// Benefit res2 = benefitService.create(bf2);
// Benefit res3 = benefitService.create(bf3);
//
// Assertions.assertNotNull(res1);
// Assertions.assertNotNull(res2);
// Assertions.assertNotNull(res3);
// }
// @Test
// void CreateAchievements(){
// Achievement ac1 = Achievement.builder()
// .title("ГТО 1 степени")
// .points(5)
// .build();
// Achievement ac2 = Achievement.builder()
// .title("ГТО 2 степени")
// .points(3)
// .build();
// Achievement ac3 = Achievement.builder()
// .title("Красный диплом")
// .points(7)
// .build();
//
// Achievement res1 = achievementService.create(ac1);
// Achievement res2 = achievementService.create(ac2);
// Achievement res3 = achievementService.create(ac3);
//
// Assertions.assertNotNull(res1);
// Assertions.assertNotNull(res2);
// Assertions.assertNotNull(res3);
// }
// @Test
// void CreateExamResult(){
//
// User us1 = userService.findById(1);
// User us2 = userService.findById(52);
//
// Exam ex1 = examService.findById(1);
// Exam ex2 = examService.findById(2);
// Exam ex3 = examService.findById(3);
//
// ExamResult er1 = ExamResult.builder()
// .user(us1)
// .exam(ex1)
// .points(30)
// .build();
// ExamResult er2 = ExamResult.builder()
// .user(us1)
// .exam(ex2)
// .points(30)
// .build();
//
// ExamResult er3 = ExamResult.builder()
// .user(us2)
// .exam(ex2)
// .points(50)
// .build();
// ExamResult er4 = ExamResult.builder()
// .user(us2)
// .exam(ex3)
// .points(60)
// .build();
//
// var res1 = examResultService.create(er1);
// var res2 = examResultService.create(er2);
// var res3 = examResultService.create(er3);
// var res4 = examResultService.create(er4);
//
// Assertions.assertNotNull(res1);
// Assertions.assertNotNull(res2);
// Assertions.assertNotNull(res3);
// Assertions.assertNotNull(res4);
// }
//
// @Test
// void CreateTraining() {
// Training tr1 = Training.builder()
// .title("ПИбд")
// .num("09.09.01.01")
// .desc("Типо программисты")
// .prof("бэкендер, фронтендер, аналитикffff")
// .basic_places(100)
// .benefit_places(10)
// .build();
// Training tr2 = Training.builder()
// .title("ИВТ")
// .num("09.09.02")
// .desc("Типо программисты-инженеры")
// .prof("на заваод")
// .basic_places(120)
// .benefit_places(12)
// .build();
// Training tr3 = Training.builder()
// .title("ИСЭ")
// .num("09.09.03")
// .desc("Типо программисты-инженеры")
// .prof("цифровой экономист")
// .basic_places(120)
// .benefit_places(12)
// .build();
//
// var res1 = trainingService.create(tr1);
// var res2 = trainingService.create(tr2);
// var res3 = trainingService.create(tr3);
//
// Assertions.assertNotNull(res1);
// Assertions.assertNotNull(res2);
// Assertions.assertNotNull(res3);
// }
//
// @Test
// void CreateTrainingExam(){
//
// Training tr1 = trainingService.findById(1);
// Training tr2 = trainingService.findById(2);
//
// Exam ex1 = examService.findById(1);
// Exam ex2 = examService.findById(2);
// Exam ex3 = examService.findById(4);
//
// TrainingExam te1 = TrainingExam.builder()
// .training(tr1)
// .exam(ex1)
// .points(30)
// .build();
// TrainingExam te2 = TrainingExam.builder()
// .training(tr1)
// .exam(ex2)
// .points(30)
// .build();
// TrainingExam te3 = TrainingExam.builder()
// .training(tr2)
// .exam(ex2)
// .points(5)
// .build();
// TrainingExam te4 = TrainingExam.builder()
// .training(tr2)
// .exam(ex3)
// .points(66)
// .build();
//
// var res1 = trainingExamService.create(te1);
// var res2 = trainingExamService.create(te2);
// var res3 = trainingExamService.create(te3);
// var res4 = trainingExamService.create(te4);
//
// Assertions.assertNotNull(res1);
// Assertions.assertNotNull(res2);
// Assertions.assertNotNull(res3);
// Assertions.assertNotNull(res4);
//
// }
// @Test
// void CreateOrder(){
// Training tr1 = trainingService.findById(1);
// Training tr2 = trainingService.findById(3);
//
// User us1 = userService.findById(2);
// User us2 = userService.findById(2);
//
// Order o1 = Order.builder()
// .user(us1)
// .training(tr1)
// .build();
// Order o2 = Order.builder()
// .user(us2)
// .training(tr2)
// .build();
//
// var res1 = orderService.create(o1);
// var res2 = orderService.create(o2);
//
// Assertions.assertNotNull(res1);
// Assertions.assertNotNull(res2);
// }
//
// @Test
// void updateUser(){
// User us1 = userService.findById(1);
// String oldName = us1.getName();
// us1.setName(oldName + "Upd");
// var res = userService.update(us1);
// Assertions.assertNotNull(res);
//
// }
//
// @Test
// void updateUserAdd(){
// User us1 = userService.findById(1);
// Achievement ach1 = achievementService.findById(1);
// Achievement ach2 = achievementService.findById(2);
// List<User> res = new ArrayList<>();
// res.add(userService.addAchievement(us1, ach2));
// res.add(userService.addAchievement(us1, ach1));
//
// User us2 = userService.findById(52);
// Benefit b1 = benefitService.findById(2);
// Benefit b2 = benefitService.findById(3);
//
// res.add(userService.addAchievement(us2, ach2));
// res.add(userService.addBenefit(us2, b2));
// res.add(userService.addBenefit(us1,b2));
// Assertions.assertEquals(res.size(),4);
// }
//
// @Test
// void updateUserRemove(){
// User us1 = userService.findById(52);
// Benefit b1 = benefitService.findById(3);
// int countB = us1.getBenefits().size();
// us1 = userService.removeBenefit(us1,b1);
// Assertions.assertEquals(countB-1,us1.getBenefits().size());
// }
//
// @Test
// void updateExam(){
// Exam ex = examService.findById(1);
// String oldName = ex.getTitle();
// ex.setTitle(ex.getTitle() + "UPD");
// var res = examService.update(ex);
// Assertions.assertEquals(oldName + "UPD", res.getTitle());
// }
//
// @Test
// void updateExamResult(){
// ExamResult er = examResultService.findById(1,1);
// er.setPoints(100);
// var res = examResultService.update(er);
// Assertions.assertEquals(100,res.getPoints());
// }
//
// @Test
// void updateBenefit(){
// Benefit b = benefitService.findById(1);
// String oldName = b.getTitle();
// b.setTitle(b.getTitle() + "UPD");
// var res = benefitService.update(b);
// Assertions.assertEquals(oldName + "UPD", res.getTitle());
// }
//
// @Test
// void updateAchievement(){
// Achievement b = achievementService.findById(1);
// String oldName = b.getTitle();
// b.setTitle(b.getTitle() + "UPD");
// var res = achievementService.update(b);
// Assertions.assertEquals(oldName + "UPD", res.getTitle());
//
// }
//
// @Test
// void updateTraining(){
// Training b = trainingService.findById(1);
// String oldName = b.getTitle();
// b.setTitle(b.getTitle() + "UPD");
// var res = trainingService.update(b);
// Assertions.assertEquals(oldName + "UPD", res.getTitle());
//
// }
//
// @Test
// void updateTrainigExam(){
// TrainingExam er = trainingExamService.findById(1,1);
// er.setPoints(90);
// var res = trainingExamService.update(er);
// Assertions.assertEquals(110,res.getPoints());
//
// }
//
// @Test
// void deleteExam(){
// int countBefore = examService.findAll().size();
// Exam ex = examService.findById(3);
// examService.delete(ex);
// int countAfter = examService.findAll().size();
// Assertions.assertEquals(countBefore-1,countAfter);
// }
//
// @Test
// void deleteBenefit(){
// int countBefore = benefitService.findAll().size();
// Benefit ex = benefitService.findById(3);
// benefitService.delete(ex);
// int countAfter = benefitService.findAll().size();
// Assertions.assertEquals(countBefore-1,countAfter);
// }
//
// @Test
// void deleteAchievement(){
// int countBefore = achievementService.findAll().size();
// Achievement ex = achievementService.findById(2);
// achievementService.delete(ex);
// int countAfter = achievementService.findAll().size();
// Assertions.assertEquals(countBefore-1,countAfter);
// }
//
// @Test
// void deleteExamResult(){
// var res = examResultService.delete(1,1);
// Assertions.assertEquals(1,res.getUser().getId());
// }
//
// @Test
// void deleteTraining(){
// int countBefore = trainingService.findAll().size();
// Training ex = trainingService.findById(2);
// trainingService.delete(ex);
// int countAfter = trainingService.findAll().size();
// Assertions.assertEquals(countBefore-1,countAfter);
// }
//
// @Test
// void deleteTrainingExam(){
// var res = trainingExamService.delete(1,1);
// Assertions.assertEquals(1,res.getTraining().getId());
// }
//
// @Test
// void deleteOrder(){
// var res = orderService.delete(2,3);
// System.out.println(res.getUser().getName());
// }
//
// @Test
// void findUser(){
// userService.findByEmailAndPassword("email1","12345678");
// }
// @Test
// void findExam(){
// User user = userService.findById(2);
//
// var res1 = examResultService.findByUser(user);
// System.out.println("Exems have:" + user.getId());
// for(var res: res1)
// {
// System.out.println(res.getExam().getTitle());
// }
//
// var res2 = examService.findNotUser(user);
// System.out.println("Exems havent:" + user.getId());
// for(var res: res2)
// {
// System.out.println(res.getTitle());
// }
//
// Training training = trainingService.findById(1);
// var res3 = trainingExamService.findByTraining(training);
// System.out.println("Exems have:" + training.getId());
// for(var res: res3)
// {
// System.out.println(res.getExam().getTitle());
// }
//
// var res4 = examService.findNotTraining(training);
// System.out.println("Exems havent:" + training.getId());
// for(var res: res4)
// {
// System.out.println(res.getTitle());
// }
// }
// @Test
// void findBenefits(){
// User user = userService.findById(2);
//
// var res1 = benefitService.findByUser(user);
// System.out.println("Benefits have:" + user.getId());
// for(var res: res1)
// {
// System.out.println(res.getId());
// }
//
// var res2 = benefitService.findByUserNot(user);
// System.out.println("Benefits havent:" + user.getId());
// for(var res: res2)
// {
// System.out.println(res.getId());
// }
// }
//
// @Test
// void findAchievements(){
// User user = userService.findById(2);
//
// var res1 = achievementService.findByUser(user);
// System.out.println("chievement have:" + user.getId());
// for(var res: res1)
// {
// System.out.println(res.getId());
// }
//
// var res2 = achievementService.findByUserNot(user);
// System.out.println("chievement havent:" + user.getId());
// for(var res: res2)
// {
// System.out.println(res.getId());
// }
// }
//
// @Test
// void findTraining(){
// var res = trainingService.findByTitle("пи");
// for(var r: res)
// {
// System.out.println(r.getTitle());
// }
// }
//
// @Test
// void findOrders(){
// var res = orderService.findByNameOrSnils(1,"111");
// for(var r: res)
// {
// System.out.println(r.getUser().getName());
// }
// }
// @Test
// void findOrdersBenefit(){
// var res = orderService.findForBenefit(1);
// System.out.println("benefit");
// for(var r: res)
// {
// System.out.println(r.getUser().getName());
// }
//
// var res2 = orderService.findForBasic(1);
// System.out.println("basic");
// for(var r: res2)
// {
// System.out.println(r.getUser().getName());
// }
// }
}