From 8082eb8c65aeff5555b2bf1a02217c2502b3334e Mon Sep 17 00:00:00 2001 From: yukudaa Date: Wed, 27 Mar 2024 13:36:01 +0900 Subject: [PATCH] feat/16-save-lesson --- .../org/doochul/application/LessonService.java | 17 ++++++++++++----- .../java/org/doochul/domain/lesson/Lesson.java | 18 ++++-------------- .../domain/lesson/LessonRepository.java | 3 ++- .../java/org/doochul/ui/LessonController.java | 18 +++++++++--------- .../java/org/doochul/ui/dto/LessonRequest.java | 16 ++++++++++++++++ .../org/doochul/ui/dto/LessonResponse.java | 9 +++------ 6 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 doochul/src/main/java/org/doochul/ui/dto/LessonRequest.java diff --git a/doochul/src/main/java/org/doochul/application/LessonService.java b/doochul/src/main/java/org/doochul/application/LessonService.java index 4dcd074..466e3b8 100644 --- a/doochul/src/main/java/org/doochul/application/LessonService.java +++ b/doochul/src/main/java/org/doochul/application/LessonService.java @@ -3,10 +3,15 @@ import lombok.RequiredArgsConstructor; import org.doochul.domain.lesson.Lesson; import org.doochul.domain.lesson.LessonRepository; +import org.doochul.domain.membership.MemberShip; +import org.doochul.domain.membership.MemberShipRepository; import org.doochul.domain.user.User; import org.doochul.domain.user.UserRepository; -import org.doochul.ui.dto.LessonResponse; +import org.doochul.ui.dto.LessonRequest; + import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + import java.util.List; @Service @@ -14,11 +19,13 @@ public class LessonService { private final LessonRepository lessonRepository; - private final UserRepository userRepository; + private final MemberShipRepository memberShipRepository; - public Long save(final Long userId, final LessonResponse lessonResponse) { - final User user = userRepository.findById(userId).orElseThrow(); - return lessonRepository.save(Lesson.of(user,lessonResponse)).getId(); + @Transactional + // request 받아서 save 하는 로직 + public Long save(final Long membershipId, final LessonRequest lessonRequest) { + final MemberShip memberShip = memberShipRepository.findById(membershipId).orElseThrow(); + return lessonRepository.save(Lesson.of(memberShip, lessonRequest.getStartedAt(), lessonRequest.getEndedAt(), lessonRequest.getRecord())).getId(); } diff --git a/doochul/src/main/java/org/doochul/domain/lesson/Lesson.java b/doochul/src/main/java/org/doochul/domain/lesson/Lesson.java index b6d4c32..09c9c88 100644 --- a/doochul/src/main/java/org/doochul/domain/lesson/Lesson.java +++ b/doochul/src/main/java/org/doochul/domain/lesson/Lesson.java @@ -15,6 +15,7 @@ import org.doochul.domain.BaseEntity; import org.doochul.domain.membership.MemberShip; import org.doochul.domain.user.User; +import org.doochul.ui.dto.LessonRequest; import org.doochul.ui.dto.LessonResponse; @Entity @@ -30,31 +31,20 @@ public class Lesson extends BaseEntity { @JoinColumn(name = "membership_id") private MemberShip memberShip; - @ManyToOne - @JoinColumn(name = "general_id") - private User general; - - @ManyToOne - @JoinColumn(name = "teacher_id") - private User teacher; - private LocalDateTime startedAt; private LocalDateTime endedAt; private String record; - private Lesson(Long id, MemberShip memberShip, User general, User teacher, LocalDateTime startedAt, LocalDateTime endedAt, String record) { + public Lesson(Long id, MemberShip memberShip, LocalDateTime startedAt, LocalDateTime endedAt, String record) { this.id = id; this.memberShip = memberShip; - this.general = general; - this.teacher = teacher; this.startedAt = startedAt; this.endedAt = endedAt; this.record = record; } - - public static Lesson of(User user,LessonResponse lessonResponse) { - return new Lesson(lessonResponse.id(), lessonResponse.memberShip(), user, user,lessonResponse.startedAt(), lessonResponse.endedAt(), lessonResponse.record()); + public static Lesson of(MemberShip memberShip, LocalDateTime startedAt, LocalDateTime endedAt, String record) { + return new Lesson(null, memberShip,startedAt,endedAt,record); } } diff --git a/doochul/src/main/java/org/doochul/domain/lesson/LessonRepository.java b/doochul/src/main/java/org/doochul/domain/lesson/LessonRepository.java index ed5cdc0..b2fc77b 100644 --- a/doochul/src/main/java/org/doochul/domain/lesson/LessonRepository.java +++ b/doochul/src/main/java/org/doochul/domain/lesson/LessonRepository.java @@ -2,15 +2,16 @@ import org.doochul.domain.product.Product; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; import java.time.LocalDateTime; import java.util.List; +@Repository public interface LessonRepository extends JpaRepository { default Lesson getById(final Long id) { return findById(id).orElseThrow(() -> new IllegalArgumentException("해당 수업이 없습니다.")); } - //List findByUserId(Long userId); List findByStartedAtBefore(final LocalDateTime currentServerTime); } diff --git a/doochul/src/main/java/org/doochul/ui/LessonController.java b/doochul/src/main/java/org/doochul/ui/LessonController.java index 79be02a..98ff921 100644 --- a/doochul/src/main/java/org/doochul/ui/LessonController.java +++ b/doochul/src/main/java/org/doochul/ui/LessonController.java @@ -2,14 +2,15 @@ import lombok.RequiredArgsConstructor; import org.doochul.application.LessonService; -import org.doochul.application.ProductService; +import org.doochul.domain.lesson.Lesson; +import org.doochul.domain.user.User; + +import org.doochul.ui.dto.LessonRequest; import org.doochul.ui.dto.LessonResponse; -import org.doochul.ui.dto.ProductResponse; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.net.URI; -import java.util.List; + @RestController @RequiredArgsConstructor @@ -17,10 +18,9 @@ public class LessonController { private final LessonService lessonService; - - @PostMapping("/save") - public ResponseEntity save(final Long userId, @RequestBody LessonResponse lessonResponse) { - Long lessonId = lessonService.save(userId, lessonResponse); - return ResponseEntity.created(URI.create("/lesson/"+ lessonId)).build(); + @PostMapping("/save/{membershipId}") + public Long save(final @PathVariable Long membershipId, final @RequestBody LessonRequest lessonRequest) { + return lessonService.save(membershipId,lessonRequest); } + } diff --git a/doochul/src/main/java/org/doochul/ui/dto/LessonRequest.java b/doochul/src/main/java/org/doochul/ui/dto/LessonRequest.java new file mode 100644 index 0000000..52f74e2 --- /dev/null +++ b/doochul/src/main/java/org/doochul/ui/dto/LessonRequest.java @@ -0,0 +1,16 @@ +package org.doochul.ui.dto; + +import lombok.Getter; +import org.doochul.domain.lesson.Lesson; +import org.doochul.domain.membership.MemberShip; +import org.doochul.domain.user.User; + +import java.time.LocalDateTime; + +@Getter +public class LessonRequest { + private LocalDateTime startedAt; + private LocalDateTime endedAt; + private String record; + +} diff --git a/doochul/src/main/java/org/doochul/ui/dto/LessonResponse.java b/doochul/src/main/java/org/doochul/ui/dto/LessonResponse.java index c5dde39..508f458 100644 --- a/doochul/src/main/java/org/doochul/ui/dto/LessonResponse.java +++ b/doochul/src/main/java/org/doochul/ui/dto/LessonResponse.java @@ -11,18 +11,15 @@ public record LessonResponse( Long id, MemberShip memberShip, - User general, - User teacher, LocalDateTime startedAt, LocalDateTime endedAt, String record ) { - public static LessonResponse from(Lesson lesson) { - return new LessonResponse(lesson.getId(), lesson.getMemberShip(), lesson.getGeneral(), - lesson.getTeacher(), lesson.getStartedAt(), lesson.getEndedAt(),lesson.getRecord()); + public static LessonResponse from(final Lesson lesson) { + return new LessonResponse(lesson.getId(), lesson.getMemberShip(), lesson.getStartedAt(), lesson.getEndedAt(),lesson.getRecord()); } - public static List fromList(List lessons) { + public static List fromList(final List lessons) { return lessons.stream() .map(LessonResponse::from) .toList();