Skip to content

Commit

Permalink
feat: add delete and add services
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenke committed Mar 1, 2024
1 parent ea57a4d commit 0b405c2
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 59 deletions.
22 changes: 22 additions & 0 deletions src/main/java/org/tkit/onecx/theme/domain/daos/ImageDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Image> {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,7 +34,7 @@ public class ThemesRestController implements ThemesInternalApi {
ThemeDAO dao;

@Inject
ImageDAO imageDAO;
ThemesService themeService;

@Inject
ThemeMapper mapper;
Expand All @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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_______");
}

}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}

}
15 changes: 10 additions & 5 deletions src/main/openapi/onecx-image-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,22 @@ 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
in: path
required: true
schema:
type: string
- name: refType
in: path
required: true
schema:
$ref: "#/components/schemas/RefType"
responses:
"200":
description: OK
Expand All @@ -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
Expand Down
Loading

0 comments on commit 0b405c2

Please sign in to comment.