Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Study Controller, Service 추가 #11

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
22dd9b1
feat: 스터디 생성 기능 추가
ybkang1108 Jan 16, 2025
90ad73f
feat: StudyCreateResponse DTO 추가
ybkang1108 Jan 16, 2025
2ea0826
feat: 스터디 전체 목록 조회 기능 추가
ybkang1108 Jan 16, 2025
3310c20
feat: 스터디 정보 조회 기능 추가
ybkang1108 Jan 16, 2025
b700946
feat: 스터디 삭제 기능 추가
ybkang1108 Jan 16, 2025
e99cab5
refactor: delete, detail response dto수정 및 cascade 설정
ybkang1108 Jan 17, 2025
52858dc
feat: 스터디 수정 기능 추가
ybkang1108 Jan 17, 2025
33b6e77
refactor: response에 메시지 제거
ybkang1108 Jan 17, 2025
baf1a6e
refactor: dto를 record로 변경
ybkang1108 Jan 21, 2025
e45b8ad
feat: exception 추가
ybkang1108 Jan 21, 2025
a412bbf
refactor: curriculum 수정
ybkang1108 Jan 22, 2025
2a4f89f
feat: day에 create 추가
ybkang1108 Jan 22, 2025
d864b11
refactor: record로 변경
ybkang1108 Jan 22, 2025
eb57a8f
feat: custom exception 추가
ybkang1108 Jan 22, 2025
6633caf
refactor: record로 변경
ybkang1108 Jan 22, 2025
047112b
feat: study 도메인에 create, update 추가
ybkang1108 Jan 22, 2025
2ce3d78
refactor: study service 수정
ybkang1108 Jan 22, 2025
889051a
refactor: study controller 수정
ybkang1108 Jan 22, 2025
ee5864b
refactor: springdoc 버전 수정
ybkang1108 Jan 23, 2025
ff5eb99
refactor: 불필요한 생성자 제거
ybkang1108 Jan 24, 2025
31ad47d
refactor: create, update 메소드 도메인으로 이동
ybkang1108 Jan 24, 2025
872757d
fix: 잘못된 로직 수정
ybkang1108 Jan 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.Setter;
import lombok.*;

