Skip to content

Commit

Permalink
[Feature] 메모 기능 추가 (#40)
Browse files Browse the repository at this point in the history
* feature: 메모 추가하기 (#39)

* feat: 메모 수정하기 대응(#39)

* feat: 메모 삭제하기 (#39)

* refactor: postReader 메소드 명 수정 (#39)

* refactor: swagger yml 분리 (#22)
  • Loading branch information
yongbin97 authored Oct 1, 2024
1 parent 96a4c87 commit 8ebf370
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ application-jwt.yml
application-local.yml
application-oauth.yml
application-ai.yml
application-swagger.yml

### java ###
LocalSecurityConfig.java
30 changes: 30 additions & 0 deletions src/main/java/project/backend/business/memo/MemoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package project.backend.business.memo;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import project.backend.business.memo.implement.MemoManager;
import project.backend.business.memo.request.CreateUpdateMemoServiceRequest;
import project.backend.business.post.implement.PostReader;
import project.backend.entity.post.Post;

@Service
@RequiredArgsConstructor
public class MemoService {

private final PostReader postReader;
private final MemoManager memoManager;

@Transactional
public void createUpdateMemo(Long userId, CreateUpdateMemoServiceRequest memoServiceRequest) {
Post post = postReader.readActivatedPublishedPost(userId, memoServiceRequest.getPostId());
memoManager.createUpdateMemo(post, memoServiceRequest.getContent());
}

@Transactional
public void deleteMemo(Long userId, Long postId) {
Post post = postReader.readActivatedPublishedPost(userId, postId);
memoManager.deleteMemo(post);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package project.backend.business.memo.implement;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import project.backend.entity.post.Post;
import project.backend.repository.post.PostRepository;

@Slf4j
@Component
@AllArgsConstructor
public class MemoManager {

private final PostRepository postRepository;

public void createUpdateMemo(Post post, String content) {
post.setMemo(content);
post.setMemoCreatedAt(LocalDateTime.now());
postRepository.save(post);
}

public void deleteMemo(Post post) {
post.setMemo(null);
post.setMemoCreatedAt(null);
postRepository.save(post);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package project.backend.business.memo.request;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class CreateUpdateMemoServiceRequest {

private Long postId;
private String content;

}
6 changes: 3 additions & 3 deletions src/main/java/project/backend/business/post/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public CreateUpdatePostResponse createNewPostDetail(Long userId,
@Transactional
public CreateUpdatePostResponse updatePostDetail(Long userId, Long postId,
PostDetailDto postDetailDto) {
Post post = postReader.read(userId, postId);
Post post = postReader.readActivatedPost(userId, postId);
postManager.updatePost(post, postDetailDto);
Long id = post.getId();

Expand All @@ -86,14 +86,14 @@ public CreateUpdatePostResponse updatePostDetail(Long userId, Long postId,

@Transactional
public void deletePostDetail(Long userId, Long postId) {
Post post = postReader.read(userId, postId);
Post post = postReader.readActivatedPost(userId, postId);
postManager.deletePost(post);
}

@Transactional
public CreateUpdatePostResponse updateSummaryPost(Long userId, Long postId,
CreatePostServiceRequest createPostServiceRequest) {
Post post = postReader.readPostAndUser(postId);
Post post = postReader.readActivatedPostAndWriter(postId);

if (post.getUser() != null && !Objects.equals(post.getUser().getId(), userId)) {
throw new CustomException(ErrorCode.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import project.backend.common.error.CustomException;
import project.backend.common.error.ErrorCode;
import project.backend.entity.post.Post;
import project.backend.entity.post.PostStatus;
import project.backend.repository.post.PostRepository;

@Slf4j
Expand All @@ -25,13 +26,20 @@ public class PostReader {
private final PostRepository postRepository;
private final TagReader tagReader;

public Post readPostAndUser(Long postId) {
return postRepository.findPostAndUserAndActivatedTrueById(postId)
public Post readActivatedPost(Long userId, Long postId) {
return postRepository.findByIdAndUserIdAndActivatedTrue(postId, userId)
.orElseThrow(() -> new CustomException(ErrorCode.BAD_REQUEST));
}

public Post read(Long userId, Long postId) {
return postRepository.findByIdAndUserIdAndActivatedTrue(postId, userId)
public Post readActivatedPublishedPost(Long userId, Long PostId) {
return postRepository.findPostByIdAndUserIdAndStatusAndActivatedTrue(PostId, userId,
PostStatus.PUBLISHED)
.orElseThrow(() -> new CustomException(
ErrorCode.BAD_REQUEST));
}

public Post readActivatedPostAndWriter(Long postId) {
return postRepository.findPostAndUserAndActivatedTrueById(postId)
.orElseThrow(() -> new CustomException(ErrorCode.BAD_REQUEST));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package project.backend.presentation.memo.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import project.backend.business.memo.MemoService;
import project.backend.presentation.memo.request.CreateUpdateMemoRequest;
import project.backend.security.aop.AssignCurrentUserInfo;
import project.backend.security.aop.CurrentUserInfo;

@RestController
@RequiredArgsConstructor
@RequestMapping("/memo")
public class MemoController implements MemoControllerDocs {

private final MemoService memoService;

@AssignCurrentUserInfo
@PostMapping
public ResponseEntity<Void> createUpdateMemo(CurrentUserInfo userInfo, @RequestParam Long postId,
@RequestBody CreateUpdateMemoRequest memoRequest) {
memoService.createUpdateMemo(userInfo.getUserId(), memoRequest.toServiceRequest(postId));
return new ResponseEntity<>(HttpStatus.OK);
}

@AssignCurrentUserInfo
@DeleteMapping
public ResponseEntity<Void> deleteMemo(CurrentUserInfo userInfo, @RequestParam Long postId) {
memoService.deleteMemo(userInfo.getUserId(), postId);
return new ResponseEntity<>(HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package project.backend.presentation.memo.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestParam;
import project.backend.presentation.memo.request.CreateUpdateMemoRequest;
import project.backend.security.aop.CurrentUserInfo;

@Tag(name = "메모 API")
public interface MemoControllerDocs {

@Operation(summary = "메모 추가/수정 API", description = "게시글에 메모를 추가 및 수정합니다.")
@Parameter(name = "userInfo", hidden = true)
@Parameter(name = "postId", description = "메모 추가/수정할 게시글 id")
ResponseEntity<Void> createUpdateMemo(CurrentUserInfo userInfo, Long postId,
CreateUpdateMemoRequest memoRequest);

@Operation(summary = "메모 삭제 API", description = "게시글의 메모를 삭제 합니다.")
@Parameter(name = "userInfo", hidden = true)
@Parameter(name = "postId", description = "메모 삭제할 게시글 id")
ResponseEntity<Void> deleteMemo(CurrentUserInfo userInfo, @RequestParam Long postId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package project.backend.presentation.memo.request;

import lombok.Getter;
import project.backend.business.memo.request.CreateUpdateMemoServiceRequest;

@Getter
public class CreateUpdateMemoRequest {

private String content;

public CreateUpdateMemoServiceRequest toServiceRequest(Long postId) {
return CreateUpdateMemoServiceRequest.builder()
.postId(postId)
.content(content)
.build();
}
}
22 changes: 2 additions & 20 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spring:
profiles:
active:
dev
local
group:
local-env:
- local
Expand All @@ -13,22 +13,4 @@ spring:
- jwt
- oauth
- ai

springdoc:
swagger-ui:
groups-order: DESC
tags-sorter: alpha
operations-sorter: method
disable-swagger-default-url: true
display-request-duration: true
defaultModelsExpandDepth: 2
defaultModelExpandDepth: 2
api-docs:
path: /api-docs
show-actuator: true
default-consumes-media-type: application/json
default-produces-media-type: application/json
writer-with-default-pretty-printer: true
model-and-view-allowed: true
paths-to-match:
- /**
- swagger

0 comments on commit 8ebf370

Please sign in to comment.