Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
dev/db/Добавил миграцию базы данных (#3)
Browse files Browse the repository at this point in the history
* dev/db/Добавил миграцию базы данных

* dev/db/Добавил entity для бд

* dev/db/Добавил jpa репозитории
  • Loading branch information
AlexanderGarifullin authored Dec 9, 2024
1 parent 48f993d commit 4d49095
Show file tree
Hide file tree
Showing 20 changed files with 397 additions and 1 deletion.
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ repositories {
}

dependencies {
// spring
implementation 'org.springframework.boot:spring-boot-starter-web'

// db
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.liquibase:liquibase-core:4.29.2'
implementation 'org.postgresql:postgresql:42.7.4'

// lombok
implementation 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
Expand Down
Empty file added docs/dataModel/schema.sql
Empty file.
34 changes: 34 additions & 0 deletions src/main/java/com/cf/cfteam/models/entities/codeforces/CfUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cf.cfteam.models.entities.codeforces;

import jakarta.persistence.*;
import lombok.*;

import java.time.Instant;

@Builder
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = "group")
@Entity
@Table(name = "cf_users", schema = "codeforces")
public class CfUser {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "c_name", nullable = false)
private String name;

@Column(name = "c_description", nullable = true)
private String description;

@Column(name = "с_time", nullable = false)
private Instant createdTime;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "c_group_id", nullable = false)
private CfUsersGroup group;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.cf.cfteam.models.entities.codeforces;

import com.cf.cfteam.models.entities.security.User;
import jakarta.persistence.*;
import lombok.*;

import java.time.Instant;
import java.util.List;

@Builder
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = {"user", "cfUsers"})
@Entity
@Table(name = "t_cf_users_groups", schema = "codeforces")
public class CfUsersGroup {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "c_name", nullable = false)
private String name;

@Column(name = "c_description", nullable = true)
private String description;

@Column(name = "с_time", nullable = false)
private Instant createdTime;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "c_user_id", nullable = false)
private User user;

@OneToMany(mappedBy = "group", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CfUser> cfUsers;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.cf.cfteam.models.entities.codeforces;

import jakarta.persistence.*;
import lombok.*;

import java.time.Instant;

@Builder
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = "group")
@Entity
@Table(name = "t_cf_teams", schema = "codeforces")
public class CfUsersTeam {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "c_name", nullable = false)
private String name;

@Column(name = "c_description", nullable = true)
private String description;

@Column(name = "c_first_user_login", nullable = true)
private String firstUser;

@Column(name = "c_second_user_login", nullable = true)
private String secondUser;

@Column(name = "c_third_user_login", nullable = true)
private String thirdUser;

@Column(name = "с_time", nullable = false)
private Instant createdTime;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "c_group_id", nullable = false)
private CfUsersTeamsGroup group;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.cf.cfteam.models.entities.codeforces;

import com.cf.cfteam.models.entities.security.User;
import jakarta.persistence.*;
import lombok.*;

import java.time.Instant;
import java.util.List;

@Builder
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = {"user", "cfTeams"})
@Entity
@Table(name = "t_cf_users_teams_groups", schema = "codeforces")
public class CfUsersTeamsGroup {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "c_name", nullable = false)
private String name;

@Column(name = "c_description", nullable = true)
private String description;

@Column(name = "с_time", nullable = false)
private Instant createdTime;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "c_user_id", nullable = false)
private User user;

@OneToMany(mappedBy = "group", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CfUsersTeam> cfTeams;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.cf.cfteam.models.entities.security;

public enum Role {
USER,
ADMIN
}
34 changes: 34 additions & 0 deletions src/main/java/com/cf/cfteam/models/entities/security/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cf.cfteam.models.entities.security;

import jakarta.persistence.*;
import lombok.*;

import java.time.Instant;

@Builder
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = "user")
@Entity
@Table(name = "t_tokens", schema = "security")
public class Token {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "c_token", nullable = false)
private String token;

@Column(name = "c_revoked", nullable = false)
private boolean revoked;

@Column(name = "с_time", nullable = false)
private Instant createdTime;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "c_user_id", nullable = false)
private User user;
}
41 changes: 41 additions & 0 deletions src/main/java/com/cf/cfteam/models/entities/security/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.cf.cfteam.models.entities.security;

import jakarta.persistence.*;
import lombok.*;

import java.time.Instant;
import java.util.List;

