Skip to content

Commit

Permalink
Feat: 게시글 생성, 삭제
Browse files Browse the repository at this point in the history
* Feat: 파일 업로드

* Feat: 게시글 생성

* Feat: 게시글 삭제

* cleeanup: 중복 코드 삭제

* cleeanup: 게시글 삭제 코드

* Fix: api uri 수정
  • Loading branch information
gourderased authored Feb 27, 2024
1 parent a88f6d8 commit 6fce5d0
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 192 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.example.tripy.domain.currency;

import com.example.tripy.domain.currency.dto.CurrencyResponseDto;
import com.example.tripy.domain.post.dto.PostResponseDto;
import com.example.tripy.global.common.response.ApiResponse;
import java.util.List;
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.RequestMapping;
Expand Down
44 changes: 11 additions & 33 deletions src/main/java/com/example/tripy/domain/post/Post.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package com.example.tripy.domain.post;

import com.example.tripy.domain.city.City;
import com.example.tripy.domain.post.dto.PostCreateRequestDto;
import com.example.tripy.domain.postfile.PostFile;
import com.example.tripy.domain.posttag.PostTag;
import com.example.tripy.domain.travelplan.TravelPlan;
import com.example.tripy.domain.member.Member;
import com.example.tripy.domain.post.dto.PostRequestDto.CreatePostRequest;
import com.example.tripy.domain.travelplan.TravelPlan;
import com.example.tripy.global.utils.BaseTimeEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -43,13 +36,11 @@ public class Post extends BaseTimeEntity {
@NotNull
private String content;

private String address;

@ColumnDefault("0")
private Long view;

@ColumnDefault("0")
private Integer thumbs;
private Integer recommendationCount;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
Expand All @@ -59,31 +50,18 @@ public class Post extends BaseTimeEntity {
@JoinColumn(name = "city_id")
private City city;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "travelplan_id")
private TravelPlan travelPlan;

public void viewCountUp() {
this.view++;
}

public void thumbsCountUp() {
this.thumbs++;
}

public void thumbsCountDown() {
this.thumbs--;
}

// TODO: 2024/01/05 PostFile, PostTag리스트 추가
public static Post toEntity(PostCreateRequestDto requestDto, TravelPlan travelPlan){
public static Post toEntity(CreatePostRequest createPostRequest, Member member, City city
, TravelPlan travelPlan) {
return Post.builder()
.title(requestDto.getTitle())
.content(requestDto.getContent())
.address(requestDto.getAddress())
.travelPlan(travelPlan)
.build();
.title(createPostRequest.getTitle())
.content(createPostRequest.getContent())
.member(member)
.city(city)
.travelPlan(travelPlan)
.build();
}

}
62 changes: 23 additions & 39 deletions src/main/java/com/example/tripy/domain/post/PostController.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,40 @@
package com.example.tripy.domain.post;

import com.example.tripy.domain.post.dto.PostCreateRequestDto;
import com.example.tripy.domain.post.dto.PostResponseDto;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import com.example.tripy.domain.post.dto.PostRequestDto.CreatePostRequest;
import com.example.tripy.global.common.response.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import org.springframework.web.bind.annotation.DeleteMapping;
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
@RestController
public class PostController {

private final PostService postService;

/**
* [POST] 게시글 작성
*/
@PostMapping
public ResponseEntity<?> addCorporation(@RequestPart(value = "postCreateRequestDto") @Parameter(schema = @Schema(type = "string", format = MediaType.APPLICATION_JSON_VALUE)) PostCreateRequestDto postCreateRequestDto) throws IOException {
postService.addPost(postCreateRequestDto);
return new ResponseEntity<>(HttpStatus.CREATED);
}


/**
* [GET] 전체 글 조회
*/
@GetMapping
public ResponseEntity<List<PostResponseDto>> getPostList(){
return ResponseEntity.ok(postService.getPostList());
@PostMapping("api/posts")
public ApiResponse<String> createPost(
@RequestBody CreatePostRequest createPostRequest,
@RequestParam(required = false) Long travelPlanId,
@RequestParam Long cityId) {
postService.addPost(createPostRequest, travelPlanId, cityId);
return ApiResponse.onSuccess("글 작성에 성공했습니다.");
}

/**
* [GET] 도시가 속한 나라별 전체 글 조회
*/
// @GetMapping("/{cityId}")
// public ResponseEntity<List<PostResponseDto>> getPostListByCity(@PathVariable Long cityId){
// return ResponseEntity.ok(postService.getPostList(cityId));
// }

/**
* [GET] 특정 글 상세 조회
* [DELETE] 게시글 삭제
*/
@GetMapping("/detail/{postId}")
public ResponseEntity<PostResponseDto> getPost(@PathVariable Long postId){
return ResponseEntity.ok(postService.getPostDetail(postId));
@DeleteMapping("api/posts/{postsId}")
public ApiResponse<String> deletePost(
@PathVariable Long postsId) {
postService.deletePost(postsId);
return ApiResponse.onSuccess("글 삭제에 성공했습니다.");
}


}
}
97 changes: 57 additions & 40 deletions src/main/java/com/example/tripy/domain/post/PostService.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,83 @@
package com.example.tripy.domain.post;

import com.example.tripy.domain.city.City;
import com.example.tripy.domain.city.CityRepository;
import com.example.tripy.domain.post.dto.PostCreateRequestDto;
import com.example.tripy.domain.post.dto.PostResponseDto;
import com.example.tripy.domain.member.Member;
import com.example.tripy.domain.member.MemberRepository;
import com.example.tripy.domain.post.dto.PostRequestDto.CreatePostRequest;
import com.example.tripy.domain.postfile.FileType;
import com.example.tripy.domain.postfile.PostFileService;
import com.example.tripy.domain.posttag.PostTagService;
import com.example.tripy.domain.travelplan.TravelPlan;
import com.example.tripy.domain.travelplan.TravelPlanRepository;
import com.example.tripy.global.common.response.code.status.ErrorStatus;
import com.example.tripy.global.common.response.exception.GeneralException;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class PostService {

private final PostRepository postRepository;
private final TravelPlanRepository travelPlanRepository;
private final CityRepository cityRepository;
private final PostFileService postFileService;
private final MemberRepository memberRepository;
private final PostTagService postTagService;

// TODO: 2024/01/01 예외처리 및 MultipartFile 처리
@Transactional
public void addPost(PostCreateRequestDto postCreateRequestDto){
try{
TravelPlan travelPlan = travelPlanRepository.findById(postCreateRequestDto.getTravelPlanId()).get();
Post post = Post.toEntity(postCreateRequestDto, travelPlan);
postRepository.save(post);
public void addPost(CreatePostRequest createPostRequest, Long travelPlanId, Long cityId) {

Member member = memberRepository.findById(1L)
.orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_MEMBER));

TravelPlan travelPlan = null;
if (travelPlanId != null) {
travelPlan = travelPlanRepository.findById(travelPlanId)
.orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_TRAVEL_PLAN));
}
catch (Exception e){
}
}
City city = cityRepository.findById(cityId)
.orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_CITY));

