Skip to content

Commit

Permalink
Merge pull request #34 from KOA-TF/feature/notice-improved
Browse files Browse the repository at this point in the history
Feature/notice update & delete / improved response type
  • Loading branch information
Jeongh00 authored Nov 9, 2023
2 parents 08eb734 + 367dd5e commit eff29a1
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.koa.apimodule.command.api;

import com.koa.coremodule.member.domain.entity.Member;
import com.koa.coremodule.member.domain.utils.MemberUtils;
import com.koa.commonmodule.common.ApplicationResponse;
import com.koa.coremodule.notice.application.dto.fcm.RegisterTokenRequest;
import com.koa.coremodule.notice.application.dto.fcm.SendNotificationRequest;
import com.koa.coremodule.notice.application.service.NoticeFcmUseCase;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -20,24 +18,21 @@
public class FcmController {

private final NoticeFcmUseCase noticeFcmUseCase;
private final MemberUtils memberUtils;

@PostMapping("/register")
public ResponseEntity<String> registerToken(@RequestBody RegisterTokenRequest request) {
public ApplicationResponse<Void> registerToken(@RequestBody RegisterTokenRequest request) {

Member memberRequest = memberUtils.getAccessMember();
noticeFcmUseCase.registerFcmToken(memberRequest.getId(), request.fcmToken());
noticeFcmUseCase.registerFcmToken(request.fcmToken());

return ResponseEntity.ok("성공적으로 토큰이 등록되었습니다.");
return ApplicationResponse.ok(null, "성공적으로 토큰이 등록되었습니다.");
}

@PostMapping("/send")
public ResponseEntity<String> sendNotification(@RequestBody SendNotificationRequest request) {
public ApplicationResponse<Void> sendNotification(@RequestBody SendNotificationRequest request) {

Member memberRequest = memberUtils.getAccessMember();
noticeFcmUseCase.sendNotification(memberRequest.getId(), request.title(), request.content());
noticeFcmUseCase.sendNotification(request.title(), request.content());

return ResponseEntity.ok("성공적으로 알림을 보냈습니다.");
return ApplicationResponse.ok(null, "성공적으로 알림을 보냈습니다.");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.koa.apimodule.command.api;

import com.koa.commonmodule.common.ApplicationResponse;
import com.koa.coremodule.notice.application.service.NoticeFindUseCase;
import com.koa.coremodule.notice.application.service.NoticeSaveUseCase;
import com.koa.coremodule.member.domain.entity.Member;
Expand Down Expand Up @@ -28,63 +29,89 @@ public class NoticeController {
* 공지 전체 조회
*/
@GetMapping(value = "")
public ResponseEntity<List<NoticeListResponse>> selectAllNotice() {
public ApplicationResponse<List<NoticeListResponse>> selectAllNotice() {

Member memberRequest = memberUtils.getAccessMember();

List<NoticeListResponse> response = noticeFindUseCase.selectNotice(memberRequest.getId());

return ResponseEntity.ok(response);
return ApplicationResponse.ok(response, "공지 전체 조회에 성공했습니다.");
}

/**
* 커리큘럼 공지 조회
*/
@GetMapping(value = "/curriculum")
public ResponseEntity<List<CurriculumResponse>> selectCurriculum() {
public ApplicationResponse<List<CurriculumResponse>> selectCurriculum() {

List<CurriculumResponse> response = noticeFindUseCase.selectCurriculum();

return ResponseEntity.ok(response);
return ApplicationResponse.ok(response, "커리큘럼 공지 조회에 성공했습니다.");
}

/**
* 토글 누를 시 공지 조회
*/
@GetMapping(value = "/curriculum/list")
public ResponseEntity<List<CurriculumListResponse>> selectCurriculumList(
public ApplicationResponse<List<CurriculumListResponse>> selectCurriculumList(
@RequestParam Long curriculumId
) {

List<CurriculumListResponse> responses = noticeFindUseCase.selectCurriculumList(curriculumId);

return ResponseEntity.ok(responses);
return ApplicationResponse.ok(responses, "공지 조회에 성공했습니다.");
}

/**
* 공지 작성
*/
@PostMapping(value = "")
public ResponseEntity<Void> saveNotice(
public ApplicationResponse<Long> saveNotice(
@RequestPart(value = "dto") NoticeRequest request,
@RequestPart(value = "file") MultipartFile multipartFile) {

Member memberRequest = memberUtils.getAccessMember();
request.setMemberId(memberRequest.getId());

Long noticeId = noticeSaveUseCase.saveNotice(request, multipartFile);
return ResponseEntity.created(URI.create("/notice/" + noticeId)).build();
return ApplicationResponse.ok(noticeId, "공지 작성에 성공했습니다.");
}

/**
* 공지 수정
*/
@PatchMapping(value = "")
public ApplicationResponse<Long> updateNotice(
@RequestPart(value = "dto") NoticeRequest request,
@RequestPart(value = "file") MultipartFile multipartFile) {

Member memberRequest = memberUtils.getAccessMember();
request.setMemberId(memberRequest.getId());

Long noticeId = noticeSaveUseCase.updateNotice(request, multipartFile);
return ApplicationResponse.ok(noticeId, "공지 수정에 성공했습니다.");
}

/**
* 공지 삭제
*/
@DeleteMapping(value = "/{noticeId}")
public ApplicationResponse<Void> deleteNotice(
@PathVariable Long noticeId) {

noticeSaveUseCase.deleteNotice(noticeId);
return ApplicationResponse.ok(null, "공지 삭제에 성공했습니다.");
}

/**
* 공지 상세 조회 (내용)
*/
@GetMapping(value = "/{noticeId}/detail")
public ResponseEntity<NoticeDetailResponse> noticeDetail(
public ApplicationResponse<NoticeDetailResponse> noticeDetail(
@PathVariable Long noticeId) {

NoticeDetailResponse response = noticeSaveUseCase.selectNoticeDetail(noticeId);
return ResponseEntity.ok(response);
return ApplicationResponse.ok(response, "공지 상세 조회에 성공했습니다.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ void selectNotice() {
List<NoticeListResponse> response = noticeFindUseCase.selectNotice(MEMBER_ID);

//then
System.out.println("response = " + response);
Assertions.assertThat(response.get(0).noticeId()).isEqualTo(1L);
Assertions.assertThat(response.get(0).title()).isEqualTo("제목입니다.");

Assertions.assertThat(response.get(0).imageUrl()).isEqualTo("image.png");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void saveNotice() {
//given
byte[] content = "dummy content".getBytes();
MultipartFile file = new MockMultipartFile("testFile.jpg", "testFile.jpg", "image/jpeg", content);
NoticeRequest noticeRequest = new NoticeRequest(MEMBER_ID, TEAM_ID, CURRICULUM_ID, null, "제목입니다.", "내용입니다.");
NoticeRequest noticeRequest = new NoticeRequest(MEMBER_ID, TEAM_ID, CURRICULUM_ID, "image.png", "제목입니다.", "내용입니다.");

//when
Long response = noticeSaveUseCase.saveNotice(noticeRequest, file);
Expand All @@ -47,6 +47,39 @@ void saveNotice() {

}

@Test
void updateNotice() {

//given
NoticeRequest noticeRequest = new NoticeRequest(MEMBER_ID, TEAM_ID, CURRICULUM_ID, null, "수정 제목입니다.", "수정 내용입니다.");

//when
Long response = noticeSaveUseCase.updateNotice(noticeRequest, null);

//then
System.out.println("response ::" + response);
Assertions.assertThat(response).isEqualTo(3L);

}

@Test
void deleteNotice() {

//given
NoticeRequest noticeRequest = new NoticeRequest(MEMBER_ID, TEAM_ID, CURRICULUM_ID, null, "제목입니다.", "내용입니다.");

Long saveResponse = noticeSaveUseCase.saveNotice(noticeRequest, null);

//when
noticeSaveUseCase.deleteNotice(saveResponse);
NoticeDetailResponse response = noticeSaveUseCase.selectNoticeDetail(NOTICE_ID);

//then
System.out.println("save " + saveResponse);
System.out.println("delete " + response);

}

@Test
void selectNoticeDetail() {

Expand Down
16 changes: 8 additions & 8 deletions api-module/src/test/resources/notice_fixture.sql
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
-- notice 테이블 sample datas
INSERT INTO member (member_id, name, email, profile_image_url, password, authority)
values (1, '안정후', '[email protected]', 'picture.com', '20001215', 'MEMBER');
INSERT INTO member (member_id, name, email, password, fcm_token, authority)
values (1, '안정후', '[email protected]', '20001215', 'fcm_token', 'MEMBER');

INSERT INTO member_info (member_info_id, member_id, major, part, phone_number, description)
values (1, 1, '전자공학과', '개발팀', '010-6570-8852', '설명입니다.');

INSERT INTO curriculum (curriculum_id, curriculum_name)
values (1, '기업프로젝트');

INSERT INTO notice (notice_id, member_id, curriculum_id, title, content)
values (1, 1, 1, '제목입니다.', '내용입니다.');
INSERT INTO notice (notice_id, member_id, curriculum_id, title, content, is_deleted)
values (1, 1, 1, '제목입니다.', '내용입니다.', false);

INSERT INTO notice (notice_id, member_id, curriculum_id, title, content)
values (2, 1, 1, '제목입니다.2', '내용입니다.2');
INSERT INTO notice (notice_id, member_id, curriculum_id, title, content, is_deleted)
values (2, 1, 1, '제목입니다.2', '내용입니다.2', false);

INSERT INTO notice_team (notice_team_id, notice_id, team_name)
values (1, 1, '경영총괄팀');

INSERT INTO notice_image (notice_image_id, notice_id, image_url)
values (1, 1, 'image.png');
INSERT INTO notice_image (notice_image_id, notice_id, image_url, is_deleted)
values (1, 1, 'image.png', false);

INSERT INTO notice_view (notice_view_id, member_id, notice_id, view)
values (1, 1, 1, 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.koa.commonmodule.common;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.jackson.Jacksonized;
import org.springframework.http.HttpStatus;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Jacksonized
public class ApplicationResponse<T> {
private ApplicationResult result;
private T payload;

public static <T> ApplicationResponse<T> ok(T payload) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.OK.value())
.message("API 호출 성공")
.build())
.payload(payload)
.build();
}

public static <T> ApplicationResponse<T> ok(T payload, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.OK.value())
.message(message)
.build())
.payload(payload)
.build();
}

public static <T> ApplicationResponse<T> badRequest(T payload) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.BAD_REQUEST.value())
.message("잘못된 요청입니다.")
.build())
.payload(payload)
.build();
}

public static <T> ApplicationResponse<T> badRequest(T payload, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.BAD_REQUEST.value())
.message(message)
.build())
.payload(payload)
.build();
}

public static <T> ApplicationResponse<T> notAuthenticated(T payload) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.UNAUTHORIZED.value())
.message("잘못된 접근입니다.")
.build())
.payload(payload)
.build();
}

public static <T> ApplicationResponse<T> notAuthenticated(T payload, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.UNAUTHORIZED.value())
.message(message)
.build())
.payload(payload)
.build();
}

public static <T> ApplicationResponse<T> server(T payload) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.INTERNAL_SERVER_ERROR.value())
.message("API 호출 실패")
.build())
.payload(payload)
.build();
}

