Skip to content

Commit

Permalink
refactor: 업데이트 시 도메인을 의존하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
sa46lll committed Dec 24, 2023
1 parent d93aead commit d497433
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import com.flow.sa46lll.fileshield.application.dto.BlockCustomExtensionResponse;
import com.flow.sa46lll.fileshield.application.dto.GetExtensionsResponse;
import com.flow.sa46lll.fileshield.application.port.in.BlockCustomExtensionUseCase;
import com.flow.sa46lll.fileshield.application.port.in.BlockFixedExtensionUseCase;
import com.flow.sa46lll.fileshield.application.port.in.GetExtensionQuery;
import com.flow.sa46lll.fileshield.application.port.in.UnblockCustomExtensionUseCase;
import com.flow.sa46lll.fileshield.application.port.in.UnblockFixedExtensionUseCase;
import com.flow.sa46lll.fileshield.application.port.in.UpdateFixedExtensionUsecase;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand All @@ -24,19 +23,16 @@ public class ExtensionRestController {

private final BlockCustomExtensionUseCase blockCustomExtensionUseCase;
private final UnblockCustomExtensionUseCase unblockCustomExtensionUseCase;
private final BlockFixedExtensionUseCase blockFixedExtensionUseCase;
private final UnblockFixedExtensionUseCase unblockFixedExtensionUseCase;
private final UpdateFixedExtensionUsecase updateFixedExtensionUsecase;
private final GetExtensionQuery getExtensionQuery;

public ExtensionRestController(final BlockCustomExtensionUseCase blockCustomExtensionUseCase,
final UnblockCustomExtensionUseCase unblockCustomExtensionUseCase,
final BlockFixedExtensionUseCase blockFixedExtensionUseCase,
final UnblockFixedExtensionUseCase unblockFixedExtensionUseCase,
final UpdateFixedExtensionUsecase UpdateFixedExtensionUsecase,
final GetExtensionQuery getExtensionQuery) {
this.blockCustomExtensionUseCase = blockCustomExtensionUseCase;
this.unblockCustomExtensionUseCase = unblockCustomExtensionUseCase;
this.blockFixedExtensionUseCase = blockFixedExtensionUseCase;
this.unblockFixedExtensionUseCase = unblockFixedExtensionUseCase;
this.updateFixedExtensionUsecase = UpdateFixedExtensionUsecase;
this.getExtensionQuery = getExtensionQuery;
}

Expand All @@ -47,7 +43,7 @@ public ExtensionRestController(final BlockCustomExtensionUseCase blockCustomExte
*/
@PatchMapping("/{extensionId}/fixed-block")
public ApiResponse<Void> blockFixed(@PathVariable("extensionId") final Long extensionId) {
blockFixedExtensionUseCase.blockFixed(extensionId);
updateFixedExtensionUsecase.blockFixed(extensionId);
return ApiResponse.ok();
}

Expand All @@ -58,7 +54,7 @@ public ApiResponse<Void> blockFixed(@PathVariable("extensionId") final Long exte
*/
@PatchMapping("/{extensionId}/fixed-unblock")
public ApiResponse<Void> unblockFixed(@PathVariable("extensionId") final Long extensionId) {
unblockFixedExtensionUseCase.unblockFixed(extensionId);
updateFixedExtensionUsecase.unblockFixed(extensionId);
return ApiResponse.ok();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.flow.sa46lll.fileshield.application.dto;

import com.flow.sa46lll.fileshield.BlockedExtension;

public class BlockedExtensionMapper {

private BlockedExtensionMapper() {
}

public static BlockedExtension toDomainFromUpdateDto(final Long extensionId) {
return new BlockedExtension(extensionId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.flow.sa46lll.fileshield.application.port.in;

public interface UnblockFixedExtensionUseCase {
public interface UpdateFixedExtensionUsecase {

void blockFixed(final Long extensionId);

void unblockFixed(final Long extensionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ public interface ExtensionPersistencePort {

List<BlockedExtension> findAll();

Long blockCustomExtension(BlockedExtension blockedExtension);
Long blockCustomExtension(final BlockedExtension blockedExtension);

void deleteCustomExtensionById(Long extensionId);
void deleteCustomExtensionById(final Long extensionId);

void blockFixedExtension(Long extensionId);

void unblockFixedExtension(Long extensionId);
void updateExtensionBlockStatus(final BlockedExtension blockedExtension);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import com.flow.sa46lll.fileshield.BlockedExtension;
import com.flow.sa46lll.fileshield.application.dto.BlockCustomExtensionCommand;
import com.flow.sa46lll.fileshield.application.dto.BlockCustomExtensionResponse;
import com.flow.sa46lll.fileshield.application.dto.BlockedExtensionMapper;
import com.flow.sa46lll.fileshield.application.mapper.BlockCustomExtensionResponseMapper;
import com.flow.sa46lll.fileshield.application.port.in.BlockCustomExtensionUseCase;
import com.flow.sa46lll.fileshield.application.port.in.BlockFixedExtensionUseCase;
import com.flow.sa46lll.fileshield.application.port.in.UpdateFixedExtensionUsecase;
import com.flow.sa46lll.fileshield.application.port.in.UnblockCustomExtensionUseCase;
import com.flow.sa46lll.fileshield.application.port.in.UnblockFixedExtensionUseCase;
import com.flow.sa46lll.fileshield.application.port.out.ExtensionPersistencePort;
import org.springframework.stereotype.Service;

@Service
public class ExtensionBlockService implements BlockCustomExtensionUseCase,
UnblockCustomExtensionUseCase, BlockFixedExtensionUseCase, UnblockFixedExtensionUseCase {
public class ExtensionUpdateService implements BlockCustomExtensionUseCase,
UnblockCustomExtensionUseCase, UpdateFixedExtensionUsecase {

private final ExtensionPersistencePort extensionPersistencePort;

public ExtensionBlockService(final ExtensionPersistencePort extensionPersistencePort) {
public ExtensionUpdateService(final ExtensionPersistencePort extensionPersistencePort) {
this.extensionPersistencePort = extensionPersistencePort;
}

Expand All @@ -29,17 +29,25 @@ public BlockCustomExtensionResponse blockCustom(final BlockCustomExtensionComman
}

@Override
public void unblockCustom(Long extensionId) {
public void unblockCustom(final Long extensionId) {
extensionPersistencePort.deleteCustomExtensionById(extensionId);
}

@Override
public void blockFixed(Long extensionId) {
extensionPersistencePort.blockFixedExtension(extensionId);
public void blockFixed(final Long extensionId) {
BlockedExtension blockedExtension = BlockedExtensionMapper.toDomainFromUpdateDto(extensionId);
blockedExtension.block();
updateExtensionBlockStatus(blockedExtension);
}

@Override
public void unblockFixed(Long extensionId) {
extensionPersistencePort.unblockFixedExtension(extensionId);
public void unblockFixed(final Long extensionId) {
BlockedExtension blockedExtension = BlockedExtensionMapper.toDomainFromUpdateDto(extensionId);
blockedExtension.unblock();
updateExtensionBlockStatus(blockedExtension);
}

private void updateExtensionBlockStatus(final BlockedExtension blockedExtension) {
extensionPersistencePort.updateExtensionBlockStatus(blockedExtension);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public BlockedExtension(final Long id,
this.isBlocked = isBlocked;
}

public BlockedExtension(final Long id) {
this.id = id;
}

public Long getId() {
return id;
}
Expand All @@ -40,4 +44,12 @@ public boolean isFixed() {
public boolean isCustom() {
return extensionType == ExtensionType.CUSTOM;
}

public void block() {
isBlocked = true;
}

public void unblock() {
isBlocked = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.flow.sa46lll.fileshield.BlockedExtension;
import com.flow.sa46lll.fileshield.application.port.out.ExtensionPersistencePort;
import com.flow.sa46lll.fileshield.entity.BlockedExtensionEntity;
import com.flow.sa46lll.fileshield.entity.ExtensionTypeEntity;
import com.flow.sa46lll.fileshield.mapper.BlockedExtensionMapper;
import com.flow.sa46lll.fileshield.repository.BlockedExtensionRepository;
Expand Down Expand Up @@ -40,20 +39,11 @@ public void deleteCustomExtensionById(Long extensionId) {
}

@Override
public void blockFixedExtension(Long extensionId) {
BlockedExtensionEntity blockedExtension =
blockedExtensionRepository.findByExtensionTypeAndId(ExtensionTypeEntity.FIXED, extensionId)
.orElseThrow(() -> new IllegalArgumentException("확장자를 찾을 수 없습니다"));

blockedExtension.block(); // 개선 필요
}

@Override
public void unblockFixedExtension(Long extensionId) {
BlockedExtensionEntity blockedExtension =
blockedExtensionRepository.findByExtensionTypeAndId(ExtensionTypeEntity.FIXED, extensionId)
.orElseThrow(() -> new IllegalArgumentException("확장자를 찾을 수 없습니다"));

blockedExtension.unblock();
public void updateExtensionBlockStatus(BlockedExtension blockedExtension) {
blockedExtensionRepository.findByExtensionTypeAndId(ExtensionTypeEntity.FIXED, blockedExtension.getId())
.ifPresent(persistedEntity -> {
BlockedExtensionMapper.toUpdateEntity(blockedExtension, persistedEntity);
blockedExtensionRepository.save(persistedEntity);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public class BlockedExtensionEntity extends AuditEntity {
@Column(name = "is_blocked")
private boolean isBlocked;

public BlockedExtensionEntity(final Long id,
final String extension,
final ExtensionTypeEntity extensionType,
final boolean isBlocked) {
this.id = id;
this.extension = extension;
this.extensionType = extensionType;
this.isBlocked = isBlocked;
}

public BlockedExtensionEntity(final String extension,
final ExtensionTypeEntity extensionType,
final boolean isBlocked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@
import com.flow.sa46lll.fileshield.ExtensionType;
import com.flow.sa46lll.fileshield.entity.BlockedExtensionEntity;
import com.flow.sa46lll.fileshield.entity.ExtensionTypeEntity;
import java.util.List;

public class BlockedExtensionMapper {

private BlockedExtensionMapper() {
}

public static List<BlockedExtension> toDomain(List<BlockedExtensionEntity> entities) {
return entities.stream()
.map(BlockedExtensionMapper::toDomain)
.toList();
}

public static BlockedExtension toDomain(BlockedExtensionEntity entity) {
return new BlockedExtension(
entity.getId(),
Expand All @@ -32,4 +25,12 @@ public static BlockedExtensionEntity toEntity(BlockedExtension blockedExtension)
ExtensionTypeEntity.from(blockedExtension.getExtensionType().name()),
blockedExtension.isBlocked());
}

public static BlockedExtensionEntity toUpdateEntity(BlockedExtension domain, BlockedExtensionEntity entity) { // 개선 필요
return new BlockedExtensionEntity(
domain.getId(),
entity.getExtension(),
entity.getExtensionType(),
domain.isBlocked());
}
}

0 comments on commit d497433

Please sign in to comment.