@Builder
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = "tokens")
@Entity
@Table(name = "t_users", schema = "security")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "c_name", nullable = false)
private String name;

@Column(name = "c_login", unique = true, nullable = false)
private String login;

@Column(name = "c_hashed_password", nullable = false)
private String hashedPassword;

@Enumerated(EnumType.STRING)
@Column(name = "c_role", nullable = false)
private Role role;

@Column(name = "с_time", nullable = false)
private Instant createdTime;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Token> tokens;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cf.cfteam.repositories.jpa.codeforces;

import com.cf.cfteam.models.entities.codeforces.CfUser;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CfUserRepository extends JpaRepository<CfUser,Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cf.cfteam.repositories.jpa.codeforces;

import com.cf.cfteam.models.entities.codeforces.CfUsersGroup;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CfUsersGroupRepository extends JpaRepository<CfUsersGroup, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cf.cfteam.repositories.jpa.codeforces;

import com.cf.cfteam.models.entities.codeforces.CfUsersTeam;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CfUsersTeamRepository extends JpaRepository<CfUsersTeam, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cf.cfteam.repositories.jpa.codeforces;

import com.cf.cfteam.models.entities.codeforces.CfUsersTeamsGroup;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CfUsersTeamsGroupRepository extends JpaRepository<CfUsersTeamsGroup, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.cf.cfteam.repositories.jpa.security;

import com.cf.cfteam.models.entities.security.Token;
import com.cf.cfteam.models.entities.security.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface TokenRepository extends JpaRepository<Token, Long> {
Optional<Token> findByToken(String token);

List<Token> findAllByUserAndRevoked(User user, boolean revoked);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cf.cfteam.repositories.jpa.security;

import com.cf.cfteam.models.entities.security.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByLogin(String login);
}
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
spring:
application:
name: T-Bank2024.Fintech-Java-dev-Spring
datasource:
url: jdbc:postgresql://${POSTGRES_CONTAINER_NAME:localhost}:${POSTGRES_PORT:5432}/${POSTGRES_DB:CodeforcesLocalDB}
username: ${POSTGRES_USER:postgres}
password: ${POSTGRES_PASSWORD:123}
driver-class-name: org.postgresql.Driver
profiles:
active: prod
52 changes: 52 additions & 0 deletions src/main/resources/db/changelog/codeforces-changelog.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--liquibase formatted sql

--changeset alexandergarifullin:1
CREATE SCHEMA IF NOT EXISTS codeforces;

--changeset alexandergarifullin:2
CREATE TABLE codeforces.t_cf_users_teams_groups
(
id BIGSERIAL PRIMARY KEY,
c_name TEXT NOT NULL,
c_description TEXT,
с_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
c_user_id BIGINT NOT NULL,
CONSTRAINT fk_team_group_user FOREIGN KEY (c_user_id) REFERENCES security.t_users (id)
);

--changeset alexandergarifullin:3
CREATE TABLE codeforces.t_cf_teams
(
id BIGSERIAL PRIMARY KEY,
c_name TEXT NOT NULL,
c_description TEXT,
c_first_user_login TEXT,
c_second_user_login TEXT,
c_third_user_login TEXT,
с_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
c_group_id BIGINT NOT NULL,
CONSTRAINT fk_cf_team_team_group FOREIGN KEY (c_group_id ) REFERENCES codeforces.t_cf_users_teams_groups (id)
);

--changeset alexandergarifullin:4
CREATE TABLE codeforces.t_cf_users_groups
(
id BIGSERIAL PRIMARY KEY,
c_name TEXT NOT NULL,
c_description TEXT,
с_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
c_user_id BIGINT NOT NULL,
CONSTRAINT fk_group_user FOREIGN KEY (c_user_id) REFERENCES security.t_users (id)
);

--changeset alexandergarifullin:5
CREATE TABLE codeforces.cf_users
(
id BIGSERIAL PRIMARY KEY,
c_name TEXT NOT NULL,
c_description TEXT,
с_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
c_group_id BIGINT NOT NULL,
CONSTRAINT fk_cf_user_group FOREIGN KEY (c_group_id) REFERENCES codeforces.t_cf_users_groups (id)
);

5 changes: 5 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
databaseChangeLog:
- include:
file: db/changelog/security-changelog.sql
- include:
file: db/changelog/codeforces-changelog.sql
Loading

0 comments on commit 4d49095

Please sign in to comment.