Skip to content

Commit

Permalink
Merge pull request #218 from swm-nodriversomabus/BUS-222-mypage-1-mvp
Browse files Browse the repository at this point in the history
Bus 222 mypage 1 mvp
  • Loading branch information
Lemonade255 authored Nov 16, 2023
2 parents b9bb53e + b715f04 commit 69d55d9
Show file tree
Hide file tree
Showing 18 changed files with 394 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.example.api.blocklist.adapter.in.rest;


import com.example.api.auth.domain.SecurityUser;
import com.example.api.blocklist.application.port.in.AddBlockUseccase;
import com.example.api.blocklist.application.port.in.ReleaseBlockUsecase;
import com.example.api.blocklist.application.port.in.GetListUsecase;
import com.example.api.blocklist.domain.BlockList;
import com.example.api.blocklist.dto.AddBlockDto;
import com.example.api.blocklist.dto.DeleteBlockDto;
import com.example.api.common.exception.CustomException;
import com.example.api.common.type.ErrorCodeEnum;
import com.example.api.common.utils.AuthenticationUtils;
import com.example.api.user.domain.BlockUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
@Tag(name = "BlockList", description = "Blocklist API")
public class BlockListController {
private final AddBlockUseccase addBlockUseccase;
private final ReleaseBlockUsecase releaseBlockUsecase;
private final GetListUsecase getListUsecase;

@Operation(summary = "add Block User", description = "차단할 유저 추가")
@PostMapping("/block")
public void addBlockUser(@Valid @RequestBody AddBlockDto addBlockDto){
SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication();
if (securityUser == null) {
log.error("BlockListController::addBlockUser: Login is needed");
throw new CustomException(ErrorCodeEnum.LOGIN_IS_NOT_DONE);
}
addBlockUseccase.addBlockUser(addBlockDto, securityUser.getUserId());
}

@Operation(summary = "get List block User", description = "차단한 유저 리스트제공")
@GetMapping("/block")
public List<BlockUser> getBlockList(@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC, size = 30) Pageable pageable){
SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication();
if (securityUser == null) {
log.error("BlockListController::getBlockList: Login is needed");
throw new CustomException(ErrorCodeEnum.LOGIN_IS_NOT_DONE);
}
return getListUsecase.getBlockList(pageable, securityUser.getUserId());
}

