Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 게시글 생성 API #39

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/post/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.todaysgym.todaysgym.post.comment.Comment;
import com.todaysgym.todaysgym.post.photo.PostPhoto;
import com.todaysgym.todaysgym.record.Record;
import com.todaysgym.todaysgym.record.photo.RecordPhoto;
import com.todaysgym.todaysgym.utils.BaseTimeEntity;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -63,4 +64,25 @@ public void deleteRecord(){
public void addReportCount() {
this.report++;
}

public static Post createPost(Category category, Member writer, String title, String content, Record record){
Post post = new Post();
post.setCategory(category);
post.setRecord(record);
post.createTitle(title);
post.createContent(content);
post.createMember(writer);
return post;
}

public void setCategory(Category category) {this.category = category;}
public void setRecord(Record record) {this.record = record;}
public void createMember(Member member){this.member = member;}
public void createContent(String content){this.content = content;}
public void createTitle(String title){this.title = title;}

public void createPhotoList(PostPhoto postPhoto){
photoList.add(postPhoto);
postPhoto.createPost(this);
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/post/PostController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.todaysgym.todaysgym.post;

import com.todaysgym.todaysgym.config.response.BaseResponse;
import com.todaysgym.todaysgym.member.Member;
import com.todaysgym.todaysgym.post.dto.PostPostReq;
import com.todaysgym.todaysgym.record.dto.RecordGetReq;
import com.todaysgym.todaysgym.report.dto.ReportReq;
import com.todaysgym.todaysgym.utils.UtilService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequiredArgsConstructor
public class PostController {

private final UtilService utilService;
private final PostService postService;

/** 게시글 생성하기
* [POST] /post
*/
@PostMapping("/post")
public BaseResponse<String> createPost(@RequestPart(value = "image", required = false) List<MultipartFile> multipartFiles,
@RequestPart(value = "postPostReq") @Valid PostPostReq postPostReq){
Member writer = utilService.findByMemberIdWithValidation(1L);
return new BaseResponse<>(postService.createPost(writer, postPostReq, multipartFiles));
}

}
35 changes: 35 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/post/PostService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package com.todaysgym.todaysgym.post;

import com.todaysgym.todaysgym.config.exception.BaseException;
import com.todaysgym.todaysgym.member.Member;
import com.todaysgym.todaysgym.post.PostRepository;
import com.todaysgym.todaysgym.post.dto.PostPostReq;
import com.todaysgym.todaysgym.post.photo.PostPhotoRepository;
import com.todaysgym.todaysgym.post.photo.PostPhotoService;
import com.todaysgym.todaysgym.record.Record;
import com.todaysgym.todaysgym.record.dto.RecordGetReq;
import com.todaysgym.todaysgym.record.photo.RecordPhotoRepository;
import com.todaysgym.todaysgym.record.photo.RecordPhotoService;
import com.todaysgym.todaysgym.utils.S3Service;
import com.todaysgym.todaysgym.utils.UtilService;
import com.todaysgym.todaysgym.utils.dto.getS3Res;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

import static com.todaysgym.todaysgym.config.exception.errorCode.RecordErrorCode.EMPTY_RECORD;
Expand All @@ -15,11 +27,34 @@
@RequiredArgsConstructor
public class PostService {
private final PostRepository postRepository;
private final PostPhotoService postPhotoService;
private final S3Service s3Service;
private final UtilService utilService;

/**
* 기록과 관련된 게시글 조회 후 기록 null로 변경
*/
public List<Post> deleteRecord(Long recordId){
return postRepository.findPostByRecord(recordId);
}

@Transactional
public String createPost(Member writer, PostPostReq postPostReq, List<MultipartFile> multipartFiles) throws BaseException {
// 1. 기록 첨부 여부 조회 -> 기록 없으면 NULL로 저장
Record record = null;
if(postPostReq.getRecordId() != null) {
record = utilService.findByRecordIdWithValidation(postPostReq.getRecordId());
}
// 2. 게시글 생성
Post post = Post.createPost(postPostReq.getCategory(), writer, postPostReq.getTitle(), postPostReq.getContent(), record);
postRepository.save(post);

// 3. 이미지 첨부했다면 저장
if(multipartFiles != null) {
List<getS3Res> imgUrls = s3Service.uploadFile(multipartFiles);
postPhotoService.saveAllPostPhotoByPost(imgUrls, post);
}

return "postId: " + post.getPostId() + "인 게시글을 생성했습니다.";
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/todaysgym/todaysgym/post/dto/PostPostReq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.todaysgym.todaysgym.post.dto;

import com.todaysgym.todaysgym.category.Category;
import com.todaysgym.todaysgym.tag.Tag;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Data
public class PostPostReq {
@NotNull
private Category category;
@NotBlank
private String title;
@NotBlank
private String content;

private Long recordId; // null 일 수도 있음

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.todaysgym.todaysgym.record.Record;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "post_photo")
Expand All @@ -30,4 +34,8 @@ public class PostPhoto {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;

public void createPost(Post post){
this.post = post;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package com.todaysgym.todaysgym.post.photo;

import com.todaysgym.todaysgym.record.photo.RecordPhoto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PostPhotoRepository extends JpaRepository<PostPhoto, Long> {

@Query("SELECT pp FROM PostPhoto pp WHERE pp.post.postId = :postId")
List<PostPhoto> findAllByPost(@Param("postId") Long postId);

@Query("SELECT pp.id FROM PostPhoto pp WHERE pp.post.postId = :postId")
List<Long> findAllId(@Param("postId") Long postId);

@Modifying
@Query("DELETE FROM PostPhoto pp WHERE pp.id IN :ids")
Integer deleteAllByPost(@Param("ids") List<Long> ids);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.todaysgym.todaysgym.post.photo;

import com.todaysgym.todaysgym.post.Post;
import com.todaysgym.todaysgym.record.photo.RecordPhoto;
import com.todaysgym.todaysgym.utils.S3Service;
import com.todaysgym.todaysgym.utils.dto.getS3Res;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@AllArgsConstructor
public class PostPhotoService {

private final PostPhotoRepository postPhotoRepository;
private final S3Service s3Service;

@Transactional
public void savePostPhoto(List<PostPhoto> postPhotos){
postPhotoRepository.saveAll(postPhotos);
}

public List<PostPhoto> findByPostId(Long postId){ return postPhotoRepository.findAllByPost(postId); }

/**
* 여러개의 postPhoto 저장
*/
@Transactional
public void saveAllPostPhotoByPost(List<getS3Res> getS3ResList, Post post) {
List<PostPhoto> postPhotos = new ArrayList<>();
for(getS3Res getS3Res : getS3ResList){
PostPhoto postPhoto = PostPhoto.builder()
.imgUrl(getS3Res.getImgUrl())
.fileName(getS3Res.getFileName())
.build();
postPhotos.add(postPhoto);
post.createPhotoList(postPhoto);
}
savePostPhoto(postPhotos);
}

/**
* 게시글과 연관된 모든 postPhoto 삭제
*/
@Transactional
public void deleteAllPostPhotos(List<PostPhoto> postPhotos){
for (PostPhoto postPhoto : postPhotos) {
s3Service.deleteFile(postPhoto.getFileName());
}
}

@Transactional
public void deleteAllPostPhotoByPost(List<Long> ids){
postPhotoRepository.deleteAllByPost(ids);
}

/**
* post와 연관된 모든 id 조회
*/
public List<Long> findAllId(Long postId){
return postPhotoRepository.findAllId(postId);
}
}