Skip to content

simulations v0.7

Albert edited this page Apr 24, 2024 · 19 revisions

Pull to update local from remote subscription/payment/card domain

image

tracking connections in Git

By default, branches in Git have nothing to do with each other. However, when you tell a local branch to "track" a remote branch, you create a connection between these two branches. Your local branch now has a "counterpart" on the remote server.

$ git checkout --track origin/dev
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'

git fetch

The git fetch command downloads commits, files, and refs from a remote repository into your local repo.

Fetching is what you do when you want to see what everybody else has been working on. It lets you see how the central history has progressed, but it doesn’t force you to actually merge the changes into your repository. Git isolates fetched content from existing local content; it has absolutely no effect on your local development work.

git pull

git pull is actually an alias to 2 commands: git fetch + git merge

# Update your local repo with the latest code:
git fetch --all --prune

# Merge the changes (you already did a pull)`
git merge origin master

remote repo

repo by enchufin with the social domain done and ready to add upstream.

image

remote repo, albertprofe, is adding commits day after day. Thus remote repo, enchufin. is a lot of commits behind.

image

Important

So, in this case, enchufin is going to:

  1. update/sync his local repo with the upstream updates (git checkout & pull)
  2. solving the conflicts with merge local IntellJIdea tool on a new branch (AlbertProfe-master)
  3. create remote repo branch (AlbertProfe-master)
  4. push to remote enchufin repo branch (AlbertProfe-master)
  5. and after that enchufin will make a pull-request to albertprofe with social domain with no conflicts.

And albertprofe will:

  1. accept the pull-request with no conflicts
  2. update his local repo

Download albertprofe project to enchufin and solve conflicts

First at all, enchufin will download the project to local:

image

warning zoom

First at all: checkout

git checkout -b <new-branch> <existing-branch>

By default git checkout -b will base the new-branch off the current HEAD. An optional additional branch parameter can be passed to git checkout. In the above example, <existing-branch> is passed which then bases new-branch off of existing-branch instead of the current HEAD.

And then, git pull

git pull https://github.com/AlbertProfe/simulations.git master

Then with IntellJIdea tool enchufin will resolve Git conflicts and then merge-commit.

image

image

image

enchufin pull-request

After that, enchufin will make a pull-request:

image

The pull-request in this case will have no conflict! (on github cloud)

image

image

AlbertProfe merged commit f1812d4 into AlbertProfe:master now

update albertprofe local

Last step, update local project albertprofe:

image

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

Model

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

package com.example.demo.model;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;


@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<>();

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

    public void addSimulation(Simulation simulation) {
        this.getSimulations().add(simulation);
        //if (simulation.getId() != null) simulation.getId().getSimulations().remove(simulation);
        simulation.setPlayer(this);
    }
    public void addSocial(Social social) {
        this.getSocials().add(social);
        social.setPlayer(this);
    }


    public void addCard(Card card) {
        this.getCards().add(card);
        card.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 addSubscription(Subscription subscription) {
        this.getSubscriptions().add(subscription);
        //if (subscription.getId() != null) subscription.getId().getSubscriptions().remove(subscription);
        subscription.setPlayer(this);

    }
}

Social

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.ArrayList;
import java.util.List;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Social {
    @Id
    private String id;
    private String name;
    private boolean isActive;
    private int numberDays;
    private String socialUrl;
    private String icon;
    @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.Social;
import org.springframework.data.repository.CrudRepository;

public interface SocialRepository extends CrudRepository<Social, String> {}

Populate DB with fake data

Fake data for social object

Command line runner

Runner

RunnerFillingDB.java

Service to Populate DB

Player & Social

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/social/socials

image

Clone this wiki locally