From c418fe76ffc4dce36bbd1b97adcd4e17d88609e4 Mon Sep 17 00:00:00 2001 From: Elis Date: Mon, 13 Nov 2023 09:59:36 +0100 Subject: [PATCH] get all --- .../model/dto/ResourceCreationDto.java | 5 +-- .../model/mapper/ResourceEntityMapper.java | 27 +++++++----- .../service/model/model/ResourceDTO.java | 6 +-- .../resource/ResourceEntityResource.java | 18 +++++++- .../model/service/ResourceEntityService.java | 20 +++++---- .../impl/ResourceEntityServiceImpl.java | 44 +++++++++++-------- 6 files changed, 75 insertions(+), 45 deletions(-) diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/ResourceCreationDto.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/ResourceCreationDto.java index df33f658..992a1aaf 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/ResourceCreationDto.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/ResourceCreationDto.java @@ -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 { diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/ResourceEntityMapper.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/ResourceEntityMapper.java index 01801693..bc711f26 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/ResourceEntityMapper.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/ResourceEntityMapper.java @@ -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 toDTOList(List list) { + return list.stream().map(this::toDTO).collect(Collectors.toList()); + } } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/model/ResourceDTO.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/model/ResourceDTO.java index 2f36c612..95acb9f6 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/model/ResourceDTO.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/model/ResourceDTO.java @@ -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 diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/ResourceEntityResource.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/ResourceEntityResource.java index 58e277a3..341c40db 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/ResourceEntityResource.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/ResourceEntityResource.java @@ -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; @@ -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; @@ -21,6 +23,7 @@ import java.io.IOException; import java.security.NoSuchAlgorithmException; +import java.util.List; @ApplicationScoped @Path("/resources") @@ -33,6 +36,19 @@ public class ResourceEntityResource { @Inject ResourceEntityService resourceEntityService; + @GET + @Produces(MediaType.APPLICATION_JSON) + public Uni> 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) @@ -42,7 +58,7 @@ public Uni 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))); } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/ResourceEntityService.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/ResourceEntityService.java index 96a93d84..112838df 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/ResourceEntityService.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/ResourceEntityService.java @@ -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 save(ResourceEntity resourceEntity); - Uni delete(BpmnVersionPK bpmnVersionPK); + Uni save(ResourceEntity resourceEntity); + + Uni delete(BpmnVersionPK bpmnVersionPK); + + Uni> findBySHA256(String sha256); - Uni> findBySHA256(String sha256); + Uni> findByUUID(UUID uuid); - Uni> findByUUID(UUID uuid); + Uni saveAndUpload(ResourceEntity resourceEntity, File file, String filename, + String path); - Uni saveAndUpload(ResourceEntity resourceEntity, File file, String filename, String path); + Uni createResource(ResourceEntity resourceEntity, File file, String filename, + String path); - Uni createResource(ResourceEntity resourceEntity, File file, String filename,String path); + Uni> getAll(); } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java index b94310c1..aa0559a6 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java @@ -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; @@ -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 @@ -41,6 +41,12 @@ public class ResourceEntityServiceImpl implements ResourceEntityService { final S3ResourceTypeEnum resourceType = S3ResourceTypeEnum.HTML; + @Override + @WithSession + public Uni> getAll() { + return this.resourceEntityRepository.findAll().list(); + } + @Override @WithTransaction public Uni save(ResourceEntity resourceEntity) { @@ -72,17 +78,19 @@ public Uni> findByUUID(UUID uuid) { public Uni 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