Skip to content

Commit

Permalink
add entity
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksandrSaven committed Oct 2, 2023
1 parent ffaa383 commit 8d51559
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/project/carsharingapp/model/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.project.carsharingapp.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;

@Entity
@Data
@Table(name = "roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false, unique = true)
@Enumerated(EnumType.STRING)
private RoleName roleName;
}
6 changes: 6 additions & 0 deletions src/main/java/com/project/carsharingapp/model/RoleName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.project.carsharingapp.model;

public enum RoleName {
MANAGER,
CUSTOMER
}
48 changes: 48 additions & 0 deletions src/main/java/com/project/carsharingapp/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.project.carsharingapp.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import java.util.HashSet;
import java.util.Set;

@Entity
@Data
@SQLDelete(sql = "UPDATE users SET is_deleted = true WHERE id =?")
@Where(clause = "is_deleted=false")
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String firstName;
@Column(nullable = false)
private String lastName;
@Column(nullable = false)
private String password;
private String phone;
@Column(nullable = false)
@ManyToMany
@JoinTable(name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
@ToString.Exclude
@EqualsAndHashCode.Exclude
private Set<Role> roles = new HashSet<>();
@Column(name = "is_deleted",nullable = false)
private boolean isDeleted = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.project.carsharingapp.repository;

import com.project.carsharingapp.model.Role;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RoleRepository extends JpaRepository<Role, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.project.carsharingapp.repository;

import com.project.carsharingapp.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}
21 changes: 21 additions & 0 deletions src/main/resources/db.changelog/changes/create-roles-table.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
databaseChangeLog:
- changeSet:
id: create-roles-table
author: osa
changes:
- createTable:
tableName: roles
columns:
- column:
name: id
type: bigint
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: name
type: varchar(255)
constraints:
unique: true
nullable: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
databaseChangeLog:
- changeSet:
id: create-user-role-table
author: osa
changes:
- createTable:
tableName: user_roles
columns:
- column:
name: user_id
type: bigint
constraints:
primaryKey: true
nullable: false
references: users(id)
foreignKeyName: fk_user_roles_user_id
- column:
name: role_id
type: bigint
constraints:
primaryKey: true
nullable: false
references: roles(id)
foreignKeyName: fk_user_roles_role_id
47 changes: 47 additions & 0 deletions src/main/resources/db.changelog/changes/create-users-table.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
databaseChangeLog:
- changeSet:
id: create-users-table
author: osa
changes:
- createTable:
tableName: users
columns:
- column:
name: id
type: bigint
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: email
type: varchar(255)
constraints:
nullable: false
unique: true
- column:
name: first_name
type: varchar(255)
constraints:
nullable: false
- column:
name: last_name
type: varchar(255)
constraints:
nullable: false
- column:
name: password
type: varchar(255)
constraints:
nullable: false
- column:
name: phone
type: varchar(255)
constraints:
nullable: false
- column:
name: is_deleted
type: boolean
defaultValueBoolean: false
constraints:
nullable: false
7 changes: 7 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,7 @@
databaseChangeLog:
- include:
file: db.changelog/changes/create-roles-table.yaml
- include:
file: db.changelog/changes/create-users-table.yaml
- include:
file: db.changelog/changes/create-user-roles-table.yaml

0 comments on commit 8d51559

Please sign in to comment.