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

Picture upload fixed

parent 9afb6403
Branches
Tags
No related merge requests found
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();
}
......
......@@ -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());
......
......@@ -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
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment