Skip to content

Commit

Permalink
Merge pull request #13 from 7th-UMC-Hackathon-TeamV/feat/post
Browse files Browse the repository at this point in the history
Feat/post
  • Loading branch information
chaechaen authored Jan 11, 2025
2 parents c4f9bad + 631ac9e commit 78bf54d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public enum ErrorStatus implements BaseErrorCode {
MEMBER_NOT_FOUND(HttpStatus.BAD_REQUEST, "MEMBER4001", "사용자가 없습니다."),
NICKNAME_NOT_EXIST(HttpStatus.BAD_REQUEST, "MEMBER4002", "닉네임은 필수 입니다."),

// 그룹 에러
TEAMGROUP_NOT_FOUND(HttpStatus.BAD_REQUEST, "TEAMGroup4001", "그룹이 없습니다."),

// 예시,,,
ARTICLE_NOT_FOUND(HttpStatus.NOT_FOUND, "ARTICLE4001", "게시글이 없습니다."),

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/banban/springboot/domain/entity/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class News {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group_id", nullable = false)
private TeamGroup teamGroup;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import banban.springboot.domain.entity.TeamGroup;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface GroupRepository extends JpaRepository<TeamGroup, Long> {
Optional<TeamGroup> findByGroupKey(String groupKey);
boolean existsByGroupKey(String groupKey);
}
9 changes: 8 additions & 1 deletion src/main/java/banban/springboot/service/NewsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import banban.springboot.apiPayload.exception.handler.NewsHandler;
import banban.springboot.domain.entity.Member;
import banban.springboot.domain.entity.News;
import banban.springboot.domain.entity.TeamGroup;
import banban.springboot.repository.GroupRepository;
import banban.springboot.repository.MemberRepository;
import banban.springboot.repository.NewsRepository;
import banban.springboot.web.dto.request.NewsRequestDTO;
Expand All @@ -20,15 +22,20 @@
public class NewsService {
private final NewsRepository newsRepository;
private final MemberRepository memberRepository;
private final GroupRepository groupRepository;

@Transactional
public NewsResponseDTO.NewsCreateResponseDTO createNews(Long groupId, Long memberId, NewsRequestDTO newsRequestDTO) {
public NewsResponseDTO.NewsCreateResponseDTO createNews(String groupKey, Long memberId, NewsRequestDTO newsRequestDTO) {

TeamGroup teamGroup = groupRepository.findByGroupKey(groupKey)
.orElseThrow(() -> new GeneralException(ErrorStatus.TEAMGROUP_NOT_FOUND));

Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

News news = News.builder()

.teamGroup(teamGroup)
.member(member)
.headline(newsRequestDTO.getHeadline())
.content(newsRequestDTO.getContent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class NewsController {
private final NewsService newsService;

@Operation(summary = "뉴스 생성")
@PostMapping("/{groupId}/users/news/{memberId}")
public ApiResponse<NewsResponseDTO.NewsCreateResponseDTO> createNews(@PathVariable Long groupId, @PathVariable Long memberId, @Valid @RequestBody NewsRequestDTO newsRequestDTO) {
NewsResponseDTO.NewsCreateResponseDTO news = newsService.createNews(groupId, memberId, newsRequestDTO);
@PostMapping("/{groupKey}/users/news/{memberId}")
public ApiResponse<NewsResponseDTO.NewsCreateResponseDTO> createNews(@PathVariable String groupKey, @PathVariable Long memberId, @Valid @RequestBody NewsRequestDTO newsRequestDTO) {
NewsResponseDTO.NewsCreateResponseDTO news = newsService.createNews(groupKey, memberId, newsRequestDTO);
return ApiResponse.onSuccess(news);
}
// 뉴스 공감 누르기
Expand Down

0 comments on commit 78bf54d

Please sign in to comment.