Skip to content

Commit

Permalink
[api-server-v1] wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gdtknight committed Oct 5, 2023
1 parent 39725a2 commit 2b4fe39
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();

Expand All @@ -37,38 +40,37 @@ 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);

return BlockResponseDTO.fromEntity(mapBlock);
}

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public ApiResponse.Result<Object> moveBlocks(
public ApiResponse.Result<SharePageTreeResponseDTO.PageTreeNode> pageBreadCrumbBar(
@PathVariable UUID pageId) {

log.info("[SharePageController][GET] BreadCrumbBar for Page Id : {}", pageId);

SharePageTreeResponseDTO.PageTreeNode response = sharePageService.getPageBreadCrumbBar(pageId);

return ApiResponse.success(response);
Expand All @@ -86,13 +88,13 @@ public ApiResponse.Result<SharePageTreeResponseDTO.PageTreeNode> pageBreadCrumbB
* @return 생성된 페이지 정보
*/
@PostMapping("/new")
public ApiResponse.Result<SharePageProfileImageResponseDTO> createSharePage(
public ApiResponse.Result<Object> 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(
Expand Down Expand Up @@ -139,8 +141,9 @@ public ApiResponse.Result<SharePagePrivilegeResponseDTO> getPrivilege(
}

@PutMapping("/{pageId}/privilege")
public ApiResponse.Result<SharePagePrivilegeDTO> modifyPrivilege(
public ApiResponse.Result<Object> modifyPrivilege(
@AuthenticationPrincipal CustomUserDetails loginUser,
@PathVariable UUID pageId,
@RequestBody SharePagePrivilegeDTO sharePagePrivilegeDTO,
Errors errors) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -120,21 +119,20 @@ 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(
SpaceInviteRequestDTO spaceInviteRequestDTO, PrivilegeType privilegeType) {

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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@ public class SpaceController {
@PostMapping("/new")
public ApiResponse.Result<Object> 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();
}

Expand Down
Loading

0 comments on commit 2b4fe39

Please sign in to comment.