@Operation(summary = "release block user", description = "차단한 유저 해제")
@DeleteMapping("/block")
public void releaseBlockUser(@Valid @RequestBody DeleteBlockDto deleteBlockDto){
SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication();
if (securityUser == null) {
log.error("BlockListController::releaseBlockUser: Login is needed");
throw new CustomException(ErrorCodeEnum.LOGIN_IS_NOT_DONE);
}
releaseBlockUsecase.releaseBlockUser(deleteBlockDto, securityUser.getUserId());

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.api.blocklist.adapter.out.persistence;

import com.example.api.common.entity.BaseEntity;
import com.example.api.user.adapter.out.persistence.UserEntity;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.UUID;


@Entity
@EntityListeners(AuditingEntityListener.class)
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@IdClass(BlockListPK.class)
@Table(name="blocklist")
public class BlockListEntity extends BaseEntity {
@Id
private UUID userId;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(nullable = false, name="blocklist_userid", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@ToString.Exclude
@Id
private UserEntity blocklistUserId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.api.blocklist.adapter.out.persistence;

import com.example.api.user.adapter.out.persistence.UserEntity;

import java.io.Serializable;
import java.util.UUID;

public class BlockListPK implements Serializable {
private UUID userId;
private UserEntity blocklistUserId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.api.blocklist.adapter.out.persistence;


import com.example.api.blocklist.domain.BlockList;
import com.example.api.blocklist.dto.AddBlockDto;
import com.example.api.user.domain.BlockUser;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;

import java.util.List;

@Mapper(componentModel = "spring",
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface BlockMapperInterface {
@Mapping(source = "blocklistUserId", target = "blocklistUserId.userId")
BlockList toDomain(AddBlockDto addBlockDto);
@Mapping(source = "blocklistUserId.userId", target = "blocklistUserId.userId")
List<BlockList> toDomainList(List<BlockListEntity> blockListEntities);
@Mapping(source = "blocklistUserId.userId", target = "blocklistUserId.userId")
BlockListEntity toEntity(BlockList blockList);
@Mapping(source = "blocklistUserId.userId", target = "userId")
@Mapping(source = "blocklistUserId.username", target = "username")
List<BlockUser> toDomainBlockUserList(List<BlockList> blockLists);
// @Mapping(source = "blocklistUserId.userId", target = "userId")
// List<BlockUser> toDomainList(List<BlockListEntity> blockListEntities);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.api.blocklist.adapter.out.persistence;

import com.example.api.blocklist.application.port.out.AddBlockUserPort;
import com.example.api.blocklist.application.port.out.GetBlockListPort;
import com.example.api.blocklist.application.port.out.ReleaseBlockPort;
import com.example.api.blocklist.domain.BlockList;
import com.example.api.blocklist.repositorty.BlockListRepository;
import com.example.api.chat.adapter.out.persistence.ChatEntity;
import com.example.api.user.domain.BlockUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.core.Block;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;



@Repository
@RequiredArgsConstructor
@Slf4j
public class BlockUserPersistentAdapter implements AddBlockUserPort, GetBlockListPort, ReleaseBlockPort {
private final BlockListRepository blockListRepository;
private final BlockMapperInterface blockMapperInterface;

@Override
public void addBlockUser(BlockList blockList) {
blockListRepository.save(blockMapperInterface.toEntity(blockList));
}

@Override
public List<BlockList> getBlockUserList(UUID userId, Pageable pageable) {
Page<BlockListEntity> ret = blockListRepository.findAllByUserId(userId, pageable);
if (ret != null && ret.hasContent()){
return blockMapperInterface.toDomainList(ret.getContent());
}
return new ArrayList<>();
}

@Override
public void deleteBlockUser(UUID userId, UUID blockUserId) {
blockListRepository.deleteByUserIdAndBlocklistUserId_UserId(userId, blockUserId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.api.blocklist.application.port.in;

import com.example.api.blocklist.dto.AddBlockDto;

import java.util.UUID;

public interface AddBlockUseccase {
void addBlockUser(AddBlockDto addBlockDto, UUID userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.api.blocklist.application.port.in;

import com.example.api.blocklist.domain.BlockList;
import com.example.api.user.domain.BlockUser;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.UUID;

public interface GetListUsecase {
List<BlockUser> getBlockList(Pageable pageable, UUID userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.api.blocklist.application.port.in;

import com.example.api.blocklist.dto.AddBlockDto;
import com.example.api.blocklist.dto.DeleteBlockDto;

import java.util.UUID;

public interface ReleaseBlockUsecase {
void releaseBlockUser(DeleteBlockDto addBlockDto, UUID userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.api.blocklist.application.port.out;

import com.example.api.blocklist.domain.BlockList;

import java.util.UUID;

public interface AddBlockUserPort {
void addBlockUser(BlockList blockList);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.api.blocklist.application.port.out;

import com.example.api.blocklist.domain.BlockList;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.UUID;

public interface GetBlockListPort {
List<BlockList> getBlockUserList(UUID userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.api.blocklist.application.port.out;

import com.example.api.blocklist.domain.BlockList;

import java.util.UUID;

public interface ReleaseBlockPort {
void deleteBlockUser(UUID userId, UUID blockUserId);
}

15 changes: 15 additions & 0 deletions src/main/java/com/example/api/blocklist/domain/BlockList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.api.blocklist.domain;

import com.example.api.user.domain.BlockUser;
import lombok.*;

import java.util.UUID;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BlockList {
private UUID userId;
private BlockUser blocklistUserId;
}
17 changes: 17 additions & 0 deletions src/main/java/com/example/api/blocklist/dto/AddBlockDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.api.blocklist.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Data;

import java.util.UUID;

@Data
public class AddBlockDto {
@NotNull
// @Pattern(regexp = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}")
private UUID blocklistUserId;

private UUID userId;
}
13 changes: 13 additions & 0 deletions src/main/java/com/example/api/blocklist/dto/DeleteBlockDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.api.blocklist.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.UUID;

@Data
public class DeleteBlockDto {
@NotNull
private UUID blocklistUserId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.api.blocklist.repositorty;

import com.example.api.blocklist.adapter.out.persistence.BlockListEntity;
import com.example.api.blocklist.adapter.out.persistence.BlockListPK;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;

import java.util.UUID;

public interface BlockListRepository extends JpaRepository<BlockListEntity, BlockListPK> {
void deleteByUserIdAndBlocklistUserId_UserId(UUID userId, UUID blockListUserId);

@EntityGraph(attributePaths = {"blocklistUserId"})
Page<BlockListEntity> findAllByUserId(@Param("userId") UUID userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.api.blocklist.service;


import com.example.api.blocklist.adapter.out.persistence.BlockMapperInterface;
import com.example.api.blocklist.application.port.in.AddBlockUseccase;
import com.example.api.blocklist.application.port.in.GetListUsecase;
import com.example.api.blocklist.application.port.in.ReleaseBlockUsecase;
import com.example.api.blocklist.application.port.out.AddBlockUserPort;
import com.example.api.blocklist.application.port.out.GetBlockListPort;
import com.example.api.blocklist.application.port.out.ReleaseBlockPort;
import com.example.api.blocklist.domain.BlockList;
import com.example.api.blocklist.dto.AddBlockDto;
import com.example.api.blocklist.dto.DeleteBlockDto;
import com.example.api.user.domain.BlockUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class BlockListService implements AddBlockUseccase, GetListUsecase, ReleaseBlockUsecase {
private final AddBlockUserPort addBlockUserPort;
private final GetBlockListPort getBlockListPort;
private final ReleaseBlockPort releaseBlockPort;
private final BlockMapperInterface blockMapperInterface;

@Override
@Transactional
public void addBlockUser(AddBlockDto addBlockDto, UUID userId) {
addBlockDto.setUserId(userId);
BlockList blockList = blockMapperInterface.toDomain(addBlockDto);
addBlockUserPort.addBlockUser(blockList);
}

@Override
public List<BlockUser> getBlockList(Pageable pageable, UUID userId) {
List<BlockList> blockList = getBlockListPort.getBlockUserList(userId, pageable);
return blockList.stream().map(BlockList::getBlocklistUserId).toList();
}

@Override
@Transactional
public void releaseBlockUser(DeleteBlockDto addBlockDto, UUID userId) {
releaseBlockPort.deleteBlockUser(userId, addBlockDto.getBlocklistUserId());
}
}
Loading

0 comments on commit 69d55d9

Please sign in to comment.