From 1a22c0159c7ad56dafc96762a38826921d4d45a4 Mon Sep 17 00:00:00 2001 From: fakanpeter <fakanpeti@gmail.com> Date: Tue, 14 May 2024 11:07:59 +0200 Subject: [PATCH] Picture upload fixed --- .../hu/pazmany/controller/Controller.java | 38 ++++++++++++++----- .../java/hu/pazmany/service/DogService.java | 15 +++++--- .../src/main/resources/application.properties | 3 ++ frontend/src/components/dogs/EditDog.vue | 2 +- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/backend/src/main/java/hu/pazmany/controller/Controller.java b/backend/src/main/java/hu/pazmany/controller/Controller.java index 847a7a2..efcdbde 100644 --- a/backend/src/main/java/hu/pazmany/controller/Controller.java +++ b/backend/src/main/java/hu/pazmany/controller/Controller.java @@ -1,5 +1,6 @@ package hu.pazmany.controller; +import com.fasterxml.jackson.databind.ObjectMapper; import hu.pazmany.dto.DetailedDogDTO; import hu.pazmany.dto.DogDTO; import hu.pazmany.dto.UserDTO; @@ -9,9 +10,12 @@ import hu.pazmany.service.UserService; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; import java.util.List; import java.util.Optional; @@ -48,23 +52,39 @@ public class Controller { @PostMapping("/newdog") public ResponseEntity<?> addNewDog(@RequestBody DetailedDogDTO dto, @RequestHeader("Authorization") String token) { if(!isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); - - dogService.addNewDog(dto); - return ResponseEntity.status(HttpStatus.CREATED).build(); - + // Save the dog and picture + try { + dogService.addNewDog(dto); + } catch (IOException e) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body("Wrong picture format, vagy nem tudom én ide csak feljárok"); + } + return ResponseEntity.status(HttpStatus.CREATED).body("Dog created successfully"); } - @PostMapping("/dogs/{id}/edit") - public ResponseEntity<?> editDog(@PathVariable Integer id, @RequestBody DetailedDogDTO dto, @RequestHeader("Authorization") String token) { + @PostMapping(value = "/dogs/{id}/edit", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity<?> editDog(@PathVariable Integer id, @RequestHeader("Authorization") String token, @RequestParam("dog") String stringDogDTO, @RequestParam("picture") MultipartFile mpf) { if (!isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + System.out.println(stringDogDTO); + + // Retrieve the dog entity from the database Optional<DetailedDogDTO> optionalDog = dogService.get(id); + ObjectMapper objectMapper = new ObjectMapper(); + DetailedDogDTO dogDTO; if (optionalDog.isPresent()) { - + try { + dogDTO = objectMapper.readValue(stringDogDTO, DetailedDogDTO.class); + } catch (IOException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid DogDTO JSON format"); + } // Save the updated dog entity - dogService.editDog(id,dto); + try { + dogService.editDog(id, dogDTO, mpf); + } catch (IOException e) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body("Wrong picture format, vagy nem tudom én ide csak feljárok"); + } - return ResponseEntity.ok("Dog attributes updated successfully"); + return ResponseEntity.ok("Dog attributes updated successfully"); } else { return ResponseEntity.notFound().build(); } diff --git a/backend/src/main/java/hu/pazmany/service/DogService.java b/backend/src/main/java/hu/pazmany/service/DogService.java index 558d3f9..6ca8a00 100644 --- a/backend/src/main/java/hu/pazmany/service/DogService.java +++ b/backend/src/main/java/hu/pazmany/service/DogService.java @@ -6,7 +6,10 @@ import hu.pazmany.jpe.DogEntity; import hu.pazmany.jpe.DogRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; import java.util.List; import java.util.Optional; @@ -30,20 +33,20 @@ public class DogService { dogEntity.getPicture(), dogEntity.getAge(), dogEntity.getBreed())); } - public void addNewDog(DetailedDogDTO dto) { + public void addNewDog(DetailedDogDTO dto) throws IOException { DogEntity newDog = new DogEntity(); //newDog.setId(dogRepository.findAllDogs().size()); //System.out.println(newDog.getId()); + newDog.setName(dto.getName()); - newDog.setPicture(dto.getPicture()); + //newDog.setPicture(pic.getBytes()); newDog.setBreed(dto.getBreed()); newDog.setAge(dto.getAge()); dogRepository.save(newDog); } - public void editDog(Integer id, DetailedDogDTO editRequest) { + public void editDog(Integer id,DetailedDogDTO editRequest, MultipartFile mpf) throws IOException { Optional<DogEntity> optionalDog = dogRepository.findById(id); - if (optionalDog.isPresent()) { DogEntity dogEntity = optionalDog.get(); @@ -51,8 +54,8 @@ public class DogService { if (editRequest.getName() != null) { dogEntity.setName(editRequest.getName()); } - if (editRequest.getPicture() != null) { - dogEntity.setPicture(editRequest.getPicture()); + if (mpf.getBytes() != null) { + dogEntity.setPicture(mpf.getBytes()); } if (editRequest.getAge() != null) { dogEntity.setAge(editRequest.getAge()); diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index d542bda..17e1461 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -13,6 +13,9 @@ spring.h2.console.path=/h2-console spring.h2.console.settings.trace=false spring.h2.console.settings.web-allow-others=true +spring.servlet.multipart.enabled=true +spring.servlet.multipart.max-file-size=10MB +spring.servlet.multipart.max-request-size=10MB springdoc.swagger-ui.path=/swagger-ui springdoc.swagger-ui.enabled=true diff --git a/frontend/src/components/dogs/EditDog.vue b/frontend/src/components/dogs/EditDog.vue index 5381132..0810ac2 100644 --- a/frontend/src/components/dogs/EditDog.vue +++ b/frontend/src/components/dogs/EditDog.vue @@ -117,7 +117,7 @@ export default { }; try { - await axios.post(`/api/dogs/${this.$route.params.id}/edit`, this.dog, config); + await axios.post(`/api/dogs/${this.$route.params.id}/edit`, formData, config); this.$router.push(`/dog/${this.$route.params.id}`); } catch (error) { console.error('Error editing dog:', error); -- GitLab