Skip to content

Commit

Permalink
Merge pull request #98 from CSID-DGU/backend/feature/crew
Browse files Browse the repository at this point in the history
BE: [feat] 공지글 작성하기 #83
  • Loading branch information
Seoyoung2222 authored Nov 25, 2024
2 parents 1b7b2ee + 618f0d1 commit 9ecc3f1
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
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);
}
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);
}
}
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;
}
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("공지글 작성 권한이 없습니다.");
}
}
}
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);
}

0 comments on commit 9ecc3f1

Please sign in to comment.