Skip to content

Commit

Permalink
refactor : ProblemController 관련 API 엔드포인트 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
rladmstn committed Nov 15, 2024
1 parent a01315a commit 40e93a0
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -31,60 +32,62 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/problem")
@RequestMapping("/api")
@Tag(name = "문제 API", description = "그룹별 문제 관련 API")

public class ProblemController {
private final ProblemService problemService;

@PostMapping
@PostMapping(value = "/groups/{groupId}/problems")
@Operation(summary = "문제 생성 API")
public ResponseEntity<Void> createProblem(@AuthedUser User user,
@PathVariable Long groupId,
@Valid @RequestBody CreateProblemRequest request, Errors errors) {
if (errors.hasErrors())
throw new RequestException("문제 생성 요청이 올바르지 않습니다.", errors);
problemService.createProblem(user, request);
problemService.createProblem(user, groupId, request);
return ResponseEntity.ok().build();
}

@PatchMapping
@PatchMapping(value = "/problems/{problemId}")
@Operation(summary = "문제 마감 기한 수정 API")
public ResponseEntity<Void> editProblemDeadline(@AuthedUser User user,
@PathVariable Long problemId,
@Valid @RequestBody EditProblemRequest request, Errors errors) {
if (errors.hasErrors())
throw new RequestException("문제 마감 기한 수정 요청이 올바르지 않습니다.", errors);
problemService.editProblem(user, request);
problemService.editProblem(user, problemId, request);
return ResponseEntity.ok().build();
}

@GetMapping
@GetMapping(value = "/groups/{groupId}/problems")
@Operation(summary = "문제 조회 API", description = "특정 그룹에 대한 문제를 모두 조회하는 API")
public ResponseEntity<GetProblemListsResponse> getProblemList(@AuthedUser User user,
@RequestParam Long groupId,
@PathVariable Long groupId,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
Pageable pageable = PageRequest.of(page, size);
GetProblemListsResponse response = problemService.getProblemList(user, groupId, pageable);
return ResponseEntity.ok().body(response);
}

@GetMapping("/deadline-reached")
@GetMapping("/groups/{groupId}/problems/deadline-reached")
@Operation(summary = "마감 기한이 내일까지인 문제들 조회 API")
public ResponseEntity<List<GetProblemResponse>> getDeadlineReachedProblemList(@AuthedUser User user,
@RequestParam Long groupId) {
@PathVariable Long groupId) {
return ResponseEntity.ok().body(problemService.getDeadlineReachedProblemList(user, groupId));
}

@GetMapping("/queued-problems")
@GetMapping("/groups/{groupId}/problems/queued")
@Operation(summary = "시작 예정인 문제들 조회 API")
public ResponseEntity<List<GetProblemResponse>> getQueuedProblemList(@AuthedUser User user,
@RequestParam Long groupId) {
@PathVariable Long groupId) {
return ResponseEntity.ok().body(problemService.getQueuedProblemList(user, groupId));
}

@DeleteMapping
@DeleteMapping(value = "/problems/{problemId}")
@Operation(summary = "문제 삭제 API")
public ResponseEntity<Void> deleteProblem(@AuthedUser User user, @RequestParam Long problemId) {
public ResponseEntity<Void> deleteProblem(@AuthedUser User user, @PathVariable Long problemId) {
problemService.deleteProblem(user, problemId);
return ResponseEntity.ok().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import lombok.Builder;

@Builder
public record CreateProblemRequest(@NotNull(message = "그룹 고유 아이디는 필수 입력 입니다.") Long groupId,
@NotBlank(message = "문제 링크는 필수 입력 입니다.") String link,
public record CreateProblemRequest(@NotBlank(message = "문제 링크는 필수 입력 입니다.") String link,
@NotNull(message = "시작 날짜는 필수 입력 입니다.") LocalDate startDate,
@NotNull(message = "마감 날짜는 필수 입력 입니다.") LocalDate endDate) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import java.time.LocalDate;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;

@Builder
public record EditProblemRequest(@NotNull(message = "문제 고유 아이디는 필수 입력 입니다.") Long problemId,
LocalDate startDate,
public record EditProblemRequest(LocalDate startDate,
LocalDate endDate) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public class ProblemService {
private final RestTemplate restTemplate;

@Transactional
public void createProblem(User user, CreateProblemRequest request) {
StudyGroup group = getGroup(request.groupId());
public void createProblem(User user, Long groupId, CreateProblemRequest request) {
StudyGroup group = getGroup(groupId);
GroupMember groupMember = groupMemberRepository.findByUserAndStudyGroup(user, group)
.orElseThrow(
() -> new StudyGroupValidationException(HttpStatus.FORBIDDEN.value(), "참여하지 않은 그룹 입니다."));
Expand Down Expand Up @@ -94,8 +94,8 @@ public void createProblem(User user, CreateProblemRequest request) {
}

@Transactional
public void editProblem(User user, EditProblemRequest request) {
Problem problem = getProblem(request.problemId());
public void editProblem(User user, Long problemId, EditProblemRequest request) {
Problem problem = getProblem(problemId);
StudyGroup group = getGroup(problem.getStudyGroup().getId());
GroupMember groupMember = groupMemberRepository.findByUserAndStudyGroup(user, group)
.orElseThrow(
Expand Down
Loading

0 comments on commit 40e93a0

Please sign in to comment.