|
| 1 | +package app.domain.products; |
| 2 | + |
| 3 | +import app.domain.employees.Employee; |
| 4 | +import app.domain.employees.EmployeeResource; |
| 5 | +import io.quarkus.qute.CheckedTemplate; |
| 6 | +import io.quarkus.qute.TemplateInstance; |
| 7 | +import io.smallrye.common.annotation.Blocking; |
| 8 | +import jakarta.inject.Inject; |
| 9 | +import jakarta.transaction.Transactional; |
| 10 | +import jakarta.validation.ConstraintViolation; |
| 11 | +import jakarta.validation.Validator; |
| 12 | +import jakarta.ws.rs.Consumes; |
| 13 | +import jakarta.ws.rs.GET; |
| 14 | +import jakarta.ws.rs.POST; |
| 15 | +import jakarta.ws.rs.Path; |
| 16 | +import jakarta.ws.rs.core.MediaType; |
| 17 | +import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 18 | +import org.jboss.resteasy.reactive.multipart.FileUpload; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Paths; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; |
| 29 | + |
| 30 | +@Path("/products") |
| 31 | +@Blocking |
| 32 | +public class ProductResource { |
| 33 | + @Inject |
| 34 | + Validator validator; |
| 35 | + |
| 36 | + @Inject |
| 37 | + ProductRepository repository; |
| 38 | + |
| 39 | + @ConfigProperty(name = "photos.dir") |
| 40 | + String photosDir; |
| 41 | + |
| 42 | + @GET |
| 43 | + public TemplateInstance findProducts() { |
| 44 | + List<Product> products = repository.findAll().list(); |
| 45 | + return Templates.list(products); |
| 46 | + } |
| 47 | + |
| 48 | + @GET |
| 49 | + @Path("/new") |
| 50 | + public TemplateInstance newProduct() { |
| 51 | + return Templates.create().data("form", null).data("errors", Map.of()); |
| 52 | + } |
| 53 | + |
| 54 | + @POST |
| 55 | + @Blocking |
| 56 | + @Consumes(MediaType.MULTIPART_FORM_DATA) |
| 57 | + @Transactional |
| 58 | + public TemplateInstance createProduct(CreateProductForm form) throws IOException { |
| 59 | + var violations = validator.validate(form); |
| 60 | + if (!violations.isEmpty()) { |
| 61 | + var errors = violations.stream() |
| 62 | + .collect(Collectors.toMap(v -> v.getPropertyPath().toString(), |
| 63 | + ConstraintViolation::getMessage)); |
| 64 | + return EmployeeResource.Templates.create().data("errors", errors).data("form", form); |
| 65 | + } |
| 66 | + |
| 67 | + //create product |
| 68 | + Product product = new Product(); |
| 69 | + product.setName(form.name()); |
| 70 | + product.setDescription(form.description()); |
| 71 | + product.setPrice(form.price()); |
| 72 | + product.setStock(form.inStock()); |
| 73 | + repository.persist(product); |
| 74 | + |
| 75 | + for (FileUpload file : form.photos()) { |
| 76 | + //persist photo data in database |
| 77 | + var photo = new Photo(); |
| 78 | + var filename = generateUniqueFileName(file.fileName()); |
| 79 | + photo.setName(filename); |
| 80 | + photo.setSize(file.size()); |
| 81 | + photo.setUrl(photosDir.concat("/").concat(filename)); |
| 82 | + photo.setProduct(product); |
| 83 | + repository.savePhoto(photo); |
| 84 | + |
| 85 | + //copy file in filesystem |
| 86 | + var path = file.uploadedFile(); |
| 87 | + Files.copy(path, Paths.get(photosDir).resolve(filename), REPLACE_EXISTING); |
| 88 | + } |
| 89 | + |
| 90 | + return Templates.list(Employee.listAll()); |
| 91 | + } |
| 92 | + |
| 93 | + private String generateUniqueFileName(String originalFileName) { |
| 94 | + String extension = ""; |
| 95 | + int dotIndex = originalFileName.lastIndexOf('.'); |
| 96 | + if (dotIndex > 0 && dotIndex < originalFileName.length() - 1) { |
| 97 | + extension = originalFileName.substring(dotIndex); |
| 98 | + } |
| 99 | + extension = extension.replaceAll("[^a-zA-Z0-9.]", ""); |
| 100 | + return UUID.randomUUID() + extension; |
| 101 | + } |
| 102 | + |
| 103 | + @CheckedTemplate(requireTypeSafeExpressions = false) |
| 104 | + public static class Templates { |
| 105 | + public static native TemplateInstance list(List<Product> products); |
| 106 | + |
| 107 | + public static native TemplateInstance create(); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments