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

BE: [feat] 공지글 작성하기 #83 #98

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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);
}