Skip to content
Snippets Groups Projects
Select Git revision
3 results Searching

Controller.java

Blame
  • Controller.java 6.55 KiB
    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;
    import hu.pazmany.security.JwtTokenProvider;
    import hu.pazmany.service.DogService;
    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;
    import java.util.regex.Pattern;
    
    @RestController
    @RequestMapping("/api")
    public class Controller {
    	private final DogService dogService;
    	private final UserService userService;
    	private static final JwtTokenProvider jwtTokenProvider = JwtTokenProvider.getInstance();
    
    	@Autowired
    	public Controller(DogService dogService, UserService userService) {
    		this.dogService = dogService;
    		this.userService = userService;
    	}
    
    
    	@GetMapping("/dogs")
    	public List<DogDTO> getAllDogs(){
    		return dogService.getAllDogs();
    	}
    
    	@GetMapping("/dogs/{id}")
    	public ResponseEntity<?> getDogById(@PathVariable Integer id) {
    		return dogService.get(id)
    				.map(dogEntity -> ResponseEntity.ok(new DetailedDogDTO(
    						dogEntity.getId(),
    						dogEntity.getName(),
    						dogEntity.getPicture(),
    						dogEntity.getAge(),
    						dogEntity.getBreed())))
    				.orElse(ResponseEntity.notFound().build());
    	}
    
    	@PostMapping("/newdog")
    	public ResponseEntity<?> addNewDog(@RequestBody DetailedDogDTO dto, @RequestHeader("Authorization") String token) {
    		System.out.println(token);
    		if(!isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).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(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();