Skip to content

Commit

Permalink
BE: [feat] 공지글 상세조회 CSID-DGU#83
Browse files Browse the repository at this point in the history
  • Loading branch information
Seoyoung2222 committed Nov 27, 2024
1 parent 8e4ca42 commit 1fb43c5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
package RunningMachines.R2R.domain.crew.post.notice.controller;

import RunningMachines.R2R.domain.crew.post.entity.CrewPost;
import RunningMachines.R2R.domain.crew.post.notice.dto.NoticePostCreateRequestDto;
import RunningMachines.R2R.domain.crew.post.notice.dto.NoticePostResponseDto;
import RunningMachines.R2R.domain.crew.post.notice.service.NoticePostCommandService;
import RunningMachines.R2R.domain.crew.post.notice.service.NoticePostQueryService;
import RunningMachines.R2R.domain.user.entity.User;
import RunningMachines.R2R.domain.user.service.AuthCommandService;
import RunningMachines.R2R.domain.user.service.UserCommandService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/crew/{crewId}/notice")
@RequiredArgsConstructor
public class NoticePostController {
private final NoticePostCommandService noticePostCommandService;
private final NoticePostQueryService noticePostQueryService;
private final AuthCommandService authCommandService;

@PostMapping()
@PostMapping
public ResponseEntity<Long> createNoticePost(@PathVariable Long crewId, @RequestBody NoticePostCreateRequestDto requestDto) {
User currentUser = authCommandService.getCurrentUser();
noticePostCommandService.createNoticePost(crewId, currentUser, requestDto);
return ResponseEntity.ok(crewId);
}

@GetMapping("/{crewPostId}")
public ResponseEntity<NoticePostResponseDto> getNoticePostDetail(@PathVariable Long crewId, @PathVariable Long crewPostId) {
return ResponseEntity.ok(noticePostQueryService.getPostDetail(crewId, crewPostId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package RunningMachines.R2R.domain.crew.post.notice.dto;

import RunningMachines.R2R.domain.crew.post.entity.CrewPost;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@Builder
public class NoticePostResponseDto {
private String title;
private String content;
private String author;
private String crewName;
private LocalDateTime createdDate;

public static NoticePostResponseDto fromEntity(CrewPost crewPost) {
return NoticePostResponseDto.builder()
.title(crewPost.getTitle())
.content(crewPost.getContent())
.author(crewPost.getUser().getNickname())
.crewName(crewPost.getCrew().getTitle())
.createdDate(crewPost.getCreatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.post.entity.CrewPost;
import RunningMachines.R2R.domain.crew.post.notice.dto.NoticePostResponseDto;
import RunningMachines.R2R.domain.crew.post.repository.CrewPostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class NoticePostQueryService {
private final CrewPostRepository crewPostRepository;

public NoticePostResponseDto getPostDetail(Long crewId, Long crewpostId) {
CrewPost noticePost = crewPostRepository.findByIdAndCrewIdAndBoard(crewpostId, crewId, CrewBoard.NOTICE)
.orElseThrow(() -> new IllegalArgumentException("공지글을 찾을 수 없습니다."));
return NoticePostResponseDto.fromEntity(noticePost);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
public interface CrewPostRepository extends JpaRepository<CrewPost, Long> {
List<CrewPost> findAllByUserIdAndBoard(Long userId, CrewBoard board);
List<CrewPost> findAllByCrewIdAndBoard(Long crewId, CrewBoard board);

Optional<CrewPost> findByIdAndCrewIdAndBoard(Long id, Long crewId, CrewBoard board);
}

0 comments on commit 1fb43c5

Please sign in to comment.