Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/main/java/pilot/instagram/domain/post/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import pilot.instagram.domain.post.dto.request.PostRequest;
import pilot.instagram.domain.post.dto.response.PostDeleteResponse;
import pilot.instagram.domain.post.dto.response.PostPagingResponse;
import pilot.instagram.domain.post.dto.response.PostResponse;
import pilot.instagram.domain.post.service.PostService;
Expand Down Expand Up @@ -36,4 +37,10 @@ public ApiResponse<Page<PostPagingResponse>> getPosts(HttpSession session, Pagea
String userId = session.getAttribute("userId").toString();
return ApiResponse.of(HttpStatus.OK, postService.getPosts(userId, pageable));
}

@DeleteMapping("/{postId}")
public ApiResponse<PostDeleteResponse> deletePost(@PathVariable("postId") Long postId, HttpSession session) {
String userId = session.getAttribute("userId").toString();
return ApiResponse.of(HttpStatus.OK, postService.deletePost(postId, userId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pilot.instagram.domain.post.dto.response;

import lombok.Builder;
import lombok.Getter;
import pilot.instagram.domain.post.entity.Post;

@Getter
public class PostDeleteResponse {
private Long id;

@Builder
private PostDeleteResponse(Long id) {
this.id = id;
}

public static PostDeleteResponse of(Post post) {
return PostDeleteResponse.builder()
.id(post.getId())
.build();
}
}
12 changes: 12 additions & 0 deletions src/main/java/pilot/instagram/domain/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pilot.instagram.domain.post.dto.request.PostRequest;
import pilot.instagram.domain.post.dto.response.PostDeleteResponse;
import pilot.instagram.domain.post.dto.response.PostPagingResponse;
import pilot.instagram.domain.post.dto.response.PostResponse;
import pilot.instagram.domain.post.entity.Post;
Expand Down Expand Up @@ -38,4 +39,15 @@ public PostResponse getPost(Long postId) {
public Page<PostPagingResponse> getPosts(String userId, Pageable pageable) {
return postRepository.findPostByUserIdWithPaged(userId, pageable);
}

@Transactional
public PostDeleteResponse deletePost(Long postId, String userId) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException(ErrorCode.POST_NOT_FOUND.getMessage()));
if(!post.getUser().getId().equals(userId)) {
throw new IllegalArgumentException(ErrorCode.UNAUTHORIZED_POST_DELETE.getMessage());
}
postRepository.delete(post);
return PostDeleteResponse.of(post);
}
}
3 changes: 2 additions & 1 deletion src/main/java/pilot/instagram/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum ErrorCode {
USER_NOT_FOUND("아이디를 찾을 수 없습니다"),

// POST
POST_NOT_FOUND("게시글을 찾을 수 없습니다.");
POST_NOT_FOUND("게시글을 찾을 수 없습니다."),
UNAUTHORIZED_POST_DELETE("게시글 삭제 권한이 없습니다.");

private final String message;
}