Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null 359 recent memos #32

Merged
merged 4 commits into from
Sep 18, 2024
Merged
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
49 changes: 36 additions & 13 deletions src/main/java/com/example/oatnote/memotag/MemoTagApiDoc.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.oatnote.memotag;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -14,13 +16,15 @@
import com.example.oatnote.memotag.dto.CreateMemoRequest;
import com.example.oatnote.memotag.dto.CreateMemoResponse;
import com.example.oatnote.memotag.dto.CreateMemosRequest;
import com.example.oatnote.memotag.dto.MemosResponse;
import com.example.oatnote.memotag.dto.PagedMemosResponse;
import com.example.oatnote.memotag.dto.SearchMemoRequest;
import com.example.oatnote.memotag.dto.SearchMemoResponse;
import com.example.oatnote.memotag.dto.UpdateMemoRequest;
import com.example.oatnote.memotag.dto.UpdateMemoResponse;
import com.example.oatnote.memotag.dto.UpdateTagRequest;
import com.example.oatnote.memotag.dto.UpdateTagResponse;
import com.example.oatnote.memotag.dto.enums.SortOrderTypeEnum;
import com.example.oatnote.memotag.dto.innerDto.TagResponse;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -29,6 +33,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;

@Tag(name = "Memo Tag", description = "๋ฉ”๋ชจ ํƒœ๊ทธ ๊ด€๋ฆฌ API")
public interface MemoTagApiDoc {
Expand Down Expand Up @@ -57,7 +62,7 @@ ResponseEntity<CreateMemoResponse> createMemo(
}
)
@Operation(summary = "์ด๋ฉ”์ผ์„ ํ†ตํ•œ ๋ฉ”๋ชจ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ")
@PostMapping("/memos")
@PostMapping("/memos/email")
ResponseEntity<Void> createMemosByEmail(
@RequestBody @Valid CreateMemosRequest createMemosTagsRequest
);
Expand Down Expand Up @@ -87,14 +92,13 @@ ResponseEntity<SearchMemoResponse> getMemosByAISearch(
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true))),
}
)
@Operation(summary = "์ž์‹ ํƒœ๊ทธ๋“ค๊ณผ ๊ด€๋ จ๋œ ๋ฉ”๋ชจ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ")
@GetMapping("/tags/memos")
ResponseEntity<ChildTagsWithMemosResponse> getChildTagsWithMemos(
@RequestParam(value = "parentTagId", required = false) String parentTagId,
@RequestParam(name = "tagPage", defaultValue = "1") Integer tagPage,
@RequestParam(name = "tagLimit", defaultValue = "10") Integer tagLimit,
@Operation(summary = "ํŠน์ • ํƒœ๊ทธ์˜ ๋ฉ”๋ชจ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ")
@GetMapping("/tag/memos")
ResponseEntity<PagedMemosResponse> getMemosByTag(
@RequestParam("tagId") String tagId,
@RequestParam(name = "memoPage", defaultValue = "1") Integer memoPage,
@RequestParam(name = "memoLimit", defaultValue = "10") Integer memoLimit,
@RequestParam(name = "memoLimit", defaultValue = "5") Integer memoLimit,
@RequestParam(name = "sortOrder") SortOrderTypeEnum sortOrder,
@AuthenticationPrincipal String userId
);

Expand All @@ -107,12 +111,31 @@ ResponseEntity<ChildTagsWithMemosResponse> getChildTagsWithMemos(
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true))),
}
)
@Operation(summary = "ํŠน์ • ํƒœ๊ทธ์˜ ๋ฉ”๋ชจ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ")
@GetMapping("/tag/memos")
ResponseEntity<MemosResponse> getMemosByTag(
@RequestParam("tagId") String tagId,
@Operation(summary = "์ „์ฒด ์ž์‹ ํƒœ๊ทธ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ")
@GetMapping("/childTags")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์–˜ ๋Œ€๋ฌธ์ž ์จ๋„ ๋˜๋‚˜์š”?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋Œ€๋ฌธ์ž or ํ•˜์ดํ”ˆ์€ ์ทจํ–ฅ์ฐจ์ด์ด๊ธดํ•œ๋ฐ, ๋ฉ˜ํ† ๋‹˜์€ ๋Œ€๋ฌธ์ž ์„ ํ˜ธํ•œ๋‹คํ•˜์…”์„œ ์ €๋„ ๋Œ€๋ฌธ์ž ์จ๋ดค์Šต๋‹ˆ๋‹ค.

ResponseEntity<List<TagResponse>> getChildTags(
@RequestParam(value = "parentTagId", required = false) String parentTagId,
@AuthenticationPrincipal String userId
);

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
@ApiResponse(responseCode = "400", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "401", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "403", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true))),
}
)
@Operation(summary = "์ž์‹ ํƒœ๊ทธ ๋ฆฌ์ŠคํŠธ์™€ ๊ด€๋ จ๋œ ๋ฉ”๋ชจ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ")
@GetMapping("/childTags/memos")
ResponseEntity<ChildTagsWithMemosResponse> getChildTagsWithMemos(
@RequestParam(value = "parentTagId", required = false) String parentTagId,
@RequestParam(name = "tagPage", defaultValue = "1") Integer tagPage,
@RequestParam(name = "tagLimit", defaultValue = "10") Integer tagLimit,
@RequestParam(name = "memoPage", defaultValue = "1") Integer memoPage,
@RequestParam(name = "memoLimit", defaultValue = "10") Integer memoLimit,
@RequestParam(name = "sortOrder") SortOrderTypeEnum sortOrder,
@AuthenticationPrincipal String userId
);

