Skip to content

simulations v0.6

Albert edited this page Apr 12, 2024 · 10 revisions

Pull-request (2) to add payment/card domain

pull-request Conflicts

pull-request Conflicts must be resolved: this branch has conflicts that must be resolved.

Card

savasto wants to merge 2 commits into AlbertProfe:master from savasto:master

Use the web editor or the to resolve conflicts.

image

Payment

Cath2Bcn wants to merge 2 commits into AlbertProfe:master from Cath2Bcn:master

Use the web editor or the to resolve conflicts.

image

Project structure

image

POM

pom.xml

  • new dependencies: JPA and H2
<dependency>
	<groupId>com.h2database</groupId>
	<artifactId>h2</artifactId>
	<scope>runtime</scope>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Model

JPA @OneToMany bidirectional

image

Entities

Annotations:

  • @OneToMany: Establishes one-to-many relationship. Example: Player can have multiple Simulation. Used in JPA/Hibernate. Annotation: @OneToMany(mappedBy = "Player").mappedBy: Indicates the field on the inverse side of a bidirectional relationship. Specifies the field in the owning side that maps the relationship. Example: In a bidirectional OneToMany, mappedBy points to the field in the ManyToOne side.
  • @ManyToOne: Specifies many-to-one relationship. Multiple instances are associated with one instance of another entity. Example: Many simulation belong to one player.
  • @JoinColumn: Customizes join column. Example: Specifies foreign key column in @ManyToOne or @OneToOne relationships. Annotation: @JoinColumn(name = "PLAYER_FK").
  • @JsonIgnore: Excludes field from serialization/deserialization. Example: Hides sensitive or irrelevant data in JSON responses. Annotation: @JsonIgnore.

Player

image

package com.example.demo.model;
//.. imports

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Player {

    @Id
    private String id;
    private String player;
    private int age;
    private boolean active;

    @OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
    private List<Simulation> simulations = new ArrayList<>();

    @OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
    private List<Subscription> subscriptions = new ArrayList<>();

    @OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
    private List<Payment> payments = new ArrayList<>();

    @OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
    private List<Card> cards = new ArrayList<>();

    public void addSimulation(Simulation simulation) {
        this.getSimulations().add(simulation);
        //if (simulation.getPlayer() != null) simulation.getPlayer().getSimulations().remove(simulation);
        simulation.setPlayer(this);
    }

    public void addSubscription(Subscription subscription) {
        this.getSubscriptions().add(subscription);
        //if (subscription.getgetPlayer() != null) subscription.getPlayer().getSubscriptions().remove(subscription);
        subscription.setPlayer(this);
    }

    public void addPayment(Payment payment) {
        this.getPayments().add(payment);
        //if (simulation.getId() != null) simulation.getId().getSimulations().remove(simulation);
        payment.setPlayer(this);
    }

    public void addCard(Card card) {
        this.getCards().add(card);
        card.setPlayer(this);
    }
}

Payment

package com.example.demo.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;

@Data
@NoArgsConstructor @AllArgsConstructor
@Entity
public class Payment {

    @Id
    private String id;
    private String date;
    private double cost;
    private String concept;
    private boolean isPaid;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="PLAYER_FK")
    private Player player;

}

Card

package com.example.demo.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor @AllArgsConstructor
@Entity
public class Card {

    @Id
    private String id;
    private int numberOfCard;
    private int maxCredit;
    private int expDate;
    private boolean active;
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="PLAYER_FK")
    private Player player;

}

DataBase

H2 in-memory configuration

datasource

application.properties

server.port=8089

#H2 DATASOURCE
spring.datasource.url=jdbc:h2:mem:0f74afd0-eefb-4b37-af9f-64249cb2ffa2
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

#DDL
spring.jpa.hibernate.ddl-auto=create-drop

CRUD Repository

package com.example.demo.repository;

import com.example.demo.model.Payment;
import org.springframework.data.repository.CrudRepository;

public interface PaymentRepository extends CrudRepository<Payment, String> {}
package com.example.demo.repository;

import com.example.demo.model.Card;
import org.springframework.data.repository.CrudRepository;

public interface CardRepository extends CrudRepository<Card, String> {}

Populate DB with fake data

Fake data for subscription object

Command line runner

Runner

RunnerFillingDB.java

Service to Populate DB

Player & Payment/Card

Controller

Rest Controller

Output

H2 console

http://localhost:8089/h2-console/

Postman API Rest

postman api docs simulation

http://localhost:8089/api/v1/player/players

http://localhost:8089/api/v1/payment/payments

http://localhost:8089/api/v1/card/cards

image

image

image