2023-06-19 19:12:35 +04:00

101 lines
2.2 KiB
Java

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);
}
}