| /* |
| ******************************************************************************* |
| * Copyright (c) 2019 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ******************************************************************************* |
| */ |
| package org.eclipse.openk.contactbasedata.controller; |
| |
| import io.swagger.annotations.ApiOperation; |
| import io.swagger.annotations.ApiResponse; |
| import io.swagger.annotations.ApiResponses; |
| import lombok.extern.log4j.Log4j2; |
| import org.eclipse.openk.contactbasedata.exceptions.BadRequestException; |
| import org.eclipse.openk.contactbasedata.exceptions.ConflictException; |
| import org.eclipse.openk.contactbasedata.service.SalutationService; |
| import org.eclipse.openk.contactbasedata.viewmodel.SalutationDto; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.http.HttpStatus; |
| import org.springframework.http.ResponseEntity; |
| import org.springframework.security.access.annotation.Secured; |
| import org.springframework.validation.annotation.Validated; |
| import org.springframework.web.bind.annotation.*; |
| import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
| |
| import java.net.URI; |
| import java.util.List; |
| import java.util.UUID; |
| |
| @Log4j2 |
| @RestController |
| @RequestMapping("/salutations") |
| public class SalutationController { |
| @Autowired |
| private SalutationService salutationService; |
| |
| @ApiOperation(value = "Anzeigen aller Anreden") |
| @Secured({"ROLE_KON-READER", "ROLE_KON-WRITER", "ROLE_KON-ADMIN"}) |
| @ApiResponses(value = { @ApiResponse(code = 200, message = "Erfolgreich durchgeführt")}) |
| @ResponseStatus(HttpStatus.OK) |
| @GetMapping |
| public List<SalutationDto> getSalutations() { |
| return salutationService.findAllSalutations(); |
| } |
| |
| |
| |
| @GetMapping("/{salutationUuid}") |
| @Secured({"ROLE_KON-READER", "ROLE_KON-WRITER", "ROLE_KON-ADMIN"}) |
| @ApiOperation(value = "Suchen einer Anrede per UUID") |
| @ResponseStatus(HttpStatus.OK) |
| @ApiResponses(value = { |
| @ApiResponse(code = 400, message = "Ungültige Anfrage."), |
| @ApiResponse(code = 200, message = "Anrede gefunden")}) |
| public SalutationDto getSalutation(@PathVariable UUID salutationUuid) { |
| |
| return salutationService.getSalutationByUuid(salutationUuid); |
| } |
| |
| |
| @PostMapping |
| @Secured("ROLE_KON-ADMIN") |
| @ApiOperation(value = "Anlegen einer neuen Anrede") |
| @ApiResponses(value = { |
| @ApiResponse(code = 200, message = "Anrede erfolgreich angelegt"), |
| @ApiResponse(code = 500, message = "Konnte nicht durchgeführt werden") |
| }) |
| public ResponseEntity<SalutationDto> insertSalutation(@Validated @RequestBody SalutationDto salutationDto) { |
| SalutationDto savedSalutationDto = salutationService.insertSalutation(salutationDto); |
| URI location = ServletUriComponentsBuilder |
| .fromCurrentRequestUri() |
| .path("/{uuid}") |
| .buildAndExpand(savedSalutationDto.getUuid()) |
| .toUri(); |
| return ResponseEntity.created(location).body(savedSalutationDto); |
| } |
| |
| |
| @PutMapping("/{salutationUuid}") |
| @Secured("ROLE_KON-ADMIN") |
| @ApiOperation(value = "Ändern einer Anrede") |
| @ApiResponses(value = { |
| @ApiResponse(code = 200, message = "Anrede wurde erfolgreich aktualisiert"), |
| @ApiResponse(code = 400, message = "Ungültige Eingabe"), |
| @ApiResponse(code = 404, message = "Nicht gefunden")}) |
| public ResponseEntity updateSalutation(@PathVariable UUID salutationUuid, @Validated @RequestBody SalutationDto salutationDto) { |
| |
| if (!salutationDto.getUuid().equals(salutationUuid)) { |
| throw new BadRequestException("invalid.uuid.path.object"); |
| } |
| |
| salutationService.updateSalutation(salutationDto); |
| return ResponseEntity.ok().build(); |
| } |
| |
| |
| @DeleteMapping("/{uuid}") |
| @Secured("ROLE_KON-ADMIN") |
| @ResponseStatus(HttpStatus.OK) |
| @ApiOperation(value = "Anrede löschen") |
| @ApiResponses(value = { |
| @ApiResponse(code = 204, message = "Erfolgreich durchgeführt"), |
| @ApiResponse(code = 400, message = "Ungültige Anfrage"), |
| @ApiResponse(code = 404, message = "Nicht gefunden"), |
| @ApiResponse(code = 409, message = "Datensatz konnte nicht gelöscht werden")}) |
| public void removeSalutation(@PathVariable UUID uuid) { |
| try { |
| salutationService.removeSalutation(uuid); |
| } |
| catch( Exception ex ) { |
| throw new ConflictException("Salutation couldn't be deleted"); |
| } |
| } |
| |
| } |