forked from Qortal/qortal
Added API call to list registered names that are for sale
This commit is contained in:
parent
e04f9df0dc
commit
c81a3838fc
@ -350,4 +350,34 @@ public class NamesResource {
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/forsale")
|
||||
@Operation(
|
||||
summary = "List all registered names up for sale",
|
||||
responses = {
|
||||
@ApiResponse(
|
||||
description = "registered name info",
|
||||
content = @Content(
|
||||
mediaType = MediaType.APPLICATION_JSON,
|
||||
array = @ArraySchema(schema = @Schema(implementation = NameData.class))
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
@ApiErrors({ApiError.REPOSITORY_ISSUE})
|
||||
public List<NameData> getNamesForSale(@Parameter(ref = "limit") @QueryParam("limit") int limit, @Parameter(ref = "offset") @QueryParam("offset") int offset) {
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
List<NameData> names = repository.getNameRepository().getNamesForSale();
|
||||
|
||||
// Pagination would take effect here (or as part of the repository access)
|
||||
int fromIndex = Integer.min(offset, names.size());
|
||||
int toIndex = limit == 0 ? names.size() : Integer.min(fromIndex + limit, names.size());
|
||||
names = names.subList(fromIndex, toIndex);
|
||||
|
||||
return names;
|
||||
} catch (DataException e) {
|
||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,9 @@ import java.math.BigDecimal;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlTransient;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
// All properties to be converted to JSON via JAX-RS
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@ -15,6 +18,9 @@ public class NameData {
|
||||
private String data;
|
||||
private long registered;
|
||||
private Long updated;
|
||||
// No need to expose this via API
|
||||
@XmlTransient
|
||||
@Schema(hidden = true)
|
||||
private byte[] reference;
|
||||
private boolean isForSale;
|
||||
private BigDecimal salePrice;
|
||||
|
@ -12,6 +12,8 @@ public interface NameRepository {
|
||||
|
||||
public List<NameData> getAllNames() throws DataException;
|
||||
|
||||
public List<NameData> getNamesForSale() throws DataException;
|
||||
|
||||
public List<NameData> getNamesByOwner(String address) throws DataException;
|
||||
|
||||
public void save(NameData nameData) throws DataException;
|
||||
|
@ -86,6 +86,38 @@ public class HSQLDBNameRepository implements NameRepository {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NameData> getNamesForSale() throws DataException {
|
||||
List<NameData> names = new ArrayList<>();
|
||||
|
||||
try (ResultSet resultSet = this.repository
|
||||
.checkedExecute("SELECT name, data, owner, registered, updated, reference, sale_price FROM Names WHERE is_for_sale = TRUE")) {
|
||||
if (resultSet == null)
|
||||
return names;
|
||||
|
||||
do {
|
||||
String name = resultSet.getString(1);
|
||||
String data = resultSet.getString(2);
|
||||
String owner = resultSet.getString(3);
|
||||
long registered = resultSet.getTimestamp(4, Calendar.getInstance(HSQLDBRepository.UTC)).getTime();
|
||||
|
||||
// Special handling for possibly-NULL "updated" column
|
||||
Timestamp updatedTimestamp = resultSet.getTimestamp(5, Calendar.getInstance(HSQLDBRepository.UTC));
|
||||
Long updated = updatedTimestamp == null ? null : updatedTimestamp.getTime();
|
||||
|
||||
byte[] reference = resultSet.getBytes(6);
|
||||
boolean isForSale = true;
|
||||
BigDecimal salePrice = resultSet.getBigDecimal(7);
|
||||
|
||||
names.add(new NameData(owner, name, data, registered, updated, reference, isForSale, salePrice));
|
||||
} while (resultSet.next());
|
||||
|
||||
return names;
|
||||
} catch (SQLException e) {
|
||||
throw new DataException("Unable to fetch names from repository", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NameData> getNamesByOwner(String owner) throws DataException {
|
||||
List<NameData> names = new ArrayList<>();
|
||||
|
Loading…
Reference in New Issue
Block a user