Expand Down
51 changes: 37 additions & 14 deletions src/main/java/com/example/oatnote/memotag/MemoTagController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.oatnote.memotag;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -16,15 +18,18 @@
import com.example.oatnote.memotag.dto.CreateMemoRequest;
import com.example.oatnote.memotag.dto.CreateMemoResponse;
import com.example.oatnote.memotag.dto.CreateMemosRequest;
import com.example.oatnote.memotag.dto.MemosResponse;
import com.example.oatnote.memotag.dto.PagedMemosResponse;
import com.example.oatnote.memotag.dto.SearchMemoRequest;
import com.example.oatnote.memotag.dto.SearchMemoResponse;
import com.example.oatnote.memotag.dto.UpdateMemoRequest;
import com.example.oatnote.memotag.dto.UpdateMemoResponse;
import com.example.oatnote.memotag.dto.UpdateTagRequest;
import com.example.oatnote.memotag.dto.UpdateTagResponse;
import com.example.oatnote.memotag.dto.enums.SortOrderTypeEnum;
import com.example.oatnote.memotag.dto.innerDto.TagResponse;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;

@RestController
Expand All @@ -42,7 +47,7 @@ public ResponseEntity<CreateMemoResponse> createMemo(
return ResponseEntity.status(HttpStatus.CREATED).body(createMemoResponse);
}

