From c0bb430d4e843aa7f30837891135b5219b6ce22a Mon Sep 17 00:00:00 2001 From: ilya Date: Tue, 14 May 2024 11:18:26 +0300 Subject: [PATCH] add endpoint for minascan --- .../client/IpfsZkCloudWorkerClient.java | 4 +- .../config/security/ApiKeyRequestFilter.java | 9 ++--- .../controller/DomainController.java | 12 ++++++ ...pfsDomainMetadataNftMetadataZkDataDTO.java | 1 - .../minanames/dto/SimpleDomainDTO.java | 18 +++++++++ .../dto/request/sort/DomainsSortColumn.java | 1 + .../minanames/entity/ActivityStatus.java | 3 +- .../minanames/entity/DomainEntity.java | 2 +- .../repository/DomainRepository.java | 19 ++++++++- .../minanames/service/DomainService.java | 5 +++ .../service/impl/ActivityServiceImpl.java | 2 +- .../service/impl/DomainServiceImpl.java | 39 +++++++++++++++++++ .../impl/ZkCloudWorkerServiceImpl.java | 14 +++++-- 13 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/staketab/minanames/dto/SimpleDomainDTO.java diff --git a/src/main/java/com/staketab/minanames/client/IpfsZkCloudWorkerClient.java b/src/main/java/com/staketab/minanames/client/IpfsZkCloudWorkerClient.java index f113d7e..f145363 100644 --- a/src/main/java/com/staketab/minanames/client/IpfsZkCloudWorkerClient.java +++ b/src/main/java/com/staketab/minanames/client/IpfsZkCloudWorkerClient.java @@ -15,9 +15,9 @@ public class IpfsZkCloudWorkerClient { @Value("${zk-cloud-worker.ipfs-token}") private String token; - private static final String PATH_TO_ZK_CLOUD_WORKER = "/ipfs/{ipfs}?pinataGatewayToken={token}"; + private static final String PATH_TO_IPFS_ZK_CLOUD_WORKER = "/ipfs/{ipfs}?pinataGatewayToken={token}"; public IpfsZkCloudWorkerResponse getBlockByIpfs(String ipfs) { - return ipfsZkCloudWorkerRestTemplate.getForObject(PATH_TO_ZK_CLOUD_WORKER, IpfsZkCloudWorkerResponse.class, ipfs, token); + return ipfsZkCloudWorkerRestTemplate.getForObject(PATH_TO_IPFS_ZK_CLOUD_WORKER, IpfsZkCloudWorkerResponse.class, ipfs, token); } } diff --git a/src/main/java/com/staketab/minanames/config/security/ApiKeyRequestFilter.java b/src/main/java/com/staketab/minanames/config/security/ApiKeyRequestFilter.java index 414c37f..e86e17a 100644 --- a/src/main/java/com/staketab/minanames/config/security/ApiKeyRequestFilter.java +++ b/src/main/java/com/staketab/minanames/config/security/ApiKeyRequestFilter.java @@ -21,10 +21,8 @@ @Slf4j public class ApiKeyRequestFilter extends GenericFilterBean { private final ApiKeyRepository apiKeyRepository; - public static final String API_CREATE = "/api/domains/save"; - public static final String API_UPDATE = "/api/domains/edit"; - public static final String API_RESERVE = "/api/domains/reserve"; - public static final String API_RESERVE_APPLY = "/api/domains/reserve/apply"; + public static final String API_DOMAINS = "/api/domains"; + public static final String API_ACTIVITIES = "/api/activities"; public ApiKeyRequestFilter(ApiKeyRepository apiKeyRepository) { this.apiKeyRepository = apiKeyRepository; @@ -36,8 +34,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo String path = req.getRequestURI(); String key = req.getHeader(API_KEY_HEADER) == null ? "" : req.getHeader(API_KEY_HEADER); - if (!path.startsWith(API_CREATE) && !path.startsWith(API_UPDATE) && - !path.startsWith(API_RESERVE) && !path.startsWith(API_RESERVE_APPLY)) { + if (!path.startsWith(API_DOMAINS) && !path.startsWith(API_ACTIVITIES)) { filterChain.doFilter(servletRequest, servletResponse); return; } diff --git a/src/main/java/com/staketab/minanames/controller/DomainController.java b/src/main/java/com/staketab/minanames/controller/DomainController.java index c644771..4584ce2 100644 --- a/src/main/java/com/staketab/minanames/controller/DomainController.java +++ b/src/main/java/com/staketab/minanames/controller/DomainController.java @@ -7,6 +7,7 @@ import com.staketab.minanames.dto.DomainReservationDTO; import com.staketab.minanames.dto.DomainUpdateDTO; import com.staketab.minanames.dto.ReservedDomainDTO; +import com.staketab.minanames.dto.SimpleDomainDTO; import com.staketab.minanames.dto.request.BaseRequest; import com.staketab.minanames.dto.request.SearchParams; import com.staketab.minanames.dto.request.sort.DomainsSortColumn; @@ -66,6 +67,12 @@ public Page getAccountDomains(@Valid @ParameterObject BaseRequest return domainService.findAllByAccountPageable(request.withSortColumn(sortBy), accountAddress, searchParams); } + @GetMapping("/simple") + @Operation(summary = "getDomains", description = "Get a page of all domainDTOs.") + public Page getDomainDTOs(@Valid @ParameterObject BaseRequest request) { + return domainService.findAllSimpleDomainsByPageable(request.withSortColumn(DomainsSortColumn.START_TIMESTAMP)); + } + @PostMapping("/save") public ResponseEntity create(@RequestBody DomainReservationDTO domainRequest) { return ok(domainService.create(domainRequest)); @@ -110,4 +117,9 @@ public ResponseEntity isDomainNameReserved(@PathVariable Stri public ResponseEntity setDefaultDomain(@PathVariable String id) { return ok(domainService.setDefaultDomain(id)); } + + @PutMapping("/{id}/default/remove") + public ResponseEntity removeDefaultDomain(@PathVariable String id) { + return ok(domainService.removeDefaultDomain(id)); + } } diff --git a/src/main/java/com/staketab/minanames/dto/IpfsDomainMetadataNftMetadataZkDataDTO.java b/src/main/java/com/staketab/minanames/dto/IpfsDomainMetadataNftMetadataZkDataDTO.java index 5588d2f..09e4e75 100644 --- a/src/main/java/com/staketab/minanames/dto/IpfsDomainMetadataNftMetadataZkDataDTO.java +++ b/src/main/java/com/staketab/minanames/dto/IpfsDomainMetadataNftMetadataZkDataDTO.java @@ -1,6 +1,5 @@ package com.staketab.minanames.dto; -import com.fasterxml.jackson.annotation.JsonInclude; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/staketab/minanames/dto/SimpleDomainDTO.java b/src/main/java/com/staketab/minanames/dto/SimpleDomainDTO.java new file mode 100644 index 0000000..806f7bb --- /dev/null +++ b/src/main/java/com/staketab/minanames/dto/SimpleDomainDTO.java @@ -0,0 +1,18 @@ +package com.staketab.minanames.dto; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class SimpleDomainDTO { + + private String id; + private String ownerAddress; + private String ipfs; + private String domainName; + private String domainImg; + private long timestamp; + private boolean isDefault; + private String status; +} diff --git a/src/main/java/com/staketab/minanames/dto/request/sort/DomainsSortColumn.java b/src/main/java/com/staketab/minanames/dto/request/sort/DomainsSortColumn.java index 1e99210..fef6cd2 100644 --- a/src/main/java/com/staketab/minanames/dto/request/sort/DomainsSortColumn.java +++ b/src/main/java/com/staketab/minanames/dto/request/sort/DomainsSortColumn.java @@ -9,6 +9,7 @@ public enum DomainsSortColumn implements DbColumnProvider { AMOUNT("amount"), STATUS("status"), RESERVATION_TIMESTAMP("reservation_timestamp"), + START_TIMESTAMP("start_timestamp"), IS_SEND_TO_CLOUD_WORKER("is_send_to_cloud_worker"); private final String dbColumn; diff --git a/src/main/java/com/staketab/minanames/entity/ActivityStatus.java b/src/main/java/com/staketab/minanames/entity/ActivityStatus.java index 62b17b7..2ce2984 100644 --- a/src/main/java/com/staketab/minanames/entity/ActivityStatus.java +++ b/src/main/java/com/staketab/minanames/entity/ActivityStatus.java @@ -10,13 +10,14 @@ public enum ActivityStatus { APPLY_CART_RESERVED_DOMAINS("Apply cart reserved domains", null, false), SEND_DOMAIN_TO_ZK_CLOUD_WORKER("Send domain to zk cloud worker", null, false), SET_ACTIVE_STATUS_FOR_DOMAIN("Buy", "Price: %s MINA", true), - UPDATE_DOMAIN_IMAGE("Update record", "Image updated", true), + UPDATE_DOMAIN_IMAGE("Update image", "IPFS: %s", true), UPDATE_DOMAIN_DESCRIPTION("Update record", "Description updated", true), FAILED("Failed tx", null, false), INCORRECT_AMOUNT("Incorrect amount", null, false), APPLIED("Applied tx", null, false), REMOVE_RESERVATION("Remove reservation", null, false), SET_DEFAULT_DOMAIN("Set as default domain", null, true), + REMOVE_DEFAULT_DOMAIN("Remove default domain", null, true), REMOVE_CART_RESERVATION("Remove cart reservation", null, false); private final String message; diff --git a/src/main/java/com/staketab/minanames/entity/DomainEntity.java b/src/main/java/com/staketab/minanames/entity/DomainEntity.java index 39a5937..8803371 100644 --- a/src/main/java/com/staketab/minanames/entity/DomainEntity.java +++ b/src/main/java/com/staketab/minanames/entity/DomainEntity.java @@ -63,7 +63,7 @@ public class DomainEntity { @Column(name = "name", columnDefinition = "TEXT", unique = true) private String domainName; @Column(name = "img", columnDefinition = "TEXT") - private String domainImg; //remove? + private String domainImg; private Long amount; diff --git a/src/main/java/com/staketab/minanames/repository/DomainRepository.java b/src/main/java/com/staketab/minanames/repository/DomainRepository.java index fc5da1c..cfa0e33 100644 --- a/src/main/java/com/staketab/minanames/repository/DomainRepository.java +++ b/src/main/java/com/staketab/minanames/repository/DomainRepository.java @@ -25,6 +25,14 @@ public interface DomainRepository extends JpaRepository { where (:searchStr is null or name = :searchStr)""") Page findAllDomains(String searchStr, Pageable buildPageable); + @Query(nativeQuery = true, + value = """ + select * + from domains + where status = 'ACTIVE' + """) + Page findAllDomains(Pageable buildPageable); + @Query(nativeQuery = true, value = """ select * @@ -59,9 +67,18 @@ public interface DomainRepository extends JpaRepository { ELSE false END WHERE owner_address = (select owner_address from domains where id = :id) - """) + """) int setDefaultDomain(String id); + @Modifying + @Query(nativeQuery = true, + value = """ + UPDATE domains + SET is_default = false + WHERE id = :id + """) + int removeDefaultDomain(String id); + Optional findDomainEntityByDomainName(String domainName); Optional findDomainEntityByDomainNameAndOwnerAddress(String domainName, String ownerAddress); diff --git a/src/main/java/com/staketab/minanames/service/DomainService.java b/src/main/java/com/staketab/minanames/service/DomainService.java index 3f88e06..6f0810a 100644 --- a/src/main/java/com/staketab/minanames/service/DomainService.java +++ b/src/main/java/com/staketab/minanames/service/DomainService.java @@ -7,6 +7,7 @@ import com.staketab.minanames.dto.DomainReservationDTO; import com.staketab.minanames.dto.DomainUpdateDTO; import com.staketab.minanames.dto.ReservedDomainDTO; +import com.staketab.minanames.dto.SimpleDomainDTO; import com.staketab.minanames.dto.request.BaseRequest; import com.staketab.minanames.dto.request.SearchParams; import com.staketab.minanames.entity.DomainEntity; @@ -15,6 +16,8 @@ public interface DomainService { Page findAllByPageable(BaseRequest request, SearchParams searchParams); + Page findAllSimpleDomainsByPageable(BaseRequest request); + Page findAllByAccountPageable(BaseRequest request, String accountAddress, SearchParams searchParams); DomainEntity create(DomainReservationDTO domainRequest); @@ -35,6 +38,8 @@ public interface DomainService { Boolean setDefaultDomain(String id); + Boolean removeDefaultDomain(String id); + void removeReservedDomains(); void removeCartReservedDomains(); diff --git a/src/main/java/com/staketab/minanames/service/impl/ActivityServiceImpl.java b/src/main/java/com/staketab/minanames/service/impl/ActivityServiceImpl.java index 13e1a53..3d9fb7f 100644 --- a/src/main/java/com/staketab/minanames/service/impl/ActivityServiceImpl.java +++ b/src/main/java/com/staketab/minanames/service/impl/ActivityServiceImpl.java @@ -53,7 +53,7 @@ private ActivityEntity buildActivityEntity(DomainEntity domainEntity, ActivitySt .ownerAddress(domainEntity.getOwnerAddress()) .timestamp(System.currentTimeMillis()) .ipfs(domainEntity.getIpfs()) - .amount(domainEntity.getAmount()) + .amount(equals ? domainEntity.getAmount() : null) .isShow(status.isShow()) .build(); } 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 c01f94c..337535d 100644 --- a/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java +++ b/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java @@ -9,6 +9,7 @@ import com.staketab.minanames.dto.DomainUpdateDTO; import com.staketab.minanames.dto.OldMetadataDTO; import com.staketab.minanames.dto.ReservedDomainDTO; +import com.staketab.minanames.dto.SimpleDomainDTO; import com.staketab.minanames.dto.request.BaseRequest; import com.staketab.minanames.dto.request.SearchParams; import com.staketab.minanames.entity.DomainEntity; @@ -23,6 +24,7 @@ import com.staketab.minanames.service.TxService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -41,6 +43,7 @@ import static com.staketab.minanames.entity.ActivityStatus.CREATE; import static com.staketab.minanames.entity.ActivityStatus.DELETE_CART_RESERVE; import static com.staketab.minanames.entity.ActivityStatus.REMOVE_CART_RESERVATION; +import static com.staketab.minanames.entity.ActivityStatus.REMOVE_DEFAULT_DOMAIN; import static com.staketab.minanames.entity.ActivityStatus.REMOVE_RESERVATION; import static com.staketab.minanames.entity.ActivityStatus.SET_DEFAULT_DOMAIN; import static com.staketab.minanames.entity.DomainStatus.PENDING; @@ -53,6 +56,11 @@ @Slf4j public class DomainServiceImpl implements DomainService { + private static final String QUERY_PARAM_TO_IPFS_ZK_CLOUD_WORKER = "?pinataGatewayToken=%s"; + + @Value("${zk-cloud-worker.ipfs-token}") + private String token; + private final ActivityService activityService; private final DomainRepository domainRepository; private final TxService txService; @@ -62,6 +70,12 @@ public Page findAllByPageable(BaseRequest request, SearchParams se return domainRepository.findAllDomains(searchParams.getSearchStr(), request.buildPageable()); } + @Override + public Page findAllSimpleDomainsByPageable(BaseRequest request) { + return domainRepository.findAllDomains(request.buildPageable()) + .map(this::buildSimpleDomainDTO); + } + @Override public Page findAllByAccountPageable(BaseRequest request, String accountAddress, SearchParams searchParams) { return domainRepository.findAllDomainsByAccount(searchParams.getSearchStr(), accountAddress, request.buildPageable()); @@ -171,6 +185,17 @@ public Boolean setDefaultDomain(String id) { return result; } + @Override + @Transactional + public Boolean removeDefaultDomain(String id) { + boolean result = domainRepository.removeDefaultDomain(id) > 0; + if (result) { + DomainEntity domainEntity = domainRepository.findById(id).get(); + activityService.saveActivity(domainEntity, REMOVE_DEFAULT_DOMAIN, null); + } + return result; + } + @Override @Transactional public void removeReservedDomains() { @@ -266,6 +291,20 @@ private DomainDTO buildDomainDTO(DomainEntity domainEntity) { .build(); } + private SimpleDomainDTO buildSimpleDomainDTO(DomainEntity domainEntity) { + String image = domainEntity.getDomainImg() + String.format(QUERY_PARAM_TO_IPFS_ZK_CLOUD_WORKER, token); + return SimpleDomainDTO.builder() + .id(domainEntity.getId()) + .domainName(domainEntity.getDomainName()) + .isDefault(domainEntity.getIsDefault()) + .ipfs(domainEntity.getIpfs()) + .ownerAddress(domainEntity.getOwnerAddress()) + .timestamp(domainEntity.getStartTimestamp()) + .status(domainEntity.getDomainStatus()) + .domainImg(domainEntity.getDomainImg() != null ? image : null) + .build(); + } + private OldMetadataDTO buildOldMetadataDTO(DomainEntity domainEntity) { return OldMetadataDTO.builder() .xTwitter(domainEntity.getXTwitter()) diff --git a/src/main/java/com/staketab/minanames/service/impl/ZkCloudWorkerServiceImpl.java b/src/main/java/com/staketab/minanames/service/impl/ZkCloudWorkerServiceImpl.java index 1bb3c42..ddbfb2d 100644 --- a/src/main/java/com/staketab/minanames/service/impl/ZkCloudWorkerServiceImpl.java +++ b/src/main/java/com/staketab/minanames/service/impl/ZkCloudWorkerServiceImpl.java @@ -64,9 +64,14 @@ @RequiredArgsConstructor public class ZkCloudWorkerServiceImpl implements ZkCloudWorkerService { + private static final String PATH_TO_IPFS_ZK_CLOUD_WORKER = "/ipfs/%s"; + @Value("${zk-cloud-worker.mns-contract}") private String mnsContract; + @Value("${zk-cloud-worker.ipfs-url}") + private String ipfsZkCloudWorkerUrl; + private final Gson gson; private final ObjectMapper objectMapper; private final ActivityService activityService; @@ -220,20 +225,23 @@ private void updateDomain(ZkCloudWorkerBlocksResponse finalBlock, String name, S return; } DomainEntity domainEntity = domain.get(); - setMetadata(newMetadata, domainEntity); + setMetadata(newMetadata, domainEntity, finalBlock.getIpfs()); domainEntity.setDomainMetadata(newMetadata); domainEntity.setBlockNumber(finalBlock.getBlockNumber()); domainEntity.setIpfs(finalBlock.getIpfs()); domainRepository.save(domainEntity); } - private void setMetadata(String newMetadata, DomainEntity domainEntity) { + private void setMetadata(String newMetadata, DomainEntity domainEntity, String blockIpfs) { IpfsDomainMetadataZkDataDTO domainMetadata = getDomainMetadata(newMetadata); Map properties = domainMetadata.getNft().getProperties(); IpfsDomainMetadataNftMetadataZkDataDTO image = properties.get(IpfsMetadataCloudWorkerProperty.IMAGE.getName()); if (image != null) { + String storage = image.getLinkedObject().getStorage(); + String imgIpfs = ipfsZkCloudWorkerUrl + String.format(PATH_TO_IPFS_ZK_CLOUD_WORKER, storage.substring(2)); + domainEntity.setDomainImg(imgIpfs); domainEntity.setIpfsImg(mapIpfsImgToString(image)); - activityService.saveActivity(domainEntity, UPDATE_DOMAIN_IMAGE, UPDATE_DOMAIN_IMAGE.getDetails()); + activityService.saveActivity(domainEntity, UPDATE_DOMAIN_IMAGE, String.format(UPDATE_DOMAIN_IMAGE.getDetails(), blockIpfs)); } IpfsDomainMetadataNftMetadataZkDataDTO description = properties.get(IpfsMetadataCloudWorkerProperty.DESCRIPTION.getName()); if (description != null) {