Skip to content

Commit

Permalink
Merge pull request #28 from gooiman/refactor/memo
Browse files Browse the repository at this point in the history
[Refactor] memo, page 리팩토링
  • Loading branch information
EATSTEAK authored Sep 28, 2024
2 parents 891b52e + 1c28b9e commit 9901f75
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import java.util.UUID;

public record CommonIdResponseDto(String id) {
public record CommonIdResponseDto(UUID id) {

public CommonIdResponseDto(UUID id) {
this(id.toString());
}
}
78 changes: 20 additions & 58 deletions src/main/java/dev/gooiman/server/memo/application/MemoService.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package dev.gooiman.server.memo.application;


import dev.gooiman.server.memo.application.dto.MemoDto;
import dev.gooiman.server.common.dto.CommonSuccessDto;
import dev.gooiman.server.common.dto.CommonIdResponseDto;
import dev.gooiman.server.common.dto.CommonSuccessDto;
import dev.gooiman.server.common.exception.CommonException;
import dev.gooiman.server.common.exception.ErrorCode;
import dev.gooiman.server.memo.application.dto.CreateMemoRequestDto;
import dev.gooiman.server.memo.application.dto.GetMemoResponseDto;
import dev.gooiman.server.memo.application.dto.MemoDto;
import dev.gooiman.server.memo.application.dto.UpdateMemoRequestDto;
import dev.gooiman.server.memo.repository.MemoRepository;
import dev.gooiman.server.memo.repository.entity.Memo;
import dev.gooiman.server.page.application.PageService;
import dev.gooiman.server.page.repository.PageRepository;
import dev.gooiman.server.page.repository.entity.Page;
import dev.gooiman.server.user.application.UserService;
import dev.gooiman.server.user.repository.UserRepository;
import dev.gooiman.server.user.repository.entity.User;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
Expand All @@ -31,11 +29,14 @@ public class MemoService {
private final MemoRepository memoRepository;
private final UserService userService;
private final PageService pageService;
private final UserRepository userRepository;
private final PageRepository pageRepository;

public Memo findMemo(UUID memoId) {
return memoRepository.findById(memoId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_MEMO));
}

public MemoDto[] listMemo(UUID pageId, String category) {
if(category != null) {
if (category != null) {
return memoRepository.findMemosByPage_PageIdAndCategory(pageId, category)
.stream()
.map(MemoDto::fromEntity)
Expand All @@ -47,76 +48,37 @@ public MemoDto[] listMemo(UUID pageId, String category) {
.toArray(MemoDto[]::new);
}

// public CreateMemoResponseDto.Res create(CreateMemoResponseDto createMemoDto)
// throws BaseException {
// String pageId = createMemoDto.getPageId();
// String author = createMemoDto.getAuthor();
// String title = createMemoDto.getTitle();
// String category = createMemoDto.getCategory();
// String content = createMemoDto.getContent();
// String subCategory = createMemoDto.getSubCategory();
//
// Optional<User> user = userRepository.findByPageAndName(pageId, author);
// Optional<Page> page = pageRepository.findById(UUID.fromString(pageId));
//
// if (!page.isPresent()) {
// throw new BaseException();
// }
// if (!user.isPresent()) {
// throw new BaseException();
// }
// User userEntity = user.get();
// Page pageEntity = page.get();
// Memo memo = Memo.builder().title(title)
// .category(category)
// .subCategory(subCategory)
// .content(content)
// .user(userEntity)
// .page(pageEntity)
// .build();
// Memo savedMemo = memoRepository.save(memo);
// CreateMemoResponseDto.Res res = CreateMemoResponseDto.Res.mapEntityToDto(savedMemo);
// return res;
// }

@Transactional
public CommonSuccessDto updateMemo(String memoId, @RequestBody UpdateMemoRequestDto dto) {
UUID uuid = UUID.fromString(memoId);
Memo memo = memoRepository.findById(uuid)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_MEMO));
public CommonSuccessDto updateMemo(UUID memoId, @RequestBody UpdateMemoRequestDto dto) {
Memo memo = findMemo(memoId);
User user = userService.getUserByName(dto.author());
memo.updateInfo(dto.title(), dto.content(), dto.category(), dto.subCategory(), dto.color(), user);
memo.updateInfo(dto.title(), dto.content(), dto.category(), dto.subCategory(), dto.color(),
user);

return CommonSuccessDto.fromEntity(true);
}

@Transactional
public CommonSuccessDto deleteMemo(String memoId) {
UUID uuid = UUID.fromString(memoId);
Memo memo = memoRepository.findById(uuid)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_MATCH_AUTH_CODE));
public CommonSuccessDto deleteMemo(UUID memoId) {
Memo memo = findMemo(memoId);
memoRepository.delete(memo);

return CommonSuccessDto.fromEntity(true);
}

@Transactional
public CommonIdResponseDto createMemo(CreateMemoRequestDto dto) {
UUID id = UUID.randomUUID();
User user = userService.getUserByName(dto.author());
Page page = pageService.getPageById(dto.pageId());
Memo memo = new Memo(id, dto.category(), dto.subCategory(), dto.title(), dto.color(), dto.content(),
page, user);
memoRepository.save(memo);
Memo memo = new Memo(dto.category(), dto.subCategory(), dto.title(), dto.color(),
dto.content(), page, user);
Memo savedMemo = memoRepository.save(memo);

return new CommonIdResponseDto(id);
return new CommonIdResponseDto(savedMemo.getMemoId());
}

public GetMemoResponseDto getMemo(String memoId) {
UUID uuid = UUID.fromString(memoId);

Memo memo = memoRepository.findById(uuid)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_MEMO));
public GetMemoResponseDto getMemo(UUID memoId) {
Memo memo = findMemo(memoId);

return new GetMemoResponseDto(memo.getMemoId(), memo.getTitle(), memo.getContent(),
memo.getUsername(), memo.getCategory(), memo.getSubCategory());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package dev.gooiman.server.memo.application.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import java.util.UUID;

public record CreateMemoRequestDto(@JsonProperty(value = "page_id") UUID pageId,
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record CreateMemoRequestDto(UUID pageId,
String author,
String title,
String category,
@JsonProperty(value = "sub_category") String subCategory,
String subCategory,
String content,
String color
) {
) {


}
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package dev.gooiman.server.memo.application.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class MemoSummariesResponseDto {

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record MemoSummariesResponseDto(String name,
Map<String, Map<String, List<String>>> memoSummaries) {

@Getter
@AllArgsConstructor
@Builder
public static class Res {

private String name;
private Map<String, Map<String, List<String>>> memoSummaries;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package dev.gooiman.server.memo.application.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record UpdateMemoRequestDto(String title,
String content,
String author,
String category,
String subCategory,
String color
) {
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import dev.gooiman.server.common.dto.CommonSuccessDto;
import dev.gooiman.server.common.dto.ResponseDto;
import dev.gooiman.server.memo.application.MemoService;
import dev.gooiman.server.memo.application.dto.CreateMemoRequestDto;
import dev.gooiman.server.memo.application.dto.GetMemoResponseDto;
import dev.gooiman.server.memo.application.dto.MemoDto;
import dev.gooiman.server.memo.application.dto.UpdateMemoRequestDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.UUID;
import dev.gooiman.server.memo.application.dto.CreateMemoRequestDto;
import dev.gooiman.server.memo.application.dto.GetMemoResponseDto;
import dev.gooiman.server.memo.application.dto.UpdateMemoRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
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.PutMapping;
Expand All @@ -33,13 +33,14 @@ public class MemoController {

@GetMapping("")
@Operation(summary = "메모 목록 조회", description = "페이지에 속한 메모 목록을 조회합니다. 카테고리를 지정하면 해당 카테고리에 속한 메모만 조회합니다.")
public ResponseDto<MemoDto[]> listMemo(@RequestParam("page_id") UUID pageId, @RequestParam(required = false) String category) {
public ResponseDto<MemoDto[]> listMemo(@RequestParam("page_id") UUID pageId,
@RequestParam(required = false) String category) {
return ResponseDto.ok(memoService.listMemo(pageId, category));
}

@PatchMapping("/{memoId}")
@Operation(summary = "메모 수정", description = "메모를 수정합니다. 메모 작성자만 수정이 가능합니다.")
public ResponseDto<CommonSuccessDto> updateMemo(@PathVariable("memoId") String memoId,
public ResponseDto<CommonSuccessDto> updateMemo(@PathVariable("memoId") UUID memoId,
@RequestBody UpdateMemoRequestDto dto) {
return ResponseDto.ok(memoService.updateMemo(memoId, dto));
}
Expand All @@ -52,13 +53,13 @@ public ResponseDto<CommonIdResponseDto> createMemo(@RequestBody CreateMemoReques

@GetMapping("/{memoId}")
@Operation(summary = "메모 조회", description = "메모를 조회합니다.")
public ResponseDto<GetMemoResponseDto> getMemo(@PathVariable("memoId") String memoId) {
public ResponseDto<GetMemoResponseDto> getMemo(@PathVariable("memoId") UUID memoId) {
return ResponseDto.ok(memoService.getMemo(memoId));
}

@DeleteMapping("/{memoId}")
@Operation(summary = "메모 삭제", description = "메모를 삭제합니다. 메모 작성자만 삭제가 가능합니다.")
public ResponseDto<CommonSuccessDto> deleteMemo(@PathVariable("memoId") String memoId) {
public ResponseDto<CommonSuccessDto> deleteMemo(@PathVariable("memoId") UUID memoId) {
return ResponseDto.ok(memoService.deleteMemo(memoId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.gooiman.server.memo.repository.entity.Memo;

import dev.gooiman.server.memo.repository.view.MemoSummariesView;
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -17,6 +18,6 @@ public interface MemoRepository extends JpaRepository<Memo, UUID> {
List<Memo> findMemosByPage_PageId(UUID pageId);

@Query("select m.title, m.category, m.subCategory from Memo m where m.page.pageId = :pageId")
List<Object[]> getMemoSummaries(@Param("pageId") UUID pageId);
List<MemoSummariesView> getMemoSummaries(@Param("pageId") UUID pageId);

}
11 changes: 11 additions & 0 deletions src/main/java/dev/gooiman/server/memo/repository/entity/Memo.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public String getUsername() {
return user.getName();
}

public Memo(String category, String subCategory, String title, String color,
String content, Page page, User user) {
this.category = category;
this.subCategory = subCategory;
this.title = title;
this.color = color;
this.content = content;
this.page = page;
this.user = user;
}

public void updateInfo(String title, String content, String category, String subCategory,
String color, User user) {
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.gooiman.server.memo.repository.view;

public interface MemoSummariesView {
String getTitle();
String getCategory();
String getSubCategory();
}
42 changes: 17 additions & 25 deletions src/main/java/dev/gooiman/server/page/application/PageService.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package dev.gooiman.server.page.application;

import dev.gooiman.server.common.dto.CommonIdResponseDto;
import dev.gooiman.server.common.exception.CommonException;
import dev.gooiman.server.common.exception.ErrorCode;
import dev.gooiman.server.memo.application.dto.MemoSummariesResponseDto;
import dev.gooiman.server.memo.repository.MemoRepository;
import dev.gooiman.server.page.application.dto.CreatePageResponseDto;
import dev.gooiman.server.memo.repository.view.MemoSummariesView;
import dev.gooiman.server.page.application.dto.CreatePageRequestDto;
import dev.gooiman.server.page.repository.PageRepository;
import dev.gooiman.server.page.repository.entity.Page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -30,29 +30,21 @@ public Page getPageById(UUID id) {
}

@Transactional
public CreatePageResponseDto.Res create(CreatePageResponseDto createPageDto) {
String name = createPageDto.getName();
Page page = Page.builder().pageName(name).build();
public CommonIdResponseDto create(CreatePageRequestDto dto) {
Page page = new Page(dto.name());
Page savedPage = pageRepository.save(page);
return CreatePageResponseDto.Res.mapEntityToDto(savedPage);
return new CommonIdResponseDto(savedPage.getPageId());
}

public MemoSummariesResponseDto.Res memoSummaries(UUID pageId) {
Optional<Page> page = pageRepository.findById(pageId);
Page pageEntity = page.get();
String pageName = pageEntity.getPageName();
Map<String, Map<String, List<String>>> memoSummaries = new HashMap<>();
List<Object[]> result = memoRepository.getMemoSummaries(pageId);
for (Object[] row : result) {
String title = (String) row[0];
String category = (String) row[1];
String subCategory = (String) row[2];

memoSummaries.computeIfAbsent(category, k -> new HashMap<>())
.computeIfAbsent(subCategory, k -> new ArrayList<>())
.add(title);
}
return new MemoSummariesResponseDto.Res(pageName, memoSummaries);
public MemoSummariesResponseDto memoSummaries(UUID pageId) {
Page page = getPageById(pageId);
String name = page.getPageName();
Map<String, Map<String, List<String>>> memoSummaries = memoRepository.getMemoSummaries(
pageId)
.stream().collect(
Collectors.groupingBy(MemoSummariesView::getCategory,
Collectors.groupingBy(MemoSummariesView::getSubCategory,
Collectors.mapping(MemoSummariesView::getTitle, Collectors.toList()))));
return new MemoSummariesResponseDto(name, memoSummaries);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.gooiman.server.page.application.dto;

public record CreatePageRequestDto(String name) {

}
Loading

0 comments on commit 9901f75

Please sign in to comment.