Skip to content
Snippets Groups Projects
Commit 1a22c015 authored by fakanpeter's avatar fakanpeter
Browse files

Picture upload fixed

parent 9afb6403
No related branches found
No related tags found
No related merge requests found
package hu.pazmany.controller; package hu.pazmany.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import hu.pazmany.dto.DetailedDogDTO; import hu.pazmany.dto.DetailedDogDTO;
import hu.pazmany.dto.DogDTO; import hu.pazmany.dto.DogDTO;
import hu.pazmany.dto.UserDTO; import hu.pazmany.dto.UserDTO;
...@@ -9,9 +10,12 @@ import hu.pazmany.service.UserService; ...@@ -9,9 +10,12 @@ import hu.pazmany.service.UserService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -48,21 +52,37 @@ public class Controller { ...@@ -48,21 +52,37 @@ public class Controller {
@PostMapping("/newdog") @PostMapping("/newdog")
public ResponseEntity<?> addNewDog(@RequestBody DetailedDogDTO dto, @RequestHeader("Authorization") String token) { public ResponseEntity<?> addNewDog(@RequestBody DetailedDogDTO dto, @RequestHeader("Authorization") String token) {
if(!isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); if(!isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
// Save the dog and picture
try {
dogService.addNewDog(dto); dogService.addNewDog(dto);
return ResponseEntity.status(HttpStatus.CREATED).build(); } 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") @PostMapping(value = "/dogs/{id}/edit", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> editDog(@PathVariable Integer id, @RequestBody DetailedDogDTO dto, @RequestHeader("Authorization") String token) { 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(); if (!isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
System.out.println(stringDogDTO);
// Retrieve the dog entity from the database // Retrieve the dog entity from the database
Optional<DetailedDogDTO> optionalDog = dogService.get(id); Optional<DetailedDogDTO> optionalDog = dogService.get(id);
ObjectMapper objectMapper = new ObjectMapper();
DetailedDogDTO dogDTO;
if (optionalDog.isPresent()) { 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 // 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 { } else {
......
...@@ -6,7 +6,10 @@ import hu.pazmany.jpe.DogEntity; ...@@ -6,7 +6,10 @@ import hu.pazmany.jpe.DogEntity;
import hu.pazmany.jpe.DogRepository; import hu.pazmany.jpe.DogRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; 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.List;
import java.util.Optional; import java.util.Optional;
...@@ -30,20 +33,20 @@ public class DogService { ...@@ -30,20 +33,20 @@ public class DogService {
dogEntity.getPicture(), dogEntity.getAge(), dogEntity.getBreed())); dogEntity.getPicture(), dogEntity.getAge(), dogEntity.getBreed()));
} }
public void addNewDog(DetailedDogDTO dto) { public void addNewDog(DetailedDogDTO dto) throws IOException {
DogEntity newDog = new DogEntity(); DogEntity newDog = new DogEntity();
//newDog.setId(dogRepository.findAllDogs().size()); //newDog.setId(dogRepository.findAllDogs().size());
//System.out.println(newDog.getId()); //System.out.println(newDog.getId());
newDog.setName(dto.getName()); newDog.setName(dto.getName());
newDog.setPicture(dto.getPicture()); //newDog.setPicture(pic.getBytes());
newDog.setBreed(dto.getBreed()); newDog.setBreed(dto.getBreed());
newDog.setAge(dto.getAge()); newDog.setAge(dto.getAge());
dogRepository.save(newDog); 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); Optional<DogEntity> optionalDog = dogRepository.findById(id);
if (optionalDog.isPresent()) { if (optionalDog.isPresent()) {
DogEntity dogEntity = optionalDog.get(); DogEntity dogEntity = optionalDog.get();
...@@ -51,8 +54,8 @@ public class DogService { ...@@ -51,8 +54,8 @@ public class DogService {
if (editRequest.getName() != null) { if (editRequest.getName() != null) {
dogEntity.setName(editRequest.getName()); dogEntity.setName(editRequest.getName());
} }
if (editRequest.getPicture() != null) { if (mpf.getBytes() != null) {
dogEntity.setPicture(editRequest.getPicture()); dogEntity.setPicture(mpf.getBytes());
} }
if (editRequest.getAge() != null) { if (editRequest.getAge() != null) {
dogEntity.setAge(editRequest.getAge()); dogEntity.setAge(editRequest.getAge());
......
...@@ -13,6 +13,9 @@ spring.h2.console.path=/h2-console ...@@ -13,6 +13,9 @@ spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=true 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.path=/swagger-ui
springdoc.swagger-ui.enabled=true springdoc.swagger-ui.enabled=true
......
...@@ -117,7 +117,7 @@ export default { ...@@ -117,7 +117,7 @@ export default {
}; };
try { 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}`); this.$router.push(`/dog/${this.$route.params.id}`);
} catch (error) { } catch (error) {
console.error('Error editing dog:', error); console.error('Error editing dog:', error);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment