Skip to content

Commit

Permalink
feat/16-save-lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
yukudaa committed Mar 27, 2024
1 parent 7f7c569 commit 8082eb8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
17 changes: 12 additions & 5 deletions doochul/src/main/java/org/doochul/application/LessonService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
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
@RequiredArgsConstructor
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();
}


Expand Down
18 changes: 4 additions & 14 deletions doochul/src/main/java/org/doochul/domain/lesson/Lesson.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Lesson, Long> {
default Lesson getById(final Long id) {
return findById(id).orElseThrow(() -> new IllegalArgumentException("해당 수업이 없습니다."));
}
//List<Lesson> findByUserId(Long userId);
List<Lesson> findByStartedAtBefore(final LocalDateTime currentServerTime);

}
18 changes: 9 additions & 9 deletions doochul/src/main/java/org/doochul/ui/LessonController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

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
@RequestMapping("/lesson")
public class LessonController {

private final LessonService lessonService;

@PostMapping("/save")
public ResponseEntity<Void> 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);
}

}
16 changes: 16 additions & 0 deletions doochul/src/main/java/org/doochul/ui/dto/LessonRequest.java
Original file line number Diff line number Diff line change
@@ -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;

}
9 changes: 3 additions & 6 deletions doochul/src/main/java/org/doochul/ui/dto/LessonResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<LessonResponse> fromList(List<Lesson> lessons) {
public static List<LessonResponse> fromList(final List<Lesson> lessons) {
return lessons.stream()
.map(LessonResponse::from)
.toList();
Expand Down

0 comments on commit 8082eb8

Please sign in to comment.