Skip to content

Commit

Permalink
[TDC-100] Post 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
yerimkoko committed Jun 20, 2024
1 parent 6a23554 commit 6a900ba
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,38 @@
import com.threedollar.common.dto.response.ApiResponse;
import com.threedollar.config.interceptor.ApiKeyContext;
import com.threedollar.config.resolver.RequestApiKey;
import com.threedollar.service.post.PostService;
import com.threedollar.service.post.PostFacadeService;
import com.threedollar.service.post.request.PostAddRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class PostController {

private final PostService postService;
private final PostFacadeService postFacadeService;

@PostMapping("/v1/post")
public ApiResponse<String> addPost(@RequestApiKey ApiKeyContext workspaceId,
@RequestParam String accountId,
@Valid @RequestBody PostAddRequest request) {

postService.addPost(request, workspaceId.getWorkspaceId());
postFacadeService.addPost(request, workspaceId.getWorkspaceId(), accountId);
return ApiResponse.OK;

}

@DeleteMapping("/v1/post")
public ApiResponse<String> deletePost(Long postId,
String accountId,
String workspaceId) {

postFacadeService.deletePost(workspaceId, accountId, postId);
return ApiResponse.OK;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.threedollar.service.post;

import com.threedollar.service.post.request.PostAddRequest;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class PostFacadeService {

private final PostService postService;

public void addPost(PostAddRequest request,
@NotBlank String workspaceId,
String accountId) {
postService.addPost(request, workspaceId, accountId);
}


public void deletePost(@NotBlank String workspaceId,
@NotBlank String accountId,
@NotNull Long postId) {

postService.deletePost(workspaceId, accountId, postId);

}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.threedollar.service.post;

import com.threedollar.common.exception.NotFoundException;
import com.threedollar.domain.post.Post;
import com.threedollar.domain.post.repository.PostRepository;
import com.threedollar.service.post.request.PostAddRequest;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -13,13 +15,28 @@ public class PostService {

private final PostRepository postRepository;


@Transactional
public void addPost(PostAddRequest request,
@NotBlank String workspaceId) {
@NotBlank String workspaceId,
String accountId) {

postRepository.save(request.toEntity(workspaceId));
postRepository.save(request.toEntity(workspaceId, accountId));

}

@Transactional
public void deletePost(String workspaceId,
String accountId,
Long postId) {

Post post = postRepository.findByIdAndWorkspaceIdAndAccountId(postId, accountId, workspaceId);
if (post == null) {
throw new NotFoundException(String.format("(%s)에 해당하는 postId 는 존재하지 않거나 잘못된 접근입니다", postId));
}

post.delete();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ public class PostAddRequest {
@NotBlank
private String content;

@NotBlank
private String accountId;

@NotNull
private List<PostSectionRequest> postSectionRequests;

Expand All @@ -37,11 +34,10 @@ public PostAddRequest(PostGroup postGroup, Long parentId, String title, String c
this.parentId = parentId;
this.title = title;
this.content = content;
this.accountId = accountId;
this.postSectionRequests = postSectionRequests;
}

public Post toEntity(String workspaceId) {
public Post toEntity(String workspaceId, String accountId) {

Post post = Post.of(postGroup, parentId, workspaceId, title, content, accountId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.threedollar.IntegrationTest;
import com.threedollar.domain.post.Post;
import com.threedollar.domain.post.PostGroup;
import com.threedollar.domain.post.PostStatus;
import com.threedollar.domain.post.PostType;
import com.threedollar.domain.post.repository.PostRepository;
import com.threedollar.service.post.request.PostAddRequest;
Expand All @@ -29,16 +30,33 @@ public class PostServiceTest extends IntegrationTest {

// given
PostAddRequest request = newRequest();
String workspaceId = "user111";
String workspaceId = "three-user";
String accountId = "user22";

// when
postService.addPost(request, workspaceId);
postService.addPost(request, workspaceId, accountId);

// then
Post post = postRepository.findAll().get(0);
assertThat(postRepository.findAll()).hasSize(1);
assertThat(workspaceId).isEqualTo(post.getWorkspaceId());
assertPost(post, request.getPostGroup(), request.getTitle(), request.getContent(), request.getAccountId());
assertPost(post, request.getPostGroup(), request.getTitle(), request.getContent(), accountId);
}

@Test
void 사장님이_소식을_지운다() {
// given
String workspaceId = "three-dollar-dev";
String accountId = "user222";
Post post = postRepository.save(newRequest().toEntity(workspaceId, accountId));

// when
postService.deletePost(workspaceId, accountId, post.getId());

// then
List<Post> posts = postRepository.findAll();
assertThat(posts.get(0).getStatus()).isEqualTo(PostStatus.DELETED);

}

private void assertPost(Post post, PostGroup postGroup, String title, String content, String accountId) {
Expand All @@ -50,13 +68,11 @@ private void assertPost(Post post, PostGroup postGroup, String title, String con

}


private PostAddRequest newRequest() {

PostGroup postGroup = PostGroup.BOSS_NEWS;
String title = "은평구 핫도그 아저씨";
String content = "콘텐트";
String accountId = "user222";
PostType postType = PostType.IMAGE;
int height = 400;
int width = 200;
Expand All @@ -73,8 +89,8 @@ private PostAddRequest newRequest() {
.postGroup(postGroup)
.title(title)
.content(content)
.accountId(accountId)
.postSectionRequests(List.of(postSectionRequest))
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public void add(PostSection postSection) {
this.postSection.add(postSection);
}

public void delete() {
this.status = PostStatus.DELETED;
}

public static Post of(PostGroup postGroup, Long parentId, String workspaceId,
String title, String content, String accountId) {
return Post.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package com.threedollar.domain.post.repository;

import com.threedollar.domain.post.Post;

public interface PostRepositoryCustom {

Post findByIdAndWorkspaceIdAndAccountId(Long postId,
String accountId,
String workspaceId);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
package com.threedollar.domain.post.repository;

public class PostRepositoryCustomImpl implements PostRepositoryCustom{
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.threedollar.domain.post.Post;
import com.threedollar.domain.post.PostStatus;
import lombok.RequiredArgsConstructor;

import static com.threedollar.domain.post.QPost.post;

@RequiredArgsConstructor
public class PostRepositoryCustomImpl implements PostRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

@Override
public Post findByIdAndWorkspaceIdAndAccountId(Long postId, String accountId, String workspaceId) {
return jpaQueryFactory.selectFrom(post)
.where(
post.workspaceId.eq(workspaceId),
post.accountId.eq(accountId),
post.id.eq(postId),
post.status.eq(PostStatus.ACTIVE)
)
.fetchOne();
}
}

0 comments on commit 6a900ba

Please sign in to comment.