This commit is contained in:
1yuee 2023-06-19 19:12:35 +04:00
parent 8ae10007bb
commit 0fb27f8d13
5 changed files with 378 additions and 0 deletions

View File

@ -0,0 +1,77 @@
package com.webproglabs.lab1.lab34.model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
@Column(unique = true)
private String Name;
@OneToMany(mappedBy = "Category")
private List<Product> Products = new ArrayList<>();
public Category(String Name) {
this.Name = Name;
}
public Category(){}
public Long getId() {
return Id;
}
public String getName() {
return Name;
}
public List<Product> getProducts() {
return Products;
}
public void setName(String Name) {
this.Name = Name;
}
public void addProduct(Product Product) {
this.Products.add(Product);
Product.setCategory(this);
}
public void removeProduct(Product Product) {
this.Products.remove(Product);
Product.setCategory(null);
}
public void setProducts (List<Product> Products) {
this.Products = Products;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Category category = (Category) o;
return Objects.equals(Id, category.Id);
}
@Override
public int hashCode() {
return Objects.hash(Id);
}
@Override
public String toString() {
return "Category{" +
"Id=" + Id +
", Name='" + Name +
'}';
}
}

View File

@ -0,0 +1,103 @@
package com.webproglabs.lab1.lab34.model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
private String Name;
public Double Cost;
@ManyToOne()
private Shop Shop;
@ManyToOne()
private Category Category;
@ManyToMany()
private List<User> Users = new ArrayList<>();
public Product (String Name, Double Cost, Shop Shop, Category Category) {
this.Name = Name;
this.Shop = Shop;
this.Cost = Cost;
this.Category = Category;
}
public Product(){}
public Long getId() {
return Id;
}
public String getName() {
return Name;
}
public Double getCost(){ return Cost; }
public Shop getShop() {
return Shop;
}
public Category getCategory() {
return Category;
}
public List<User> getUsers() {
return Users;
}
public void setName(String Name) {
this.Name = Name;
}
public void setCost(Double Cost) { this.Cost = Cost; }
public void setShop(Shop Shop) {
this.Shop.removeProduct(this);
Shop.addProduct(this);
}
public void setCategory(Category Category) {
this.Category.removeProduct(this);
Category.addProduct(this);
}
public void addUser(User User) {
Users.add(User);
}
public void removeUser(User User) {
Users.remove(User);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
return Objects.equals(Id, product.Id);
}
@Override
public int hashCode() {
return Objects.hash(Id);
}
@Override
public String toString() {
return "Product{" +
"Id=" + Id +
", name='" + Name +
'}';
}
}

View File

@ -0,0 +1,75 @@
package com.webproglabs.lab1.lab34.model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Shop {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
@Column(unique = true)
private String Name;
@OneToMany(mappedBy = "Shop")
private List<Product> Products = new ArrayList<>();
public Shop(String Name) {
this.Name = Name;
}
public Shop(){}
public Long getId() {
return Id;
}
public String getName() {
return Name;
}
public List<Product> getProducts() {
return Products;
}
public void setName(String Name) {
this.Name = Name;
}
public void addProduct(Product Product) {
this.Products.add(Product);
Product.setShop(this);
}
public void removeProduct(Product Product) {
this.Products.remove(Product);
Product.setShop(null);
}
public void setProducts (List<Product> Products) {
this.Products = Products;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Shop shop = (Shop) o;
return Objects.equals(Id, shop.Id);
}
@Override
public int hashCode() {
return Objects.hash(Id);
}
@Override
public String toString() {
return "Shop{" +
"Id=" + Id +
", Name='" + Name +
'}';
}
}

View File

@ -0,0 +1,100 @@
package com.webproglabs.lab1.lab34.model;
import com.webproglabs.lab1.lab34.model.enums.UserRole;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
@Column(nullable = false, unique = true, length = 64)
@NotBlank
@Size(min = 3, max = 32)
private String login;
@Column(nullable = false, length = 64)
@NotBlank
@Size(min = 6, max = 32)
private String Password;
@ManyToMany(mappedBy = "Users")
private List<Product> Products = new ArrayList<>();
private UserRole Role;
public User(String login, String password, UserRole role) {
this.login = login;
this.Password = password;
this.Role = role;
}
public User(String Login, String Password) {
this(Login, Password, UserRole.USER);
}
public User() {}
public Long getId() {
return Id;
}
public String getLogin() {
return login;
}
public void setLogin(String Login) {
this.login = Login;
}
public String getPassword() {
return Password;
}
public void setPassword(String Password) {
this.Password = Password;
}
public List<Product> getProducts() {
return Products;
}
public void addProduct(Product Product) {
this.Products.add(Product);
Product.addUser(this);
}
public void removeProduct(Product Product) {
this.Products.remove(Product);
Product.removeUser(this);
}
public UserRole getRole() {
return Role;
}
public void setRole(UserRole Role) {
this.Role = Role;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(Id, user.Id) && Objects.equals(login, user.login);
}
@Override
public int hashCode() {
return Objects.hash(Id, login);
}
}

View File

@ -0,0 +1,23 @@
package com.webproglabs.lab1.lab34.model.enums;
import org.springframework.security.core.GrantedAuthority;
public enum UserRole implements GrantedAuthority {
ADMIN,
USER,
;
private static final String PREFIX = "ROLE_";
@Override
public String getAuthority() {
return PREFIX + this.name();
}
public static final class AsString {
public static final String ADMIN = PREFIX + "ADMIN";
public static final String USER = PREFIX + "USER";
}
}