HorseEndpoint.java 5.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package at.ac.tuwien.sepm.assignment.individual.endpoint;

import at.ac.tuwien.sepm.assignment.individual.endpoint.dto.HorseDto;
import at.ac.tuwien.sepm.assignment.individual.endpoint.mapper.HorseMapper;
import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.service.HorseService;
import at.ac.tuwien.sepm.assignment.individual.util.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
15
import org.springframework.web.multipart.MultipartFile;
16 17
import org.springframework.web.server.ResponseStatusException;

18
import java.io.IOException;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
import java.lang.invoke.MethodHandles;

@RestController
@RequestMapping(at.ac.tuwien.sepm.assignment.individual.endpoint.HorseEndpoint.BASE_URL)
public class HorseEndpoint {

    static final String BASE_URL = "/horses";
    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    private final HorseService horseService;
    private final HorseMapper horseMapper;

    @Autowired
    public HorseEndpoint(HorseService horseService, HorseMapper horseMapper) {
        this.horseService = horseService;
        this.horseMapper = horseMapper;
    }

    @GetMapping(value = "/{id}")
    public HorseDto getOneById(@PathVariable("id") Long id) {
        LOGGER.info("GET " + BASE_URL + "/{}", id);
        try {
            return horseMapper.entityToDto(horseService.findOneById(id));
        } catch (NotFoundException e) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Error during reading horse", e);
        }
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public HorseDto addHorse(@RequestBody HorseDto horse) {
        LOGGER.info("POST " + BASE_URL);
        try {
            Horse horseEntity = horseMapper.dtoToEntity(horse);
            return horseMapper.entityToDto(horseService.addHorse(horseEntity));
        } catch (ValidationException e) {
            LOGGER.error(e.getMessage());
55
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
56
                "Error during adding new horse: " + e.getMessage(), e);
57 58 59
        } catch (DataAccessException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
60
                "Something went wrong during the communication with the database");
61 62 63 64 65 66 67 68 69 70 71 72 73 74
        }
    }

    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.OK)
    public HorseDto updateHorse(@PathVariable("id") Long id, @RequestBody HorseDto horse) {
        LOGGER.info("PUT " + BASE_URL + "/{}", id);
        try {
            Horse horseEntity = horseMapper.dtoToEntity(horse);
            horseEntity.setId(id);
            return horseMapper.entityToDto((horseService.updateHorse(horseEntity)));
        } catch (ValidationException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
75
                "Error during updating horse with id " + id + ": " + e.getMessage());
76 77 78
        } catch (DataAccessException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
                "Something went wrong during the communication with the database");
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.PARTIAL_CONTENT,
                "Operation completed with errors: image could not be saved");
        }
    }

    @DeleteMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteHorse(@PathVariable("id") Long id) {
        LOGGER.info("DELETE " + BASE_URL + "/{}", id);
        try {
            horseService.deleteHorse(id);
        } catch (DataAccessException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
                "Something went wrong during the communication with the database");
        } catch (NotFoundException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.NOT_FOUND,
                "The requested horse has not been found");
        } catch (IOException e) {
            // Log the error and return as it does not concern the user
            LOGGER.error(e.getMessage());
104 105
        }
    }
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    @PostMapping(value = "/upload")
    @ResponseStatus(HttpStatus.CREATED)
    public void addImage(@RequestParam("file") MultipartFile image) {
        LOGGER.info("POST " + BASE_URL + "/upload");
        try {
            horseService.saveImage(image);
        } catch(ValidationException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
                "Unsupported file type provided. Supported file types are jpg and png.");
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
                "Something went wrong while uploading the file");
        }
    }
123
}