Skip to content

Commit

Permalink
Merge pull request #53 from ssu-student-union/feat/48-post
Browse files Browse the repository at this point in the history
[feat] : #48 Post 생성, 수정, 파일 관련 로직 추가, 자잘한 로직 수정
  • Loading branch information
chahyunsoo authored Aug 15, 2024
2 parents 44d794e + ec74332 commit dad075b
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ussum.homepage.application.post.service.PostManageService;
import ussum.homepage.application.post.service.dto.request.PostCreateRequest;
import ussum.homepage.application.post.service.dto.request.PostUpdateRequest;
import ussum.homepage.application.post.service.dto.request.PostUserRequest;
import ussum.homepage.global.ApiResponse;
import ussum.homepage.global.config.auth.UserId;

@RestController
@RequiredArgsConstructor
Expand All @@ -30,4 +34,32 @@ public ResponseEntity<ApiResponse<?>> getBoardPost(@PathVariable(name = "boardCo
return ApiResponse.success(postManageService.getPost(postUserRequest, boardCode, postId));
}

@PostMapping("/{boardCode}/posts")
public ResponseEntity<ApiResponse<?>> createBoardPost(@UserId Long userId,
@PathVariable(name = "boardCode") String boardCode,
@RequestBody PostCreateRequest postCreateRequest){
return ApiResponse.success(postManageService.createBoardPost(userId, boardCode, postCreateRequest));
}

@PostMapping("/{boardCode}/posts/files")
public ResponseEntity<ApiResponse<?>> createBoardPostFile(@UserId Long userId,
@PathVariable(name = "boardCode") String boardCode,
@RequestPart(value = "files") MultipartFile[] files,
@RequestParam(value = "type") String typeName){
return ApiResponse.success(postManageService.createBoardPostFile(userId, boardCode, files, typeName));
}

@PatchMapping("/{boardCode}/posts/{postId}")
public ResponseEntity<ApiResponse<?>> editBoardPost(@PathVariable(name = "boardCode") String boardCode,
@PathVariable(name = "postId") Long postId,
@RequestBody PostUpdateRequest postUpdateRequest) {
return ApiResponse.success(postManageService.editBoardPost(boardCode, postId, postUpdateRequest));
}

@PostMapping("/userIdTest")
public ResponseEntity<ApiResponse<?>> apiTest(@UserId Long userId) {
System.out.println("userId = " + userId);;
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import ussum.homepage.application.post.service.dto.request.PostCreateRequest;
import ussum.homepage.application.post.service.dto.request.PostUpdateRequest;
import ussum.homepage.application.post.service.dto.request.PostUserRequest;
import ussum.homepage.application.post.service.dto.response.postDetail.*;
import ussum.homepage.application.post.service.dto.response.postList.*;
import ussum.homepage.application.post.service.dto.response.postSave.PostCreateResponse;
import ussum.homepage.application.post.service.dto.response.postSave.PostFileResponse;
import ussum.homepage.domain.post.Board;
import ussum.homepage.domain.post.Category;
import ussum.homepage.domain.post.Post;
Expand All @@ -20,10 +25,14 @@
import ussum.homepage.global.common.PageInfo;
import ussum.homepage.global.error.exception.GeneralException;
import ussum.homepage.global.error.status.ErrorStatus;
import ussum.homepage.infra.jpa.post.entity.OngoingStatus;
import ussum.homepage.infra.utils.S3utils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -35,7 +44,11 @@ public class PostManageService {
private final CategoryReader categoryReader;
private final UserReader userReader;
private final PostFileReader postFileReader;
private final PostAppender postAppender;
private final PostFileAppender postFileAppender;
private final PostModifier postModifier;
private final PostStatusProcessor postStatusProcessor;
private final S3utils s3utils;

private final Map<String, BiFunction<Post, Integer, ? extends PostListResDto>> postResponseMap = Map.of(
"공지사항게시판", (post, ignored) -> NoticePostResponse.of(post),
Expand Down Expand Up @@ -115,6 +128,41 @@ public PostDetailRes<?> getPost(PostUserRequest postUserRequest, String boardCod

return PostDetailRes.of(response);
}

@Transactional
public PostCreateResponse createBoardPost(Long userId, String boardCode, PostCreateRequest postCreateRequest){
Board board = boardReader.getBoardWithBoardCode(boardCode);
Category category = categoryReader.getCategoryWithCode(postCreateRequest.categoryCode());
User user = userReader.getUserWithId(userId);
String onGoingStatus = Objects.equals(boardCode, "PETITION") ? "IN_PROGRESS" : null;

Post post = postAppender.createPost(postCreateRequest.toDomain(board, user, category, onGoingStatus));
postFileAppender.updatePostIdForIds(postCreateRequest.postFileList(), post.getId());
return PostCreateResponse.of(post.getId(), boardCode);
}

@Transactional
public List<PostFileResponse> createBoardPostFile(Long userId, String boardCode, MultipartFile[] files, String typeName){
List<String> urlList = s3utils.uploadFileWithPath(userId, boardCode, files, typeName);
List<PostFile> postFiles = convertUrlsToPostFiles(urlList, typeName);
List<PostFile> afterSaveList = postFileAppender.saveAllPostFile(postFiles);

return afterSaveList.stream()
.map(postFile -> PostFileResponse.of(postFile.getId(), postFile.getUrl()))
.collect(Collectors.toList());
}

private List<PostFile> convertUrlsToPostFiles(List<String> urlList, String typeName) {
return urlList.stream()
.map(url -> PostFile.of(null, typeName, url, null, null))
.collect(Collectors.toList());
}

@Transactional
public Long editBoardPost(String boardCode, Long postId, PostUpdateRequest postUpdateRequest){
Post post = postModifier.updatePost(boardCode, postId, postUpdateRequest);
return post.getId();
}
}
//스위치 사용 로직
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void createPost(Long userId, String boardCode, PostCreateRequest postCrea
//user도 찾아 와야 하지 않을까
User user = userReader.getUserWithId(userId);

postAppender.createPost(postCreateRequest.toDomain(board, user, category));
postAppender.createPost(postCreateRequest.toDomain(board, user, category, null));
}

public PostResponse editPost(String boardCode,Long postId, PostUpdateRequest postUpdateRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
import ussum.homepage.domain.post.Post;
import ussum.homepage.domain.user.User;

import java.util.List;

public record PostCreateRequest(
String title,
String content,
String categoryCode,
String thumbNailImage
String thumbNailImage,
List<Long> postFileList
) {
public Post toDomain(Board board, User user, Category category) {
public Post toDomain(Board board, User user, Category category, String OnGoingStatus) {
return Post.of(
null,
title,
content,
1,
thumbNailImage,
"새로운",
null,
OnGoingStatus,
null,
null,
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ussum.homepage.application.post.service.dto.response.postSave;

import lombok.Builder;
import lombok.Getter;
import ussum.homepage.domain.post.Post;

@Getter
@Builder
public class PostCreateResponse {
private Long post_id;
private String boardCode;

public static PostCreateResponse of(Long id, String boardCode){
return PostCreateResponse.builder()
.post_id(id)
.boardCode(boardCode)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ussum.homepage.application.post.service.dto.response.postSave;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class PostFileResponse {
private Long id;
private String url;

public static PostFileResponse of(Long id, String url){
return PostFileResponse.builder()
.id(id)
.url(url)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import ussum.homepage.application.user.service.dto.request.OnBoardingRequest;
import ussum.homepage.application.user.service.dto.response.OnBoardingResponse;
import ussum.homepage.domain.csv_user.StudentCsv;
import ussum.homepage.domain.csv_user.StudentCsvRepository;
import ussum.homepage.domain.csv_user.service.StudentCsvModifier;
import ussum.homepage.domain.csv_user.service.StudentCsvReader;
import ussum.homepage.domain.user.User;
import ussum.homepage.domain.user.service.UserModifier;
import ussum.homepage.domain.user.service.UserReader;
import ussum.homepage.global.error.exception.GeneralException;
import ussum.homepage.infra.jpa.csv_user.StudentCsvMapper;

import java.util.Optional;

import static ussum.homepage.global.error.status.ErrorStatus.USER_NOT_FOUND;

Expand All @@ -27,7 +20,6 @@ public class OnBoardingService {
private final UserModifier userModifier;
private final UserReader userReader;
private final StudentCsvReader studentCsvReader;
private final StudentCsvModifier studentCsvModifier;

public void saveUserOnBoarding(Long userId, OnBoardingRequest request){
User user = userReader.getUserWithId(userId);
Expand All @@ -38,7 +30,7 @@ public void saveUserOnBoarding(Long userId, OnBoardingRequest request){
/*
TO DO // MemberEntity, GroupEntity 연결
*/
user.updateOnBoardingUser(request);
user.updateOnBoardingUser(request); // 이 메소드 수정 필요
userModifier.save(user);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ussum.homepage.domain.post;

import java.util.List;
import java.util.Optional;

public interface PostFileRepository {
List<PostFile> findAllByPostId(Long postId);
Optional<PostFile> findById(Long id);
List<PostFile> saveAll(List<PostFile> postFiles);
void updatePostIdForIds(List<Long> postFileIds, Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class PostAppender {
private final PostAclRepository postAclRepository;

@Transactional
public void createPost(Post post) {
postRepository.save(post);
public Post createPost(Post post) {
return postRepository.save(post);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ussum.homepage.domain.post.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ussum.homepage.domain.post.Post;
import ussum.homepage.domain.post.PostFile;
import ussum.homepage.domain.post.PostFileRepository;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class PostFileAppender {
private final PostFileRepository postFileRepository;

@Transactional
public List<PostFile> saveAllPostFile(List<PostFile> fileList) {
return postFileRepository.saveAll(fileList);
}

@Transactional
public void updatePostIdForIds(List<Long> postFileIds, Long postId) {
postFileRepository.updatePostIdForIds(postFileIds, postId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public class SecurityConfig {
"/swagger-ui/**",
"/swagger-resources/**",
"/v3/api-docs/**",
"/board/{boardCode}/posts/{postId}",
"/board/posts/{postId}/comments"
// "/board/{boardCode}/posts/{postId}",
// "/board/posts/{postId}/comments"

};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public enum ErrorStatus implements BaseErrorCode {
//온보딩에러
INVALID_ONBOARDING_REQUEST(HttpStatus.BAD_REQUEST,"ONBOARDING_001","온보딩 정보가 올바르지 않습니다."),

//S3에러
S3_ERROR(HttpStatus.INTERNAL_SERVER_ERROR,"S3_001","S3에 파일 저장이 실패했습니다."),

//Body 에러
INVALID_BODY(HttpStatus.BAD_REQUEST, "BODY_ERROR", "Body가 올바르지 않습니다.");

Expand Down
20 changes: 19 additions & 1 deletion src/main/java/ussum/homepage/infra/jpa/post/PostFileMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import ussum.homepage.domain.post.PostFile;
import ussum.homepage.infra.jpa.post.entity.PostFileEntity;

import java.util.List;

@Component
public class PostFileMapper {
public PostFile toDomain(PostFileEntity postFileEntity) {
Expand All @@ -12,7 +14,23 @@ public PostFile toDomain(PostFileEntity postFileEntity) {
postFileEntity.getTypeName(),
postFileEntity.getUrl(),
postFileEntity.getSize(),
postFileEntity.getPostEntity().getId()
null
);
}

public List<PostFile> toDomain(List<PostFileEntity> postFileEntities) {
return postFileEntities.stream()
.map(this::toDomain)
.toList();
}

public PostFileEntity toEntity(PostFile postFile) {
return PostFileEntity.of(
postFile.getId(),
postFile.getTypeName(),
postFile.getUrl(),
null,
null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ussum.homepage.infra.jpa.post.repository.PostFileJpaRepository;

import java.util.List;
import java.util.Optional;

import static ussum.homepage.infra.jpa.post.entity.QPostFileEntity.postFileEntity;

Expand All @@ -28,4 +29,27 @@ public List<PostFile> findAllByPostId(Long postId) {
.map(postFileMapper::toDomain).toList();
}

@Override
public Optional<PostFile> findById(Long id) {
return postFileJpaRepository.findById(id).map(postFileMapper::toDomain);
}

@Override
public List<PostFile> saveAll(List<PostFile> fileList) {
return postFileMapper.toDomain(
postFileJpaRepository.saveAll(
fileList.stream()
.map(file -> postFileMapper.toEntity(file))
.toList())
);
}

@Override
public void updatePostIdForIds(List<Long> postFileIds, Long postId) {
queryFactory
.update(postFileEntity)
.set(postFileEntity.postEntity.id, postId)
.where(postFileEntity.id.in(postFileIds))
.execute();
}
}
Loading

0 comments on commit dad075b

Please sign in to comment.