Skip to content

Commit

Permalink
Merge pull request #30 from Staketab/dev
Browse files Browse the repository at this point in the history
refactor some entities and solve some problems
  • Loading branch information
MrFoxogen authored Mar 19, 2024
2 parents dfb718e + 86ddfda commit 15a0666
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import com.staketab.minanames.dto.request.SearchParams;
import com.staketab.minanames.dto.request.sort.DomainsSortColumn;
import com.staketab.minanames.entity.DomainEntity;
import com.staketab.minanames.service.abstraction.DomainService;
import com.staketab.minanames.entity.dto.DomainDTO;
import com.staketab.minanames.service.DomainService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -71,7 +72,7 @@ public ResponseEntity<DomainEntity> create(@RequestBody DomainReservationDTO dom
}

@GetMapping("/{id}")
public ResponseEntity<DomainEntity> retrieve(@PathVariable String id) {
public ResponseEntity<DomainDTO> retrieve(@PathVariable String id) {
return ok(domainService.retrieve(id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DomainEntity {
@Column(name = "img", columnDefinition = "TEXT")
private String domainImg;

private Double amount;
private Long amount;

@Column(name = "reservation_timestamp")
private Long reservationTimestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public class PayableTransactionEntity {
private Set<DomainEntity> domains;

@Column(name = "amount")
private Double txAmount;
private Long txAmount;
}
23 changes: 23 additions & 0 deletions src/main/java/com/staketab/minanames/entity/dto/DomainDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.staketab.minanames.entity.dto;

import lombok.Builder;
import lombok.Data;

import java.math.BigDecimal;

@Data
@Builder
public class DomainDTO {

private String id;
private String ownerAddress;
private String transaction;
private String domainImg;
private BigDecimal amount;
private Long reservationTimestamp;
private Integer expirationTime;
private Long startTimestamp;
private DomainStatus domainStatus;
private Boolean isSendToCloudWorker;
private Boolean isDefault;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.staketab.minanames.scheduler;

import com.staketab.minanames.service.abstraction.TxService;
import com.staketab.minanames.service.TxService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.staketab.minanames.scheduler;

import com.staketab.minanames.service.abstraction.DomainService;
import com.staketab.minanames.service.DomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.staketab.minanames.service.abstraction;
package com.staketab.minanames.service;

import com.staketab.minanames.dto.DomainReservationDTO;
import com.staketab.minanames.dto.DomainUpdateDTO;
import com.staketab.minanames.dto.ReservedDomainDTO;
import com.staketab.minanames.dto.request.BaseRequest;
import com.staketab.minanames.dto.request.SearchParams;
import com.staketab.minanames.entity.DomainEntity;
import com.staketab.minanames.entity.dto.DomainDTO;
import org.springframework.data.domain.Page;

public interface DomainService {
Page<DomainEntity> findAllByPageable(BaseRequest request, SearchParams searchParams);
Page<DomainEntity> findAllByAccountPageable(BaseRequest request, String accountAddress, SearchParams searchParams);
DomainEntity create(DomainReservationDTO domainRequest);
DomainEntity retrieve(String id);
DomainDTO retrieve(String id);
DomainEntity update(DomainUpdateDTO domainUpdateDTO);
ReservedDomainDTO isNameReserved(String name);
Boolean setDefaultDomain(String id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.staketab.minanames.service.abstraction;
package com.staketab.minanames.service;

import com.staketab.minanames.entity.PayableTransactionEntity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@
import com.staketab.minanames.dto.request.BaseRequest;
import com.staketab.minanames.dto.request.SearchParams;
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.NotFoundException;
import com.staketab.minanames.repository.DomainRepository;
import com.staketab.minanames.service.abstraction.DomainService;
import com.staketab.minanames.service.abstraction.TxService;
import com.staketab.minanames.service.DomainService;
import com.staketab.minanames.service.TxService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
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;

@Service
@RequiredArgsConstructor
@Slf4j
Expand All @@ -45,7 +51,7 @@ public DomainEntity create(DomainReservationDTO request) {
.ownerAddress(request.getOwnerAddress())
.transaction(txService.getOrCreate(request.getTxHash()))
.domainName(request.getDomainName())
.amount(request.getAmount())
.amount(Math.round(request.getAmount() * MINA_DENOMINATION))
.expirationTime(request.getExpirationTime())
.reservationTimestamp(System.currentTimeMillis())
.domainStatus(DomainStatus.PENDING)
Expand All @@ -56,8 +62,9 @@ public DomainEntity create(DomainReservationDTO request) {
}

@Override
public DomainEntity retrieve(String id) {
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)));
}

Expand Down Expand Up @@ -97,4 +104,20 @@ private ReservedDomainDTO mapToReservedDomainDTO(DomainEntity domainEntity) {
.id(domainEntity.getId())
.build();
}

private DomainDTO buildDomainDTO(DomainEntity domainEntity) {
return DomainDTO.builder()
.id(domainEntity.getId())
.domainImg(domainEntity.getDomainImg())
.amount(BigDecimal.valueOf(domainEntity.getAmount()).divide(DEFAULT_DENOMINATION, RoundingMode.HALF_UP))
.startTimestamp(domainEntity.getStartTimestamp())
.domainStatus(domainEntity.getDomainStatus())
.expirationTime(domainEntity.getExpirationTime())
.isDefault(domainEntity.getIsDefault())
.isSendToCloudWorker(domainEntity.getIsSendToCloudWorker())
.ownerAddress(domainEntity.getOwnerAddress())
.reservationTimestamp(domainEntity.getReservationTimestamp())
.transaction(domainEntity.getTransaction().getTxHash())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.staketab.minanames.minascan.TxProjection;
import com.staketab.minanames.repository.DomainRepository;
import com.staketab.minanames.repository.PayableTransactionRepository;
import com.staketab.minanames.service.abstraction.TxService;
import com.staketab.minanames.service.TxService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -42,7 +42,6 @@ public void checkTransactions() {
domainRepository.deleteAllByTransactionIn(txStatusListMap.get(TxStatus.FAILED));
}

//todo set amount column
private void applyReservedTxs(List<PayableTransactionEntity> pendingTxs) {
List<PayableTransactionEntity> appliedTxs = pendingTxs.stream()
.peek(payableTransaction -> payableTransaction.setTxStatus(TxStatus.APPLIED))
Expand All @@ -62,11 +61,13 @@ private static Map<TxStatus, List<PayableTransactionEntity>> getSplitedMap(List<
List<PayableTransactionEntity> failed = new ArrayList<>();
List<PayableTransactionEntity> applied = new ArrayList<>();
for (TxProjection txProjection : txStatusByTxHashIn) {
PayableTransactionEntity payableTransactionEntity = mapPayableTransactions.get(txProjection.getTxHash());
if (TxStatus.APPLIED.name().equals(txProjection.getStatus().toUpperCase())) {
applied.add(mapPayableTransactions.get(txProjection.getTxHash()));
payableTransactionEntity.setTxAmount(txProjection.getAmount());
applied.add(payableTransactionEntity);
}
if (TxStatus.FAILED.name().equals(txProjection.getStatus().toUpperCase())) {
failed.add(mapPayableTransactions.get(txProjection.getTxHash()));
failed.add(payableTransactionEntity);
}
}
return Map.of(TxStatus.APPLIED, applied, TxStatus.FAILED, failed);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/staketab/minanames/utils/Constants.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.staketab.minanames.utils;

import lombok.experimental.UtilityClass;

import java.math.BigDecimal;

@UtilityClass
public class Constants {
//common
public static final String MINA_ID = "mina";
public static final String API_KEY_HEADER = "x-api-key";

public static final Long MINA_DENOMINATION = 1_000_000_000L;
public static final BigDecimal DEFAULT_DENOMINATION = BigDecimal.valueOf(1_000_000_000L);
}

0 comments on commit 15a0666

Please sign in to comment.