From 591f84718abb19d9f4553cbaf6f0a9951af45ae0 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Fri, 21 Apr 2023 14:22:01 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB?= =?UTF-8?q?=D0=B5=D1=80=D1=8B=20=D0=B8=20DTO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IP/University/Contoller/DTO/GroupDTO.java | 21 ++++++++ .../University/Contoller/DTO/StudentDTO.java | 29 +++++++++++ .../University/Contoller/DTO/SubjectDTO.java | 21 ++++++++ .../Contoller/REST/GroupController.java | 45 +++++++++++++++++ .../Contoller/REST/StudentController.java | 49 +++++++++++++++++++ .../Contoller/REST/SubjectController.java | 47 ++++++++++++++++++ .../Contoller/StudentController.java | 9 ---- 7 files changed, 212 insertions(+), 9 deletions(-) create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/GroupDTO.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/StudentDTO.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/SubjectDTO.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/GroupController.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/StudentController.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/SubjectController.java delete mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Contoller/StudentController.java diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/GroupDTO.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/GroupDTO.java new file mode 100644 index 0000000..74c2c41 --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/GroupDTO.java @@ -0,0 +1,21 @@ +package ru.IP_LabWorks.IP.University.Contoller.DTO; + +import ru.IP_LabWorks.IP.University.Model.Group; + +public class GroupDTO { + private final long id; + private final String name; + + public GroupDTO(Group group){ + this.id = group.getId(); + this.name = group.getName(); + } + + public Long getId(){ + return id; + } + + public String getName(){ + return name; + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/StudentDTO.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/StudentDTO.java new file mode 100644 index 0000000..fb6388f --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/StudentDTO.java @@ -0,0 +1,29 @@ +package ru.IP_LabWorks.IP.University.Contoller.DTO; + +import ru.IP_LabWorks.IP.University.Model.Student; + +import java.time.LocalDate; + +public class StudentDTO { + private final long id; + private final String name; + private final LocalDate birthDate; + + public StudentDTO(Student student) { + this.id = student.getId(); + this.name = student.getName(); + this.birthDate = student.getBirthDate(); + } + + public Long getId(){ + return id; + } + + public String getName(){ + return name; + } + + public LocalDate getBirthDate(){ + return birthDate; + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/SubjectDTO.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/SubjectDTO.java new file mode 100644 index 0000000..e699597 --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/SubjectDTO.java @@ -0,0 +1,21 @@ +package ru.IP_LabWorks.IP.University.Contoller.DTO; + +import ru.IP_LabWorks.IP.University.Model.Subject; + +public class SubjectDTO { + private final long id; + private final String name; + + public SubjectDTO(Subject subject){ + this.id = subject.getId(); + this.name = subject.getName(); + } + + public Long getId(){ + return id; + } + + public String getName(){ + return name; + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/GroupController.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/GroupController.java new file mode 100644 index 0000000..aef77ac --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/GroupController.java @@ -0,0 +1,45 @@ +package ru.IP_LabWorks.IP.University.Contoller.REST; + +import org.springframework.web.bind.annotation.*; +import ru.IP_LabWorks.IP.University.Contoller.DTO.GroupDTO; +import ru.IP_LabWorks.IP.University.Service.GroupService; + +import java.util.List; + +@RestController +@RequestMapping("/group") +public class GroupController { + private final GroupService groupService; + + public GroupController(GroupService groupService) { + this.groupService = groupService; + } + + @GetMapping("/{id}") + public GroupDTO getGroup(@PathVariable Long id){ + return new GroupDTO(groupService.findGroup(id)); + } + + @GetMapping + public List getGroups(){ + return groupService.findAllGroups().stream() + .map(GroupDTO::new) + .toList(); + } + + @PostMapping + public GroupDTO createGroup(@RequestParam("name") String name){ + return new GroupDTO(groupService.addGroup(name)); + } + + @PutMapping("/{id}") + public GroupDTO updateGroup(@PathVariable Long id, + @RequestParam("name") String name){ + return new GroupDTO(groupService.updateGroup(id, name)); + } + + @DeleteMapping("/{id}") + public GroupDTO deleteGroup(@PathVariable Long id){ + return new GroupDTO(groupService.deleteGroup(id)); + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/StudentController.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/StudentController.java new file mode 100644 index 0000000..55bf1ef --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/StudentController.java @@ -0,0 +1,49 @@ +package ru.IP_LabWorks.IP.University.Contoller.REST; + +import org.springframework.web.bind.annotation.*; +import ru.IP_LabWorks.IP.University.Contoller.DTO.StudentDTO; +import ru.IP_LabWorks.IP.University.Service.StudentService; + +import java.time.LocalDate; +import java.util.List; + +@RestController +@RequestMapping("/student") +public class StudentController { + private final StudentService studentService; + + public StudentController(StudentService studentService) { + this.studentService = studentService; + } + + @GetMapping("/{id}") + public StudentDTO getStudent(@PathVariable Long id){ + return new StudentDTO(studentService.findStudent(id)); + } + + @GetMapping + public List getStudents(){ + return studentService.findAllStudents().stream() + .map(StudentDTO::new) + .toList(); + } + + @PostMapping + public StudentDTO createStudent(@RequestParam("name") String name, + @RequestParam("birthDate")LocalDate birthDate){ + return new StudentDTO(studentService.addStudent(name, birthDate)); + } + + @PutMapping("/{id}") + public StudentDTO updateStudent(@PathVariable Long id, + @RequestParam("name") String name, + @RequestParam("birthDate") LocalDate birthDate){ + return new StudentDTO(studentService.updateStudent(id, name, birthDate)); + } + + @DeleteMapping("/{id}") + public StudentDTO deleteStudent(@PathVariable Long id){ + return new StudentDTO(studentService.deleteStudent(id)); + } + +} diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/SubjectController.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/SubjectController.java new file mode 100644 index 0000000..a0c158a --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/SubjectController.java @@ -0,0 +1,47 @@ +package ru.IP_LabWorks.IP.University.Contoller.REST; + +import org.springframework.web.bind.annotation.*; +import ru.IP_LabWorks.IP.University.Contoller.DTO.GroupDTO; +import ru.IP_LabWorks.IP.University.Contoller.DTO.SubjectDTO; +import ru.IP_LabWorks.IP.University.Service.GroupService; +import ru.IP_LabWorks.IP.University.Service.SubjectService; + +import java.util.List; + +@RestController +@RequestMapping("/subject") +public class SubjectController { + private final SubjectService subjectService; + + public SubjectController(SubjectService subjectService) { + this.subjectService = subjectService; + } + + @GetMapping("/{id}") + public SubjectDTO getSubject(@PathVariable Long id){ + return new SubjectDTO(subjectService.findSubject(id)); + } + + @GetMapping + public List getSubject(){ + return subjectService.findAllSubjects().stream() + .map(SubjectDTO::new) + .toList(); + } + + @PostMapping + public SubjectDTO createSubject(@RequestParam("name") String name){ + return new SubjectDTO(subjectService.addSubject(name)); + } + + @PutMapping("/{id}") + public SubjectDTO updateSubject(@PathVariable Long id, + @RequestParam("name") String name){ + return new SubjectDTO(subjectService.updateSubject(id, name)); + } + + @DeleteMapping("/{id}") + public SubjectDTO deleteSubject(@PathVariable Long id){ + return new SubjectDTO(subjectService.deleteSubject(id)); + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/StudentController.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/StudentController.java deleted file mode 100644 index 7b727a2..0000000 --- a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/StudentController.java +++ /dev/null @@ -1,9 +0,0 @@ -package ru.IP_LabWorks.IP.University.Contoller; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/student") -public class StudentController { -}