Skip to content

Commit

Permalink
Merge pull request #31 from Staketab/dev
Browse files Browse the repository at this point in the history
update logic for creating new domain
  • Loading branch information
MrFoxogen authored Mar 20, 2024
2 parents 15a0666 + c10ead0 commit b8e4453
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ public Page<DomainEntity> getAccountDomains(@Valid @ParameterObject BaseRequest

@PostMapping("/save")
public ResponseEntity<DomainEntity> create(@RequestBody DomainReservationDTO domainRequest) {
try {
return ok(domainService.create(domainRequest));
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
return ok(domainService.create(domainRequest));
}

@GetMapping("/{id}")
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/staketab/minanames/entity/DomainEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.staketab.minanames.exception;

public class DuplicateKeyException extends RuntimeException {
public DuplicateKeyException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,7 +15,13 @@ public class RestExceptionHandler {

@ResponseStatus(NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<ApiError> handleException(Exception e) {
public ResponseEntity<ApiError> handleNotFoundException(Exception e) {
return ResponseEntity.status(NOT_FOUND).body(new ApiError(NOT_FOUND, e.getMessage()));
}

@ResponseStatus(CONFLICT)
@ExceptionHandler(DuplicateKeyException.class)
public ResponseEntity<ApiError> handleDuplicateKeyException(Exception e) {
return ResponseEntity.status(CONFLICT).body(new ApiError(CONFLICT, e.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -47,36 +47,30 @@ public Page<DomainEntity> 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);
}

@Override
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<DomainEntity> 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
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit b8e4453

Please sign in to comment.