-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feat: 파일 업로드 * Feat: 게시글 생성 * Feat: 게시글 삭제 * cleeanup: 중복 코드 삭제 * cleeanup: 게시글 삭제 코드 * Fix: api uri 수정
- Loading branch information
1 parent
a88f6d8
commit 6fce5d0
Showing
17 changed files
with
256 additions
and
192 deletions.
There are no files selected for viewing
2 changes: 0 additions & 2 deletions
2
src/main/java/com/example/tripy/domain/currency/CurrencyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 23 additions & 39 deletions
62
src/main/java/com/example/tripy/domain/post/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
97
src/main/java/com/example/tripy/domain/post/PostService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
src/main/java/com/example/tripy/domain/post/dto/PostCreateRequestDto.java
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
src/main/java/com/example/tripy/domain/post/dto/PostRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
src/main/java/com/example/tripy/domain/post/dto/PostResponseDto.java
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
src/main/java/com/example/tripy/domain/postfile/FileType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.