From c10ead051e57ce3b9176ffc7784d9402a94990a4 Mon Sep 17 00:00:00 2001 From: ilya Date: Wed, 20 Mar 2024 11:35:55 +0300 Subject: [PATCH] update logic for creating new domain --- .../controller/DomainController.java | 6 +-- .../minanames/entity/DomainEntity.java | 18 +++++-- .../exception/DuplicateKeyException.java | 7 +++ .../exception/RestExceptionHandler.java | 9 +++- .../service/impl/DomainServiceImpl.java | 48 +++++++++++-------- 5 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/staketab/minanames/exception/DuplicateKeyException.java diff --git a/src/main/java/com/staketab/minanames/controller/DomainController.java b/src/main/java/com/staketab/minanames/controller/DomainController.java index 794dad1..685b031 100644 --- a/src/main/java/com/staketab/minanames/controller/DomainController.java +++ b/src/main/java/com/staketab/minanames/controller/DomainController.java @@ -64,11 +64,7 @@ public Page getAccountDomains(@Valid @ParameterObject BaseRequest @PostMapping("/save") public ResponseEntity create(@RequestBody DomainReservationDTO domainRequest) { - try { - return ok(domainService.create(domainRequest)); - } catch (Exception e) { - return ResponseEntity.internalServerError().build(); - } + return ok(domainService.create(domainRequest)); } @GetMapping("/{id}") diff --git a/src/main/java/com/staketab/minanames/entity/DomainEntity.java b/src/main/java/com/staketab/minanames/entity/DomainEntity.java index fde30e9..d4b3a07 100644 --- a/src/main/java/com/staketab/minanames/entity/DomainEntity.java +++ b/src/main/java/com/staketab/minanames/entity/DomainEntity.java @@ -4,7 +4,15 @@ import com.fasterxml.jackson.annotation.JsonIdentityReference; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import com.staketab.minanames.entity.dto.DomainStatus; -import jakarta.persistence.*; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -26,12 +34,12 @@ public class DomainEntity { private String ownerAddress; @ManyToOne(cascade = CascadeType.ALL) - @JoinColumn(name="transaction", nullable=false) - @JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="txHash") - @JsonIdentityReference(alwaysAsId=true) + @JoinColumn(name = "transaction", nullable = false) + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "txHash") + @JsonIdentityReference(alwaysAsId = true) private PayableTransactionEntity transaction; - @Column(name = "name", columnDefinition = "TEXT") + @Column(name = "name", columnDefinition = "TEXT", unique = true) private String domainName; @Column(name = "img", columnDefinition = "TEXT") private String domainImg; diff --git a/src/main/java/com/staketab/minanames/exception/DuplicateKeyException.java b/src/main/java/com/staketab/minanames/exception/DuplicateKeyException.java new file mode 100644 index 0000000..1eb9642 --- /dev/null +++ b/src/main/java/com/staketab/minanames/exception/DuplicateKeyException.java @@ -0,0 +1,7 @@ +package com.staketab.minanames.exception; + +public class DuplicateKeyException extends RuntimeException { + public DuplicateKeyException(String message) { + super(message); + } +} diff --git a/src/main/java/com/staketab/minanames/exception/RestExceptionHandler.java b/src/main/java/com/staketab/minanames/exception/RestExceptionHandler.java index 0c83f0b..cd7e57f 100644 --- a/src/main/java/com/staketab/minanames/exception/RestExceptionHandler.java +++ b/src/main/java/com/staketab/minanames/exception/RestExceptionHandler.java @@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; +import static org.springframework.http.HttpStatus.CONFLICT; import static org.springframework.http.HttpStatus.NOT_FOUND; @Slf4j @@ -14,7 +15,13 @@ public class RestExceptionHandler { @ResponseStatus(NOT_FOUND) @ExceptionHandler(NotFoundException.class) - public ResponseEntity handleException(Exception e) { + public ResponseEntity handleNotFoundException(Exception e) { return ResponseEntity.status(NOT_FOUND).body(new ApiError(NOT_FOUND, e.getMessage())); } + + @ResponseStatus(CONFLICT) + @ExceptionHandler(DuplicateKeyException.class) + public ResponseEntity handleDuplicateKeyException(Exception e) { + return ResponseEntity.status(CONFLICT).body(new ApiError(CONFLICT, e.getMessage())); + } } diff --git a/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java b/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java index 5591ee9..977271b 100644 --- a/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java +++ b/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java @@ -8,6 +8,7 @@ import com.staketab.minanames.entity.DomainEntity; import com.staketab.minanames.entity.dto.DomainDTO; import com.staketab.minanames.entity.dto.DomainStatus; +import com.staketab.minanames.exception.DuplicateKeyException; import com.staketab.minanames.exception.NotFoundException; import com.staketab.minanames.repository.DomainRepository; import com.staketab.minanames.service.DomainService; @@ -22,7 +23,6 @@ import java.math.RoundingMode; import java.sql.Timestamp; import java.time.LocalDateTime; -import java.util.Optional; import static com.staketab.minanames.utils.Constants.DEFAULT_DENOMINATION; import static com.staketab.minanames.utils.Constants.MINA_DENOMINATION; @@ -47,17 +47,12 @@ public Page findAllByAccountPageable(BaseRequest request, String a @Override public DomainEntity create(DomainReservationDTO request) { - DomainEntity domain = DomainEntity.builder() - .ownerAddress(request.getOwnerAddress()) - .transaction(txService.getOrCreate(request.getTxHash())) - .domainName(request.getDomainName()) - .amount(Math.round(request.getAmount() * MINA_DENOMINATION)) - .expirationTime(request.getExpirationTime()) - .reservationTimestamp(System.currentTimeMillis()) - .domainStatus(DomainStatus.PENDING) - .isSendToCloudWorker(false) - .isDefault(false) - .build(); + String domainName = request.getDomainName(); + domainRepository.findDomainEntityByDomainName(domainName) + .ifPresent(domainEntity -> { + throw new DuplicateKeyException(String.format("Domain already exist with name: %s", domainName)); + }); + DomainEntity domain = buildDomainEntity(request); return domainRepository.save(domain); } @@ -65,18 +60,17 @@ public DomainEntity create(DomainReservationDTO request) { public DomainDTO retrieve(String id) { return domainRepository.findById(id) .map(this::buildDomainDTO) - .orElseThrow(() -> new NotFoundException(String.format("Domain doesn't found by id: %s", id))); + .orElseThrow(() -> new NotFoundException(String.format("Domain isn’t found by id: %s", id))); } @Override public DomainEntity update(DomainUpdateDTO domainUpdateDTO) { - Optional optionalDomainEntity = domainRepository.findById(domainUpdateDTO.getId()); - if (optionalDomainEntity.isEmpty()) { - throw new NotFoundException(String.format("Domain doesn't found by id: %s", domainUpdateDTO.getId())); - } - DomainEntity domainEntity = optionalDomainEntity.get(); - domainEntity.setDomainImg(domainUpdateDTO.getImg()); - return domainRepository.save(domainEntity); + return domainRepository.findById(domainUpdateDTO.getId()) + .map(domainEntity -> { + domainEntity.setDomainImg(domainUpdateDTO.getImg()); + return domainRepository.save(domainEntity); + }) + .orElseThrow(() -> new NotFoundException("Domain not found by id: " + domainUpdateDTO.getId())); } @Override @@ -105,6 +99,20 @@ private ReservedDomainDTO mapToReservedDomainDTO(DomainEntity domainEntity) { .build(); } + private DomainEntity buildDomainEntity(DomainReservationDTO request) { + return DomainEntity.builder() + .ownerAddress(request.getOwnerAddress()) + .transaction(txService.getOrCreate(request.getTxHash())) + .domainName(request.getDomainName()) + .amount(Math.round(request.getAmount() * MINA_DENOMINATION)) + .expirationTime(request.getExpirationTime()) + .reservationTimestamp(System.currentTimeMillis()) + .domainStatus(DomainStatus.PENDING) + .isSendToCloudWorker(false) + .isDefault(false) + .build(); + } + private DomainDTO buildDomainDTO(DomainEntity domainEntity) { return DomainDTO.builder() .id(domainEntity.getId())