@PostMapping("/memos")
@PostMapping("/memos/email")
public ResponseEntity<Void> createMemosByEmail(
@RequestBody @Valid CreateMemosRequest createMemosRequest
) {
Expand All @@ -59,13 +64,41 @@ public ResponseEntity<SearchMemoResponse> getMemosByAISearch(
return ResponseEntity.status(HttpStatus.OK).body(searchMemoResponse);
}

@GetMapping("/tags/memos")
@GetMapping("/tag/memos")
public ResponseEntity<PagedMemosResponse> getMemosByTag(
@RequestParam(value = "tagId", required = false) String tagId,
@RequestParam(name = "memoPage", defaultValue = "1") Integer memoPage,
@RequestParam(name = "memoLimit", defaultValue = "10") Integer memoLimit,
@RequestParam(name = "sortOrder") SortOrderTypeEnum sortOrder,
@AuthenticationPrincipal String userId
) {
PagedMemosResponse pagedMemosResponse = memoTagService.getMemos(
tagId,
memoPage,
memoLimit,
sortOrder,
userId
);
return ResponseEntity.status(HttpStatus.OK).body(pagedMemosResponse);
}

@GetMapping("/childTags")
public ResponseEntity<List<TagResponse>> getChildTags(
@RequestParam(value = "parentTagId", required = false) String parentTagId,
@AuthenticationPrincipal String userId
) {
List<TagResponse> childTags = memoTagService.getChildTags(parentTagId, userId);
return ResponseEntity.status(HttpStatus.OK).body(childTags);
}

@GetMapping("/childTags/memos")
public ResponseEntity<ChildTagsWithMemosResponse> getChildTagsWithMemos(
@RequestParam(value = "parentTagId", required = false) String parentTagId,
@RequestParam(name = "tagPage", defaultValue = "1") Integer tagPage,
@RequestParam(name = "tagLimit", defaultValue = "10") Integer tagLimit,
@RequestParam(name = "memoPage", defaultValue = "1") Integer memoPage,
@RequestParam(name = "memoLimit", defaultValue = "10") Integer memoLimit,
@RequestParam(name = "sortOrder") SortOrderTypeEnum sortOrder,
@AuthenticationPrincipal String userId
) {
ChildTagsWithMemosResponse childTagsWithMemosResponse = memoTagService.getChildTagsWithMemos(
Expand All @@ -74,22 +107,12 @@ public ResponseEntity<ChildTagsWithMemosResponse> getChildTagsWithMemos(
tagLimit,
memoPage,
memoLimit,
sortOrder,
userId
);
return ResponseEntity.status(HttpStatus.OK).body(childTagsWithMemosResponse);
}

@GetMapping("/tag/memos")
public ResponseEntity<MemosResponse> getMemosByTag(
@RequestParam("tagId") String tagId,
@RequestParam(name = "memoPage", defaultValue = "1") Integer memoPage,
@RequestParam(name = "memoLimit", defaultValue = "10") Integer memoLimit,
@AuthenticationPrincipal String userId
) {
MemosResponse memosResponse = memoTagService.getMemos(tagId, memoPage, memoLimit, userId);
return ResponseEntity.status(HttpStatus.OK).body(memosResponse);
}

@PutMapping("/memo/{memoId}")
public ResponseEntity<UpdateMemoResponse> updateMemo(
@PathVariable("memoId") String memoId,
Expand Down
109 changes: 71 additions & 38 deletions src/main/java/com/example/oatnote/memotag/MemoTagService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
import com.example.oatnote.memotag.dto.CreateMemoRequest;
import com.example.oatnote.memotag.dto.CreateMemoResponse;
import com.example.oatnote.memotag.dto.CreateMemosRequest;
import com.example.oatnote.memotag.dto.MemosResponse;
import com.example.oatnote.memotag.dto.PagedMemosResponse;
import com.example.oatnote.memotag.dto.PagedTagsResponse;
import com.example.oatnote.memotag.dto.SearchMemoRequest;
import com.example.oatnote.memotag.dto.SearchMemoResponse;
import com.example.oatnote.memotag.dto.UpdateMemoRequest;
import com.example.oatnote.memotag.dto.UpdateMemoResponse;
import com.example.oatnote.memotag.dto.UpdateTagRequest;
import com.example.oatnote.memotag.dto.UpdateTagResponse;
import com.example.oatnote.memotag.dto.enums.SortOrderTypeEnum;
import com.example.oatnote.memotag.dto.innerDto.MemoResponse;
import com.example.oatnote.memotag.dto.innerDto.TagResponse;
import com.example.oatnote.memotag.service.client.AIMemoTagClient;
import com.example.oatnote.memotag.service.client.dto.AICreateEmbeddingResponse;
import com.example.oatnote.memotag.service.client.dto.AICreateMemoResponse;
Expand Down Expand Up @@ -81,52 +84,73 @@ public void createMemosTags(CreateMemosRequest createMemosRequest) {
tagService.createTagEdge(tagEdge);
}

public PagedMemosResponse getMemos(
String tagId,
Integer memoPage,
Integer memoLimit,
SortOrderTypeEnum sortOrder,
String userId
) {
if (tagId == null) {
tagId = userId;
}
Integer total = memoTagRelationService.countMemos(tagId);
Criteria criteria = Criteria.of(memoPage, memoLimit, total);
PageRequest pageRequest = createPageRequest(criteria.getPage(), criteria.getLimit(), sortOrder);

Page<Memo> result = memoService.getPagedMemos(memoTagRelationService.getMemoIds(tagId), pageRequest, userId);
Page<MemoResponse> memoTagsPage = result.map(
memo -> MemoResponse.from(
memo,
tagService.getTags(memoTagRelationService.getLinkedTagIds(memo.getId()), userId)
)
);
return PagedMemosResponse.from(memoTagsPage, criteria);
}

public List<TagResponse> getChildTags(String parentTagId, String userId) {
if (parentTagId == null) {
parentTagId = userId;
}
Tag parentTag = tagService.getTag(parentTagId, userId);
List<Tag> childTags = tagService.getChildTags(parentTag.getId(), userId);
return childTags.stream()
.map(TagResponse::from)
.toList();
}

public ChildTagsWithMemosResponse getChildTagsWithMemos(
String parentTagId,
Integer tagPage,
Integer tagLimit,
Integer memoPage,
Integer memoLimit,
SortOrderTypeEnum sortOrder,
String userId
) {
if (parentTagId == null) {
parentTagId = userId;
}
Tag parentTag = tagService.getTag(parentTagId, userId);
List<String> childTagsIds = tagService.getChildTagsIds(parentTag.getId());
List<Tag> childTags = tagService.getTags(childTagsIds, userId);

Integer total = childTags.size();
Integer total = childTagsIds.size();
Criteria criteria = Criteria.of(tagPage, tagLimit, total);
PageRequest pageRequest = PageRequest.of(
criteria.getPage(),
criteria.getLimit(),
Sort.by(Sort.Direction.DESC, "uTime")
);
Page<Tag> result = tagService.getPagedTags(childTagsIds, pageRequest, userId);
Page<MemosResponse> pagedMemosTags = result.map(
tag -> getMemos(tag.getId(), memoPage, memoLimit, userId)
);
return ChildTagsWithMemosResponse.from(childTags, pagedMemosTags, criteria);
}
PageRequest pageRequest = createPageRequest(criteria.getPage(), criteria.getLimit(), SortOrderTypeEnum.NAME);

public MemosResponse getMemos(String tagId, Integer memoPage, Integer memoLimit, String userId) {
Tag tag = tagService.getTag(tagId, userId);
Integer total = memoTagRelationService.countMemos(tagId);
Criteria criteria = Criteria.of(memoPage, memoLimit, total);
PageRequest pageRequest = PageRequest.of(
criteria.getPage(),
criteria.getLimit(),
Sort.by(Sort.Direction.DESC, "uTime")
);
Page<Memo> result = memoService.getPagedMemos(memoTagRelationService.getMemoIds(tagId), pageRequest, userId);
Page<MemoResponse> memoTagsPage = result.map(
memo -> MemoResponse.from(
memo,
tagService.getTags(memoTagRelationService.getLinkedTagIds(memo.getId()), userId)
Page<Tag> result = tagService.getPagedTags(childTagsIds, pageRequest, userId);
Page<PagedTagsResponse> pagedTags = result.map(
tag -> new PagedTagsResponse(
TagResponse.from(tag),
tagService.getChildTags(tag.getId(), userId).stream()
.map(TagResponse::from)
.toList()
)
);
return MemosResponse.from(tag, memoTagsPage, criteria);
Page<PagedMemosResponse> pagedMemos = pagedTags.map(
pagedTag -> getMemos(pagedTag.tag().id(), memoPage, memoLimit, sortOrder, userId)
);
return ChildTagsWithMemosResponse.from(pagedTags, criteria, pagedMemos);
}

public SearchMemoResponse searchMemosTags(SearchMemoRequest searchMemoRequest, String userId) {
Expand Down Expand Up @@ -179,7 +203,7 @@ public void deleteTag(String tagId, String userId) {
tagService.deleteTag(tag);
}

private Memo createMemoTags(ProcessedMemoResponse aiMemoTagsResponse, String userId) {
Memo createMemoTags(ProcessedMemoResponse aiMemoTagsResponse, String userId) {
Memo memo = new Memo(
aiMemoTagsResponse.content(),
new ArrayList<>(),
Expand All @@ -199,33 +223,42 @@ private Memo createMemoTags(ProcessedMemoResponse aiMemoTagsResponse, String use
return createdMemo;
}

private List<Tag> updateMemosTagsRelations(
List<Tag> updateMemosTagsRelations(
ProcessedMemoResponse aiMemoTagsResponse,
Memo savedMemo,
String userId
) {
List<Tag> tags = new ArrayList<>();
for (var linkedTagId : aiMemoTagsResponse.parentTagIds()) {
tags.add(tagService.getTag(linkedTagId, userId));
memoTagRelationService.createRelation(savedMemo.getId(), linkedTagId, IS_LINKED_MEMO_TAG);
List<String> parentTagIds = tagService.getParentTagsIds(linkedTagId);
createParentTagsRelations(savedMemo.getId(), parentTagIds);
}
for (var addRelation : aiMemoTagsResponse.tagsRelations().added()) {
tagService.createRelation(addRelation.parentId(), addRelation.childId());
}
for (var deletedRelation : aiMemoTagsResponse.tagsRelations().deleted()) {
tagService.deleteRelation(deletedRelation.parentId(), deletedRelation.childId());
}
for (var linkedTagId : aiMemoTagsResponse.parentTagIds()) {
tags.add(tagService.getTag(linkedTagId, userId));
memoTagRelationService.createRelation(savedMemo.getId(), linkedTagId, IS_LINKED_MEMO_TAG);
List<String> parentTagIds = tagService.getParentTagsIds(linkedTagId);
createParentTagsRelations(savedMemo.getId(), parentTagIds);
}
return tags;
}

private void createParentTagsRelations(String memoId, List<String> parentTagIds) {
void createParentTagsRelations(String memoId, List<String> parentTagIds) {
if (parentTagIds != null && !parentTagIds.isEmpty()) {
for (var tagId : parentTagIds) {
memoTagRelationService.createRelation(memoId, tagId, !IS_LINKED_MEMO_TAG);
createParentTagsRelations(memoId, tagService.getParentTagsIds(tagId));
}
}
}

PageRequest createPageRequest(Integer page, Integer limit, SortOrderTypeEnum sortOrder) {
Sort sort = switch (sortOrder) {
case NAME -> Sort.by(Sort.Direction.ASC, "name");
case OLDEST -> Sort.by(Sort.Direction.ASC, "uTime");
default -> Sort.by(Sort.Direction.DESC, "uTime");
};
return PageRequest.of(page, limit, sort);
}
}
Loading
Loading