From 2b4fe394deddf068f979070c93c00657169acedf Mon Sep 17 00:00:00 2001 From: YongHo Shin Date: Fri, 6 Oct 2023 00:43:20 +0900 Subject: [PATCH] [api-server-v1] wip --- ...BlockRequestDTO.java => LinkBlockDTO.java} | 4 +- .../v1/block/service/ImageBlockService.java | 2 +- .../v1/block/service/LinkBlockService.java | 42 +++++++-------- .../v1/block/service/MapBlockService.java | 52 ++++++++++--------- .../block/service/TemplateBlockService.java | 28 +++++----- .../page/controller/SharePageController.java | 11 ++-- .../service/SharePagePrivilegeService.java | 14 +++-- .../v1/space/controller/SpaceController.java | 13 +++-- .../kr/joberchip/core/block/LinkBlock.java | 16 +----- 9 files changed, 85 insertions(+), 97 deletions(-) rename api/server-v1/src/main/java/kr/joberchip/server/v1/block/controller/dto/{LinkBlockRequestDTO.java => LinkBlockDTO.java} (74%) diff --git a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/controller/dto/LinkBlockRequestDTO.java b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/controller/dto/LinkBlockDTO.java similarity index 74% rename from api/server-v1/src/main/java/kr/joberchip/server/v1/block/controller/dto/LinkBlockRequestDTO.java rename to api/server-v1/src/main/java/kr/joberchip/server/v1/block/controller/dto/LinkBlockDTO.java index 1ba9e8d8..333bce38 100755 --- a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/controller/dto/LinkBlockRequestDTO.java +++ b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/controller/dto/LinkBlockDTO.java @@ -3,7 +3,7 @@ import java.util.UUID; import kr.joberchip.core.block.LinkBlock; -public record LinkBlockRequestDTO( +public record LinkBlockDTO( UUID pageId, String title, String description, @@ -15,6 +15,6 @@ public record LinkBlockRequestDTO( Boolean visible) { public LinkBlock toEntity() { - return LinkBlock.of(title, description, link, x, y, w, h, visible); + return LinkBlock.of(title, description, link, x, y, w, h); } } diff --git a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/ImageBlockService.java b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/ImageBlockService.java index 7813e294..c7d56b36 100755 --- a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/ImageBlockService.java +++ b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/ImageBlockService.java @@ -44,7 +44,7 @@ public BlockResponseDTO createImageBlock(UUID pageId, ImageBlockDTO imageBlockDT return BlockResponseDTO.fromEntity(imageBlock); } - public BlockResponseDTO modifyImageBlock(UUID pageId, UUID blockId, ImageBlockDTO imageBlockDTO) { + public BlockResponseDTO modifyImageBlock(UUID blockId, ImageBlockDTO imageBlockDTO) { ImageBlock imageBlock = imageBlockRepository.findByObjectId(blockId).orElseThrow(EntityNotFoundException::new); diff --git a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/LinkBlockService.java b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/LinkBlockService.java index aab4d3de..e8737fe3 100755 --- a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/LinkBlockService.java +++ b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/LinkBlockService.java @@ -7,7 +7,7 @@ import kr.joberchip.server.v1._errors.ErrorMessage; import kr.joberchip.server.v1._errors.exceptions.ApiClientException; import kr.joberchip.server.v1.block.controller.dto.BlockResponseDTO; -import kr.joberchip.server.v1.block.controller.dto.LinkBlockRequestDTO; +import kr.joberchip.server.v1.block.controller.dto.LinkBlockDTO; import kr.joberchip.server.v1.block.repository.LinkBlockRepository; import kr.joberchip.server.v1.page.repository.SharePageRepository; import lombok.RequiredArgsConstructor; @@ -23,7 +23,7 @@ public class LinkBlockService { private final SharePageRepository sharePageRepository; @Transactional - public BlockResponseDTO createLinkBlock(UUID pageId, LinkBlockRequestDTO createLinkBlock) { + public BlockResponseDTO createLinkBlock(UUID pageId, LinkBlockDTO createLinkBlock) { LinkBlock newLinkBlock = createLinkBlock.toEntity(); linkBlockRepository.save(newLinkBlock); @@ -36,37 +36,37 @@ public BlockResponseDTO createLinkBlock(UUID pageId, LinkBlockRequestDTO createL } @Transactional - public BlockResponseDTO modifyLinkBlock(UUID pageId, UUID blockId, LinkBlockRequestDTO modifyRequestDTO) { + public BlockResponseDTO modifyLinkBlock(UUID blockId, LinkBlockDTO modifyRequestDTO) { - LinkBlock target = - linkBlockRepository.findById(blockId).orElseThrow(EntityNotFoundException::new); - - if (modifyRequestDTO.title() != null) target.modifyTitle(modifyRequestDTO.title()); + LinkBlock linkBlock = + linkBlockRepository + .findById(blockId) + .orElseThrow(() -> new ApiClientException(ErrorMessage.BLOCK_ENTITY_NOT_FOUND)); + if (modifyRequestDTO.title() != null) linkBlock.setTitle(modifyRequestDTO.title()); if (modifyRequestDTO.description() != null) - target.modifyDescription(modifyRequestDTO.description()); - - if (modifyRequestDTO.link() != null) target.modifyLink(modifyRequestDTO.link()); + linkBlock.setDescription(modifyRequestDTO.description()); + if (modifyRequestDTO.link() != null) linkBlock.setLink(modifyRequestDTO.link()); + if (modifyRequestDTO.visible() != null) linkBlock.setVisible(modifyRequestDTO.visible()); - if (modifyRequestDTO.visible() != null) target.setVisible(modifyRequestDTO.visible()); + linkBlockRepository.save(linkBlock); - linkBlockRepository.save(target); - - return BlockResponseDTO.fromEntity(target); + return BlockResponseDTO.fromEntity(linkBlock); } + @Transactional public void deleteLinkBlock(UUID pageId, UUID blockId) { + SharePage parentPage = + sharePageRepository + .findSharePageByObjectId(pageId) + .orElseThrow(() -> new ApiClientException(ErrorMessage.SHARE_PAGE_ENTITY_NOT_FOUND)); + LinkBlock block = linkBlockRepository .findById(blockId) - .orElseThrow(() -> new ApiClientException(ErrorMessage.ENTITY_NOT_FOUND)); - - SharePage parent = - sharePageRepository - .findSharePageByObjectId(pageId) - .orElseThrow(EntityNotFoundException::new); + .orElseThrow(() -> new ApiClientException(ErrorMessage.BLOCK_ENTITY_NOT_FOUND)); - parent.getLinkBlocks().remove(block); + parentPage.getLinkBlocks().remove(block); linkBlockRepository.delete(block); } diff --git a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/MapBlockService.java b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/MapBlockService.java index 6ee969a6..4c51cca8 100644 --- a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/MapBlockService.java +++ b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/MapBlockService.java @@ -1,10 +1,10 @@ package kr.joberchip.server.v1.block.service; import java.util.UUID; -import javax.persistence.EntityNotFoundException; import kr.joberchip.core.block.MapBlock; import kr.joberchip.core.page.SharePage; import kr.joberchip.server.v1._errors.ErrorMessage; +import kr.joberchip.server.v1._errors.exceptions.ApiClientException; import kr.joberchip.server.v1.block.controller.dto.BlockResponseDTO; import kr.joberchip.server.v1.block.controller.dto.MapBlockDTO; import kr.joberchip.server.v1.block.repository.MapBlockRepository; @@ -23,9 +23,12 @@ public class MapBlockService { private final SharePageRepository sharePageRepository; @Transactional - public BlockResponseDTO createMapBlock(UUID pageId, MapBlockDTO.Create crateMapBlockDTO) { + public BlockResponseDTO createMapBlock(UUID pageId, MapBlockDTO crateMapBlockDTO) { + SharePage parentPage = - getParentPage(pageId); + sharePageRepository + .findSharePageByObjectId(pageId) + .orElseThrow(() -> new ApiClientException(ErrorMessage.SHARE_PAGE_ENTITY_NOT_FOUND)); MapBlock newMapBlock = crateMapBlockDTO.toEntity(); @@ -37,30 +40,17 @@ public BlockResponseDTO createMapBlock(UUID pageId, MapBlockDTO.Create crateMapB return BlockResponseDTO.fromEntity(newMapBlock); } - private SharePage getParentPage(UUID pageId) { - return sharePageRepository - .findSharePageByObjectId(pageId) - .orElseThrow(EntityNotFoundException::new); - } - @Transactional - public BlockResponseDTO modifyMapBlock( - UUID pageId, UUID blockId, MapBlockDTO.Modify modifyMapBlockDTO) { + public BlockResponseDTO modifyMapBlock(UUID blockId, MapBlockDTO modifyMapBlockDTO) { MapBlock mapBlock = mapBlockRepository .findById(blockId) - .orElseThrow( - () -> { - log.error("존재하지 않는 blockId - blockId: {}", blockId); - return new EntityNotFoundException(ErrorMessage.ENTITY_NOT_FOUND); - }); - - if (modifyMapBlockDTO.getAddress() != null) mapBlock.setAddress(modifyMapBlockDTO.getAddress()); - if (modifyMapBlockDTO.getLatitude() != null) - mapBlock.setLatitude(modifyMapBlockDTO.getLatitude()); - if (modifyMapBlockDTO.getLongitude() != null) - mapBlock.setLongitude(modifyMapBlockDTO.getLongitude()); - if (modifyMapBlockDTO.getVisible() != null) mapBlock.setVisible(modifyMapBlockDTO.getVisible()); + .orElseThrow(() -> new ApiClientException(ErrorMessage.BLOCK_ENTITY_NOT_FOUND)); + + if (modifyMapBlockDTO.address() != null) mapBlock.setAddress(modifyMapBlockDTO.address()); + if (modifyMapBlockDTO.latitude() != null) mapBlock.setLatitude(modifyMapBlockDTO.latitude()); + if (modifyMapBlockDTO.longitude() != null) mapBlock.setLongitude(modifyMapBlockDTO.longitude()); + if (modifyMapBlockDTO.visible() != null) mapBlock.setVisible(modifyMapBlockDTO.visible()); mapBlockRepository.save(mapBlock); @@ -68,7 +58,19 @@ public BlockResponseDTO modifyMapBlock( } @Transactional - public void deleteMapBlock(UUID blockId) { - mapBlockRepository.deleteById(blockId); + public void deleteMapBlock(UUID pageId, UUID blockId) { + SharePage parentPage = + sharePageRepository + .findById(pageId) + .orElseThrow(() -> new ApiClientException(ErrorMessage.SHARE_PAGE_ENTITY_NOT_FOUND)); + + MapBlock mapBlock = + mapBlockRepository + .findById(blockId) + .orElseThrow(() -> new ApiClientException(ErrorMessage.BLOCK_ENTITY_NOT_FOUND)); + + parentPage.getMapBlocks().remove(mapBlock); + + mapBlockRepository.delete(mapBlock); } } diff --git a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/TemplateBlockService.java b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/TemplateBlockService.java index ae960a45..706575ad 100755 --- a/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/TemplateBlockService.java +++ b/api/server-v1/src/main/java/kr/joberchip/server/v1/block/service/TemplateBlockService.java @@ -3,39 +3,31 @@ import java.util.UUID; import kr.joberchip.core.block.TemplateBlock; import kr.joberchip.core.page.SharePage; -import kr.joberchip.core.page.types.PrivilegeType; import kr.joberchip.server.v1._errors.ErrorMessage; import kr.joberchip.server.v1._errors.exceptions.ApiClientException; import kr.joberchip.server.v1.block.controller.dto.BlockResponseDTO; import kr.joberchip.server.v1.block.controller.dto.TemplateBlockDTO; import kr.joberchip.server.v1.block.repository.TemplateBlockRepository; -import kr.joberchip.server.v1.page.repository.SharePagePrivilegeRepository; import kr.joberchip.server.v1.page.repository.SharePageRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Slf4j @Service @RequiredArgsConstructor public class TemplateBlockService { - private final SharePagePrivilegeRepository sharePagePrivilegeRepository; private final SharePageRepository sharePageRepository; private final TemplateBlockRepository templateBlockRepository; - public BlockResponseDTO createTemplateBlock( - Long userId, UUID pageId, TemplateBlockDTO templateBlockDTO) { - - PrivilegeType privilegeType = - sharePagePrivilegeRepository - .findByUserIdAndSharePageId(userId, pageId) - .orElseThrow(() -> new ApiClientException(ErrorMessage.FORBIDDEN)) - .getPrivilegeType(); + @Transactional + public BlockResponseDTO createTemplateBlock(UUID pageId, TemplateBlockDTO templateBlockDTO) { SharePage sharePage = sharePageRepository .findById(pageId) - .orElseThrow(() -> new ApiClientException(ErrorMessage.ENTITY_NOT_FOUND)); + .orElseThrow(() -> new ApiClientException(ErrorMessage.SHARE_PAGE_ENTITY_NOT_FOUND)); TemplateBlock newTemplateBlock = templateBlockDTO.toEntity(); templateBlockRepository.save(newTemplateBlock); @@ -44,19 +36,25 @@ public BlockResponseDTO createTemplateBlock( return BlockResponseDTO.fromEntity(newTemplateBlock); } - public BlockResponseDTO modifyTemplateBlock( - Long userId, UUID pageId, UUID blockId, TemplateBlockDTO templateBlockDTO) { + @Transactional + public BlockResponseDTO modifyTemplateBlock(UUID blockId, TemplateBlockDTO templateBlockDTO) { TemplateBlock templateBlock = templateBlockRepository .findById(blockId) .orElseThrow(() -> new ApiClientException(ErrorMessage.BLOCK_ENTITY_NOT_FOUND)); + if (templateBlockDTO.title() != null) templateBlock.setTitle(templateBlockDTO.title()); + if (templateBlockDTO.description() != null) + templateBlock.setDescription(templateBlockDTO.description()); + if (templateBlockDTO.visible() != null) templateBlock.setVisible(templateBlockDTO.visible()); + templateBlockRepository.save(templateBlock); - return null; + return BlockResponseDTO.fromEntity(templateBlock); } + @Transactional public void deleteTemplateBlock(UUID pageId, UUID blockId) { SharePage sharePage = sharePageRepository diff --git a/api/server-v1/src/main/java/kr/joberchip/server/v1/page/controller/SharePageController.java b/api/server-v1/src/main/java/kr/joberchip/server/v1/page/controller/SharePageController.java index 4a901572..fcb155ee 100755 --- a/api/server-v1/src/main/java/kr/joberchip/server/v1/page/controller/SharePageController.java +++ b/api/server-v1/src/main/java/kr/joberchip/server/v1/page/controller/SharePageController.java @@ -73,6 +73,8 @@ public ApiResponse.Result moveBlocks( public ApiResponse.Result pageBreadCrumbBar( @PathVariable UUID pageId) { + log.info("[SharePageController][GET] BreadCrumbBar for Page Id : {}", pageId); + SharePageTreeResponseDTO.PageTreeNode response = sharePageService.getPageBreadCrumbBar(pageId); return ApiResponse.success(response); @@ -86,13 +88,13 @@ public ApiResponse.Result pageBreadCrumbB * @return 생성된 페이지 정보 */ @PostMapping("/new") - public ApiResponse.Result createSharePage( + public ApiResponse.Result createSharePage( @AuthenticationPrincipal CustomUserDetails loginUser, @RequestBody @Valid SharePageRequestDTO createSharePageRequestDTO, Errors errors) { - log.info("[SharePageController] CustomUserDetails : {}", loginUser); - log.info("[SharePageController] SharePageRequestDTO : {}", createSharePageRequestDTO); + log.info("[SharePageController][POST] Current Username : {}", loginUser.user().getUsername()); + log.info("[SharePageController][POST] {}", createSharePageRequestDTO); // 하위 페이지를 생성하기 위해서는 선택된 페이지에 대한 수정 권한이 있어야 함. sharePagePrivilegeService.checkEditPrivilege( @@ -139,8 +141,9 @@ public ApiResponse.Result getPrivilege( } @PutMapping("/{pageId}/privilege") - public ApiResponse.Result modifyPrivilege( + public ApiResponse.Result modifyPrivilege( @AuthenticationPrincipal CustomUserDetails loginUser, + @PathVariable UUID pageId, @RequestBody SharePagePrivilegeDTO sharePagePrivilegeDTO, Errors errors) { diff --git a/api/server-v1/src/main/java/kr/joberchip/server/v1/page/service/SharePagePrivilegeService.java b/api/server-v1/src/main/java/kr/joberchip/server/v1/page/service/SharePagePrivilegeService.java index debe8030..90ad9061 100644 --- a/api/server-v1/src/main/java/kr/joberchip/server/v1/page/service/SharePagePrivilegeService.java +++ b/api/server-v1/src/main/java/kr/joberchip/server/v1/page/service/SharePagePrivilegeService.java @@ -106,10 +106,9 @@ public void checkReadPrivilege(Long userId, UUID pageId) { .orElseThrow(() -> new ApiClientException(ErrorMessage.NO_PRIVILEGE)); log.info( - "Privilege Checked : userId - {}, pageId - {}, privilege type - {}", + "[SharePagePrivilegeService][checkReadPrivilege] Privilege Checked : userId - {}, pageId - {}", userId, - pageId, - PrivilegeType.READ); + pageId); } @Transactional(readOnly = true) @@ -120,10 +119,9 @@ public void checkEditPrivilege(Long userId, UUID pageId) { .orElseThrow(() -> new ApiClientException(ErrorMessage.NO_PRIVILEGE)); log.info( - "Privilege Checked : userId - {}, pageId - {}, privilege type - {}", + "[SharePagePrivilegeService][checkEditPrivilege] Privilege Checked : userId - {}, pageId - {}", userId, - pageId, - PrivilegeType.EDIT); + pageId); } public void registerGivenPrivilegeForAllSpaceSubPage( @@ -131,10 +129,10 @@ public void registerGivenPrivilegeForAllSpaceSubPage( Long targetUserId = spaceInviteRequestDTO.userId(); - if (userRepository.existsById(targetUserId)) + if (!userRepository.existsById(targetUserId)) throw new ApiClientException(ErrorMessage.USER_ENTITY_NOT_FOUND); - if (spaceRepository.existsById(spaceInviteRequestDTO.spaceId())) + if (!spaceRepository.existsById(spaceInviteRequestDTO.spaceId())) throw new ApiClientException(ErrorMessage.SPACE_ENTITY_NOT_FOUND); UUID currentPageId = diff --git a/api/server-v1/src/main/java/kr/joberchip/server/v1/space/controller/SpaceController.java b/api/server-v1/src/main/java/kr/joberchip/server/v1/space/controller/SpaceController.java index 166b98e2..1e72eea0 100755 --- a/api/server-v1/src/main/java/kr/joberchip/server/v1/space/controller/SpaceController.java +++ b/api/server-v1/src/main/java/kr/joberchip/server/v1/space/controller/SpaceController.java @@ -29,21 +29,20 @@ public class SpaceController { @PostMapping("/new") public ApiResponse.Result createSpace( @AuthenticationPrincipal CustomUserDetails loginUser) { - - log.info("[SpaceController] loginUser : {}", loginUser); - UUID defaultPageId = sharePageService.createDefaultPage(loginUser.user().getUserId()); - log.info("[SpaceController] Generated Default Page Id : {}", defaultPageId); - sharePagePrivilegeService.registerPrivilege( - loginUser.user().getUserId(), defaultPageId, PrivilegeType.EDIT); + loginUser.user().getUserId(), defaultPageId, PrivilegeType.EDIT); UUID generatedSpaceId = spaceService.createSpace(loginUser.user().getUserId(), defaultPageId); - log.info("[SpaceController] Generated Space Id : {}", generatedSpaceId); spaceParticipationInfoService.registerOwnerInfo(loginUser.user().getUserId(), generatedSpaceId); + log.info("[SpaceController][POST] Create Space - Current Username : {}", loginUser.user().getUsername()); + log.info("[SpaceController][POST] Create Space - Generated Default Page Id : {}", defaultPageId); + log.info("[SpaceController][POST] Create Space - Generated Space Id : {}", generatedSpaceId); + + return ApiResponse.success(); } diff --git a/libs/core/src/main/java/kr/joberchip/core/block/LinkBlock.java b/libs/core/src/main/java/kr/joberchip/core/block/LinkBlock.java index bddda1b5..52100d0c 100644 --- a/libs/core/src/main/java/kr/joberchip/core/block/LinkBlock.java +++ b/libs/core/src/main/java/kr/joberchip/core/block/LinkBlock.java @@ -12,6 +12,7 @@ @AllArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor @Getter +@Setter public class LinkBlock extends BaseObject { @Column(name = "title") private String title; @@ -24,7 +25,7 @@ public class LinkBlock extends BaseObject { private String link; public static LinkBlock of( - String title, String description, String link, Integer x, Integer y, Integer w, Integer h, Boolean visible) { + String title, String description, String link, Integer x, Integer y, Integer w, Integer h) { LinkBlock generated = new LinkBlock(title, description, link); @@ -32,20 +33,7 @@ public static LinkBlock of( generated.setY(y); generated.setW(w); generated.setH(h); - generated.setVisible(visible); return generated; } - - public void modifyTitle(String title) { - this.title = title; - } - - public void modifyDescription(String description) { - this.description = description; - } - - public void modifyLink(String link) { - this.link = link; - } }