Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
42 changes: 0 additions & 42 deletions .github/workflows/deploy.yml

This file was deleted.

1 change: 1 addition & 0 deletions .github/workflows/spring-delploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- closed
branches:
- develop
- main
paths:
- 'BACK/spring-app/**'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.starchive.springapp.category.dto;

import com.starchive.springapp.category.domain.Category;
import lombok.Data;

@Data
public class CategorySimpleDto {
private Long categoryId;
private String categoryName;

public static CategorySimpleDto from(Category category) {
CategorySimpleDto categorySimpleDto = new CategorySimpleDto();
categorySimpleDto.categoryId = category.getId();
categorySimpleDto.categoryName = category.getName();

return categorySimpleDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.data.repository.query.Param;

public interface CategoryRepository extends JpaRepository<Category, Long> {
@Query("SELECT c FROM Category c LEFT JOIN FETCH c.children WHERE c.id = :id")
@Query("SELECT c FROM Category c LEFT JOIN FETCH c.children left join fetch c.parent WHERE c.id = :id")
Optional<Category> findByIdWithChildren(@Param("id") Long id);

@Query("SELECT DISTINCT c FROM Category c LEFT JOIN FETCH c.children WHERE c.parent IS NULL")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public List<CategoryDto> findAll() {
}

public Category findOne(Long id) {
return categoryRepository.findById(id).orElseThrow(CategoryNotFoundException::new);
return categoryRepository.findByIdWithChildren(id).orElseThrow(CategoryNotFoundException::new);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public class ErrorMessage {
final public static String NOT_IMAGE_EXTENSION = "올바르지 않은 이미지 확장자(허용 확장자: \"jpg\", \"png\", \"gif\", \"jpeg\"";
final public static String INVALID_FILE_SIZE = "파일 크기가 너무 큽니다. 최대 이미지 크기: 5MB";
final public static String CATEGORY_NOT_FOUND = "카테고리가 존재하지 않습니다.";
final public static String HASHTAG_NOT_FOUND = "카테고리가 존재하지 않습니다.";
final public static String HASHTAG_NOT_FOUND = "해쉬태그가 존재하지 않습니다.";
final public static String POST_NOT_FOUND = "게시글이 존재하지 않습니다.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.starchive.springapp.category.exception.CategoryNotFoundException;
import com.starchive.springapp.hashtag.exception.HashTagNotFoundException;
import com.starchive.springapp.post.exception.PostNotFoundException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -33,8 +34,14 @@ public ResponseEntity<String> handleMaxSizeException(CategoryNotFoundException e
.body(ex.getMessage());
}

@ExceptionHandler(PostNotFoundException.class)
public ResponseEntity<String> handlePostNotFoundException(PostNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ex.getMessage());
}

@ExceptionHandler(HashTagNotFoundException.class)
public ResponseEntity<String> handleMaxSizeException(HashTagNotFoundException ex) {
public ResponseEntity<String> handleHashTagNotFoundException(HashTagNotFoundException ex) {
return ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE)
.body(INVALID_FILE_SIZE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.starchive.springapp.hashtag.dto;

import com.starchive.springapp.hashtag.domain.HashTag;
import lombok.Data;

@Data
public class HashTagResponse {
private Long hashTagId;
private String name;

public static HashTagResponse from(HashTag hashTag) {
HashTagResponse hashTagDto = new HashTagResponse();
hashTagDto.hashTagId = hashTag.getId();
hashTagDto.name = hashTag.getName();
return hashTagDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public interface HashTagRepository extends JpaRepository<HashTag, Long> {
+ "where c.id = :categoryId")
List<HashTag> findAllByCategoryId(@Param("categoryId") Long categoryId);

@Query("select h from HashTag h "
+ "join fetch PostHashTag ph on h.id = ph.hashTag.id "
+ "where ph.post.id = :postId")
List<HashTag> findAllByPostId(@Param("postId") Long postId);

List<HashTag> findManyByIdIn(@Param("ids") List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.starchive.springapp.category.repository.CategoryRepository;
import com.starchive.springapp.hashtag.domain.HashTag;
import com.starchive.springapp.hashtag.dto.HashTagDto;
import com.starchive.springapp.hashtag.dto.HashTagResponse;
import com.starchive.springapp.hashtag.exception.HashTagNotFoundException;
import com.starchive.springapp.hashtag.repository.HashTagRepository;
import com.starchive.springapp.posthashtag.repository.PostHashTagRepository;
Expand All @@ -20,6 +21,7 @@ public class HashTagService {
private final HashTagRepository hashTagRepository;
private final CategoryRepository categoryRepository;
private final PostHashTagRepository postHashTagRepository;
//private final PostService postService;

public HashTag save(String name) {
HashTag hashTag = new HashTag(name);
Expand Down Expand Up @@ -54,6 +56,11 @@ public List<HashTagDto> findManyByCategory(Long CategoryId) {
return hashTagRepository.findAllByCategoryId(category.getId()).stream().map(HashTagDto::from).toList();
}

public List<HashTagResponse> findManyByPost(Long postId) {

return hashTagRepository.findAllByPostId(postId).stream().map(HashTagResponse::from).toList();
}

public HashTagDto updateName(Long hashTagId, String newName) {
HashTag hashTag = hashTagRepository.findById(hashTagId).orElseThrow(HashTagNotFoundException::new);
hashTag.changeName(newName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.starchive.springapp.post.controller;

import com.starchive.springapp.post.dto.PostCreateRequest;
import com.starchive.springapp.post.dto.PostDto;
import com.starchive.springapp.post.dto.PostListResponse;
import com.starchive.springapp.post.service.PostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
Expand All @@ -25,4 +30,24 @@ public ResponseEntity<?> post(@Valid @RequestBody PostCreateRequest request) {

return ResponseEntity.status(201).build();
}

@GetMapping("/posts")
@Operation(summary = "게시글 목록 조회")
public ResponseEntity<?> findPosts(
@RequestParam(name = "category", required = false) Long category,
@RequestParam(name = "tag", required = false) Long tag,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {

PostListResponse postListResponse = postService.findPosts(category, tag, page, pageSize);

return ResponseEntity.ok(postListResponse);
}

@GetMapping("/post/{postId}")
@Operation(summary = "상세조회")
public ResponseEntity<PostDto> findPost(@PathVariable("postId") Long postId) {
PostDto postDto = postService.findOne(postId);
return ResponseEntity.ok(postDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.starchive.springapp.post.dto;

import com.starchive.springapp.category.domain.Category;
import com.starchive.springapp.category.dto.CategorySimpleDto;
import com.starchive.springapp.hashtag.dto.HashTagResponse;
import com.starchive.springapp.post.domain.Post;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;

@Data
public class PostDto {
private List<CategorySimpleDto> categoryHier;
private Long postId;
private String title;
private String author;
private LocalDateTime createdAt;
private String content;
private List<HashTagResponse> hashTags;

public static PostDto of(Post post, List<HashTagResponse> hashTagDtos) {
PostDto postDto = new PostDto();

postDto.categoryHier = setCategoryHier(post.getCategory());

postDto.postId = post.getId();
postDto.title = post.getTitle();
postDto.author = post.getAuthor();
postDto.createdAt = post.getCreateAt();
postDto.content = post.getContent();

postDto.hashTags = hashTagDtos;

return postDto;

}

private static List<CategorySimpleDto> setCategoryHier(Category category) {
List<CategorySimpleDto> categoryHier = new ArrayList<>();
if (category == null) {
return categoryHier;
}
if (category.getParent() != null) {
Category parent = category.getParent();
CategorySimpleDto parentCategoryDto = CategorySimpleDto.from(parent);
categoryHier.add(parentCategoryDto);
}

CategorySimpleDto categoryDto = CategorySimpleDto.from(category);
categoryHier.add(categoryDto);
return categoryHier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.starchive.springapp.post.dto;

import java.util.List;
import lombok.Data;
import org.springframework.data.domain.Page;

@Data
public class PostListResponse {
private int currentPage;
private int totalPages;
private long totalCount;
private List<PostSimpleDto> posts;

public static PostListResponse from(Page<PostSimpleDto> dtoPage) {
PostListResponse postListResponse = new PostListResponse();
postListResponse.currentPage = dtoPage.getNumber();
postListResponse.totalPages = dtoPage.getTotalPages();
postListResponse.totalCount = dtoPage.getTotalElements();
postListResponse.posts = dtoPage.getContent();
return postListResponse;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.starchive.springapp.post.dto;

import com.starchive.springapp.category.domain.Category;
import com.starchive.springapp.category.dto.CategorySimpleDto;
import com.starchive.springapp.hashtag.dto.HashTagResponse;
import com.starchive.springapp.post.domain.Post;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;

@Data
public class PostSimpleDto {
private List<CategorySimpleDto> categoryHier;
private Long postId;
private String title;
private String author;
private LocalDateTime createdAt;
private String content;
private List<HashTagResponse> hashTags;

public static PostSimpleDto of(Post post, List<HashTagResponse> hashTagDtos) {
PostSimpleDto postDto = new PostSimpleDto();

postDto.categoryHier = setCategoryHier(post.getCategory());

postDto.postId = post.getId();
postDto.title = post.getTitle();
postDto.author = post.getAuthor();
postDto.createdAt = post.getCreateAt();
postDto.content = setContent(post.getContent());

postDto.hashTags = hashTagDtos;

return postDto;

}

private static String setContent(String content) {
if (content != null && content.length() > 350) {
return content.substring(0, 350);
}
return content;
}

private static List<CategorySimpleDto> setCategoryHier(Category category) {
List<CategorySimpleDto> categoryHier = new ArrayList<>();
if (category == null) {
return categoryHier;
}
if (category.getParent() != null) {
Category parent = category.getParent();
CategorySimpleDto parentCategoryDto = CategorySimpleDto.from(parent);
categoryHier.add(parentCategoryDto);
}

CategorySimpleDto categoryDto = CategorySimpleDto.from(category);
categoryHier.add(categoryDto);
return categoryHier;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.starchive.springapp.post.exception;

import static com.starchive.springapp.global.ErrorMessage.POST_NOT_FOUND;

public class PostNotFoundException extends RuntimeException {
public PostNotFoundException() {
super(POST_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package com.starchive.springapp.post.repository;

import com.starchive.springapp.post.domain.Post;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface PostRepository extends JpaRepository<Post, Long> {
@Query("select p from Post p "
+ "where (:categoryIds is null or p.category.id in :categoryIds) "
+ "and (:postIds is null or p.id in :postIds)")
Page<Post> findManyByCategoryIds(@Param("categoryIds") List<Long> categoryIds,
@Param("postIds") List<Long> postIds,
Pageable pageable);

}
Loading
Loading