Skip to content

Commit

Permalink
Merge pull request #52 from Staketab/dev
Browse files Browse the repository at this point in the history
add logic for update domain metadata
  • Loading branch information
MrFoxogen authored May 3, 2024
2 parents 95c7ffa + d8120ef commit a51ec0f
Show file tree
Hide file tree
Showing 21 changed files with 267 additions and 129 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/staketab/minanames/dto/DomainDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public class DomainDTO {
private DomainStatus domainStatus;
private Boolean isSendToCloudWorker;
private Boolean isDefault;
private OldMetadataDTO oldMetadata;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.staketab.minanames.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class IpfsZkCloudWorkerTransactionDataResponse {
private String operation;
private String name;
private String address;
private long expiry;
private String newDomain;
private String oldDomain;
private String metadata;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
@NoArgsConstructor
public class IpfsZkCloudWorkerTransactionFieldResponse {
private String tx;
private String oldDomain;
private String signature;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public class IpfsZkCloudWorkerTransactionResponse {
private String transaction;
private long timeReceived;
private String status;
private String newDomain;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
public class IpfsZkCloudWorkerTransactionWrapperResponse {
private IpfsZkCloudWorkerTransactionResponse tx;
private IpfsZkCloudWorkerTransactionFieldResponse fields;
private String newDomain;
}
20 changes: 20 additions & 0 deletions src/main/java/com/staketab/minanames/dto/OldMetadataDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.staketab.minanames.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@AllArgsConstructor
public class OldMetadataDTO {
private String domainMetadata;
private String ipfsImg;
private String website;
private String email;
private String discord;
private String github;
private String xTwitter;
private String telegram;
private String description;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.staketab.minanames.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ZkCloudWorkerTransactionMetadata {
private String ipfsImgUrl;
private String website;
private String email;
private String discord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "log_info")
public class LogInfoEntity {
@Table(name = "activity")
public class ActivityEntity {

@Id
@UuidGenerator
private String id;

@Column(name = "status", columnDefinition = "TEXT")
private String logInfoStatus;
private String status;

@Column(name = "owner_address", columnDefinition = "TEXT")
private String ownerAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import lombok.Getter;

@Getter
public enum LogInfoStatus {
public enum ActivityStatus {
CREATE("Create new domain"),
CART_RESERVE("Reserve new domain for cart"),
DELETE_CART_RESERVE("Remove reserved domain for cart"),
APPLY_CART_RESERVED_DOMAINS("Apply cart reserved domains"),
SEND_DOMAIN_TO_ZK_CLOUD_WORKER("Send domain to zk cloud worker"),
SET_ACTIVE_STATUS_FOR_DOMAIN("Set active status for domain"),
UPDATE_DOMAIN("Update domain"),
FAILED("Failed tx"),
INCORRECT_AMOUNT("Incorrect amount"),
APPLIED("Applied tx"),
Expand All @@ -18,7 +19,7 @@ public enum LogInfoStatus {

private final String message;

LogInfoStatus(String message) {
ActivityStatus(String message) {
this.message = message;
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/staketab/minanames/entity/DomainEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public class DomainEntity {
private String zkTxId;
@Column(name = "block_number", columnDefinition = "integer default 0")
private Integer blockNumber;
@Column(name = "domain_metadata", columnDefinition = "TEXT")
private String domainMetadata;
@Column(name = "ipfs", columnDefinition = "TEXT")
private String ipfs;
@Column(name = "ipfs_img_url", columnDefinition = "TEXT")
private String ipfsImgUrl;
@Column(name = "ipfs_img", columnDefinition = "TEXT")
private String ipfsImg;
@Column(name = "website", columnDefinition = "TEXT")
private String website;
@Column(name = "email", columnDefinition = "TEXT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.Getter;

import java.util.Arrays;

@Getter
public enum ZkCloudWorkerTxOperation {
ADD("add"),
Expand All @@ -14,4 +16,11 @@ public enum ZkCloudWorkerTxOperation {
ZkCloudWorkerTxOperation(String name) {
this.name = name;
}

public static ZkCloudWorkerTxOperation fromString(String name){
return Arrays.stream(ZkCloudWorkerTxOperation.values())
.filter(v -> name.equals(v.getName()))
.findFirst()
.orElseThrow();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.staketab.minanames.repository;

import com.staketab.minanames.entity.LogInfoEntity;
import com.staketab.minanames.entity.ActivityEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LogInfoRepository extends JpaRepository<LogInfoEntity, String> {
public interface ActivityRepository extends JpaRepository<ActivityEntity, String> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public interface DomainRepository extends JpaRepository<DomainEntity, String> {

Optional<DomainEntity> findDomainEntityByDomainName(String domainName);

Optional<DomainEntity> findDomainEntityByDomainNameAndOwnerAddress(String domainName, String ownerAddress);

@Modifying
@Transactional
void deleteAllByTransactionIn(Collection<PayableTransactionEntity> transaction);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/staketab/minanames/service/ActivityService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.staketab.minanames.service;

import com.staketab.minanames.entity.DomainEntity;
import com.staketab.minanames.entity.ActivityStatus;

import java.util.List;

public interface ActivityService {

void saveActivity(DomainEntity domainEntity, ActivityStatus status);

void saveAllActivities(List<DomainEntity> domainEntities, ActivityStatus status);
}
13 changes: 0 additions & 13 deletions src/main/java/com/staketab/minanames/service/LogInfoService.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.staketab.minanames.service.impl;

import com.staketab.minanames.entity.DomainEntity;
import com.staketab.minanames.entity.ActivityEntity;
import com.staketab.minanames.entity.ActivityStatus;
import com.staketab.minanames.repository.ActivityRepository;
import com.staketab.minanames.service.ActivityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ActivityServiceImpl implements ActivityService {

private final ActivityRepository activityRepository;

@Override
public void saveActivity(DomainEntity domainEntity, ActivityStatus status) {
activityRepository.save(buildActivityEntity(domainEntity, status));
}

@Override
public void saveAllActivities(List<DomainEntity> domainEntities, ActivityStatus status) {
List<ActivityEntity> activityEntities = domainEntities
.stream()
.map(domainEntity -> buildActivityEntity(domainEntity, status))
.toList();
activityRepository.saveAll(activityEntities);
}

private ActivityEntity buildActivityEntity(DomainEntity domainEntity, ActivityStatus status) {
return ActivityEntity.builder()
.status(status.name())
.txHash(domainEntity.getTransaction().getTxHash())
.domainName(domainEntity.getDomainName())
.amount(domainEntity.getAmount())
.ownerAddress(domainEntity.getOwnerAddress())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.staketab.minanames.dto.DomainDTO;
import com.staketab.minanames.dto.DomainReservationDTO;
import com.staketab.minanames.dto.DomainUpdateDTO;
import com.staketab.minanames.dto.OldMetadataDTO;
import com.staketab.minanames.dto.ReservedDomainDTO;
import com.staketab.minanames.dto.request.BaseRequest;
import com.staketab.minanames.dto.request.SearchParams;
Expand All @@ -17,8 +18,8 @@
import com.staketab.minanames.exception.DuplicateKeyException;
import com.staketab.minanames.exception.NotFoundException;
import com.staketab.minanames.repository.DomainRepository;
import com.staketab.minanames.service.ActivityService;
import com.staketab.minanames.service.DomainService;
import com.staketab.minanames.service.LogInfoService;
import com.staketab.minanames.service.TxService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -35,14 +36,14 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.staketab.minanames.entity.ActivityStatus.APPLY_CART_RESERVED_DOMAINS;
import static com.staketab.minanames.entity.ActivityStatus.CART_RESERVE;
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_RESERVATION;
import static com.staketab.minanames.entity.DomainStatus.PENDING;
import static com.staketab.minanames.entity.DomainStatus.RESERVED;
import static com.staketab.minanames.entity.LogInfoStatus.APPLY_CART_RESERVED_DOMAINS;
import static com.staketab.minanames.entity.LogInfoStatus.CART_RESERVE;
import static com.staketab.minanames.entity.LogInfoStatus.CREATE;
import static com.staketab.minanames.entity.LogInfoStatus.DELETE_CART_RESERVE;
import static com.staketab.minanames.entity.LogInfoStatus.REMOVE_CART_RESERVATION;
import static com.staketab.minanames.entity.LogInfoStatus.REMOVE_RESERVATION;
import static com.staketab.minanames.utils.Constants.DEFAULT_DENOMINATION;
import static com.staketab.minanames.utils.Constants.MINA_DENOMINATION;

Expand All @@ -51,7 +52,7 @@
@Slf4j
public class DomainServiceImpl implements DomainService {

private final LogInfoService logInfoService;
private final ActivityService activityService;
private final DomainRepository domainRepository;
private final TxService txService;

Expand All @@ -73,7 +74,7 @@ public DomainEntity create(DomainReservationDTO request) {
throw new DuplicateKeyException(String.format("Domain already exist with name: %s", domainName));
});
DomainEntity domain = buildDomainEntity(request);
logInfoService.saveLogInfo(domain, CREATE);
activityService.saveActivity(domain, CREATE);
return domainRepository.save(domain);
}

Expand All @@ -86,7 +87,7 @@ public DomainEntity reserve(DomainCartReservationDTO request) {
throw new DuplicateKeyException(String.format("Domain already exist with name: %s", domainName));
});
DomainEntity domain = buildDomainEntityReserve(request);
logInfoService.saveLogInfo(domain, CART_RESERVE);
activityService.saveActivity(domain, CART_RESERVE);
DomainEntity saved = domainRepository.save(domain);
List<DomainEntity> domainEntities = domainRepository.findAllCartsReservedDomains(saved.getId())
.stream()
Expand Down Expand Up @@ -129,7 +130,7 @@ public void applyReservedDomain(ApplyReservedDomainDTO domainRequest) {
@Override
public void removeReservedDomain(String id) {
domainRepository.findById(id).ifPresent(domainEntity -> {
logInfoService.saveLogInfo(domainEntity, DELETE_CART_RESERVE);
activityService.saveActivity(domainEntity, DELETE_CART_RESERVE);
domainRepository.delete(domainEntity);
});
}
Expand Down Expand Up @@ -170,7 +171,7 @@ public void removeReservedDomains() {
LocalDateTime localDateTime = LocalDateTime.now().minusDays(1);
long currentTimestamp = Timestamp.valueOf(localDateTime).getTime();
List<DomainEntity> domainEntities = domainRepository.findAllByReservationTimestampLessThan(currentTimestamp, PENDING.name());
logInfoService.saveAllLogInfos(domainEntities, REMOVE_RESERVATION);
activityService.saveAllActivities(domainEntities, REMOVE_RESERVATION);
domainRepository.deleteAll(domainEntities);
}

Expand All @@ -179,7 +180,7 @@ public void removeCartReservedDomains() {
LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(10);
long currentTimestamp = Timestamp.valueOf(localDateTime).getTime();
List<DomainEntity> domainEntities = domainRepository.findAllByReservationTimestampLessThan(currentTimestamp, RESERVED.name());
logInfoService.saveAllLogInfos(domainEntities, REMOVE_CART_RESERVATION);
activityService.saveAllActivities(domainEntities, REMOVE_CART_RESERVATION);
domainRepository.deleteAll(domainEntities);
}

Expand All @@ -195,7 +196,7 @@ private void saveUpdatedDomains(List<DomainEntity> domains, Map<String, CartRese
domainEntity.setDomainStatus(PENDING.name());
domainEntity.setEndTimestamp(endTimestamp);
}).toList();
logInfoService.saveAllLogInfos(domainEntities, APPLY_CART_RESERVED_DOMAINS);
activityService.saveAllActivities(domainEntities, APPLY_CART_RESERVED_DOMAINS);
domainRepository.saveAll(domainEntities);
}

Expand Down Expand Up @@ -255,6 +256,21 @@ private DomainDTO buildDomainDTO(DomainEntity domainEntity) {
.ownerAddress(domainEntity.getOwnerAddress())
.reservationTimestamp(domainEntity.getReservationTimestamp())
.transaction(domainEntity.getTransaction().getTxHash())
.oldMetadata(buildOldMetadataDTO(domainEntity))
.build();
}

private OldMetadataDTO buildOldMetadataDTO(DomainEntity domainEntity) {
return OldMetadataDTO.builder()
.xTwitter(domainEntity.getXTwitter())
.email(domainEntity.getEmail())
.description(domainEntity.getDescription())
.discord(domainEntity.getDiscord())
.github(domainEntity.getGithub())
.website(domainEntity.getWebsite())
.telegram(domainEntity.getTelegram())
.ipfsImg(domainEntity.getIpfsImg())
.domainMetadata(domainEntity.getDomainMetadata())
.build();
}
}
Loading

0 comments on commit a51ec0f

Please sign in to comment.