diff --git a/src/main/java/org/tkit/onecx/theme/domain/daos/ImageDAO.java b/src/main/java/org/tkit/onecx/theme/domain/daos/ImageDAO.java index d9b9c3f..3e1dd5e 100644 --- a/src/main/java/org/tkit/onecx/theme/domain/daos/ImageDAO.java +++ b/src/main/java/org/tkit/onecx/theme/domain/daos/ImageDAO.java @@ -8,6 +8,8 @@ import org.tkit.quarkus.jpa.daos.AbstractDAO; import org.tkit.quarkus.jpa.exceptions.DAOException; +import gen.org.tkit.onecx.image.rs.internal.model.RefTypeDTO; + @ApplicationScoped @Transactional public class ImageDAO extends AbstractDAO { @@ -46,6 +48,26 @@ public void deleteQueryByRefId(String refId) throws DAOException { } catch (Exception e) { throw handleConstraint(e, ErrorKeys.FAILED_TO_DELETE_BY_REF_ID_QUERY); } + + } + + @Transactional(value = Transactional.TxType.REQUIRED, rollbackOn = DAOException.class) + public void deleteQueryByRefIdAndRefType(String refId, RefTypeDTO refType) throws DAOException { + if (refId == null || refType == null) { + return; + } + try { + var cq = deleteQuery(); + var root = cq.from(Image.class); + var cb = this.getEntityManager().getCriteriaBuilder(); + + cq.where(cb.equal(root.get(Image_.REF_ID), refId)); + cq.where(cb.equal(root.get(Image_.REF_TYPE), refType.toString())); + getEntityManager().createQuery(cq).executeUpdate(); + getEntityManager().flush(); + } catch (Exception e) { + throw handleConstraint(e, ErrorKeys.FAILED_TO_DELETE_BY_REF_ID_QUERY); + } } public enum ErrorKeys { diff --git a/src/main/java/org/tkit/onecx/theme/rs/internal/controllers/ImageRestController.java b/src/main/java/org/tkit/onecx/theme/rs/internal/controllers/ImageRestController.java index f42cf90..7adbaee 100644 --- a/src/main/java/org/tkit/onecx/theme/rs/internal/controllers/ImageRestController.java +++ b/src/main/java/org/tkit/onecx/theme/rs/internal/controllers/ImageRestController.java @@ -14,6 +14,7 @@ import org.tkit.onecx.theme.domain.models.Image; import org.tkit.onecx.theme.rs.internal.mappers.ExceptionMapper; import org.tkit.onecx.theme.rs.internal.mappers.ImageMapper; +import org.tkit.onecx.theme.rs.internal.services.ImagesService; import org.tkit.quarkus.jpa.exceptions.ConstraintException; import org.tkit.quarkus.log.cdi.LogService; @@ -37,14 +38,13 @@ public class ImageRestController implements ImagesInternalApi { @Context UriInfo uriInfo; - @Context - HttpHeaders httpHeaders; - @Inject ImageMapper imageMapper; + @Inject + ImagesService imagesService; + @Override - @Transactional public Response getImage(String refId, RefTypeDTO refType) { Image image = imageDAO.findByRefIdAndRefType(refId, refType.toString()); if (image == null) { @@ -57,65 +57,39 @@ public Response getImage(String refId, RefTypeDTO refType) { @Override public Response updateImage(String refId, RefTypeDTO refType, byte[] body, Integer contentLength) { - Image image = imageDAO.findByRefIdAndRefType(refId, refType.toString()); + var image = imagesService.updateImage(refId, refType, body, contentLength); if (image == null) { return Response.status(Response.Status.NOT_FOUND).build(); } - var contentType = httpHeaders.getMediaType(); - contentType = new MediaType(contentType.getType(), contentType.getSubtype()); - - image.setLength(contentLength); - image.setMimeType(contentType.toString()); - image.setImageData(body); - - image = imageDAO.update(image); return Response.ok(imageMapper.map(image)).build(); } @Override public Response uploadImage(Integer contentLength, String refId, RefTypeDTO refType, byte[] body) { - var contentType = httpHeaders.getMediaType(); - contentType = new MediaType(contentType.getType(), contentType.getSubtype()); - var image = imageMapper.create(refId, refType.toString(), contentType.toString(), contentLength); - image.setLength(contentLength); - image.setImageData(body); - image = imageDAO.create(image); + var imageInfoDTO = imagesService.uploadImage(contentLength, refId, refType, body); - var imageInfoDTO = imageMapper.map(image); return Response.created(uriInfo.getAbsolutePathBuilder().path(imageInfoDTO.getId()).build()) .entity(imageInfoDTO) .build(); } @Override - public Response deleteImage(String refId) { - imageDAO.deleteQueryByRefId(refId); + public Response deleteImagesById(String refId) { + try{ + imagesService.deleteImagesById(refId); + }catch (Exception e){ + return Response.status(Response.Status.BAD_REQUEST).build(); + } return Response.status(Response.Status.NO_CONTENT).build(); } @Override - public Response updateImageRefType(String refId) { - Image imageForLogo = imageDAO.findByRefIdAndRefType(refId, RefTypeDTO.LOGO.toString()); - if (imageForLogo != null) { - imageForLogo.setRefId(refId); - imageDAO.update(imageForLogo); - } - Image imageForFavicon = imageDAO.findByRefIdAndRefType(refId, RefTypeDTO.FAVICON.toString()); - if (imageForFavicon != null) { - imageForFavicon.setRefId(refId); - imageDAO.update(imageForFavicon); - } - - if (imageForLogo.getRefId() != null) { - return Response.ok(imageMapper.map(imageForLogo)).build(); - } else if (imageForFavicon.getRefId() != null) { - return Response.ok(imageMapper.map(imageForFavicon)).build(); - } else { - return Response.status(Response.Status.NOT_FOUND).build(); - } + public Response deleteImage(String refId, RefTypeDTO refType) { + imagesService.deleteImage(refId, refType); + return Response.status(Response.Status.NO_CONTENT).build(); } @ServerExceptionMapper diff --git a/src/main/java/org/tkit/onecx/theme/rs/internal/controllers/ThemesRestController.java b/src/main/java/org/tkit/onecx/theme/rs/internal/controllers/ThemesRestController.java index dd9d012..c91e25f 100644 --- a/src/main/java/org/tkit/onecx/theme/rs/internal/controllers/ThemesRestController.java +++ b/src/main/java/org/tkit/onecx/theme/rs/internal/controllers/ThemesRestController.java @@ -13,10 +13,10 @@ import org.jboss.resteasy.reactive.RestResponse; import org.jboss.resteasy.reactive.server.ServerExceptionMapper; -import org.tkit.onecx.theme.domain.daos.ImageDAO; import org.tkit.onecx.theme.domain.daos.ThemeDAO; import org.tkit.onecx.theme.rs.internal.mappers.ExceptionMapper; import org.tkit.onecx.theme.rs.internal.mappers.ThemeMapper; +import org.tkit.onecx.theme.rs.internal.services.ThemesService; import org.tkit.quarkus.jpa.exceptions.ConstraintException; import org.tkit.quarkus.log.cdi.LogService; @@ -34,7 +34,7 @@ public class ThemesRestController implements ThemesInternalApi { ThemeDAO dao; @Inject - ImageDAO imageDAO; + ThemesService themeService; @Inject ThemeMapper mapper; @@ -46,7 +46,6 @@ public class ThemesRestController implements ThemesInternalApi { UriInfo uriInfo; @Override - @Transactional public Response createNewTheme(CreateThemeDTO createThemeDTO) { var theme = mapper.create(createThemeDTO); theme = dao.create(theme); @@ -57,18 +56,10 @@ public Response createNewTheme(CreateThemeDTO createThemeDTO) { } @Override - @Transactional public Response deleteTheme(String id) { - var image = dao.findById(id); - if (image == null) { - return Response.noContent().build(); - } - dao.deleteQueryById(id); - - // workaround for images - imageDAO.deleteQueryByRefId(image.getName()); + themeService.deleteTheme(id); - return Response.noContent().build(); + return Response.status(Response.Status.NO_CONTENT).build(); } @Override diff --git a/src/main/java/org/tkit/onecx/theme/rs/internal/services/ImagesService.java b/src/main/java/org/tkit/onecx/theme/rs/internal/services/ImagesService.java new file mode 100644 index 0000000..4135235 --- /dev/null +++ b/src/main/java/org/tkit/onecx/theme/rs/internal/services/ImagesService.java @@ -0,0 +1,76 @@ +package org.tkit.onecx.theme.rs.internal.services; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.MediaType; + +import org.tkit.onecx.theme.domain.daos.ImageDAO; +import org.tkit.onecx.theme.domain.models.Image; +import org.tkit.onecx.theme.rs.internal.mappers.ImageMapper; + +import gen.org.tkit.onecx.image.rs.internal.model.ImageInfoDTO; +import gen.org.tkit.onecx.image.rs.internal.model.RefTypeDTO; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@ApplicationScoped +public class ImagesService { + + @Context + HttpHeaders httpHeaders; + + @Inject + ImageMapper imageMapper; + + @Inject + ImageDAO imageDAO; + + public ImageInfoDTO uploadImage(Integer contentLength, String refId, RefTypeDTO refType, byte[] body) { + var contentType = httpHeaders.getMediaType(); + contentType = new MediaType(contentType.getType(), contentType.getSubtype()); + var image = imageMapper.create(refId, refType.toString(), contentType.toString(), contentLength); + image.setLength(contentLength); + image.setImageData(body); + image = imageDAO.create(image); + + return imageMapper.map(image); + } + + public Image updateImage(String refId, RefTypeDTO refType, byte[] body, Integer contentLength) { + Image image = imageDAO.findByRefIdAndRefType(refId, refType.toString()); + if (image == null) { + System.out.println("ITS NULLLLLLL_____________"); + return null; + } + + var contentType = httpHeaders.getMediaType(); + contentType = new MediaType(contentType.getType(), contentType.getSubtype()); + + image.setLength(contentLength); + image.setMimeType(contentType.toString()); + image.setImageData(body); + + return imageDAO.update(image); + } + + @Transactional + public void deleteImage(String refId, RefTypeDTO refType) { + + imageDAO.deleteQueryByRefIdAndRefType(refId, refType); + } + + @Transactional + public void deleteImagesById(String refId) { + + try { + imageDAO.deleteQueryByRefId(refId); + }catch (Exception e){ + System.out.println("NO DEL SUCCESS_______"); + } + + } + +} diff --git a/src/main/java/org/tkit/onecx/theme/rs/internal/services/ThemesService.java b/src/main/java/org/tkit/onecx/theme/rs/internal/services/ThemesService.java new file mode 100644 index 0000000..758f9e5 --- /dev/null +++ b/src/main/java/org/tkit/onecx/theme/rs/internal/services/ThemesService.java @@ -0,0 +1,34 @@ +package org.tkit.onecx.theme.rs.internal.services; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; + +import org.tkit.onecx.theme.domain.daos.ImageDAO; +import org.tkit.onecx.theme.domain.daos.ThemeDAO; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@ApplicationScoped +public class ThemesService { + + @Inject + ThemeDAO dao; + + @Inject + ImageDAO imageDAO; + + @Transactional + public void deleteTheme(String id) { + + var image = dao.findById(id); + if (image != null) { + dao.deleteQueryById(id); + + // workaround for images + imageDAO.deleteQueryByRefId(image.getName()); + } + } + +} diff --git a/src/main/openapi/onecx-image-internal-openapi.yaml b/src/main/openapi/onecx-image-internal-openapi.yaml index 4b5e0ed..72a26f8 100644 --- a/src/main/openapi/onecx-image-internal-openapi.yaml +++ b/src/main/openapi/onecx-image-internal-openapi.yaml @@ -126,11 +126,10 @@ paths: application/json: schema: $ref: '#/components/schemas/ProblemDetailResponse' - /internal/images/{refId}: delete: tags: - imagesInternal - description: delete Image by id + description: delete Image operationId: deleteImage parameters: - name: refId @@ -138,6 +137,11 @@ paths: required: true schema: type: string + - name: refType + in: path + required: true + schema: + $ref: "#/components/schemas/RefType" responses: "200": description: OK @@ -151,11 +155,12 @@ paths: application/json: schema: $ref: '#/components/schemas/ProblemDetailResponse' - put: + /internal/images/{refId}: + delete: tags: - imagesInternal - description: update Images ref type - operationId: updateImageRefType + description: delete Image by id + operationId: deleteImagesById parameters: - name: refId in: path diff --git a/src/test/java/org/tkit/onecx/theme/rs/internal/controllers/ImageRestControllerTest.java b/src/test/java/org/tkit/onecx/theme/rs/internal/controllers/ImageRestControllerTest.java index 1149441..1446a7e 100644 --- a/src/test/java/org/tkit/onecx/theme/rs/internal/controllers/ImageRestControllerTest.java +++ b/src/test/java/org/tkit/onecx/theme/rs/internal/controllers/ImageRestControllerTest.java @@ -225,6 +225,86 @@ void updateImage() { .statusCode(NOT_FOUND.getStatusCode()); } + @Test + void deleteImage() { + + var refId = "themeDeleteTest"; + var refType = RefTypeDTO.LOGO; + + given() + .pathParam("refId", refId) + .pathParam("refType", refType) + .when() + .body(FILE) + .contentType(MEDIA_TYPE_IMAGE_PNG) + .post("/{refType}") + .then() + .statusCode(CREATED.getStatusCode()) + .extract() + .body().as(ImageInfoDTO.class); + + var res = given() + .pathParam("refId", refId) + .pathParam("refType", refType) + .when() + .body(FILE) + .contentType(MEDIA_TYPE_IMAGE_PNG) + .delete("/{refType}") + .then() + .statusCode(NO_CONTENT.getStatusCode()); + + Assertions.assertNotNull(res); + + given() + .contentType(APPLICATION_JSON) + .pathParam("refId", refId) + .pathParam("refType", refType) + .get("/{refType}") + .then() + .statusCode(NOT_FOUND.getStatusCode()); + } + + @Test + void deleteImagesById() { + + var refId = "themedeleteByIdTest"; + var refType = RefTypeDTO.LOGO; + + given() + .pathParam("refId", refId) + .pathParam("refType", refType) + .when() + .body(FILE) + .contentType(MEDIA_TYPE_IMAGE_PNG) + .post("/{refType}") + .then() + .statusCode(CREATED.getStatusCode()) + .extract() + .body().as(ImageInfoDTO.class); + + var res = given() + .pathParam("refId", refId) + .when() + .body(FILE) + .contentType(MEDIA_TYPE_IMAGE_PNG) + .delete() + .then() + .statusCode(NO_CONTENT.getStatusCode()); + + Assertions.assertNotNull(res); + + + given() + .contentType(APPLICATION_JSON) + .pathParam("refId", refId) + .pathParam("refType", refType) + .get("/{refType}") + .then() + .statusCode(NOT_FOUND.getStatusCode()); + + } + + @Test void updateImage_returnNotFound_whenEntryNotExists() { @@ -254,6 +334,7 @@ void updateImage_returnNotFound_whenEntryNotExists() { .statusCode(NOT_FOUND.getStatusCode()); Assertions.assertNotNull(exception); + } @Test