From de539d7ff3517617deb517bb84436b80ef978d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=86=8C=EC=A0=95?= Date: Fri, 26 Jan 2024 23:35:38 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20feat:=20=EA=B3=B5=EA=B3=A0=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/test.http | 19 ++++++++-- .../controller/AnnouncementController.java | 11 +++++- .../dto/AnnouncementUpdateRequest.java | 37 +++++++++++++++++++ .../dto/AnnouncementUpdateResponse.java | 33 +++++++++++++++++ .../announcement/entity/Announcement.java | 7 ++++ .../exception/AnnouncementErrorCode.java | 1 + .../service/AnnouncementService.java | 19 ++++++++++ 7 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementUpdateRequest.java create mode 100644 src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementUpdateResponse.java diff --git a/http/test.http b/http/test.http index eaf407c8..1bfc0ba5 100644 --- a/http/test.http +++ b/http/test.http @@ -30,7 +30,7 @@ Content-Type: application/json } ### 공고 생성 -POST http://localhost:8080/api/v1/announcement +POST http://localhost:8080/api/v1/announcements Authorization: Bearer {{matsterToken}} Content-Type: application/json @@ -43,8 +43,21 @@ Content-Type: application/json } ### 상세 조회 -GET http://localhost:8080/api/v1/announcement/1 +GET http://localhost:8080/api/v1/announcements/2 ### 삭제 (생성한 단체만 삭제 가능) -DELETE http://localhost:8080/api/v1/announcement/1 +DELETE http://localhost:8080/api/v1/announcements/1 Authorization: Bearer {{matsterToken}} + +### 공고 수정 +PATCH http://localhost:8080/api/v1/announcements/1 +Authorization: Bearer {{matsterToken}} +Content-Type: application/json + +{ + "title": "스포너스 스폰서십", + "type": "SPONSORSHIP", + "category": "MARKETING", + "content": "스포너스 스폰서십을 진행할 대학교 학생회를 모집합니다.", + "status": "POSTED" +} diff --git a/src/main/java/com/sponus/sponusbe/domain/announcement/controller/AnnouncementController.java b/src/main/java/com/sponus/sponusbe/domain/announcement/controller/AnnouncementController.java index 027f1505..2d0ba502 100644 --- a/src/main/java/com/sponus/sponusbe/domain/announcement/controller/AnnouncementController.java +++ b/src/main/java/com/sponus/sponusbe/domain/announcement/controller/AnnouncementController.java @@ -16,6 +16,8 @@ import com.sponus.sponusbe.domain.announcement.dto.AnnouncementCreateRequest; import com.sponus.sponusbe.domain.announcement.dto.AnnouncementCreateResponse; import com.sponus.sponusbe.domain.announcement.dto.AnnouncementResponse; +import com.sponus.sponusbe.domain.announcement.dto.AnnouncementUpdateRequest; +import com.sponus.sponusbe.domain.announcement.dto.AnnouncementUpdateResponse; import com.sponus.sponusbe.domain.announcement.service.AnnouncementQueryService; import com.sponus.sponusbe.domain.announcement.service.AnnouncementService; import com.sponus.sponusbe.domain.organization.entity.Organization; @@ -71,8 +73,13 @@ public ApiResponse deleteAnnouncement( } @PatchMapping("/{announcementId}") - public ApiResponse updateAnnouncement(@PathVariable Long announcementId) { - return null; + public ApiResponse updateAnnouncement( + @AuthOrganization Organization authOrganization, + @PathVariable Long announcementId, + @RequestBody @Valid AnnouncementUpdateRequest request + ) { + announcementService.updateAnnouncement(authOrganization, announcementId, request); + return ApiResponse.onSuccess(announcementService.updateAnnouncement(authOrganization, announcementId, request)); } } diff --git a/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementUpdateRequest.java b/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementUpdateRequest.java new file mode 100644 index 00000000..d2ee5f08 --- /dev/null +++ b/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementUpdateRequest.java @@ -0,0 +1,37 @@ +package com.sponus.sponusbe.domain.announcement.dto; + +import com.sponus.sponusbe.domain.announcement.entity.Announcement; +import com.sponus.sponusbe.domain.announcement.entity.enums.AnnouncementCategory; +import com.sponus.sponusbe.domain.announcement.entity.enums.AnnouncementStatus; +import com.sponus.sponusbe.domain.announcement.entity.enums.AnnouncementType; +import com.sponus.sponusbe.domain.organization.entity.Organization; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Builder; + +@Builder +public record AnnouncementUpdateRequest( + @NotBlank(message = "[ERROR] 타이틀 입력은 필수 입니다.") + String title, + @NotNull(message = "[ERROR] 유형 입력은 필수 입니다.") + AnnouncementType type, + @NotNull(message = "[ERROR] 카테코리 입력은 필수 입니다.") + AnnouncementCategory category, + @NotBlank(message = "[ERROR] 내용 입력은 필수 입니다.") + String content, + + AnnouncementStatus status +) { + + public Announcement toEntity(Organization writer) { + return Announcement.builder() + .writer(writer) + .title(title) + .type(type) + .category(category) + .content(content) + .status(status) + .build(); + } +} diff --git a/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementUpdateResponse.java b/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementUpdateResponse.java new file mode 100644 index 00000000..f1877a56 --- /dev/null +++ b/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementUpdateResponse.java @@ -0,0 +1,33 @@ +package com.sponus.sponusbe.domain.announcement.dto; + +import com.sponus.sponusbe.domain.announcement.entity.Announcement; +import com.sponus.sponusbe.domain.announcement.entity.enums.AnnouncementCategory; +import com.sponus.sponusbe.domain.announcement.entity.enums.AnnouncementStatus; +import com.sponus.sponusbe.domain.announcement.entity.enums.AnnouncementType; + +import lombok.Builder; + +@Builder +public record AnnouncementUpdateResponse( + Long id, + Long writerId, + String title, + AnnouncementType type, + AnnouncementCategory category, + String content, + AnnouncementStatus status, + Long viewCount +) { + public static AnnouncementUpdateResponse from(Announcement announcement) { + return AnnouncementUpdateResponse.builder() + .id(announcement.getId()) + .writerId(announcement.getWriter().getId()) + .title(announcement.getTitle()) + .type(announcement.getType()) + .category(announcement.getCategory()) + .content(announcement.getContent()) + .status(AnnouncementStatus.POSTED) + .viewCount(announcement.getViewCount()) + .build(); + } +} diff --git a/src/main/java/com/sponus/sponusbe/domain/announcement/entity/Announcement.java b/src/main/java/com/sponus/sponusbe/domain/announcement/entity/Announcement.java index 82ff8f92..f79fe2c3 100644 --- a/src/main/java/com/sponus/sponusbe/domain/announcement/entity/Announcement.java +++ b/src/main/java/com/sponus/sponusbe/domain/announcement/entity/Announcement.java @@ -66,4 +66,11 @@ public class Announcement extends BaseEntity { public void increaseViewCount() { this.viewCount++; } + + public void update(String title, AnnouncementType type, AnnouncementCategory category, String content) { + this.title = title == null ? this.title : title; + this.type = type == null ? this.type : type; + this.category = category == null ? this.category : category; + this.content = content == null ? this.content : content; + } } diff --git a/src/main/java/com/sponus/sponusbe/domain/announcement/exception/AnnouncementErrorCode.java b/src/main/java/com/sponus/sponusbe/domain/announcement/exception/AnnouncementErrorCode.java index b28e5afc..fd7113f8 100644 --- a/src/main/java/com/sponus/sponusbe/domain/announcement/exception/AnnouncementErrorCode.java +++ b/src/main/java/com/sponus/sponusbe/domain/announcement/exception/AnnouncementErrorCode.java @@ -13,6 +13,7 @@ public enum AnnouncementErrorCode implements BaseErrorCode { ANNOUNCEMENT_ERROR(HttpStatus.BAD_REQUEST, "ANC4000", "공지사항 관련 에러"), ANNOUNCEMENT_ALREADY_DELETED(HttpStatus.BAD_REQUEST, "ANC4001", "이미 삭제된 공지사항입니다."), + INVALID_ORGANIZATION(HttpStatus.BAD_REQUEST, "ANC4002", "해당 단체의 공고가 아닙니다."), ANNOUNCEMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "ANC4040", "해당 공지사항이 존재하지 않습니다."); private final HttpStatus httpStatus; diff --git a/src/main/java/com/sponus/sponusbe/domain/announcement/service/AnnouncementService.java b/src/main/java/com/sponus/sponusbe/domain/announcement/service/AnnouncementService.java index 0601d3eb..d7fa4e9b 100644 --- a/src/main/java/com/sponus/sponusbe/domain/announcement/service/AnnouncementService.java +++ b/src/main/java/com/sponus/sponusbe/domain/announcement/service/AnnouncementService.java @@ -6,6 +6,8 @@ import com.sponus.sponusbe.domain.announcement.dto.AnnouncementCreateRequest; import com.sponus.sponusbe.domain.announcement.dto.AnnouncementCreateResponse; import com.sponus.sponusbe.domain.announcement.dto.AnnouncementResponse; +import com.sponus.sponusbe.domain.announcement.dto.AnnouncementUpdateRequest; +import com.sponus.sponusbe.domain.announcement.dto.AnnouncementUpdateResponse; import com.sponus.sponusbe.domain.announcement.entity.Announcement; import com.sponus.sponusbe.domain.announcement.exception.AnnouncementErrorCode; import com.sponus.sponusbe.domain.announcement.exception.AnnouncementException; @@ -46,4 +48,21 @@ public void deleteAnnouncement(Organization organization, Long announcementId) { announcementRepository.delete(announcement); } + public AnnouncementUpdateResponse updateAnnouncement(Organization authOrganization, Long proposeId, + AnnouncementUpdateRequest request) { + final Announcement announcement = announcementRepository.findById(proposeId) + .orElseThrow(() -> new AnnouncementException(AnnouncementErrorCode.ANNOUNCEMENT_NOT_FOUND)); + + if (!isOrganizationsAnnouncement(authOrganization.getId(), announcement)) + throw new AnnouncementException(AnnouncementErrorCode.INVALID_ORGANIZATION); + + announcement.update(request.title(), request.type(), request.category(), request.content()); + announcementRepository.save(announcement); + return AnnouncementUpdateResponse.from(announcement); + } + + private boolean isOrganizationsAnnouncement(Long organizationId, Announcement announcement) { + return announcement.getWriter().getId().equals(organizationId); + } + }