Skip to content

Commit

Permalink
Merge pull request #5 from Hey-Porori/feat/myPostList
Browse files Browse the repository at this point in the history
내 게시물 목록보기, 북마크 목록 보기, 북마크 삭제 API 및 많은 수정
  • Loading branch information
jjunjji authored Sep 25, 2023
2 parents f3db540 + 78453a1 commit 75787f1
Show file tree
Hide file tree
Showing 22 changed files with 372 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
@RequiredArgsConstructor
public enum BookmarkExceptionList {
MY_POST_ERROR("B0001", HttpStatus.FORBIDDEN, "자신의 게시글에는 북마크를 생성할 수 없습니다."),
ALREADY_BOOKMARK_ERROR("B0002", HttpStatus.CONFLICT, "이미 북마크된 게시글입니다.");
ALREADY_BOOKMARK_ERROR("B0002", HttpStatus.CONFLICT, "이미 북마크된 게시글입니다."),
NOT_FOUND_BOOKMARKID_ERROR("B0003", HttpStatus.NOT_FOUND, "해당 bookmarkId는 없거나 삭제된 북마크입니다."),
NOT_MY_BOOKMARK_ERROR("B0004", HttpStatus.FORBIDDEN, "자신이 설정한 북마크가 아닙니다.");
private final String errorCode;
private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package porori.backend.community.config.exception.bookmark;

import static porori.backend.community.config.exception.bookmark.BookmarkExceptionList.NOT_FOUND_BOOKMARKID_ERROR;

public class NotFoundBookmarkIdException extends BookmarkException {
public NotFoundBookmarkIdException(){
super(NOT_FOUND_BOOKMARKID_ERROR.getErrorCode(), NOT_FOUND_BOOKMARKID_ERROR.getHttpStatus(), NOT_FOUND_BOOKMARKID_ERROR.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package porori.backend.community.config.exception.bookmark;

import static porori.backend.community.config.exception.bookmark.BookmarkExceptionList.NOT_MY_BOOKMARK_ERROR;

public class NotMyBookmarkException extends BookmarkException{
public NotMyBookmarkException(){
super(NOT_MY_BOOKMARK_ERROR.getErrorCode(), NOT_MY_BOOKMARK_ERROR.getHttpStatus(), NOT_MY_BOOKMARK_ERROR.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package porori.backend.community.config.exception.post;

import static porori.backend.community.config.exception.post.PostExceptionList.NOT_MY_POST_ERROR;

public class NotMyPostException extends PostException{
public NotMyPostException(){
super(NOT_MY_POST_ERROR.getErrorCode(), NOT_MY_POST_ERROR.getHttpStatus(), NOT_MY_POST_ERROR.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
@Getter
@RequiredArgsConstructor
public enum PostExceptionList {
NOT_FOUND_POSTID_ERROR("P0001", HttpStatus.NOT_FOUND, "해당 postId로 Post를 찾을 수 없습니다.");
NOT_FOUND_POSTID_ERROR("P0001", HttpStatus.NOT_FOUND, "해당 postId는 없거나 삭제된 Post입니다."),
NOT_MY_POST_ERROR("P0002", HttpStatus.FORBIDDEN, "자신의 Post가 아닙니다.");

private final String errorCode;
private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Getter
@RequiredArgsConstructor
public enum UserExceptionList {
TOKEN_ERROR("U0001", HttpStatus.UNAUTHORIZED, "AccessToken 값이 잘못되었습니다"),
TOKEN_ERROR("U0001", HttpStatus.UNAUTHORIZED, "AccessToken 값이 잘못되었거나 만료되었습니다"),
USER_BAD_GATEWAY_ERROR("U0002", HttpStatus.BAD_GATEWAY, "User 서버로부터 올바른 응답을 받지 못했습니다.");
private final String errorCode;
private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import porori.backend.community.config.BaseResponse;
import porori.backend.community.dto.BookmarkResDto.*;
import porori.backend.community.dto.PostResDto.*;
import porori.backend.community.service.BookmarkService;

Expand All @@ -21,4 +22,16 @@ public class BookmarkController {
public BaseResponse<PostContentRes> createBookmark(@RequestHeader("Authorization") String token, @PathVariable("postId") Long postId){
return new BaseResponse<>(bookmarkService.createBookmark(token, postId));
}

@Operation(summary = "북마크 목록 보기", description = "page=1부터 totalPage까지 북마크 목록을 10개씩 조회합니다.")
@GetMapping("/bookmark/{page}")
public BaseResponse<BookmarkPreviewListRes> getMyBookmarkList(@RequestHeader("Authorization") String token, @PathVariable int page){
return new BaseResponse<>(bookmarkService.getMyBookmarkList(token, page));
}

@Operation(summary = "북마크 삭제", description = "해당 bookmarkId의 북마크를 삭제합니다.")
@DeleteMapping("/bookmark/{bookmarkId}")
public BaseResponse<BookmarkIdRes> deleteBookmark(@RequestHeader("Authorization") String token, @PathVariable("bookmarkId") Long bookmarkId){
return new BaseResponse<>(bookmarkService.deleteBookmark(token, bookmarkId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import porori.backend.community.config.BaseResponse;
import porori.backend.community.dto.PostReqDto;
import porori.backend.community.dto.PostResDto;
import porori.backend.community.dto.PostReqDto.*;
import porori.backend.community.dto.PostResDto.*;
import porori.backend.community.service.AmazonS3Service;
import porori.backend.community.service.PostService;

Expand All @@ -23,32 +23,50 @@ public class PostController {

@Operation(summary = "게시글 작성", description = "게시글을 작성합니다.")
@PostMapping("/post")
public BaseResponse<PostResDto.PostContentRes> createPost(@RequestHeader("Authorization") String token, @RequestBody PostReqDto.PostContentReq postContent) {
public BaseResponse<PostContentRes> createPost(@RequestHeader("Authorization") String token, @RequestBody PostContentReq postContent) {
return new BaseResponse<>(postService.createPost(token, postContent));
}

@Operation(summary = "Pre-Signed Url 발급", description = "Pre-Signed Url을 발급합니다.")
@GetMapping("/url")
public BaseResponse<PostResDto.PreSignedUrlRes> getPreSignedUrl(@RequestHeader("Authorization") String token) {
public BaseResponse<PreSignedUrlRes> getPreSignedUrl(@RequestHeader("Authorization") String token) {
return new BaseResponse<>(amazonS3Service.getPreSignedUrl(token));
}

@Operation(summary = "지도에서 게시글 목록 보기", description = "현재 위치에서 2KM 이내의 게시글 목록을 조회합니다.")
@PostMapping("/post/map")
public BaseResponse<List<PostResDto.PostOnMapRes>> getPostsOnMap(@RequestHeader("Authorization") String token, @RequestBody PostReqDto.CurrentLocationReq currentLocation){
public BaseResponse<List<PostOnMapRes>> getPostsOnMap(@RequestHeader("Authorization") String token, @RequestBody CurrentLocationReq currentLocation){
return new BaseResponse<>(postService.getPostsOnMap(token, currentLocation));
}

@Operation(summary = "게시글 스와이프 보기", description = "요청받은 postId의 게시글 목록을 조회합니다.")
@PostMapping("/post/swipe")
public BaseResponse<List<PostResDto.PostSwipeRes>> getPostsOnSwipe(@RequestHeader("Authorization") String token, @RequestBody PostReqDto.PostIdListReq postIdList){
public BaseResponse<List<PostSwipeRes>> getPostsOnSwipe(@RequestHeader("Authorization") String token, @RequestBody PostIdListReq postIdList){
return new BaseResponse<>(postService.getPostsOnSwipe(token, postIdList.getPostIdList()));
}

@Operation(summary = "게시글 상세 보기", description = "postId로 게시글을 조회합니다.")
@GetMapping("/post/{postId}")
public BaseResponse<PostResDto.PostDetailRes> getPostDetail(@RequestHeader("Authorization") String token, @PathVariable Long postId){
public BaseResponse<PostDetailRes> getPostDetail(@RequestHeader("Authorization") String token, @PathVariable Long postId){
return new BaseResponse<>(postService.getPostDetail(token, postId));
}

@Operation(summary = "내 게시글 목록 보기", description = "page=1부터 totalPage까지 내 게시글 목록을 10개씩 조회합니다.")
@GetMapping("/post/my/{page}")
public BaseResponse<PostPreviewListRes> getMyPostList(@RequestHeader("Authorization") String token, @PathVariable int page){
return new BaseResponse<>(postService.getMyPostList(token, page));
}

@Operation(summary = "게시글 수정하기", description = "postId로 게시글을 수정합니다.")
@PutMapping("/post/{postId}")
public BaseResponse<PostContentRes> editPost(@RequestHeader("Authorization") String token, @PathVariable Long postId, @RequestBody EditPostContentReq editPostContent){
return new BaseResponse<>(postService.editPost(token, postId, editPostContent));
}

@Operation(summary = "게시글 삭제하기", description = "postId로 게시글을 삭제합니다.")
@DeleteMapping("/post/{postId}")
public BaseResponse<PostContentRes> deletePost(@RequestHeader("Authorization") String token, @PathVariable Long postId){
return new BaseResponse<>(postService.deletePost(token, postId));
}

}
16 changes: 15 additions & 1 deletion src/main/java/porori/backend/community/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import porori.backend.community.domain.core.BaseTimeEntity;
import porori.backend.community.dto.PostReqDto.*;

import javax.persistence.*;
import java.util.ArrayList;
Expand Down Expand Up @@ -32,6 +33,9 @@ public class Post extends BaseTimeEntity {
@Column(nullable = false, columnDefinition = "TEXT")
private String content;

@Column(nullable = false)
private String location; //위, 경도 좌표의 주소(ex. 서울특별시 중구 필동로 1길 or 서울특별시 중구 필동 등 도로명 주소나 지번 주소 중 하나로 통일)

@Column(nullable = false)
private Double latitude;

Expand All @@ -48,10 +52,11 @@ public class Post extends BaseTimeEntity {
private List<PostTag> tagList = new ArrayList<>();

@Builder
public Post(Long userId, String title, String content, Double latitude, Double longitude, String status, List<PostAttach> imageList, List<PostTag> tagList) {
public Post(Long userId, String title, String content, String location, Double latitude, Double longitude, String status, List<PostAttach> imageList, List<PostTag> tagList) {
this.userId = userId;
this.title = title;
this.content = content;
this.location = location;
this.latitude = latitude;
this.longitude = longitude;
this.status = status;
Expand All @@ -62,4 +67,13 @@ public Post(Long userId, String title, String content, Double latitude, Double l
public void changeStatus(String newStatus) {
this.status = newStatus;
}

public void editPost(EditPostContentReq editPostContent){
this.title = editPostContent.getTitle();
this.content = editPostContent.getContent();
}

public void deletePost(){
this.status = "DELETE";
}
}
63 changes: 63 additions & 0 deletions src/main/java/porori/backend/community/dto/BookmarkResDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package porori.backend.community.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import porori.backend.community.domain.Bookmark;

import java.time.LocalDateTime;
import java.util.List;

@NoArgsConstructor

public class BookmarkResDto {
@Getter
public static class BookmarkIdRes {
private Long bookmarkId;

@Builder
public BookmarkIdRes(Long bookmarkId) {
this.bookmarkId = bookmarkId;
}
}

@Getter
@Builder
public static class BookmarkPreviewListRes {
//페이징 처리
//제목, 내용, 위치, 북마크 수 , 댓글 수, 게시글 작성 시간, 1번 이미지
int totalPage;
List<BookmarkPreview> previewList;

}

@Getter
public static class BookmarkPreview {
private Long bookmarkId;
private Long postId;
private String title;
private String content;
private String image;
private String location;
private Long bookmarkCnt;
private Long commentCnt;
private LocalDateTime createdAt;

@Builder
public BookmarkPreview(Bookmark bookmark, Long bookmarkCnt, Long commentCnt){
this.bookmarkId = bookmark.getBookmarkId();
this.postId = bookmark.getPostId().getPostId();
this.title = bookmark.getPostId().getTitle();
this.content = bookmark.getPostId().getContent();
this.image = bookmark.getPostId().getImageList()
.stream()
.findFirst()
.map(image -> image.getImageName())
.orElse(null);
this.location = bookmark.getPostId().getLocation();
this.bookmarkCnt = bookmarkCnt;
this.commentCnt = commentCnt;
this.createdAt = bookmark.getPostId().getCreatedAt();
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/porori/backend/community/dto/PostReqDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class PostReqDto {
public static class PostContentReq {
private String title;
private String content;
private String location;
private Double latitude;
private Double longitude;
private List<String> imageNameList;
Expand All @@ -28,4 +29,12 @@ public static class PostIdListReq{
private List<Long> postIdList;
}

@Getter
public static class EditPostContentReq {
private String title;
private String content;
private List<String> imageNameList;
private List<String> tagList;
}

}
52 changes: 46 additions & 6 deletions src/main/java/porori/backend/community/dto/PostResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public static class PostSwipeRes {
private String nickName;
private String imageUrl;
private String backgroundColor;
private boolean isBookmark;

@Builder
public PostSwipeRes(Post post, CommunityUserInfo user) {
public PostSwipeRes(Post post, CommunityUserInfo user, boolean isBookmark) {
this.postId = post.getPostId();
this.title = post.getTitle();
this.content = post.getContent();
Expand All @@ -73,35 +74,74 @@ public PostSwipeRes(Post post, CommunityUserInfo user) {
this.nickName = user.getNickname();
this.imageUrl = user.getImage();
this.backgroundColor = user.getBackgroundColor();
this.isBookmark = isBookmark;
}
}

@Getter
public static class PostDetailRes {
private String title;
private String content;
private Double latitude;
private Double longitude;
private String location;
private List<String> tagList;
private List<String> imageList;
private LocalDateTime createdAt;
private String nickName;
private String imageUrl;
private String backgroundColor;
private boolean isBookmark;


@Builder
public PostDetailRes(Post post, CommunityUserInfo user) {
public PostDetailRes(Post post, CommunityUserInfo user, boolean isBookmark) {
this.title = post.getTitle();
this.content = post.getContent();
this.latitude = post.getLatitude();
this.longitude = post.getLongitude();
this.location = post.getLocation();
this.tagList = post.getTagList().stream().map((postTag) -> postTag.getTagId().getName()).collect(Collectors.toList());
this.imageList = post.getImageList().stream().map(PostAttach::getImageName).collect(Collectors.toList());
this.createdAt = post.getCreatedAt();
this.nickName = user.getNickname();
this.imageUrl = user.getImage();
this.backgroundColor = user.getBackgroundColor();
this.isBookmark = isBookmark;
}
}

@Getter
@Builder
public static class PostPreviewListRes {
//페이징 처리
//제목, 내용, 위치, 북마크 수 , 댓글 수, 게시글 작성 시간, 1번 이미지
int totalPage;
List<PostPreview> previewList;

}

@Getter
public static class PostPreview {
private Long postId;
private String title;
private String content;
private String image;
private String location;
private Long bookmarkCnt;
private Long commentCnt;
private LocalDateTime createdAt;

@Builder
public PostPreview(Post post, Long bookmarkCnt, Long commentCnt){
this.postId = post.getPostId();
this.title = post.getTitle();
this.content = post.getContent();
this.image = post.getImageList()
.stream()
.findFirst()
.map(image -> image.getImageName())
.orElse(null);
this.location = post.getLocation();
this.bookmarkCnt = bookmarkCnt;
this.commentCnt = commentCnt;
this.createdAt = post.getCreatedAt();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package porori.backend.community.repository;

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 porori.backend.community.domain.Bookmark;
import porori.backend.community.domain.Post;

import java.util.Optional;

public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {
Optional<Bookmark> findByPostIdAndUserId(Post post, Long userId);

Long countByPostId(Post postId);

Page<Bookmark> findAllByUserIdOrderByBookmarkIdDesc(Long userId, Pageable pageable);
Optional<Bookmark> findByBookmarkId(Long bookmarkId);
}
Loading

0 comments on commit 75787f1

Please sign in to comment.