| | |
| | | package hu.unr.fiber.cardapi; |
| | | |
| | | import java.net.URI; |
| | | import java.util.List; |
| | | |
| | | import hu.unr.fiber.cardapi.hibernate.CardNotFoundException; |
| | | import hu.unr.fiber.cardapi.interfaces.CardInvalidUpdateException; |
| | | import hu.unr.fiber.cardapi.interfaces.CardNotFoundException; |
| | | import hu.unr.fiber.cardapi.interfaces.CardInterface; |
| | | import hu.unr.fiber.cardapi.hibernate.CardInteractor; |
| | | import hu.unr.fiber.cardapi.rest.RestCard; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.server.ResponseStatusException; |
| | | import org.springframework.web.util.UriComponentsBuilder; |
| | | |
| | | @RestController |
| | | public class CardController { |
| | |
| | | |
| | | return ResponseEntity.ok().body("OK"); |
| | | } |
| | | /* |
| | | |
| | | @PostMapping(value = "/card/{id}") |
| | | public ResponseEntity<String> updateCard(@PathVariable("id") long id, @RequestBody RestCard updatedCardEntity, UriComponentsBuilder ucBuilder) { |
| | | |
| | | logger.info("POST /card/" + id + " called, card update."); |
| | | try { |
| | | this.cardService.update(id, updatedCardEntity); |
| | | } catch (CardInvalidUpdateException e){ |
| | | |
| | | } catch (CardInvalidUpdateException e) { |
| | | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); |
| | | } catch (CardNotFoundException e) { |
| | | throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id, e); |
| | | } |
| | | |
| | | |
| | | RestCard originalCardEntity = this.cardRepository.getOne(id); |
| | | |
| | | if (originalCardEntity.equals(updatedCardEntity)) { |
| | | return ResponseEntity.accepted().body("Update has no changes."); |
| | | } |
| | | |
| | | //card number updated, we have to check if its already exists |
| | | if (!originalCardEntity.getNumber().equals(updatedCardEntity.getNumber()) && this.getCardByNumber(updatedCardEntity.getNumber()) != null) { |
| | | logger.error("Unable to update card with id {}. A different RestCard with number {} already exist", originalCardEntity.getId(), updatedCardEntity.getNumber()); |
| | | return ResponseEntity.status(HttpStatus.CONFLICT).body("RestCard with number " + updatedCardEntity.getNumber() + " already exists."); |
| | | } |
| | | |
| | | originalCardEntity.update(updatedCardEntity); |
| | | this.cardRepository.flush(); |
| | | |
| | | return ResponseEntity.accepted().body("OK"); |
| | | } |
| | | |
| | | private RestCard getCardByNumber(String number) { |
| | | |
| | | Long id = cardRepository.findIdByNumber(number); |
| | | |
| | | if (id == null) { |
| | | return null; |
| | | } |
| | | |
| | | return cardRepository.getOne(id); |
| | | } |
| | | |
| | | @PostMapping(value = "/card") |
| | | public ResponseEntity<String> createCard(@RequestBody RestCard card, UriComponentsBuilder ucBuilder) { |
| | | logger.info("Creating RestCard : {}", card.getNumber()); |
| | | |
| | | if (this.getCardByNumber(card.getNumber()) != null) { |
| | | logger.error("Unable to create. A RestCard with number {} already exist", card.getNumber()); |
| | | return ResponseEntity.status(HttpStatus.CONFLICT).body("RestCard with number " + card.getNumber() + " already exists."); |
| | | try { |
| | | CardInterface c = this.cardService.create(card); |
| | | card.setId(c.getId()); |
| | | } catch (CardInvalidUpdateException e) { |
| | | throw new ResponseStatusException(HttpStatus.CONFLICT, e.getMessage()); |
| | | } |
| | | |
| | | this.cardRepository.saveAndFlush(card); |
| | | |
| | | URI newCardURI = ucBuilder.path("/card/{id}").buildAndExpand(card.getId()).toUri(); |
| | | |
| | |
| | | .created(newCardURI) |
| | | .body(newCardURI.toString()); |
| | | } |
| | | */ |
| | | } |