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

✨ [FEATURE] 공고 수정 기능 개발 #48

Merged
merged 2 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 16 additions & 3 deletions http/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,8 +73,13 @@ public ApiResponse<Void> deleteAnnouncement(
}

@PatchMapping("/{announcementId}")
public ApiResponse<?> updateAnnouncement(@PathVariable Long announcementId) {
return null;
public ApiResponse<AnnouncementUpdateResponse> updateAnnouncement(
@AuthOrganization Organization authOrganization,
@PathVariable Long announcementId,
@RequestBody @Valid AnnouncementUpdateRequest request
) {
announcementService.updateAnnouncement(authOrganization, announcementId, request);
return ApiResponse.onSuccess(announcementService.updateAnnouncement(authOrganization, announcementId, request));
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response는 announcement.getStatus로 해도 좋을 것 같아요-!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 게시 중인 공고만 수정 가능하니 위 의견은 무시하셔도 됩니당

.viewCount(announcement.getViewCount())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

게시 중인 공고(POSTED상태)만 수정 가능하도록 하는 예외 처리도 필요할 것 같아요-!

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);
}

}
Loading