Skip to content

Commit

Permalink
get all
Browse files Browse the repository at this point in the history
  • Loading branch information
ElisKina-dev committed Nov 13, 2023
1 parent 3914a3a commit c418fe7
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package it.gov.pagopa.atmlayer.service.model.dto;

import it.gov.pagopa.atmlayer.service.model.enumeration.NoDeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.enumeration.S3ResourceTypeEnum;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.FormParam;
import java.io.File;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.File;

@Data
@NoArgsConstructor
public class ResourceCreationDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
import it.gov.pagopa.atmlayer.service.model.entity.ResourceEntity;
import it.gov.pagopa.atmlayer.service.model.model.ResourceDTO;
import it.gov.pagopa.atmlayer.service.model.utils.BpmnUtils;
import org.mapstruct.Mapper;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.stream.Collectors;
import org.mapstruct.Mapper;

@Mapper(componentModel = "cdi")
public abstract class ResourceEntityMapper {
public ResourceEntity toEntityCreation(ResourceCreationDto resourceCreationDto) throws NoSuchAlgorithmException, IOException {
ResourceEntity resourceEntity = new ResourceEntity();
resourceEntity.setSha256(BpmnUtils.calculateSha256(resourceCreationDto.getFile()));
resourceEntity.setNoDeployableResourceType(resourceCreationDto.getResourceType());
resourceEntity.setFileName(resourceCreationDto.getFilename());
return resourceEntity;
}

public abstract ResourceDTO toDTO(ResourceEntity resourceEntity);
public ResourceEntity toEntityCreation(ResourceCreationDto resourceCreationDto)
throws NoSuchAlgorithmException, IOException {
ResourceEntity resourceEntity = new ResourceEntity();
resourceEntity.setSha256(BpmnUtils.calculateSha256(resourceCreationDto.getFile()));
resourceEntity.setNoDeployableResourceType(resourceCreationDto.getResourceType());
resourceEntity.setFileName(resourceCreationDto.getFilename());
return resourceEntity;
}

public abstract ResourceDTO toDTO(ResourceEntity resourceEntity);

public List<ResourceDTO> toDTOList(List<ResourceEntity> list) {
return list.stream().map(this::toDTO).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package it.gov.pagopa.atmlayer.service.model.model;

import it.gov.pagopa.atmlayer.service.model.enumeration.NoDeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.enumeration.S3ResourceTypeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.util.UUID;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.smallrye.common.annotation.NonBlocking;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.unchecked.Unchecked;
import it.gov.pagopa.atmlayer.service.model.dto.ResourceCreationDto;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceEntity;
import it.gov.pagopa.atmlayer.service.model.mapper.ResourceEntityMapper;
Expand All @@ -11,6 +12,7 @@
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
Expand All @@ -21,6 +23,7 @@

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@ApplicationScoped
@Path("/resources")
Expand All @@ -33,6 +36,19 @@ public class ResourceEntityResource {
@Inject
ResourceEntityService resourceEntityService;

@GET
@Produces(MediaType.APPLICATION_JSON)
public Uni<List<ResourceDTO>> getAllHtml() {
return this.resourceEntityService.getAll()
.onItem()
.transform(Unchecked.function(list -> {
if (list.isEmpty()) {
log.info("No Resource files saved in database");
}
return resourceEntityMapper.toDTOList(list);
}));
}

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -42,7 +58,7 @@ public Uni<ResourceDTO> createResource(
throws NoSuchAlgorithmException, IOException {
ResourceEntity resourceEntity = resourceEntityMapper.toEntityCreation(resourceCreationDto);
return resourceEntityService.createResource(resourceEntity, resourceCreationDto.getFile(),
resourceCreationDto.getFilename(),resourceCreationDto.getPath())
resourceCreationDto.getFilename(), resourceCreationDto.getPath())
.onItem()
.transformToUni(resource -> Uni.createFrom().item(resourceEntityMapper.toDTO(resource)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package it.gov.pagopa.atmlayer.service.model.service;

import io.smallrye.mutiny.Uni;
import it.gov.pagopa.atmlayer.service.model.entity.BpmnVersion;
import it.gov.pagopa.atmlayer.service.model.entity.BpmnVersionPK;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceEntity;

import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

public interface ResourceEntityService {
Uni<ResourceEntity> save(ResourceEntity resourceEntity);

Uni<Boolean> delete(BpmnVersionPK bpmnVersionPK);
Uni<ResourceEntity> save(ResourceEntity resourceEntity);

Uni<Boolean> delete(BpmnVersionPK bpmnVersionPK);

Uni<Optional<ResourceEntity>> findBySHA256(String sha256);

Uni<Optional<ResourceEntity>> findBySHA256(String sha256);
Uni<Optional<ResourceEntity>> findByUUID(UUID uuid);

Uni<Optional<ResourceEntity>> findByUUID(UUID uuid);
Uni<ResourceEntity> saveAndUpload(ResourceEntity resourceEntity, File file, String filename,
String path);

Uni<ResourceEntity> saveAndUpload(ResourceEntity resourceEntity, File file, String filename, String path);
Uni<ResourceEntity> createResource(ResourceEntity resourceEntity, File file, String filename,
String path);

Uni<ResourceEntity> createResource(ResourceEntity resourceEntity, File file, String filename,String path);
Uni<List<ResourceEntity>> getAll();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package it.gov.pagopa.atmlayer.service.model.service.impl;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.ATMLM_500;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.OBJECT_STORE_SAVE_FILE_ERROR;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS;

import io.quarkus.hibernate.reactive.panache.common.WithSession;
import io.quarkus.hibernate.reactive.panache.common.WithTransaction;
import io.smallrye.mutiny.Uni;
Expand All @@ -15,16 +19,12 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.ATMLM_500;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.OBJECT_STORE_SAVE_FILE_ERROR;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.rest.client.inject.RestClient;

@ApplicationScoped
@Slf4j
Expand All @@ -41,6 +41,12 @@ public class ResourceEntityServiceImpl implements ResourceEntityService {

final S3ResourceTypeEnum resourceType = S3ResourceTypeEnum.HTML;

@Override
@WithSession
public Uni<List<ResourceEntity>> getAll() {
return this.resourceEntityRepository.findAll().list();
}

@Override
@WithTransaction
public Uni<ResourceEntity> save(ResourceEntity resourceEntity) {
Expand Down Expand Up @@ -72,17 +78,19 @@ public Uni<Optional<ResourceEntity>> findByUUID(UUID uuid) {
public Uni<ResourceEntity> saveAndUpload(ResourceEntity resourceEntity, File file,
String filename, String path) {
return this.save(resourceEntity)
.onItem().transformToUni(record -> this.resourceEntityStorageService.uploadFile(resourceEntity, file, filename, path)
.onFailure().recoverWithUni(failure -> {
log.error(failure.getMessage());
return Uni.createFrom().failure(new AtmLayerException(
"Failed to save Resource Entity in Object Store. Resource creation aborted",
Response.Status.INTERNAL_SERVER_ERROR, OBJECT_STORE_SAVE_FILE_ERROR));
})
.onItem().transformToUni(putObjectResponse -> {
log.info("Completed Resource Entity Creation");
return Uni.createFrom().item(record);
}));
.onItem().transformToUni(
record -> this.resourceEntityStorageService.uploadFile(resourceEntity, file, filename,
path)
.onFailure().recoverWithUni(failure -> {
log.error(failure.getMessage());
return Uni.createFrom().failure(new AtmLayerException(
"Failed to save Resource Entity in Object Store. Resource creation aborted",
Response.Status.INTERNAL_SERVER_ERROR, OBJECT_STORE_SAVE_FILE_ERROR));
})
.onItem().transformToUni(putObjectResponse -> {
log.info("Completed Resource Entity Creation");
return Uni.createFrom().item(record);
}));
}

@Override
Expand Down

0 comments on commit c418fe7

Please sign in to comment.