1. 일정관리 앱 Lv2. 유저 CRUD 생성과 일정 CRUD와 연결
- 연관관계 구현
- 일정은 이제
작성 유저명 필드 대신 유저 고유 식별자 필드를 가집니다.
2. github 주소
3. 유저와 일정의 연결
public class Schedule extends BaseEntity{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String scheduleTitle;
private String scheduleContent;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", nullable = false)
private User user;
public Schedule(User user, String scheduleTitle, String scheduleContent) {
this.user = user;
this.scheduleTitle = scheduleTitle;
this.scheduleContent = scheduleContent;
}
@PostMapping("/users/{userId}schedules")
public ResponseEntity<ScheduleCreateResponse> createSchedule(
@PathVariable Long userId,
@Valid @RequestBody ScheduleCreateRequest request
){
return ResponseEntity.status(HttpStatus.CREATED).body(scheduleService.save(userId, request));
}
@Transactional
public ScheduleCreateResponse save(Long userId, ScheduleCreateRequest request) {
User user = userRepository.findById(userId).orElseThrow(
() -> new IllegalStateException("없는 유저입니다.")
);