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 c5b1375e..20f0bcf3 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 @@ -9,11 +9,14 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import com.sponus.sponusbe.auth.annotation.AuthOrganization; import com.sponus.sponusbe.domain.announcement.dto.AnnouncementBriefResponse; 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.service.AnnouncementQueryService; import com.sponus.sponusbe.domain.announcement.service.AnnouncementService; +import com.sponus.sponusbe.domain.organization.entity.Organization; import com.sponus.sponusbe.global.common.ApiResponse; import lombok.RequiredArgsConstructor; @@ -48,9 +51,10 @@ public ApiResponse searchAnnouncement(@RequestParam String return ApiResponse.onSuccess(announcementQueryService.searchAnnouncement(search)); } - @PostMapping - public ApiResponse createAnnouncement(AnnouncementCreateRequest request) { - return ApiResponse.onSuccess(announcementQueryService.createAnnouncement(request)); + @PostMapping("/") + public ApiResponse createAnnouncement(@AuthOrganization Organization authOrganization, + AnnouncementCreateRequest request) { + return ApiResponse.onSuccess(announcementService.createAnnouncement(authOrganization, request)); } @DeleteMapping("/{announcementId}") diff --git a/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementCreateRequest.java b/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementCreateRequest.java index 40efc34e..53d8168c 100644 --- a/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementCreateRequest.java +++ b/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementCreateRequest.java @@ -1,7 +1,10 @@ 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; @@ -14,6 +17,19 @@ public record AnnouncementCreateRequest( @NotNull(message = "[ERROR] 카테코리 입력은 필수 입니다.") AnnouncementCategory category, @NotBlank(message = "[ERROR] 내용 입력은 필수 입니다.") - String content + 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/AnnouncementCreateResponse.java b/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementCreateResponse.java new file mode 100644 index 00000000..469fc6c7 --- /dev/null +++ b/src/main/java/com/sponus/sponusbe/domain/announcement/dto/AnnouncementCreateResponse.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 AnnouncementCreateResponse( + Long id, + Long writerId, + String title, + AnnouncementType type, + AnnouncementCategory category, + String content, + AnnouncementStatus status, + Long viewCount +) { + public static AnnouncementCreateResponse from(Announcement announcement) { + return AnnouncementCreateResponse.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/service/AnnouncementService.java b/src/main/java/com/sponus/sponusbe/domain/announcement/service/AnnouncementService.java index e2253783..50b30468 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 @@ -3,12 +3,26 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.sponus.sponusbe.domain.announcement.dto.AnnouncementCreateRequest; +import com.sponus.sponusbe.domain.announcement.dto.AnnouncementCreateResponse; +import com.sponus.sponusbe.domain.announcement.entity.Announcement; +import com.sponus.sponusbe.domain.announcement.repository.AnnouncementRepository; +import com.sponus.sponusbe.domain.organization.entity.Organization; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @Slf4j @RequiredArgsConstructor -@Transactional(readOnly = true) +@Transactional @Service public class AnnouncementService { + + private final AnnouncementRepository announcementRepository; + + public AnnouncementCreateResponse createAnnouncement(Organization authOrganization, + AnnouncementCreateRequest request) { + final Announcement announcement = announcementRepository.save(request.toEntity(authOrganization)); + return AnnouncementCreateResponse.from(announcement); + } }