From 618f0d1ab3290bfb20d91d2b13168408f922183a Mon Sep 17 00:00:00 2001 From: Seoyoung2222 Date: Mon, 25 Nov 2024 23:42:15 +0900 Subject: [PATCH] =?UTF-8?q?BE:=20[feat]=20=EA=B3=B5=EC=A7=80=EA=B8=80=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=ED=95=98=EA=B8=B0=20#83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/repository/CrewUserRepository.java | 7 ++- .../controller/NoticePostController.java | 25 +++++++++ .../dto/NoticePostCreateRequestDto.java | 11 ++++ .../service/NoticePostCommandService.java | 54 +++++++++++++++++++ .../post/repository/CrewPostRepository.java | 13 +++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/controller/NoticePostController.java create mode 100644 src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/dto/NoticePostCreateRequestDto.java create mode 100644 src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/service/NoticePostCommandService.java create mode 100644 src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/repository/CrewPostRepository.java diff --git a/src/backend/src/main/java/RunningMachines/R2R/domain/crew/common/repository/CrewUserRepository.java b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/common/repository/CrewUserRepository.java index cfe7fda..d25d85a 100644 --- a/src/backend/src/main/java/RunningMachines/R2R/domain/crew/common/repository/CrewUserRepository.java +++ b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/common/repository/CrewUserRepository.java @@ -1,7 +1,12 @@ package RunningMachines.R2R.domain.crew.common.repository; +import RunningMachines.R2R.domain.crew.common.entity.Crew; import RunningMachines.R2R.domain.crew.common.entity.CrewUser; +import RunningMachines.R2R.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; -public interface CrewUserRepository extends JpaRepository { +import java.util.Optional; + +public interface CrewUserRepository extends JpaRepository { + Optional findByCrewAndUser(Crew crew, User user); } diff --git a/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/controller/NoticePostController.java b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/controller/NoticePostController.java new file mode 100644 index 0000000..8a41e9f --- /dev/null +++ b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/controller/NoticePostController.java @@ -0,0 +1,25 @@ +package RunningMachines.R2R.domain.crew.post.notice.controller; + +import RunningMachines.R2R.domain.crew.post.notice.dto.NoticePostCreateRequestDto; +import RunningMachines.R2R.domain.crew.post.notice.service.NoticePostCommandService; +import RunningMachines.R2R.domain.user.entity.User; +import RunningMachines.R2R.domain.user.service.AuthCommandService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/crew/{crewId}") +@RequiredArgsConstructor +public class NoticePostController { + private final NoticePostCommandService noticePostCommandService; + private final AuthCommandService authCommandService; + + @PostMapping("/notice") + public ResponseEntity createNoticePost(@PathVariable Long crewId, @RequestBody NoticePostCreateRequestDto requestDto) { + User currentUser = authCommandService.getCurrentUser(); + noticePostCommandService.createNoticePost(crewId, currentUser, requestDto); + return ResponseEntity.ok(crewId); + } +} diff --git a/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/dto/NoticePostCreateRequestDto.java b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/dto/NoticePostCreateRequestDto.java new file mode 100644 index 0000000..51588d9 --- /dev/null +++ b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/dto/NoticePostCreateRequestDto.java @@ -0,0 +1,11 @@ +package RunningMachines.R2R.domain.crew.post.notice.dto; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class NoticePostCreateRequestDto { + private String title; + private String content; +} diff --git a/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/service/NoticePostCommandService.java b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/service/NoticePostCommandService.java new file mode 100644 index 0000000..c562baf --- /dev/null +++ b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/notice/service/NoticePostCommandService.java @@ -0,0 +1,54 @@ +package RunningMachines.R2R.domain.crew.post.notice.service; + +import RunningMachines.R2R.domain.crew.board.entity.CrewBoard; +import RunningMachines.R2R.domain.crew.common.entity.Crew; +import RunningMachines.R2R.domain.crew.common.entity.CrewRole; +import RunningMachines.R2R.domain.crew.common.entity.CrewUser; +import RunningMachines.R2R.domain.crew.common.repository.CrewRepository; +import RunningMachines.R2R.domain.crew.common.repository.CrewUserRepository; +import RunningMachines.R2R.domain.crew.post.entity.CrewPost; +import RunningMachines.R2R.domain.crew.post.notice.dto.NoticePostCreateRequestDto; +import RunningMachines.R2R.domain.crew.post.repository.CrewPostRepository; +import RunningMachines.R2R.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class NoticePostCommandService { + private final CrewPostRepository crewPostRepository; + private final CrewUserRepository crewUserRepository; + private final CrewRepository crewRepository; + + @Transactional + public void createNoticePost(Long crewId, User user, NoticePostCreateRequestDto noticePostCreateRequestDto) { + Crew crew = validateCrew(crewId); + validateLeader(crew, user); + + CrewPost crewPost = CrewPost.builder() + .title(noticePostCreateRequestDto.getTitle()) + .content(noticePostCreateRequestDto.getContent()) + .board(CrewBoard.NOTICE) + .crew(crew) + .user(user) + .build(); + + crewPostRepository.save(crewPost); + } + + private Crew validateCrew(Long crewId) { + return crewRepository.findById(crewId) + .orElseThrow(()-> new IllegalArgumentException("크루를 찾을 수 없습니다.")); + } + + private void validateLeader(Crew crew, User user) { + + CrewUser crewUser = crewUserRepository.findByCrewAndUser(crew, user) + .orElseThrow(() -> new IllegalArgumentException("해당 사용자는 크루에 속해 있지 않습니다.")); + + if (crewUser.getRole() != CrewRole.LEADER) { + throw new IllegalArgumentException("공지글 작성 권한이 없습니다."); + } + } +} diff --git a/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/repository/CrewPostRepository.java b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/repository/CrewPostRepository.java new file mode 100644 index 0000000..7562ecd --- /dev/null +++ b/src/backend/src/main/java/RunningMachines/R2R/domain/crew/post/repository/CrewPostRepository.java @@ -0,0 +1,13 @@ +package RunningMachines.R2R.domain.crew.post.repository; + +import RunningMachines.R2R.domain.crew.board.entity.CrewBoard; +import RunningMachines.R2R.domain.crew.post.entity.CrewPost; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public interface CrewPostRepository extends JpaRepository { + List findAllByUserIdAndBoard(Long userId, CrewBoard board); + List findAllByCrewIdAndBoard(Long crewId, CrewBoard board); +}