From 1fbc4460ad25887657f5de738c2d59297f1488fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Sat, 7 Dec 2024 13:43:48 +0400 Subject: [PATCH] feat: add class for models: user, achievments, benefints, exam and exam_result. Add relationship between their. --- .../java/putBit/app/models/Achievement.java | 31 ++++++++++ .../main/java/putBit/app/models/Benefit.java | 28 +++++++++ app/src/main/java/putBit/app/models/Exam.java | 29 +++++++++ .../java/putBit/app/models/ExamResult.java | 31 ++++++++++ app/src/main/java/putBit/app/models/User.java | 61 +++++++++++++++++++ .../java/putBit/app/models/enums/Role.java | 6 ++ app/src/main/resources/application.properties | 11 ++++ 7 files changed, 197 insertions(+) create mode 100644 app/src/main/java/putBit/app/models/Achievement.java create mode 100644 app/src/main/java/putBit/app/models/Benefit.java create mode 100644 app/src/main/java/putBit/app/models/Exam.java create mode 100644 app/src/main/java/putBit/app/models/ExamResult.java create mode 100644 app/src/main/java/putBit/app/models/User.java create mode 100644 app/src/main/java/putBit/app/models/enums/Role.java diff --git a/app/src/main/java/putBit/app/models/Achievement.java b/app/src/main/java/putBit/app/models/Achievement.java new file mode 100644 index 0000000..e1d40a8 --- /dev/null +++ b/app/src/main/java/putBit/app/models/Achievement.java @@ -0,0 +1,31 @@ +package putBit.app.models; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Entity(name = "achievements") +public class Achievement { + @Id + @GeneratedValue + @Column(name = "id") + private int id; + + + @Column(name = "title", unique = true, nullable = false, length = 50) + private String title ; + + @Column(name = "points", nullable = false, length = 3) + private int points ; + + @ManyToMany(mappedBy = "achievements") + List users; +} diff --git a/app/src/main/java/putBit/app/models/Benefit.java b/app/src/main/java/putBit/app/models/Benefit.java new file mode 100644 index 0000000..16b6e35 --- /dev/null +++ b/app/src/main/java/putBit/app/models/Benefit.java @@ -0,0 +1,28 @@ +package putBit.app.models; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Entity(name = "benefits") +public class Benefit { + @Id + @GeneratedValue + @Column(name = "id") + private int id; + + + @Column(name = "title", unique = true,nullable = false, length = 50) + private String title ; + + @ManyToMany(mappedBy = "benefits") + List users; +} diff --git a/app/src/main/java/putBit/app/models/Exam.java b/app/src/main/java/putBit/app/models/Exam.java new file mode 100644 index 0000000..34e2195 --- /dev/null +++ b/app/src/main/java/putBit/app/models/Exam.java @@ -0,0 +1,29 @@ +package putBit.app.models; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.UUID; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Entity(name = "exams") +public class Exam { + @Id + @GeneratedValue + @Column(name = "id") + private int id; + + + @Column(name = "title", unique = true, nullable = false, length = 50) + private String title ; + + @OneToMany(fetch = FetchType.EAGER, mappedBy = "exam") + List users; +} diff --git a/app/src/main/java/putBit/app/models/ExamResult.java b/app/src/main/java/putBit/app/models/ExamResult.java new file mode 100644 index 0000000..ca90a71 --- /dev/null +++ b/app/src/main/java/putBit/app/models/ExamResult.java @@ -0,0 +1,31 @@ +package putBit.app.models; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Entity(name = "exam_results") +public class ExamResult { + @Id + @GeneratedValue + @Column(name = "id") + private int id; + + @ManyToOne + @JoinColumn(name = "user_id", nullable = false) + private User user ; + + @ManyToOne + @JoinColumn(name = "exam_id", nullable = false) + private Exam exam ; + + @Column(name = "points", unique = true, nullable = false, length = 3) + private int points ; + +} diff --git a/app/src/main/java/putBit/app/models/User.java b/app/src/main/java/putBit/app/models/User.java new file mode 100644 index 0000000..f875042 --- /dev/null +++ b/app/src/main/java/putBit/app/models/User.java @@ -0,0 +1,61 @@ +package putBit.app.models; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import putBit.app.models.enums.Role; + +import java.util.List; +import java.util.UUID; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Entity(name = "users") +public class User { + @Id + @GeneratedValue + @Column(name = "id") + private int id; + + @Enumerated(EnumType.STRING) + @Column(name = "role", nullable = false) + private Role role; + + @Column(name = "email", nullable = false) + private String email; + + @Column(name = "password", nullable = false, length = 20) + private String password; + + @Column(name = "phone", length = 12) + private String phone; + + + @Column(name = "name", nullable = false, length = 30) + private String name; + + @Column(name = "snils", unique = true, length = 11) + private String snils; + + @ManyToMany + @JoinTable( + name = "user_achievements", + joinColumns = @JoinColumn(name = "user_id"), + inverseJoinColumns = @JoinColumn(name = "achievements_id")) + List achievements; + + @ManyToMany + @JoinTable( + name = "user_benefits", + joinColumns = @JoinColumn(name = "user_id"), + inverseJoinColumns = @JoinColumn(name = "benefits_id")) + List benefits; + + @OneToMany(fetch = FetchType.EAGER, mappedBy = "user") + List exams; + +} diff --git a/app/src/main/java/putBit/app/models/enums/Role.java b/app/src/main/java/putBit/app/models/enums/Role.java new file mode 100644 index 0000000..1b7fde8 --- /dev/null +++ b/app/src/main/java/putBit/app/models/enums/Role.java @@ -0,0 +1,6 @@ +package putBit.app.models.enums; + +public enum Role { + USER, + ADMIN, +} diff --git a/app/src/main/resources/application.properties b/app/src/main/resources/application.properties index 1e6ca6c..89358be 100644 --- a/app/src/main/resources/application.properties +++ b/app/src/main/resources/application.properties @@ -1 +1,12 @@ spring.application.name=app +spring.main.banner-mode=off +#server.port=8081 +spring.datasource.url=jdbc:h2:file:./data +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.jpa.hibernate.ddl-auto=update +spring.h2.console.enabled=true +spring.h2.console.settings.trace=false +spring.h2.console.settings.web-allow-others=false \ No newline at end of file