Skip to content

Commit

Permalink
GETP-198 refactor: service term 도메인에서 persistence adapter 분리 (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
scv1702 authored Aug 21, 2024
1 parent 0bf0e32 commit 68d20ed
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public class ServiceTermController {
@PostMapping
// TODO: 관리자만 접근 가능하도록 권한 제어
public ServiceTermResponse createServiceTerm(@RequestBody @Valid ServiceTermRequest serviceTermRequest) {
return ServiceTermResponse.from(serviceTermService.create(serviceTermRequest));
return ServiceTermResponse.from(serviceTermService.register(serviceTermRequest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import es.princip.getp.domain.member.model.ServiceTermAgreement;
import es.princip.getp.domain.member.service.ServiceTermAgreementService;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermChecker;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermRepository;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermTag;
import es.princip.getp.domain.serviceTerm.exception.NotFoundServiceTermException;
import es.princip.getp.domain.serviceTerm.port.out.CheckServiceTermPort;
import es.princip.getp.persistence.adapter.serviceTerm.NotFoundServiceTermException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -20,7 +20,7 @@
public class ServiceTermAgreementServiceImpl implements ServiceTermAgreementService {

private final ServiceTermChecker checker;
private final ServiceTermRepository serviceTermRepository;
private final CheckServiceTermPort checkServiceTermPort;

@Override
public void agreeServiceTerms(final Member member, final List<ServiceTermAgreementCommand> commands) {
Expand All @@ -32,7 +32,7 @@ public void agreeServiceTerms(final Member member, final List<ServiceTermAgreeme
}

private ServiceTermAgreement createServiceTermAgreement(final ServiceTermTag tag, final boolean agreed) {
if (!serviceTermRepository.existsByTag(tag)) {
if (!checkServiceTermPort.existsBy(tag)) {
throw new NotFoundServiceTermException();
}
return ServiceTermAgreement.of(tag, agreed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ServiceTermAgreement {
private final boolean agreed;
private final LocalDateTime agreedAt;

public static ServiceTermAgreement of(final ServiceTermTag tag, final Boolean agreed) {
public static ServiceTermAgreement of(final ServiceTermTag tag, final boolean agreed) {
return new ServiceTermAgreement(tag, agreed, LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package es.princip.getp.domain.serviceTerm.application;

import es.princip.getp.api.controller.serviceTerm.dto.reqeust.ServiceTermRequest;
import es.princip.getp.domain.serviceTerm.domain.ServiceTerm;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermRepository;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermTag;
import es.princip.getp.api.controller.serviceTerm.dto.reqeust.ServiceTermRequest;
import es.princip.getp.domain.serviceTerm.exception.DuplicatedTagException;
import es.princip.getp.domain.serviceTerm.port.out.CheckServiceTermPort;
import es.princip.getp.domain.serviceTerm.port.out.SaveServiceTermPort;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -14,15 +15,17 @@
@RequiredArgsConstructor
public class ServiceTermService {

private final ServiceTermRepository serviceTermRepository;
private final CheckServiceTermPort checkServiceTermPort;
private final SaveServiceTermPort saveServiceTermPort;

@Transactional
public ServiceTerm create(ServiceTermRequest request) {
ServiceTermTag tag = ServiceTermTag.of(request.tag());
if (serviceTermRepository.existsByTag(tag)) {
public ServiceTerm register(final ServiceTermRequest request) {
final ServiceTermTag tag = ServiceTermTag.of(request.tag());
if (checkServiceTermPort.existsBy(tag)) {
throw new DuplicatedTagException();
}
ServiceTerm serviceTerm = new ServiceTerm(tag, request.required(), request.revocable());
return serviceTermRepository.save(serviceTerm);
final ServiceTerm serviceTerm = new ServiceTerm(tag, request.required(), request.revocable());
saveServiceTermPort.save(serviceTerm);
return serviceTerm;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
package es.princip.getp.domain.serviceTerm.domain;

import es.princip.getp.common.domain.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "service_term")
@AllArgsConstructor
public class ServiceTerm extends BaseTimeEntity {

@EmbeddedId
private ServiceTermTag tag;

@Getter
@Column(name = "required")
private boolean required;

@Column(name = "revocable")
private boolean revocable;

public ServiceTerm(ServiceTermTag tag, boolean required, boolean revocable) {
this.tag = tag;
this.required = required;
this.revocable = revocable;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
package es.princip.getp.domain.serviceTerm.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import java.io.Serializable;
import java.util.Objects;

@Getter
@ToString
@Embeddable
@EqualsAndHashCode
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ServiceTermTag implements Serializable {
public class ServiceTermTag {

@Column(name = "service_term_tag")
@NotNull
private String value;
private final String value;

public ServiceTermTag(final String value) {
this.value = value;
}

public static ServiceTermTag of(final String value) {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException("ServiceTermTag value must not be null or empty");
}
Objects.requireNonNull(value);
return new ServiceTermTag(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import es.princip.getp.domain.member.command.ServiceTermAgreementCommand;
import es.princip.getp.domain.serviceTerm.domain.ServiceTerm;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermChecker;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermRepository;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermTag;
import es.princip.getp.domain.serviceTerm.exception.NotAgreedAllRequiredServiceTermException;
import es.princip.getp.domain.serviceTerm.exception.NotFoundServiceTermException;
import es.princip.getp.domain.serviceTerm.port.out.LoadServiceTermPort;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -16,18 +14,13 @@
@RequiredArgsConstructor
public class ServiceTermCheckerImpl implements ServiceTermChecker {

private final ServiceTermRepository serviceTermRepository;

private ServiceTerm findByTag(final ServiceTermTag tag) {
return serviceTermRepository.findByTag(tag)
.orElseThrow(NotFoundServiceTermException::new);
}
private final LoadServiceTermPort loadServiceTermPort;

@Override
public void checkAllRequiredServiceTermsAreAgreed(final List<ServiceTermAgreementCommand> commands) {
commands.stream()
.filter(command -> {
ServiceTerm serviceTerm = findByTag(command.tag());
ServiceTerm serviceTerm = loadServiceTermPort.loadBy(command.tag());
return serviceTerm.isRequired() && !command.agreed();
})
.findAny()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package es.princip.getp.domain.serviceTerm.port.out;

import es.princip.getp.domain.serviceTerm.domain.ServiceTermTag;

public interface CheckServiceTermPort {


boolean existsBy(ServiceTermTag tag);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package es.princip.getp.domain.serviceTerm.port.out;

import es.princip.getp.domain.serviceTerm.domain.ServiceTerm;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermTag;

public interface LoadServiceTermPort {


ServiceTerm loadBy(ServiceTermTag tag);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package es.princip.getp.domain.serviceTerm.port.out;

import es.princip.getp.domain.serviceTerm.domain.ServiceTerm;

public interface SaveServiceTermPort {


void save(ServiceTerm serviceTerm);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package es.princip.getp.persistence.adapter;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeJpaEntity {

@CreatedDate
@Column(columnDefinition = "TIMESTAMP")
protected LocalDateTime createdAt;

@LastModifiedDate
@Column(columnDefinition = "TIMESTAMP")
protected LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package es.princip.getp.persistence.adapter.member;

import es.princip.getp.common.domain.BaseTimeEntity;
import es.princip.getp.domain.member.model.MemberType;
import es.princip.getp.persistence.adapter.BaseTimeJpaEntity;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -19,7 +19,7 @@
}
)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MemberJpaEntity extends BaseTimeEntity {
public class MemberJpaEntity extends BaseTimeJpaEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package es.princip.getp.domain.serviceTerm.exception;
package es.princip.getp.persistence.adapter.serviceTerm;

import es.princip.getp.common.exception.ErrorDescription;
import es.princip.getp.common.exception.NotFoundException;

// TODO: 접근 제어자를 private-default로 변경
public class NotFoundServiceTermException extends NotFoundException {

private static final String code = "NOT_FOUND_SERVICE_TERM";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package es.princip.getp.persistence.adapter.serviceTerm;

import es.princip.getp.persistence.adapter.BaseTimeJpaEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@AllArgsConstructor
@Table(name = "service_term")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
class ServiceTermJpaEntity extends BaseTimeJpaEntity {

@Id
@Column(name = "service_term_tag")
private String tag;

@Column(name = "required")
private boolean required;

@Column(name = "revocable")
private boolean revocable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package es.princip.getp.persistence.adapter.serviceTerm;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

interface ServiceTermJpaRepository extends JpaRepository<ServiceTermJpaEntity, String> {

Optional<ServiceTermJpaEntity> findByTag(String tag);

boolean existsByTag(String tag);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package es.princip.getp.persistence.adapter.serviceTerm;

import es.princip.getp.domain.serviceTerm.domain.ServiceTerm;
import es.princip.getp.domain.serviceTerm.domain.ServiceTermTag;
import es.princip.getp.domain.serviceTerm.port.out.CheckServiceTermPort;
import es.princip.getp.domain.serviceTerm.port.out.LoadServiceTermPort;
import es.princip.getp.domain.serviceTerm.port.out.SaveServiceTermPort;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
class ServiceTermPersistenceAdapter implements LoadServiceTermPort, CheckServiceTermPort, SaveServiceTermPort {

private final ServiceTermJpaRepository serviceTermJpaRepository;
private final ServiceTermPersistenceMapper mapper;

@Override
public boolean existsBy(final ServiceTermTag tag) {
return serviceTermJpaRepository.existsByTag(tag.getValue());
}

@Override
public ServiceTerm loadBy(final ServiceTermTag tag) {
return serviceTermJpaRepository.findByTag(tag.getValue())
.map(mapper::mapToDomain)
.orElseThrow(NotFoundServiceTermException::new);
}

@Override
public void save(final ServiceTerm serviceTerm) {
serviceTermJpaRepository.save(mapper.mapToJpa(serviceTerm));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package es.princip.getp.persistence.adapter.serviceTerm;

import es.princip.getp.domain.serviceTerm.domain.ServiceTerm;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
interface ServiceTermPersistenceMapper {

@Mapping(source = "tag", target = "tag.value")
ServiceTerm mapToDomain(ServiceTermJpaEntity serviceTermJpaEntity);

@Mapping(target = "tag", source = "tag.value")
ServiceTermJpaEntity mapToJpa(ServiceTerm serviceTerm);
}

0 comments on commit 68d20ed

Please sign in to comment.