diff --git a/backend/src/main/java/com/magistr/app/controller/TestController.java b/backend/src/main/java/com/magistr/app/controller/TestController.java new file mode 100644 index 0000000..25422d4 --- /dev/null +++ b/backend/src/main/java/com/magistr/app/controller/TestController.java @@ -0,0 +1,63 @@ +package com.magistr.app.controller; + +import com.magistr.app.dto.CreateLessonRequest; +import com.magistr.app.dto.LessonResponse; +import com.magistr.app.model.Lesson; +import com.magistr.app.repository.LessonRepository; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/api/users/test") +public class TestController { + + private final LessonRepository lessonRepository; + + public TestController(LessonRepository lessonRepository) { + this.lessonRepository = lessonRepository; + } + + @PostMapping("/create") + public ResponseEntity createLesson(@RequestBody CreateLessonRequest request) { + if (request.getTeacherId() == null || request.getTeacherId() == 0) { + return ResponseEntity.badRequest().body(Map.of("message", "ID преподавателя обязателен")); + } + if (request.getGroupId() == null || request.getGroupId() == 0) { + return ResponseEntity.badRequest().body(Map.of("message", "ID группы обязателен")); + } + if (request.getLessonId() == null || request.getLessonId() == 0) { + return ResponseEntity.badRequest().body(Map.of("message", "ID предмета обязателен")); + } + if (request.getDay() == null || request.getDay().isBlank()) { + return ResponseEntity.badRequest().body(Map.of("message", "Выбор дня обязателен")); + } + if (request.getWeek() == null || request.getWeek().isBlank()) { + return ResponseEntity.badRequest().body(Map.of("message", "Выбор недели обязателен")); + } + if (request.getTime() == null || request.getTime().isBlank()) { + return ResponseEntity.badRequest().body(Map.of("message", "Время обязательно")); + } + + Lesson lesson = new Lesson(); + lesson.setTeacherId(request.getTeacherId()); + lesson.setLessonId(request.getLessonId()); + lesson.setGroupId(request.getGroupId()); + lesson.setDay(request.getDay()); + lesson.setWeek(request.getWeek()); + lesson.setTime(request.getTime()); + lessonRepository.save(lesson); + + return ResponseEntity.ok(new LessonResponse(lesson.getId(), lesson.getDay(), lesson.getWeek(), lesson.getTime())); + } + + @GetMapping + public List getAllLessons() { + return lessonRepository.findAll().stream() + .map(l -> new LessonResponse(l.getTeacherId(), l.getLessonId(), l.getDay(), l.getWeek(), l.getTime())) + .toList(); + } + +} \ No newline at end of file diff --git a/backend/src/main/java/com/magistr/app/dto/CreateLessonRequest.java b/backend/src/main/java/com/magistr/app/dto/CreateLessonRequest.java new file mode 100644 index 0000000..4f4428c --- /dev/null +++ b/backend/src/main/java/com/magistr/app/dto/CreateLessonRequest.java @@ -0,0 +1,62 @@ +package com.magistr.app.dto; + +public class CreateLessonRequest { + + private Long teacherId; + private Long groupId; + private Long lessonId; + private String day; + private String week; + private String time; + + public CreateLessonRequest() { + } + + public Long getTeacherId() { + return teacherId; + } + + public void setTeacherId(Long teacherId) { + this.teacherId = teacherId; + } + + public Long getGroupId() { + return groupId; + } + + public void setGroupId(Long groupId) { + this.groupId = groupId; + } + + public Long getLessonId() { + return lessonId; + } + + public void setLessonId(Long lessonId) { + this.lessonId= lessonId; + } + + public String getDay() { + return day; + } + + public void setDay(String day) { + this.day = day; + } + + public String getWeek() { + return week; + } + + public void setWeek(String week) { + this.week = week; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/magistr/app/dto/LessonResponse.java b/backend/src/main/java/com/magistr/app/dto/LessonResponse.java new file mode 100644 index 0000000..15151f9 --- /dev/null +++ b/backend/src/main/java/com/magistr/app/dto/LessonResponse.java @@ -0,0 +1,77 @@ +package com.magistr.app.dto; + +public class LessonResponse { + + private Long teacherId; + private Long groupId; + private Long lessonId; + private String day; + private String week; + private String time; + + public LessonResponse() { + } + + public LessonResponse(Long lessonId, String day, String week, String time) { + this.lessonId = lessonId; + this.day = day; + this.week = week; + this.time = time; + } + + public LessonResponse(Long teacherId, Long lessonId, String day, String week, String time) { + this.teacherId = teacherId; + this.lessonId = lessonId; + this.day = day; + this.week = week; + this.time = time; + } + + public Long getTeacherId() { + return teacherId; + } + + public void setTeacherId(Long teacherId) { + this.teacherId = teacherId; + } + + public Long getGroupId() { + return groupId; + } + + public void setGroupId(Long groupId) { + this.groupId = groupId; + } + + public Long getLessonId() { + return lessonId; + } + + public void setLessonId(Long lessonId) { + this.lessonId= lessonId; + } + + public String getDay() { + return day; + } + + public void setDay(String day) { + this.day = day; + } + + public String getWeek() { + return week; + } + + public void setWeek(String week) { + this.week = week; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } +} diff --git a/backend/src/main/java/com/magistr/app/model/Lesson.java b/backend/src/main/java/com/magistr/app/model/Lesson.java new file mode 100644 index 0000000..2ef4bb6 --- /dev/null +++ b/backend/src/main/java/com/magistr/app/model/Lesson.java @@ -0,0 +1,89 @@ +package com.magistr.app.model; + +import jakarta.persistence.*; + +@Entity +@Table(name = "lessons") +public class Lesson { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private Long teacherId; + + @Column(nullable = false) + private Long groupId; + + @Column(nullable = false) + private Long lessonId; + + @Column(nullable = false, length = 255) + private String day; + + @Column(nullable = false, length = 255) + private String week; + + @Column(nullable = false, length = 255) + private String time; + + public Lesson() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getTeacherId() { + return teacherId; + } + + public void setTeacherId(Long teacherId) { + this.teacherId = teacherId; + } + + public Long getGroupId() { + return groupId; + } + + public void setGroupId(Long groupId) { + this.groupId = groupId; + } + + public Long getLessonId() { + return lessonId; + } + + public void setLessonId(Long lessonId) { + this.lessonId = lessonId; + } + + public String getDay() { + return day; + } + + public void setDay(String day) { + this.day = day; + } + + public String getWeek() { + return week; + } + + public void setWeek(String week) { + this.week = week; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } +} diff --git a/backend/src/main/java/com/magistr/app/repository/LessonRepository.java b/backend/src/main/java/com/magistr/app/repository/LessonRepository.java new file mode 100644 index 0000000..c0fe030 --- /dev/null +++ b/backend/src/main/java/com/magistr/app/repository/LessonRepository.java @@ -0,0 +1,11 @@ +package com.magistr.app.repository; + +import com.magistr.app.model.Lesson; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface LessonRepository extends JpaRepository { + + Optional findByLessonId(Long lessonId); +} diff --git a/db/init/init.sql b/db/init/init.sql index a504d82..480f53f 100644 --- a/db/init/init.sql +++ b/db/init/init.sql @@ -25,3 +25,21 @@ CREATE TABLE IF NOT EXISTS student_groups ( name VARCHAR(100) UNIQUE NOT NULL, education_form_id BIGINT NOT NULL REFERENCES education_forms(id) ); + +CREATE TABLE IF NOT EXISTS lessons ( + id BIGSERIAL PRIMARY KEY, + teacher_id BIGINT NOT NULL, + group_id BIGINT NOT NULL, + lesson_id BIGINT NOT NULL, + day VARCHAR(255) NOT NULL, + week VARCHAR(255) NOT NULL, + time VARCHAR(255) NOT NULL +); + +ALTER TABLE lessons + ADD CONSTRAINT fk_lessons_teacher + FOREIGN KEY (teacher_id) REFERENCES users(id); + +ALTER TABLE lessons + ADD CONSTRAINT fk_lessons_group + FOREIGN KEY (group_id) REFERENCES student_groups(id); \ No newline at end of file