public static <T> ApplicationResponse<T> server(T payload, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.INTERNAL_SERVER_ERROR.value())
.message(message)
.build())
.payload(payload)
.build();
}

public static <T> ApplicationResponse<T> custom(T payload, Integer code, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(code)
.message(message)
.build())
.payload(payload)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.koa.commonmodule.common;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.jackson.Jacksonized;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Jacksonized
public class ApplicationResult {
private Integer code;
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public record NoticeListResponse(Long noticeId,
String team,
String title,
String content,
String imageUrl,
LocalDate date,
Boolean viewYn) {

Expand All @@ -15,7 +16,7 @@ public record NoticeListResponse(Long noticeId,
}

public NoticeListResponse withViewYn(Boolean newViewYn) {
return new NoticeListResponse(noticeId, curriculum, team, title, content, date, newViewYn);
return new NoticeListResponse(noticeId, curriculum, team, title, content, imageUrl, date, newViewYn);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface NoticeMapper {
@Mapping(source = "noticeId", target = "noticeId"),
@Mapping(source = "title", target = "title"),
@Mapping(source = "content", target = "content"),
@Mapping(source = "imageUrl", target = "imageUrl"),
@Mapping(source = "curriculumName", target = "curriculum"),
@Mapping(source = "teamName", target = "team"),
@Mapping(source = "date", target = "date")
Expand Down
Loading

0 comments on commit eff29a1

Please sign in to comment.