OwnerEndpoint.java 6.35 KB
Newer Older
1 2
package at.ac.tuwien.sepm.assignment.individual.endpoint;

3
import at.ac.tuwien.sepm.assignment.individual.endpoint.dto.HorseDto;
4
import at.ac.tuwien.sepm.assignment.individual.endpoint.dto.OwnerDto;
5
import at.ac.tuwien.sepm.assignment.individual.endpoint.mapper.HorseMapper;
6
import at.ac.tuwien.sepm.assignment.individual.endpoint.mapper.OwnerMapper;
7
import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
8 9
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
import at.ac.tuwien.sepm.assignment.individual.service.OwnerService;
10

11
import java.lang.invoke.MethodHandles;
12 13
import java.util.List;
import java.util.Map;
14 15

import at.ac.tuwien.sepm.assignment.individual.util.ValidationException;
16 17 18
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.dao.DataAccessException;
20
import org.springframework.dao.DataIntegrityViolationException;
21 22 23 24 25 26 27 28 29 30 31 32
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

@RestController
@RequestMapping(OwnerEndpoint.BASE_URL)
public class OwnerEndpoint {

    static final String BASE_URL = "/owners";
    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    private final OwnerService ownerService;
    private final OwnerMapper ownerMapper;
33
    private final HorseMapper horseMapper;
34 35

    @Autowired
36
    public OwnerEndpoint(OwnerService ownerService, OwnerMapper ownerMapper, HorseMapper horseMapper) {
37 38
        this.ownerService = ownerService;
        this.ownerMapper = ownerMapper;
39
        this.horseMapper = horseMapper;
40 41 42 43 44 45 46 47 48 49 50
    }

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

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<OwnerDto> getAll(@RequestParam Map<String, String> filters) {
        try {
            if(filters.isEmpty()) {
                LOGGER.info("GET " + BASE_URL);
                return ownerMapper.entityListToDtoList(ownerService.getAll());
            } else {
                LOGGER.info("GET " + BASE_URL + "/ with filters" + filters.entrySet());
                return ownerMapper.entityListToDtoList(ownerService.getFiltered(filters));
            }
        } catch (NotFoundException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No owners found");
        } catch (ValidationException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The request contains filters with bad values: " + e.getMessage());
        }
    }

72 73 74 75 76 77 78 79 80 81 82 83
    @GetMapping(value="/{id}/horses")
    @ResponseStatus(HttpStatus.OK)
    public List<HorseDto> getOwnedHorses(@PathVariable("id") Long id) {
        LOGGER.info("GET " + BASE_URL + "/{}/horses", id);
        try {
            return horseMapper.entityListToDtoList(ownerService.getOwnedHorses(id));
        } catch (NotFoundException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Either the owner does not exist or has no horses assigned");
        }
    }

84 85
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
86
    public OwnerDto addOwner(@RequestBody OwnerDto owner) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100
        LOGGER.info("POST " + BASE_URL);
        try {
            Owner ownerEntity = ownerMapper.dtoToEntity(owner);
            return ownerMapper.entityToDto(ownerService.addOwner(ownerEntity));
        } catch (ValidationException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
                "Error during adding new owner: " + e.getMessage());
        } catch (DataAccessException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
                "Something went wrong during the communication with the database");
        }
    }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.OK)
    public OwnerDto updateOwner(@PathVariable("id") Long id, @RequestBody OwnerDto owner) {
        LOGGER.info("PUT " + BASE_URL + "/{}", id);
        try {
            Owner ownerEntity = ownerMapper.dtoToEntity(owner);
            ownerEntity.setId(id);
            return ownerMapper.entityToDto((ownerService.updateOwner(ownerEntity)));
        } catch (ValidationException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
                "Error during updating owner with id " + id + ": " + e.getMessage());
        } 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 owner could not be found");
        }
    }
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    @DeleteMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteOwner(@PathVariable("id") Long id) {
        LOGGER.info("DELETE " + BASE_URL + "/{}", id);
        try {
            ownerService.deleteOwner(id);
        } catch (DataIntegrityViolationException e) {
            LOGGER.error(e.getMessage());
            throw new ResponseStatusException(HttpStatus.FORBIDDEN,
                "The requested owner cannot be deleted because there are horses that are assigned to him/her");
        }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 owner has not been found");
        }
    }
145
}