94fead6be729cfb23b657d853c5499709a3d27c4..c4d8396bde66ac81731b267747c524c0d9fe98c6
9 days ago Fibinger Ádám
Force example data to UTF-8
c4d839 diff | tree
9 days ago Fibinger Ádám
Decoupling version bump
bcfe33 diff | tree
9 days ago Fibinger Ádám
Decoupling final version
12abae diff | tree
1 files deleted
1 files added
1 files renamed
8 files modified
140 ■■■■■ changed files
pom.xml 2 ●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/CardController.java 52 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java 41 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInvalidUpdateException.java 15 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardCreateInterface.java 1 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardDeleteInterface.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInteractorInterface.java 3 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInvalidUpdateException.java 16 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardNotFoundException.java 2 ●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/rest/RestCard.java 2 ●●●●● patch | view | raw | blame | history
src/main/resources/application.properties 4 ●●● patch | view | raw | blame | history
pom.xml
@@ -5,7 +5,7 @@
    <groupId>hu.unr.fiber</groupId>
    <artifactId>cardapi</artifactId>
    <version>0.1.2</version>
    <version>0.1.3</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
src/main/java/hu/unr/fiber/cardapi/CardController.java
@@ -1,16 +1,20 @@
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 {
@@ -58,57 +62,32 @@
        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();
@@ -116,5 +95,4 @@
                .created(newCardURI)
                .body(newCardURI.toString());
    }
    */
}
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java
@@ -5,6 +5,8 @@
import hu.unr.fiber.cardapi.interfaces.CardInteractorInterface;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import hu.unr.fiber.cardapi.interfaces.CardInvalidUpdateException;
import hu.unr.fiber.cardapi.interfaces.CardNotFoundException;
import hu.unr.fiber.cardapi.rest.RestCard;
import org.springframework.orm.jpa.JpaObjectRetrievalFailureException;
import org.springframework.stereotype.Component;
@@ -35,7 +37,7 @@
    @Override
    public CardInterface getCardById(long id) throws CardNotFoundException {
        if (!this.cardRepository.existsById(id)) {
            throw new CardNotFoundException("No such card with id: #"+ id);
            throw new CardNotFoundException("No such card with id: #" + id);
        }
        Card c = this.cardRepository.getOne(id);
@@ -57,19 +59,48 @@
    @Override
    public CardInterface update(long id, CardInterface updatedCard) throws CardInvalidUpdateException, CardNotFoundException {
        if (updatedCard.validId() && (updatedCard.getId() != id)) {
            throw new CardInvalidUpdateException("Card ID cannot be changed! ", updatedCard);
            throw new CardInvalidUpdateException("Card ID cannot be changed! ",
                    CardInvalidUpdateException.CARD_ID_CHANGE);
        }
        if (this.getCardByNumber(updatedCard.getNumber()) != null) {
            throw new CardInvalidUpdateException("Given card number already exists", updatedCard);
        Card oldCard = this.getCardByNumber(updatedCard.getNumber());
        if (oldCard == null) {
            oldCard = this.cardRepository.getOne(id);
        } else {
            if (oldCard.getId() != id) {
                //The given card Number is already assigned to an another card with different ID
                String message = "Cannot update card number, given card number already assigned to card with ID #" + oldCard.getId();
                throw new CardInvalidUpdateException(message, CardInvalidUpdateException.CARD_NUMBER_ALREADY_ASSIGNED);
            }
        }
        this.cardRepository.saveAndFlush((Card) updatedCard);
        oldCard.update(updatedCard);
        this.cardRepository.saveAndFlush(oldCard);
        return this.getCardById(updatedCard.getId());
    }
    @Override
    public CardInterface create(CardInterface card) throws CardInvalidUpdateException {
        Card duplicateNumberedCard = this.getCardByNumber(card.getNumber());
        if (duplicateNumberedCard != null) {
            throw new CardInvalidUpdateException("Card with the same number already exists!",
                    CardInvalidUpdateException.CARD_NUMBER_ALREADY_ASSIGNED);
        }
        Card c = new Card();
        c.update(card);
        this.cardRepository.saveAndFlush(c);
        return c;
    }
    private Card getCardByNumber(String number) {
        Long id = cardRepository.findIdByNumber(number);
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInvalidUpdateException.java
File was deleted
src/main/java/hu/unr/fiber/cardapi/interfaces/CardCreateInterface.java
@@ -1,4 +1,5 @@
package hu.unr.fiber.cardapi.interfaces;
public interface CardCreateInterface {
    public CardInterface create(CardInterface card) throws Exception;
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardDeleteInterface.java
@@ -1,7 +1,5 @@
package hu.unr.fiber.cardapi.interfaces;
import hu.unr.fiber.cardapi.hibernate.CardNotFoundException;
public interface CardDeleteInterface {
    public void delete(long id) throws CardNotFoundException;
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInteractorInterface.java
@@ -4,6 +4,7 @@
        CardByIdInterface,
        CardListBoundaryInterface,
        CardDeleteInterface,
        CardModifyInterface {
        CardModifyInterface,
        CardCreateInterface {
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInvalidUpdateException.java
New file
@@ -0,0 +1,16 @@
package hu.unr.fiber.cardapi.interfaces;
public class CardInvalidUpdateException extends Exception {
    public static final int CARD_ID_CHANGE = 0;
    public static final int CARD_NUMBER_ALREADY_ASSIGNED = 1;
    protected int code;
    public CardInvalidUpdateException(String message, int code) {
        super(message);
        this.code = code;
    }
    public int getCode() {
        return code;
    }
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardNotFoundException.java
File was renamed from src/main/java/hu/unr/fiber/cardapi/hibernate/CardNotFoundException.java
@@ -1,4 +1,4 @@
package hu.unr.fiber.cardapi.hibernate;
package hu.unr.fiber.cardapi.interfaces;
public class CardNotFoundException extends Exception {
    public CardNotFoundException(String message) {
src/main/java/hu/unr/fiber/cardapi/rest/RestCard.java
@@ -1,5 +1,6 @@
package hu.unr.fiber.cardapi.rest;
import com.fasterxml.jackson.annotation.JsonCreator;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import java.util.Objects;
@@ -24,6 +25,7 @@
                .setNumber(number);
    }
    @JsonCreator
    public RestCard(long id, String name, String number, String cardHolder) {
        this.setId(id)
                .setName(name)
src/main/resources/application.properties
@@ -7,4 +7,6 @@
# Show all queries
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=trace
logging.level.org.hibernate.type=trace
spring.datasource.sqlScriptEncoding=UTF-8