@Entity
@Builder
@Getter
@Setter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
public class Curriculum {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gdgoc.study_group.curriculum.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class CurriculumDTO {
private Integer week;
private String subject;
}
10 changes: 7 additions & 3 deletions src/main/java/com/gdgoc/study_group/day/domain/Day.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gdgoc.study_group.day.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.gdgoc.study_group.study.domain.Study;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand All @@ -8,12 +9,13 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalTime;
import lombok.Getter;
import lombok.Setter;
import lombok.*;

@Entity
@Builder
@Getter
@Setter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
public class Day {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -24,5 +26,7 @@ public class Day {
private Study study;

private String day;

@JsonFormat(pattern = "HH:mm")
private LocalTime startTime;
}
15 changes: 15 additions & 0 deletions src/main/java/com/gdgoc/study_group/day/dto/DayDTO.java
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DTO 는 record로 만드는걸로 통일하면 어떨까요
JsonFormat 은 compact constructor 에 작성하면 될거 같아요

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.gdgoc.study_group.day.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalTime;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class DayDTO {
private String day;

@JsonFormat(pattern = "HH:mm") // "startTime": "14:00" 형식으로 입력
private LocalTime startTime;
}
62 changes: 62 additions & 0 deletions src/main/java/com/gdgoc/study_group/study/api/StudyController.java
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.gdgoc.study_group.study.api;

import com.gdgoc.study_group.study.application.StudyService;
import com.gdgoc.study_group.study.dao.StudyRepository;
import com.gdgoc.study_group.study.dto.*;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/studies")
public class StudyController {

public final StudyService studyService;
private final StudyRepository studyRepository;
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved

public StudyController(StudyService studyService, StudyRepository studyRepository) {
this.studyService = studyService;
this.studyRepository = studyRepository;
}

@PostMapping()
public ResponseEntity<StudyCreateResponse> createStudy(@RequestBody StudyCreateRequest request) {
StudyCreateResponse newStudy = studyService.createStudy(request);

return ResponseEntity.status(HttpStatus.CREATED).body(newStudy);
}

@GetMapping()
public ResponseEntity<List<StudyListResponse>> getStudyList() {
List<StudyListResponse> studyList = studyService.getStudyList();

return ResponseEntity.status(HttpStatus.OK).body(studyList);
}

@GetMapping("/{studyId}")
public ResponseEntity<?> getStudyDetail(@PathVariable("studyId") Long studyId) {
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
StudyDetailResponse studyDetail = studyService.getStudyDetail(studyId);

if (studyDetail == null) {
MessageResponse messageResponse = MessageResponse.builder().message("해당하는 스터디가 없습니다.").build();

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(messageResponse);
}

return ResponseEntity.status(HttpStatus.OK).body(studyDetail);
}

@DeleteMapping("/{studyId}")
public ResponseEntity<MessageResponse> deleteStudy(@PathVariable("studyId") Long studyId) {
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved

MessageResponse messageResponse = studyService.deleteStudy(studyId);

if (messageResponse == null) {
messageResponse = MessageResponse.builder().message("스터디가 삭제되었습니다.").build();
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(messageResponse);
}

return ResponseEntity.status(HttpStatus.OK).body(messageResponse);
}
}
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package com.gdgoc.study_group.study.application;

import com.gdgoc.study_group.curriculum.domain.Curriculum;
import com.gdgoc.study_group.curriculum.dto.CurriculumDTO;
import com.gdgoc.study_group.day.domain.Day;
import com.gdgoc.study_group.day.dto.DayDTO;
import com.gdgoc.study_group.study.dao.StudyRepository;
import com.gdgoc.study_group.study.domain.Study;
import com.gdgoc.study_group.study.dto.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;

/** 예외처리 + 권한 확인 아직 안 함.... 빨리 하겠슴다 */
@Service
public class StudyService {

public final StudyRepository studyRepository;

public StudyService(StudyRepository studyRepository) {
this.studyRepository = studyRepository;
}

/**
* 스터디를 생성합니다.
*
* @param createRequest 스터디 생성 DTO
* @return ResponseDTO 반환
*/
public StudyCreateResponse createStudy(StudyCreateRequest createRequest) {

Study study =
Study.builder()
.name(createRequest.getName())
.description(createRequest.getDescription())
.requirement(createRequest.getRequirements())
.question(createRequest.getQuestion())
.maxParticipants(createRequest.getMaxParticipants())
.studyStatus(createRequest.getStudyStatus())
.curriculums(new ArrayList<>()) // 빈 리스트로 초기화
.days(new ArrayList<>()) // 빈 리스트로 초기화
.isApplicationClosed(false) // 스터디 생성할 때 false로 설정
.build();

if (createRequest.getCurriculums() != null) {
for (CurriculumDTO curriculumDTO : createRequest.getCurriculums()) {
Curriculum curriculum =
Curriculum.builder()
.study(study)
.week(curriculumDTO.getWeek())
.subject(curriculumDTO.getSubject())
.build();
study.getCurriculums().add(curriculum);
}
}

if (createRequest.getDays() != null) {
for (DayDTO dayDTO : createRequest.getDays()) {
Day day =
Day.builder()
.study(study)
.day(dayDTO.getDay())
.startTime(dayDTO.getStartTime())
.build();
study.getDays().add(day);
}
}

studyRepository.save(study);

return StudyCreateResponse.builder().message("스터디가 생성되었습니다").id(study.getId()).build();
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 스터디 전체 목록을 조회합니다.
*
* @return 스터디 전체 목록 리스트
*/
public List<StudyListResponse> getStudyList() {
List<Study> studyList = studyRepository.findAll();
List<StudyListResponse> listResponse = new ArrayList<>();

for (Study study : studyList) {
StudyListResponse studyListResponse =
StudyListResponse.builder()
.id(study.getId())
.name(study.getName())
.description(study.getDescription())
.status(study.getStudyStatus())
.build();
listResponse.add(studyListResponse);
}

return listResponse;
}

/**
* 스터디 상세 정보를 조회합니다.
*
* @param studyId 조회할 스터디 아이디
* @return 스터디 정보 반환
*/
public StudyDetailResponse getStudyDetail(Long studyId) {
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
Optional<Study> study = studyRepository.findById(studyId);

if (study.isPresent()) { // 해당 아이디를 가진 스터디가 존재할 때
StudyDetailResponse detailResponse =
StudyDetailResponse.builder()
.id(studyId)
.name(study.get().getName())
.description(study.get().getDescription())
.requirement(study.get().getRequirement())
.question(study.get().getQuestion())
.curriculums(new ArrayList<>())
.days(new ArrayList<>())
.maxParticipants(study.get().getMaxParticipants())
.isApplicationClosed(study.get().getIsApplicationClosed())
.build();

// curriculum이 있다면 curriculumDTO로 변환해 response에 추가
if (study.get().getCurriculums() != null) {
for (Curriculum curriculum : study.get().getCurriculums()) {
CurriculumDTO curriculumDTO =
CurriculumDTO.builder()
.week(curriculum.getWeek())
.subject(curriculum.getSubject())
.build();
detailResponse.getCurriculums().add(curriculumDTO);
}
}

// day가 있다면 dayDTO로 변환해 response에 추가
if (study.get().getDays() != null) {
for (Day day : study.get().getDays()) {
DayDTO dayDTO = DayDTO.builder().day(day.getDay()).startTime(day.getStartTime()).build();
detailResponse.getDays().add(dayDTO);
}
}

return detailResponse;
}
return null;
}

/**
* 스터디장 권한 확인 필요 스터디를 삭제합니다
*
* @param studyId 삭제할 스터디의 아이디
* @return 삭제 완료 메시지를 담은 MessageResponse DTO
*/
public MessageResponse deleteStudy(Long studyId) {
Optional<Study> study = studyRepository.findById(studyId);

if (study.isEmpty()) {
return null;
}

studyRepository.deleteById(studyId);
return MessageResponse.builder().message("스터디가 삭제되었습니다.").build();
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 0 additions & 7 deletions src/main/java/com/gdgoc/study_group/study/domain/Status.java

This file was deleted.

9 changes: 7 additions & 2 deletions src/main/java/com/gdgoc/study_group/study/domain/Study.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import lombok.*;

@Entity
@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
public class Study {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -21,10 +26,10 @@ public class Study {
@OneToMany(mappedBy = "study")
private List<Round> rounds = new ArrayList<>();

@OneToMany(mappedBy = "study")
@OneToMany(mappedBy = "study", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Curriculum> curriculums = new ArrayList<>();

@OneToMany(mappedBy = "study")
@OneToMany(mappedBy = "study", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Day> days = new ArrayList<>();

@OneToMany(mappedBy = "study")
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/gdgoc/study_group/study/dto/MessageResponse.java
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.gdgoc.study_group.study.dto;

import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class MessageResponse {
private String message;
}
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.gdgoc.study_group.study.dto;

import com.gdgoc.study_group.curriculum.dto.CurriculumDTO;
import com.gdgoc.study_group.day.dto.DayDTO;
import com.gdgoc.study_group.study.domain.StudyStatus;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class StudyCreateRequest {
private String name;
private String description;
private String requirements;
private String question;
private Integer maxParticipants;
private StudyStatus studyStatus;
private List<CurriculumDTO> curriculums;
private List<DayDTO> days;
}
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
ybkang1108 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gdgoc.study_group.study.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class StudyCreateResponse {
private String message;
private Long id;
}
Loading