Skip to content

Commit

Permalink
[TDC-112] fix: 일단 workspaceId 는 빼고, WIP: 단건조회
Browse files Browse the repository at this point in the history
  • Loading branch information
yerimkoko committed Jun 27, 2024
1 parent 6dbc759 commit 7e61bd6
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 36 deletions.
12 changes: 9 additions & 3 deletions threedollar-application/http/post.http
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@ Content-Type: application/json


### Post 조회 (다건 조회)
GET {{host_api}}/community/v1/post-group/NEWS_POST?cursor=8&size=3&workspaceId=threedollar-dev&targetId=3&postGroup=NEWS_POST&accountId=USER222
GET {{host_api}}/community/v1/post-group/NEWS_POST?cursor=8&size=3&targetId=3&postGroup=NEWS_POST&accountId=USER222
X-Community-Api-Key: {{api_key}}
Content-Type: application/json



### Post 단건 조회
GET {{host_api}}/community/v1/post-group/NEWS_POST/post/2?accountId=USER222&workspaceId=threedollar-dev&targetId=3
GET {{host_api}}/community/v1/post-group/NEWS_POST/post/2?targetId=3
X-Community-Api-Key: {{api_key}}
Content-Type: application/json



### Post targetId 에 해당하는 post 개수 조회
GET {{host_api}}/community/v1/post-group/NEWS_POST/count?workspaceId=threedollar-dev&targetId=3
GET {{host_api}}/community/v1/post-group/NEWS_POST/count?targetId=3
X-Community-Api-Key: {{api_key}}
Content-Type: application/json


