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

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

9
import java.lang.invoke.MethodHandles;
10 11
import java.util.List;
import java.util.Map;
12 13

import at.ac.tuwien.sepm.assignment.individual.util.ValidationException;
14 15 16
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.dao.DataAccessException;
18
import org.springframework.dao.DataIntegrityViolationException;
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
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;

    @Autowired
    public OwnerEndpoint(OwnerService ownerService, OwnerMapper ownerMapper) {
        this.ownerService = ownerService;
        this.ownerMapper = ownerMapper;
    }

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

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    @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());
        }
    }

68 69
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
70
    public OwnerDto addOwner(@RequestBody OwnerDto owner) {
71 72 73 74 75 76 77 78 79 80 81 82 83 84
        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");
        }
    }
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

    @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");
        }
    }
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

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