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

refactor : NoticeController 관련 API 엔드포인트 수정 #172

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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,10 +7,10 @@
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;
Expand All @@ -28,50 +28,54 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/notice")
@RequestMapping("/api")
@Tag(name = "공지 API", description = "공지 관련 API")

public class NoticeController {
private final NoticeService noticeService;

@PostMapping
@PostMapping("/groups/{groupId}/notices")
@Operation(summary = "공지 작성 API")
public ResponseEntity<Void> createNotice(@AuthedUser User user, @Valid @RequestBody CreateNoticeRequest request,
public ResponseEntity<Void> createNotice(@AuthedUser User user,
@PathVariable Long groupId,
@Valid @RequestBody CreateNoticeRequest request,
Errors errors) {
if (errors.hasErrors())
throw new RequestException("올바르지 않은 공지 생성 요청입니다", errors);
noticeService.createNotice(user, request);
noticeService.createNotice(user, groupId, request);
return ResponseEntity.ok().build();
}

@GetMapping
@GetMapping("/notices/{noticeId}")
@Operation(summary = "공지 하나 조회 API")
public ResponseEntity<GetNoticeResponse> getNotice(@AuthedUser User user, @RequestParam Long noticeId) {
public ResponseEntity<GetNoticeResponse> getNotice(@AuthedUser User user, @PathVariable Long noticeId) {
GetNoticeResponse response = noticeService.getNotice(user, noticeId);
return ResponseEntity.ok().body(response);
}

@GetMapping(value = "/notice-list")
@GetMapping(value = "/groups/{groupId}/notices")
@Operation(summary = "공지 목록 조회 API")
public ResponseEntity<List<GetNoticeResponse>> getNoticeList(@AuthedUser User user,
@RequestParam Long studyGroupId) {
List<GetNoticeResponse> response = noticeService.getNoticeList(user, studyGroupId);
@PathVariable Long groupId) {
List<GetNoticeResponse> response = noticeService.getNoticeList(user, groupId);
return ResponseEntity.ok().body(response);
}

@PatchMapping
@PatchMapping("/notices/{noticeId}")
@Operation(summary = "공지 수정 API")
public ResponseEntity<Void> updateNotice(@AuthedUser User user, @Valid @RequestBody UpdateNoticeRequest request,
public ResponseEntity<Void> updateNotice(@AuthedUser User user,
@PathVariable Long noticeId,
@Valid @RequestBody UpdateNoticeRequest request,
Errors errors) {
if (errors.hasErrors())
throw new RequestException("올바르지 않은 수정 요청입니다", errors);
noticeService.updateNotice(user, request);
noticeService.updateNotice(user, noticeId, request);
return ResponseEntity.ok().build();
}

@DeleteMapping
@DeleteMapping("/notices/{noticeId}")
@Operation(summary = "공지 삭제 API")
public ResponseEntity<Void> deleteNotice(@AuthedUser User user, @RequestParam Long noticeId) {
public ResponseEntity<Void> deleteNotice(@AuthedUser User user, @PathVariable Long noticeId) {
noticeService.deleteNotice(user, noticeId);
return ResponseEntity.ok().build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.gamzabat.algohub.feature.notice.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record CreateNoticeRequest(@NotNull(message = "그룹 id 는 필수입니다") Long studyGroupId,
@NotBlank(message = "제목은 필수 입력입니다") String title,
public record CreateNoticeRequest(@NotBlank(message = "제목은 필수 입력입니다") String title,
@NotBlank(message = "본문은 필수 입력입니다") String content,
@NotBlank(message = "카테고리는 필수 입력입니다") String category
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.gamzabat.algohub.feature.notice.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record UpdateNoticeRequest(@NotNull(message = "게시글 id는 필수 입니다.") Long noticeId,
@NotBlank(message = "제목은 필수 입력입니다") String title,
public record UpdateNoticeRequest(@NotBlank(message = "제목은 필수 입력입니다") String title,
@NotBlank(message = "본문은 필수 입력입니다") String content,
@NotBlank(message = "카테고리는 필수 입력입니다") String category
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class NoticeService {
private final NoticeReadRepository noticeReadRepository;

@Transactional
public void createNotice(@AuthedUser User user, CreateNoticeRequest request) {
StudyGroup studyGroup = studyGroupRepository.findById(request.studyGroupId())
public void createNotice(@AuthedUser User user, Long groupId, CreateNoticeRequest request) {
StudyGroup studyGroup = studyGroupRepository.findById(groupId)
.orElseThrow(() -> new StudyGroupValidationException(HttpStatus.BAD_REQUEST.value(), "존재하지 않는 스터디 그룹입니다"));
GroupMember groupMember = groupMemberRepository.findByUserAndStudyGroup(user, studyGroup)
.orElseThrow(
Expand Down Expand Up @@ -101,8 +101,8 @@ public List<GetNoticeResponse> getNoticeList(@AuthedUser User user, Long studyGr
}

@Transactional
public void updateNotice(User user, UpdateNoticeRequest request) {
Notice notice = noticeRepository.findById(request.noticeId())
public void updateNotice(User user, Long noticeId, UpdateNoticeRequest request) {
Notice notice = noticeRepository.findById(noticeId)
.orElseThrow(() -> new NoticeValidationException("존재하지 않는 게시글입니다"));
validateStudyGroupExists(notice);
if (!user.getId().equals(notice.getAuthor().getId()))
Expand Down
40 changes: 20 additions & 20 deletions src/test/java/com/gamzabat/algohub/service/NoticeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
@DisplayName("공지 작성 성공")
void createNoticeSuccess_1() {
//given
CreateNoticeRequest request = new CreateNoticeRequest(30L, "title", "content", "category");
when(studyGroupRepository.findById(request.studyGroupId())).thenReturn(Optional.ofNullable(studyGroup));
CreateNoticeRequest request = new CreateNoticeRequest("title", "content", "category");
when(studyGroupRepository.findById(30L)).thenReturn(Optional.ofNullable(studyGroup));
when(groupMemberRepository.findByUserAndStudyGroup(user2, studyGroup)).thenReturn(
Optional.ofNullable(groupMember2));
//when
noticeService.createNotice(user2, request);
noticeService.createNotice(user2, 30L, request);
//then
verify(noticeRepository, times(1)).save(noticeCaptor.capture());
Notice result = noticeCaptor.getValue();
Expand All @@ -133,12 +133,12 @@ void createNoticeSuccess_1() {
@DisplayName("공지 작성 실패 그룹장or부방장이 아님")
void createNoticeFail_1() {
//given
CreateNoticeRequest request = new CreateNoticeRequest(30L, "title", "content", "category");
when(studyGroupRepository.findById(request.studyGroupId())).thenReturn(Optional.ofNullable(studyGroup));
CreateNoticeRequest request = new CreateNoticeRequest("title", "content", "category");
when(studyGroupRepository.findById(30L)).thenReturn(Optional.ofNullable(studyGroup));
when(groupMemberRepository.findByUserAndStudyGroup(user3, studyGroup)).thenReturn(
Optional.ofNullable(groupMember3));
//when,then
assertThatThrownBy(() -> noticeService.createNotice(user3, request))
assertThatThrownBy(() -> noticeService.createNotice(user3, 30L, request))
.isInstanceOf(UserValidationException.class)
.hasFieldOrPropertyWithValue("errors", "공지 작성 권한이 없습니다");

Expand All @@ -148,10 +148,10 @@ void createNoticeFail_1() {
@DisplayName("공지 작성 실패 존재하지 않는 그룹")
void createNoticeFail_2() {
//given
CreateNoticeRequest request = new CreateNoticeRequest(31L, "title", "content", "category");
when(studyGroupRepository.findById(request.studyGroupId())).thenReturn(Optional.empty());
CreateNoticeRequest request = new CreateNoticeRequest("title", "content", "category");
when(studyGroupRepository.findById(31L)).thenReturn(Optional.empty());
//when,then
assertThatThrownBy(() -> noticeService.createNotice(user, request))
assertThatThrownBy(() -> noticeService.createNotice(user, 31L, request))
.isInstanceOf(StudyGroupValidationException.class)
.extracting("code", "error")
.containsExactly(HttpStatus.BAD_REQUEST.value(), "존재하지 않는 스터디 그룹입니다");
Expand All @@ -161,11 +161,11 @@ void createNoticeFail_2() {
@DisplayName("공지 작성 실패 존재하지 않는 멤버")
void createNoticeFail_3() {
//given
CreateNoticeRequest request = new CreateNoticeRequest(30L, "title", "content", "category");
when(studyGroupRepository.findById(request.studyGroupId())).thenReturn(Optional.ofNullable(studyGroup));
CreateNoticeRequest request = new CreateNoticeRequest("title", "content", "category");
when(studyGroupRepository.findById(30L)).thenReturn(Optional.ofNullable(studyGroup));
when(groupMemberRepository.findByUserAndStudyGroup(user4, studyGroup)).thenReturn(Optional.empty());
//when,then
assertThatThrownBy(() -> noticeService.createNotice(user4, request))
assertThatThrownBy(() -> noticeService.createNotice(user4, 30L, request))
.isInstanceOf(StudyGroupValidationException.class)
.hasFieldOrPropertyWithValue("code", HttpStatus.FORBIDDEN.value())
.hasFieldOrPropertyWithValue("error", "참여하지 않은 그룹 입니다.");
Expand Down Expand Up @@ -285,12 +285,12 @@ void getNoticeListFailed_2() {
@DisplayName("공지 수정 성공")
void updateNoticeSuccess() {
//given
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1000L, "updateTitle", "updateContent",
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest("updateTitle", "updateContent",
"updateCategory");
when(noticeRepository.findById(1000L)).thenReturn(Optional.ofNullable(notice));
when(studyGroupRepository.findById(30L)).thenReturn(Optional.ofNullable(studyGroup));
//when
noticeService.updateNotice(user, updateNoticeRequest);
noticeService.updateNotice(user, 1000L, updateNoticeRequest);
//then
assertThat(notice.getContent()).isEqualTo("updateContent");
assertThat(notice.getTitle()).isEqualTo("updateTitle");
Expand All @@ -301,11 +301,11 @@ void updateNoticeSuccess() {
@DisplayName("공지 수정 실패(존재하지 않는 게시글)")
void updateNoticeFailed_1() {
//given
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1001L, "updateTitle", "updateContent",
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest("updateTitle", "updateContent",
"updateCategory");
when(noticeRepository.findById(1001L)).thenReturn(Optional.empty());
//when,then
assertThatThrownBy(() -> noticeService.updateNotice(user, updateNoticeRequest))
assertThatThrownBy(() -> noticeService.updateNotice(user, 1001L, updateNoticeRequest))
.isInstanceOf(NoticeValidationException.class)
.hasFieldOrPropertyWithValue("error", "존재하지 않는 게시글입니다");
}
Expand All @@ -314,12 +314,12 @@ void updateNoticeFailed_1() {
@DisplayName("공시 수정 실패(존재하지 않는 스터디 그룹)")
void updateNoticeFailed_2() {
//given
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1000L, "updateTitle", "updateContent",
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest("updateTitle", "updateContent",
"updateCategory");
when(noticeRepository.findById(1000L)).thenReturn(Optional.ofNullable(notice));
when(studyGroupRepository.findById(30L)).thenReturn(Optional.empty());
//when,then
assertThatThrownBy(() -> noticeService.updateNotice(user, updateNoticeRequest))
assertThatThrownBy(() -> noticeService.updateNotice(user, 1000L, updateNoticeRequest))
.isInstanceOf(StudyGroupValidationException.class)
.hasFieldOrPropertyWithValue("code", HttpStatus.BAD_REQUEST.value())
.hasFieldOrPropertyWithValue("error", "존재하지 않는 스터디 그룹입니다");
Expand All @@ -330,12 +330,12 @@ void updateNoticeFailed_2() {
@DisplayName("공지 수정 실패(게시글 작성자가 아님)")
void updateNoticeFailed_3() {
//given
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest(1000L, "updateTitle", "updateContent",
UpdateNoticeRequest updateNoticeRequest = new UpdateNoticeRequest("updateTitle", "updateContent",
"updateCategory");
when(noticeRepository.findById(1000L)).thenReturn(Optional.ofNullable(notice));
when(studyGroupRepository.findById(30L)).thenReturn(Optional.ofNullable(studyGroup));
//when, then
assertThatThrownBy(() -> noticeService.updateNotice(user4, updateNoticeRequest))
assertThatThrownBy(() -> noticeService.updateNotice(user4, 1000L, updateNoticeRequest))
.isInstanceOf(UserValidationException.class)
.hasFieldOrPropertyWithValue("errors", "공지를 수정할 수 있는 권한이 없습니다");
}
Expand Down
Loading