/**
* 전체 글 조회
*/
Post post = Post.toEntity(createPostRequest, member, city, travelPlan);

postRepository.save(post);
if(createPostRequest.getImageUrls() != null) {
addImages(post, createPostRequest.getImageUrls());
}
if(createPostRequest.getFileUrls() != null) {
addFiles(post, createPostRequest.getFileUrls());
}
if(createPostRequest.getTagIds() != null) {
addTags(post, createPostRequest.getTagIds());
}
}

public List<PostResponseDto> getPostList(){
List<Post> posts = postRepository.findAll();
return posts.stream()
.map(PostResponseDto::toDTO)
.collect(Collectors.toList());
private void addImages(Post post, List<String> imageUrls) {
postFileService.saveFilesByType(post, imageUrls, FileType.IMAGE);
}

// /**
// * 도시가 포함된 나라별 전체 글 조회
// */
// public List<PostResponseDto> getPostList(Long cityId){
// City city = cityRepository.findById(cityId).get();
// Long countryId = city.getCountry().getId();
// List<Post> posts = postRepository.findByCountry(countryId);
//
// return posts.stream()
// .map(PostResponseDto::toDTO)
// .collect(Collectors.toList());
// }
private void addFiles(Post post, List<String> fileUrls) {
postFileService.saveFilesByType(post, fileUrls, FileType.FILE);
}

/**
* 글 하나 조회
*/
public PostResponseDto getPostDetail(Long postId){
Post post = postRepository.findById(postId).get();
post.viewCountUp();
return PostResponseDto.toDTO(post);
private void addTags(Post post, List<Long> tagIds) {
postTagService.savePostTag(post, tagIds);
}

}
public void deletePost(Long postsId) {
Member member = memberRepository.findById(1L)
.orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_MEMBER));

Post post = postRepository.findById(postsId)
.orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_POST));

postFileService.deleteFilesByPost(post);
postTagService.deletePostTagsByPost(post);

postRepository.delete(post);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.tripy.domain.post.dto;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class PostRequestDto {

@NoArgsConstructor
@AllArgsConstructor
@Getter
public static class CreatePostRequest {

private String title;

private String content;

private List<String> imageUrls;

private List<String> fileUrls;

private List<Long> tagIds;
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/com/example/tripy/domain/postfile/FileType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.example.tripy.domain.postfile;

public enum FileType {
파일, 링크, 사진
}
IMAGE, FILE
}
Loading

0 comments on commit 6fce5d0

Please sign in to comment.