Skip to content

Commit

Permalink
chore: 코드 정렬
Browse files Browse the repository at this point in the history
  • Loading branch information
9898s committed Oct 26, 2023
1 parent aa70b76 commit 1ea7bc1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package com.snsIntegrationFeedService.post.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.snsIntegrationFeedService.post.dto.PostDetailResponseDto;
import com.snsIntegrationFeedService.post.service.PostService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Post API", description = "Post 관련 API 정보를 담고 있습니다.")
@RequiredArgsConstructor
@RestController
public class PostController {

private final PostService postService;
private final PostService postService;

@Operation(summary = "게시글 상세보기", description = "유저가 게시물을 클릭 시 사용되는 API")
@GetMapping("/api/post/{postId}")
public ResponseEntity<PostDetailResponseDto> getPostDetail(@PathVariable String postId) {
PostDetailResponseDto postDetailResponseDto = postService.getPostDetail(postId);
return ResponseEntity.ok().body(postDetailResponseDto);
}
@Operation(summary = "게시글 상세보기", description = "유저가 게시물을 클릭 시 사용되는 API")
@GetMapping("/api/post/{postId}")
public ResponseEntity<PostDetailResponseDto> getPostDetail(@PathVariable String postId) {
PostDetailResponseDto postDetailResponseDto = postService.getPostDetail(postId);
return ResponseEntity.ok().body(postDetailResponseDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.snsIntegrationFeedService.post.dto;

import com.snsIntegrationFeedService.post.entity.Post;
import java.time.LocalDateTime;
import java.util.List;

import com.snsIntegrationFeedService.post.entity.Post;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -14,29 +16,29 @@
@Getter
public class PostDetailResponseDto {

private String postId;
private String type;
private String title;
private String content;
private List<String> hashtag;
private long viewCount;
private long likeCount;
private long shareCount;
private LocalDateTime modifiedAt;
private LocalDateTime createdAt;
private String postId;
private String type;
private String title;
private String content;
private List<String> hashtag;
private long viewCount;
private long likeCount;
private long shareCount;
private LocalDateTime modifiedAt;
private LocalDateTime createdAt;

public static PostDetailResponseDto from(Post post, List<String> postHashtags) {
return PostDetailResponseDto.builder()
.postId(post.getPostId())
.type(post.getType().name())
.title(post.getTitle())
.content(post.getContent())
.hashtag(postHashtags)
.viewCount(post.getViewCount())
.likeCount(post.getLikeCount())
.shareCount(post.getShareCount())
.modifiedAt(post.getModifiedAt())
.createdAt(post.getModifiedAt())
.build();
}
public static PostDetailResponseDto from(Post post, List<String> postHashtags) {
return PostDetailResponseDto.builder()
.postId(post.getPostId())
.type(post.getType().name())
.title(post.getTitle())
.content(post.getContent())
.hashtag(postHashtags)
.viewCount(post.getViewCount())
.likeCount(post.getLikeCount())
.shareCount(post.getShareCount())
.modifiedAt(post.getModifiedAt())
.createdAt(post.getModifiedAt())
.build();
}
}
58 changes: 30 additions & 28 deletions src/main/java/com/snsIntegrationFeedService/post/entity/Post.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.snsIntegrationFeedService.post.entity;

import java.util.ArrayList;
import java.util.List;

import com.snsIntegrationFeedService.common.entity.Timestamped;
import com.snsIntegrationFeedService.postHashtag.entity.PostHashtag;
import com.snsIntegrationFeedService.user.entity.User;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand All @@ -14,48 +18,46 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;

@Entity
@Getter
public class Post extends Timestamped {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@Column(nullable = false)
private String postId;
@Column(nullable = false)
private String postId;

@Column(nullable = false)
@Enumerated(value = EnumType.STRING)
private PostTypeEnum type;
@Column(nullable = false)
@Enumerated(value = EnumType.STRING)
private PostTypeEnum type;

@Column(nullable = false)
private String title;
@Column(nullable = false)
private String title;

@Column(columnDefinition = "TEXT", nullable = false)
private String content;
@Column(columnDefinition = "TEXT", nullable = false)
private String content;

@Column(nullable = false)
private Long viewCount;
@Column(nullable = false)
private Long viewCount;

@Column(nullable = false)
private Long likeCount;
@Column(nullable = false)
private Long likeCount;

@Column(nullable = false)
private Long shareCount;
@Column(nullable = false)
private Long shareCount;

@OneToMany(mappedBy = "post", orphanRemoval = true)
private List<PostHashtag> postHashtagList = new ArrayList<>();
@OneToMany(mappedBy = "post", orphanRemoval = true)
private List<PostHashtag> postHashtagList = new ArrayList<>();

public void view() {
this.viewCount++;
}
public void view() {
this.viewCount++;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.snsIntegrationFeedService.post.repository;

import com.snsIntegrationFeedService.post.entity.Post;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.snsIntegrationFeedService.post.entity.Post;

public interface PostRepository extends JpaRepository<Post, Long> {

Optional<Post> findByPostId(String postId);
Optional<Post> findByPostId(String postId);
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
package com.snsIntegrationFeedService.post.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.snsIntegrationFeedService.common.error.CustomErrorCode;
import com.snsIntegrationFeedService.common.exception.CustomException;
import com.snsIntegrationFeedService.post.dto.PostDetailResponseDto;
import com.snsIntegrationFeedService.post.entity.Post;
import com.snsIntegrationFeedService.post.repository.PostRepository;
import java.util.List;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
@Service
public class PostService {

private final PostRepository postRepository;
private final PostRepository postRepository;

@Transactional
public PostDetailResponseDto getPostDetail(String postId) {
// 예외 처리
Post post = postRepository.findByPostId(postId).orElseThrow(
() -> new CustomException(CustomErrorCode.POST_ID_NOT_FOUND)
);
@Transactional
public PostDetailResponseDto getPostDetail(String postId) {
// 예외 처리
Post post = postRepository.findByPostId(postId).orElseThrow(
() -> new CustomException(CustomErrorCode.POST_ID_NOT_FOUND)
);

// 해시 태그명 가져오기
List<String> hashTags = post.getPostHashtagList().stream()
.map(postHashtag -> postHashtag.getHashtag().getName())
.toList();
// 해시 태그명 가져오기
List<String> hashTags = post.getPostHashtagList().stream()
.map(postHashtag -> postHashtag.getHashtag().getName())
.toList();

// 조회수 증가
post.view();
return PostDetailResponseDto.from(post, hashTags);
}
// 조회수 증가
post.view();
return PostDetailResponseDto.from(post, hashTags);
}
}

0 comments on commit 1ea7bc1

Please sign in to comment.