Skip to content

Commit

Permalink
feat/16-create-lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
yukudaa committed Mar 27, 2024
1 parent 8082eb8 commit 63cbc46
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
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.LessonRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class LessonService {
Expand All @@ -22,11 +17,8 @@ public class LessonService {
private final MemberShipRepository memberShipRepository;

@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();
}


}
6 changes: 2 additions & 4 deletions doochul/src/main/java/org/doochul/domain/lesson/Lesson.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import lombok.NoArgsConstructor;
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
@Getter
Expand Down Expand Up @@ -44,7 +41,8 @@ public Lesson(Long id, MemberShip memberShip, LocalDateTime startedAt, LocalDate
this.endedAt = endedAt;
this.record = record;
}

public static Lesson of(MemberShip memberShip, LocalDateTime startedAt, LocalDateTime endedAt, String record) {
return new Lesson(null, memberShip,startedAt,endedAt,record);
return new Lesson(null, memberShip, startedAt, endedAt, record);
}
}
12 changes: 2 additions & 10 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,17 @@

import lombok.RequiredArgsConstructor;
import org.doochul.application.LessonService;
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.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;



@RestController
@RequiredArgsConstructor
@RequestMapping("/lesson")
public class LessonController {

private final LessonService lessonService;

@PostMapping("/save/{membershipId}")
public Long save(final @PathVariable Long membershipId, final @RequestBody LessonRequest lessonRequest) {
return lessonService.save(membershipId,lessonRequest);
return lessonService.save(membershipId, lessonRequest);
}

}
5 changes: 1 addition & 4 deletions doochul/src/main/java/org/doochul/ui/dto/LessonResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ public record LessonResponse(
String record
) {
public static LessonResponse from(final Lesson lesson) {
return new LessonResponse(lesson.getId(), lesson.getMemberShip(), lesson.getStartedAt(), lesson.getEndedAt(),lesson.getRecord());
return new LessonResponse(lesson.getId(), lesson.getMemberShip(), lesson.getStartedAt(), lesson.getEndedAt(), lesson.getRecord());
}

public static List<LessonResponse> fromList(final List<Lesson> lessons) {
return lessons.stream()
.map(LessonResponse::from)
.toList();
}



}

0 comments on commit 63cbc46

Please sign in to comment.