### Post 수정
GET {{host_api}}/community/v1/post-group/NEWS_POST/post/2?targetId=3
X-Community-Api-Key: {{api_key}}

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import com.threedollar.service.post.request.GetPostRequest;
import com.threedollar.service.post.request.PostAddRequest;
import com.threedollar.service.post.request.PostAndCursorRequest;
import com.threedollar.service.post.request.PostUpdateRequest;
import com.threedollar.service.post.response.PostAndCursorResponse;
import com.threedollar.service.post.response.PostResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -37,32 +39,48 @@ public ApiResponse<Long> addPost(@RequestApiKey ApiKeyContext workspaceId,

@DeleteMapping("/v1/post-group/{postGroup}/post/{postId}")
public ApiResponse<String> deletePost(@PathVariable Long postId,
@RequestApiKey ApiKeyContext workspaceId,
@PathVariable PostGroup postGroup,
@Valid GetPostRequest request) {
postFacadeService.deletePost(request.getWorkspaceId(), request.getAccountId(), postId, postGroup, request.getTargetId());
postFacadeService.deletePost(workspaceId.getWorkspaceId(), request.getAccountId(), postId, postGroup, request.getTargetId());
return ApiResponse.OK;
}

@GetMapping("/v1/post-group/{postGroup}")
public ApiResponse<PostAndCursorResponse> getPosts(@Valid PostAndCursorRequest request,
@RequestApiKey ApiKeyContext workspaceId,
@PathVariable PostGroup postGroup) {
return ApiResponse.success(postFacadeService.getPostAndCursor(request, postGroup));
return ApiResponse.success(postFacadeService.getPostAndCursor(request, workspaceId.getWorkspaceId(), postGroup));
}

@GetMapping("/v1/post-group/{postGroup}/post/{postId}")
public ApiResponse<PostResponse> getPost(@Valid GetPostRequest postRequest,
public ApiResponse<PostResponse> getPost(@RequestParam(required = false) String accountId,
@RequestParam String targetId,
@RequestApiKey ApiKeyContext workspaceId,
@PathVariable PostGroup postGroup,
@PathVariable Long postId) {
return ApiResponse.success(postFacadeService.getPostById(postRequest.getWorkspaceId(),
postRequest.getAccountId(), postId, postGroup, postRequest.getTargetId()));
return ApiResponse.success(postFacadeService.getPostById(workspaceId.getWorkspaceId(),
accountId, postId, postGroup, targetId));

}

@GetMapping("/v1/post-group/{postGroup}/count")
public ApiResponse<Long> getPostCount(@PathVariable PostGroup postGroup,
@RequestParam String workspaceId,
@RequestParam String targetId) {
return ApiResponse.success(postFacadeService.getPostCountByTargetId(workspaceId, postGroup, targetId));
@RequestApiKey ApiKeyContext workspaceId,
@RequestParam String targetId) {
return ApiResponse.success(postFacadeService.getPostCountByTargetId(workspaceId.getWorkspaceId(), postGroup, targetId));

}

@PatchMapping("/v1/post-group/{postGroup}/post/{postId}")
public ApiResponse<String> updatePost(@Valid PostUpdateRequest request,
@RequestApiKey ApiKeyContext workspaceId,
@PathVariable PostGroup postGroup,
@PathVariable Long postId,
@RequestParam(required = false) String accountId,
@RequestParam String targetId) {
postFacadeService.updatePost(workspaceId.getWorkspaceId(), accountId, postGroup, postId, targetId, request);
return ApiResponse.OK;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ public void deletePost(@NotBlank String workspaceId,
}

public PostAndCursorResponse getPostAndCursor(@Valid PostAndCursorRequest request,
String workspaceId,
PostGroup postGroup) {
return postService.getPostsAndCursor(
postGroup,
request.getWorkspaceId(),
workspaceId,
request.getTargetId(),
request.getAccountId(),
request.getCursor(),
request.getSize());
}


public PostResponse getPostById(String workspaceId,
String accountId,
Long postId,
Expand All @@ -60,6 +62,13 @@ public Long getPostCountByTargetId(String workspaceId,
return postService.getPostCountByTargetId(workspaceId, postGroup, targetId);
}


public void updatePost(String workspaceId,
String accountId,
PostGroup postGroup,
Long postId,
String targetId,
PostUpdateRequest request) {
postService.update(workspaceId, accountId, postId, postGroup, targetId, request);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.threedollar.domain.post.PostGroup;
import com.threedollar.domain.post.repository.PostRepository;
import com.threedollar.service.post.request.PostAddRequest;
import com.threedollar.service.post.request.PostUpdateRequest;
import com.threedollar.service.post.response.PostAndCursorResponse;
import com.threedollar.service.post.response.PostResponse;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -14,6 +15,7 @@

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -72,11 +74,26 @@ public PostResponse getPostById(String workspaceId,
}

public Long getPostCountByTargetId(String workspaceId,
PostGroup postGroup,
String targetId) {
PostGroup postGroup,
String targetId) {
return postRepository.postCountByWorkspaceIdAndPostGroupAndTargetId(workspaceId, postGroup, targetId);
}


public void update(String workspaceId,
String accountId,
Long postId,
PostGroup postGroup,
String targetId,
PostUpdateRequest request) {

Post post = validatePost(workspaceId, accountId, postId, postGroup, targetId);
post.update(request.getTitle(), request.getContent(), request.getPostSections().stream()
.map(r -> r.toEntity(post))
.collect(Collectors.toList()));
}


private Post validatePost(String workspaceId,
String accountId,
Long postId,
Expand All @@ -90,7 +107,10 @@ private Post validatePost(String workspaceId,

}

private static boolean isOwner(Post post, String accountId) {
private static Boolean isOwner(Post post, String accountId) {
if (accountId == null) {
return null;
}
return Objects.equals(post.getAccountId(), accountId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ public class GetPostRequest {
@Nullable
private String accountId;

@NotBlank
private String workspaceId;

@NotBlank
private String targetId;

public GetPostRequest(String accountId, String workspaceId, String targetId) {
public GetPostRequest(String accountId, String targetId) {
this.accountId = accountId;
this.workspaceId = workspaceId;
this.targetId = targetId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class PostAddRequest {

private String title;

private String workspaceId;

private String targetId;

@NotBlank
Expand All @@ -33,11 +31,10 @@ public class PostAddRequest {
private List<PostSectionRequest> sections;

@Builder
public PostAddRequest(PostGroup postGroup, Long parentId, String title, String targetId, String content, String workspaceId, List<PostSectionRequest> sections) {
public PostAddRequest(PostGroup postGroup, Long parentId, String title, String targetId, String content, List<PostSectionRequest> sections) {
this.postGroup = postGroup;
this.parentId = parentId;
this.title = title;
this.workspaceId = workspaceId;
this.content = content;
this.targetId = targetId;
this.sections = sections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ public class PostAndCursorRequest {
@Max(30)
private int size;

@NotBlank
private String workspaceId;

@NotBlank
private String targetId;

@Nullable
private String accountId;

public PostAndCursorRequest(Long cursor, int size, String workspaceId, String targetId, String accountId) {
public PostAndCursorRequest(Long cursor, int size, String targetId, String accountId) {
this.cursor = cursor;
this.size = size;
this.workspaceId = workspaceId;
this.targetId = targetId;
this.accountId = accountId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.threedollar.service.post.request;

import com.threedollar.domain.post.Post;
import com.threedollar.domain.post.postsection.PostSection;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

@NoArgsConstructor
@Getter
public class PostUpdateRequest {

@Nullable
private String title;

@Nullable
private String content;

@NotNull
private List<PostSectionRequest> postSections;

@Builder
public PostUpdateRequest(String title, String content, List<PostSectionRequest> postSections) {
this.title = title;
this.content = content;
this.postSections = postSections;
}

public List<PostSection> toEntity(Post post) {
return postSections.stream().map(postSection -> postSection.toEntity(post))
.collect(Collectors.toList());

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class PostResponse {

private String targetId;

private boolean isOwner;
private Boolean isOwner;

private LocalDateTime createTime;

Expand All @@ -48,7 +48,7 @@ public PostResponse(Long postId, PostGroup postGroup, Long parentId, String titl
this.postSections = postSections;
}

public static PostResponse of(Post post, boolean isOwner) {
public static PostResponse of(Post post, Boolean isOwner) {
return PostResponse.builder()
.postId(post.getId())
.postGroup(post.getPostGroup())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ void clean_up() {
@Test
void 사장님이_소식을_작성한다() {
// given
PostAddRequest request = newRequest();
String workspaceId = "three-user";
PostAddRequest request = newRequest();
String accountId = "user22";

// when
Expand Down Expand Up @@ -79,7 +79,6 @@ private PostAddRequest newRequest() {

PostGroup postGroup = PostGroup.NEWS_POST;
String targetId = "33";
String workspaceId = "threedollar-dev";
String title = "은평구 핫도그 아저씨";
String content = "콘텐트";
SectionType sectionType = SectionType.IMAGE;
Expand All @@ -97,7 +96,6 @@ private PostAddRequest newRequest() {
.title(title)
.content(content)
.targetId(targetId)
.workspaceId(workspaceId)
.sections(List.of(postSectionRequest))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public void add(PostSection postSection) {
this.postSection.add(postSection);
}

public void update(String title, String content, List<PostSection> sections) {
this.postSection.clear();
this.postSection.addAll(sections);
this.title = title;
this.content = content;
}

public void delete() {
this.status = PostStatus.DELETED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public interface PostRepositoryCustom {

Post findByIdAndWorkspaceIdAndAccountIdAndGroupAndTargetId(Long postId,
String accountId,
@Nullable String accountId,
String workspaceId,
PostGroup group,
String targetId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class PostRepositoryCustomImpl implements PostRepositoryCustom {
public Post findByIdAndWorkspaceIdAndAccountIdAndGroupAndTargetId(Long postId, String accountId, String workspaceId, PostGroup postGroup, String targetId) {
return jpaQueryFactory.selectFrom(post)
.where(
existsAccountId(accountId),
post.workspaceId.eq(workspaceId),
post.accountId.eq(accountId),
post.id.eq(postId),
post.postGroup.eq(postGroup),
post.targetId.eq(targetId),
Expand Down Expand Up @@ -72,5 +72,12 @@ private BooleanExpression existsCursor(Long cursor) {
return post.id.lt(cursor);
}

private BooleanExpression existsAccountId(String accountId) {
if (accountId == null) {
return null;
}
return post.accountId.eq(accountId);
}


}

0 comments on commit 7e61bd6

Please sign in to comment.