HorseEndpoint.java 6.47 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
import java.lang.invoke.MethodHandles;
20 21
import java.util.List;
import java.util.Map;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

@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) {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Error during reading horse");
        }
    }

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<HorseDto> getAll(@RequestParam Map<String, String> filters) {
        try {
            if(filters.isEmpty()) {
                LOGGER.info("GET " + BASE_URL);
                return horseMapper.entityListToDtoList(horseService.getAll());
            } else {
                LOGGER.info("GET " + BASE_URL + "/ with filters" + filters.entrySet());
                return horseMapper.entityListToDtoList(horseService.getFiltered(filters));
            }
        } catch (NotFoundException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No horses found");
        } catch (ValidationException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The request contains filters with bad values: " + e.getMessage());
65 66 67 68 69 70 71 72 73 74 75 76
        }
    }

    @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());
77
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
78
                "Error during adding new horse: " + e.getMessage());
79 80 81
        } catch (DataAccessException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
82
                "Something went wrong during the communication with the database");
83 84 85 86 87 88 89 90 91 92 93 94 95 96
        }
    }

    @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,
97
                "Error during updating horse with id " + id + ": " + e.getMessage());
98 99 100
        } catch (DataAccessException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
101 102 103 104 105
                "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");
106 107 108 109
        } catch (NotFoundException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.NOT_FOUND,
                "The horse requested could not be found");
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        }
    }

    @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());
130 131
        }
    }
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

    @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");
        }
    }
149
}