From af8a69b83fb9d56243cf37c5c74431fd0aeaa3b6 Mon Sep 17 00:00:00 2001 From: esoe Date: Tue, 14 May 2024 15:23:25 +0300 Subject: [PATCH] course api done --- .../src/main/docs/erd/erd-course.puml | 2 +- .../controllers/CourseController.java | 69 +++++++++++++++++++ .../controllers/ProgramController.java | 6 +- .../resourceserviceapi/entities/Course.java | 47 +++++++++++++ .../resourceserviceapi/entities/Program.java | 2 - .../repositories/CourseRepositoryFace.java | 12 ++++ 6 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/CourseController.java create mode 100644 resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/entities/Course.java create mode 100644 resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/repositories/CourseRepositoryFace.java diff --git a/resource-service-api/src/main/docs/erd/erd-course.puml b/resource-service-api/src/main/docs/erd/erd-course.puml index db9cd16..748f023 100644 --- a/resource-service-api/src/main/docs/erd/erd-course.puml +++ b/resource-service-api/src/main/docs/erd/erd-course.puml @@ -22,7 +22,7 @@ table(courses){ column(report_period): VARCHAR[80] >>"Отчетный период" foreign_key(teacher_id): VARCHAR[80] >>"Идентификатор преподавателя" foreign_key(programm_id): INTEGER >>"Идентификатор программы обучения" - foreign_key(building_object_id): INTEGER >>"Идентификатор объекта строительства" + foreign_key(building_id): INTEGER >>"Идентификатор объекта строительства" } table(building_objects){ diff --git a/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/CourseController.java b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/CourseController.java new file mode 100644 index 0000000..c0678eb --- /dev/null +++ b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/CourseController.java @@ -0,0 +1,69 @@ +package ru.molokoin.resourceserviceapi.controllers; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import ru.molokoin.resourceserviceapi.entities.Course; +import ru.molokoin.resourceserviceapi.repositories.CourseRepositoryFace; + +@RestController +@RequestMapping(path = "/", consumes = {"*/*"}) +public class CourseController { + @Autowired + private CourseRepositoryFace repo; + + @GetMapping("/course/list") + public ResponseEntity> getCourses(){ + return new ResponseEntity<>(repo.findAll(), HttpStatus.OK); + } + + @GetMapping("/course/{id}") + public ResponseEntity getCourseByID(@PathVariable Integer id){ + return new ResponseEntity<>(repo.findCourseById(id), HttpStatus.OK); + } + + @PostMapping(path = "/course/create", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity saveCourse(@RequestBody Course course) { + repo.save(course); + return new ResponseEntity<>(course, HttpStatus.CREATED); + } + + @PutMapping(path = "/course/update/{id}", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity updateCourse(@PathVariable Integer id, @RequestBody Course course) { + Course c = repo.findCourseById(id); + c.setPlace(course.getPlace()); + c.setStart_date(course.getStart_date()); + c.setProtocol_date(course.getProtocol_date()); + c.setProtocol_number(course.getProtocol_number()); + c.setReport_period(course.getReport_period()); + c.setTeacher(course.getTeacher()); + c.setProgram(course.getProgram()); + c.setBuilding(course.getBuilding()); + repo.save(c); + return new ResponseEntity<>(repo.findCourseById(id), HttpStatus.CREATED); + } + + @DeleteMapping("/course/delete/{id}") + public ResponseEntity deleteCourse(@PathVariable Long id){ + Course c = repo.findCourseById(id); + System.out.println(c.toString()); + repo.delete(c); + return new ResponseEntity<>("Запись id#" + id + " удалена ... ", HttpStatus.OK); + } +} diff --git a/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/ProgramController.java b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/ProgramController.java index eabdaf0..a1d0a55 100644 --- a/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/ProgramController.java +++ b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/ProgramController.java @@ -30,14 +30,14 @@ public class ProgramController { } @GetMapping("/program/{id}") - public ResponseEntity getTeacherByID(@PathVariable Integer id){ + public ResponseEntity getProgramByID(@PathVariable Integer id){ return new ResponseEntity<>(repo.findProgramById(id), HttpStatus.OK); } @PostMapping(path = "/program/create", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity saveTeacher(@RequestBody Program program) { + public ResponseEntity saveProgram(@RequestBody Program program) { repo.save(program); return new ResponseEntity<>(program, HttpStatus.CREATED); } @@ -57,7 +57,7 @@ public class ProgramController { } @DeleteMapping("/program/delete/{id}") - public ResponseEntity deleteCretarea(@PathVariable Long id){ + public ResponseEntity deleteProgram(@PathVariable Long id){ Program p = repo.findProgramById(id); System.out.println(p.toString()); repo.delete(p); diff --git a/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/entities/Course.java b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/entities/Course.java new file mode 100644 index 0000000..168cc36 --- /dev/null +++ b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/entities/Course.java @@ -0,0 +1,47 @@ +package ru.molokoin.resourceserviceapi.entities; + +import java.util.Date; + +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + +/** + * Сведения о курсе (учебном потоке) + */ +@NoArgsConstructor +@AllArgsConstructor +@Entity +@Data +public class Course { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private long id;//уникальный идентификатор курса + private String place;//место проведения занятий + private Date start_date;//дата начала курса + private Date protocol_date;//дата протокола + private String protocol_number;//номер протоколаssss + private String report_period;//отчетный период (наименование месяца) + + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @OnDelete(action = OnDeleteAction.SET_NULL) + private Teacher teacher;//сведения о преподаватле + + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @OnDelete(action = OnDeleteAction.SET_NULL) + private Program program;//сведения о программе обучения + + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @OnDelete(action = OnDeleteAction.SET_NULL) + private Building building;//сведения об объекте строительства +} diff --git a/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/entities/Program.java b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/entities/Program.java index ef73760..ec7bde1 100644 --- a/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/entities/Program.java +++ b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/entities/Program.java @@ -3,7 +3,6 @@ package ru.molokoin.resourceserviceapi.entities; import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; -import jakarta.annotation.Nullable; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; @@ -29,6 +28,5 @@ public class Program { @ManyToOne(fetch = FetchType.EAGER, optional = false) @OnDelete(action = OnDeleteAction.SET_NULL) - // @Nullable private ProgramCretarea cretarea; } diff --git a/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/repositories/CourseRepositoryFace.java b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/repositories/CourseRepositoryFace.java new file mode 100644 index 0000000..1afdf4c --- /dev/null +++ b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/repositories/CourseRepositoryFace.java @@ -0,0 +1,12 @@ +package ru.molokoin.resourceserviceapi.repositories; + +import java.util.List; + +import org.springframework.data.repository.ListCrudRepository; + +import ru.molokoin.resourceserviceapi.entities.Course; + +public interface CourseRepositoryFace extends ListCrudRepository{ + List findAll(); + Course findCourseById(long id); +}