-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from CSID-DGU/backend/feature/crew
BE: [feat] 공지글 작성하기 #83
- Loading branch information
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
7 changes: 6 additions & 1 deletion
7
...d/src/main/java/RunningMachines/R2R/domain/crew/common/repository/CrewUserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CrewUser, Integer> { | ||
import java.util.Optional; | ||
|
||
public interface CrewUserRepository extends JpaRepository<CrewUser, Long> { | ||
Optional<CrewUser> findByCrewAndUser(Crew crew, User user); | ||
} |
25 changes: 25 additions & 0 deletions
25
...ain/java/RunningMachines/R2R/domain/crew/post/notice/controller/NoticePostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Long> createNoticePost(@PathVariable Long crewId, @RequestBody NoticePostCreateRequestDto requestDto) { | ||
User currentUser = authCommandService.getCurrentUser(); | ||
noticePostCommandService.createNoticePost(crewId, currentUser, requestDto); | ||
return ResponseEntity.ok(crewId); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...main/java/RunningMachines/R2R/domain/crew/post/notice/dto/NoticePostCreateRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
54 changes: 54 additions & 0 deletions
54
...in/java/RunningMachines/R2R/domain/crew/post/notice/service/NoticePostCommandService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("공지글 작성 권한이 없습니다."); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...end/src/main/java/RunningMachines/R2R/domain/crew/post/repository/CrewPostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CrewPost, Long> { | ||
List<CrewPost> findAllByUserIdAndBoard(Long userId, CrewBoard board); | ||
List<CrewPost> findAllByCrewIdAndBoard(Long crewId, CrewBoard board); | ||
} |