-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblemController.java
More file actions
99 lines (87 loc) · 4.33 KB
/
ProblemController.java
File metadata and controls
99 lines (87 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.gamzabat.algohub.feature.problem.controller;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.Errors;
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;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.gamzabat.algohub.common.annotation.AuthedUser;
import com.gamzabat.algohub.exception.RequestException;
import com.gamzabat.algohub.feature.problem.dto.CreateProblemRequest;
import com.gamzabat.algohub.feature.problem.dto.EditProblemRequest;
import com.gamzabat.algohub.feature.problem.dto.GetProblemResponse;
import com.gamzabat.algohub.feature.problem.enums.ProblemListStatus;
import com.gamzabat.algohub.feature.problem.exception.InvalidRequestException;
import com.gamzabat.algohub.feature.problem.service.ProblemService;
import com.gamzabat.algohub.feature.user.domain.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
@Tag(name = "문제 API", description = "그룹별 문제 관련 API")
public class ProblemController {
private final ProblemService problemService;
private final String PROBLEM_SORT_BY = "endDate";
@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, groupId, request);
return ResponseEntity.ok().build();
}
@PatchMapping(value = "/problems/{problemId}")
@Operation(summary = "문제 마감 기한 수정 API")
public ResponseEntity<Void> editProblemDeadline(@AuthedUser User user,
@PathVariable Long problemId,
@RequestBody EditProblemRequest request) {
problemService.editProblem(user, problemId, request);
return ResponseEntity.ok().build();
}
@GetMapping(value = "/groups/{groupId}/problems")
@Operation(summary = "문제 목록 조회 API", description = "특정 그룹에 대한 문제를 부분 조회하는 API")
public ResponseEntity<Page<GetProblemResponse>> getProblems(@AuthedUser User user,
@PathVariable Long groupId,
@RequestParam ProblemListStatus status,
@RequestParam(name = "unsolved-only", required = false) Boolean unsolvedOnly,
@PageableDefault(page = 0, size = 20, sort = "endDate", direction = Sort.Direction.DESC)
Pageable pageable) {
if (status == ProblemListStatus.IN_PROGRESS) {
if (unsolvedOnly == null) {
throw new InvalidRequestException(HttpStatus.BAD_REQUEST, "IN_PROGRESS 상태에서는 unsolvedOnly 는 필수입니다.");
}
}
Page<GetProblemResponse> response = problemService.getProblems(user, groupId, status, unsolvedOnly, pageable);
return ResponseEntity.ok().body(response);
}
@GetMapping("/problems/{problemId}")
@Operation(summary = "문제 단건 조회 API")
public ResponseEntity<GetProblemResponse> getProblem(@AuthedUser User user, @PathVariable Long problemId) {
GetProblemResponse response = problemService.getProblem(user, problemId);
return ResponseEntity.ok().body(response);
}
@DeleteMapping(value = "/problems/{problemId}")
@Operation(summary = "문제 삭제 API")
public ResponseEntity<Void> deleteProblem(@AuthedUser User user, @PathVariable Long problemId) {
problemService.deleteProblem(user, problemId);
return ResponseEntity.ok().build();
}
}