Skip to content

Commit

Permalink
Feat/adding entity object (#10)
Browse files Browse the repository at this point in the history
* chore: added dependencies and code to cater for entity objects

* chore: test persistence

* chore: removed unused DAO class

* chore: fixed issues w/ validation and ovverriding rules in subclasses

* chore: restored GitService
  • Loading branch information
nemo83 authored Feb 9, 2024
1 parent a8d3c80 commit c09ab92
Show file tree
Hide file tree
Showing 18 changed files with 315 additions and 118 deletions.
8 changes: 8 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cardanofoundation.tokenmetadata.registry.api.controller;

import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.tokenmetadata.registry.api.indexer.V1ApiMetadataIndexer;
import org.cardanofoundation.tokenmetadata.registry.api.model.rest.BatchRequest;
Expand All @@ -26,7 +25,7 @@ public class MetadataApiController implements MetadataApi {
private V1ApiMetadataIndexer v1ApiMetadataIndexer;

@Override
public ResponseEntity<BatchResponse> getSubjects(@NotNull final BatchRequest body) {
public ResponseEntity<BatchResponse> getSubjects(final BatchRequest body) {
try {
final Map<String, TokenMetadata> subjects = v1ApiMetadataIndexer.findSubjectsSelectProperties(
body.getSubjects(),
Expand All @@ -44,7 +43,7 @@ public ResponseEntity<BatchResponse> getSubjects(@NotNull final BatchRequest bod
}

@Override
public ResponseEntity<TokenMetadata> getAllPropertiesForSubject(@NotNull final String subject) {
public ResponseEntity<TokenMetadata> getAllPropertiesForSubject(final String subject) {
try {
return v1ApiMetadataIndexer
.findSubject(subject)
Expand All @@ -56,7 +55,7 @@ public ResponseEntity<TokenMetadata> getAllPropertiesForSubject(@NotNull final S
}

@Override
public ResponseEntity<TokenMetadata> getPropertyForSubject(@NotNull final String subject, @NotNull final String property) {
public ResponseEntity<TokenMetadata> getPropertyForSubject(final String subject, final String property) {
try {
return v1ApiMetadataIndexer
.findSubjectSelectProperties(subject, List.of(property))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public interface V2Api {
value = "/v2/subjects/{subject}",
produces = {"application/json;charset=utf-8"})
ResponseEntity<TokenMetadata> deleteSubjectV2(
@NotNull
@Parameter(
name = "subject",
required = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ public ResponseEntity verifySubjectV2(final String subject, final TokenMetadata
}

@Override
public ResponseEntity<TokenMetadata> deleteSubjectV2(@NotNull final String subject,
@NotNull final String signature,
@NotNull final String vkey) {
public ResponseEntity<TokenMetadata> deleteSubjectV2(final String subject,
final String signature,
final String vkey) {
// 1. verify signature (which is sig(subject | "VOID")) with given vkey
return ResponseEntity.status(HttpStatus.NOT_IMPLEMENTED).build();
}
Expand Down
4 changes: 4 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.cardanofoundation.tokenmetadata.registry.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.cardanofoundation.tokenmetadata.registry.model.Mapping;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.LocalDateTime;
import java.util.Objects;

@Entity
@Table(name = "logo")
@Getter
@Setter
public class TokenLogo {

@Id
private String subject;

private String logo;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TokenLogo tokenLogo = (TokenLogo) o;
return Objects.equals(subject, tokenLogo.subject);
}

@Override
public int hashCode() {
return Objects.hash(subject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.cardanofoundation.tokenmetadata.registry.entity;

import com.fasterxml.jackson.databind.JsonNode;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.cardanofoundation.tokenmetadata.registry.model.Mapping;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.LocalDateTime;
import java.util.Objects;

@Entity
@Table(name = "metadata")
@Getter
@Setter
public class TokenMetadata {

@Id
private String subject;

private String policy;

private String name;

private String ticker;

private String url;

private String description;

private Long decimals;

@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime updated;

private String updatedBy;

@JdbcTypeCode(SqlTypes.JSON)
private Mapping properties;


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TokenMetadata that = (TokenMetadata) o;
return Objects.equals(subject, that.subject);
}

@Override
public int hashCode() {
return Objects.hash(subject);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.cardanofoundation.tokenmetadata.registry.repository;

import org.cardanofoundation.tokenmetadata.registry.entity.TokenLogo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TokenLogoRepository extends JpaRepository<TokenLogo, String> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.cardanofoundation.tokenmetadata.registry.repository;

import org.cardanofoundation.tokenmetadata.registry.entity.TokenMetadata;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TokenMetadataRepository extends JpaRepository<TokenMetadata, String> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,36 @@

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.tokenmetadata.registry.model.Item;
import org.cardanofoundation.tokenmetadata.registry.model.Mapping;
import org.cardanofoundation.tokenmetadata.registry.persistence.TokenMetadataDao;
import org.cardanofoundation.tokenmetadata.registry.repository.TokenLogoRepository;
import org.cardanofoundation.tokenmetadata.registry.repository.TokenMetadataRepository;
import org.cardanofoundation.tokenmetadata.registry.util.MappingsUtil;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Optional;

import static org.cardanofoundation.tokenmetadata.registry.util.MappingsUtil.toTokenLogo;

@Component
@Slf4j
@AllArgsConstructor
public class TokenMetadataService {

private final TokenMetadataDao tokenMetadataDao;

private final TokenMetadataRepository tokenMetadataRepository;

private final TokenLogoRepository tokenLogoRepository;

public void insertMapping(Mapping mapping, LocalDateTime updatedAt, String updateBy) {
tokenMetadataDao.insertTokenMetadata(mapping.subject(),
Optional.ofNullable(mapping.policy()),
Optional.ofNullable(mapping.name()).map(Item::value),
Optional.ofNullable(mapping.ticker()).map(Item::value),
Optional.ofNullable(mapping.url()).map(Item::value),
Optional.ofNullable(mapping.description()).map(Item::value),
Optional.ofNullable(mapping.decimals()).map(Item::value).map(Integer::valueOf),
Timestamp.valueOf(updatedAt),
updateBy,
mapping
);
var tokenMetadata = MappingsUtil.toTokenMetadata(mapping, updateBy, updatedAt);
tokenMetadataRepository.save(tokenMetadata);

}

public void insertLogo(Mapping mapping) {
tokenMetadataDao.insertTokenLogo(mapping.subject(), Optional.ofNullable(mapping.logo()).map(Item::value));
var tokenLogo = toTokenLogo(mapping);
tokenLogoRepository.save(tokenLogo);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;

@Service
@Slf4j
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.cardanofoundation.tokenmetadata.registry.util;

import org.cardanofoundation.tokenmetadata.registry.entity.TokenLogo;
import org.cardanofoundation.tokenmetadata.registry.entity.TokenMetadata;
import org.cardanofoundation.tokenmetadata.registry.model.Item;
import org.cardanofoundation.tokenmetadata.registry.model.Mapping;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.function.Function;

public class MappingsUtil {

public static TokenMetadata toTokenMetadata(Mapping mapping, String updateBy, LocalDateTime updatedAt) {
var tokenMetadata = new TokenMetadata();
tokenMetadata.setSubject(mapping.subject());
tokenMetadata.setPolicy(mapping.policy());
tokenMetadata.setName(getValue(mapping.name()));
tokenMetadata.setTicker(getValue(mapping.ticker()));
tokenMetadata.setUrl(getValue(mapping.url()));
tokenMetadata.setDescription(getValue(mapping.description()));
tokenMetadata.setDecimals(getValue(mapping.decimals(), Long::valueOf));
tokenMetadata.setUpdated(updatedAt);
tokenMetadata.setUpdatedBy(updateBy);
tokenMetadata.setProperties(mapping);
return tokenMetadata;
}

public static TokenLogo toTokenLogo(Mapping mapping) {
var tokenLogo = new TokenLogo();
tokenLogo.setSubject(mapping.subject());
tokenLogo.setLogo(getValue(mapping.logo()));
return tokenLogo;
}


private static String getValue(Item item) {
return getValue(item, Function.identity());
}

private static <T> T getValue(Item item, Function<String, T> f) {
return Optional.ofNullable(item).map(Item::value).map(f).orElse(null);
}


}
Loading

0 comments on commit c09ab92

Please sign in to comment.