This commit is contained in:
1yuee 2023-06-20 09:34:21 +04:00
parent 8ae10007bb
commit e656fe0664
5 changed files with 382 additions and 0 deletions

View File

@ -0,0 +1,82 @@
package com.webproglabs.lab1.models;
import javax.persistence.*;
import java.util.Objects;
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String text;
@ManyToOne()
private User owner;
@ManyToOne()
private Post post;
public Comment(){};
public Comment(String text, User owner, Post post) {
this.text = text;
this.owner = owner;
this.post = post;
}
public Long getId() {
return id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public User getOwner() {
return owner;
}
public void setOwner(User owner) {
this.owner.getComments().remove(this);
this.owner = owner;
if (!owner.getComments().contains(this)) {
owner.getComments().add(this);
}
}
public Post getPost() {return post; }
public void setPost(Post post) {
this.post.getComments().remove(this);
this.post = post;
if (!post.getComments().contains(this)) {
post.getComments().add(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Comment comment = (Comment) o;
return Objects.equals(id, comment.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String ToString() {
return "Comment{" +
"id=" + id +
", text='" + text + '\'' +
", owner='" + owner.ToString() + '\'' +
", post='" + post.ToString() + '\'' +
'}';
}
}

View File

@ -0,0 +1,97 @@
package com.webproglabs.lab1.models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String text;
@OneToMany(mappedBy = "post", orphanRemoval = true, fetch = FetchType.EAGER)
private List<Comment> comments = new ArrayList<Comment>();
@ManyToOne()
private User author;
@ManyToOne()
private Topic topic;
public Post(){}
public Post(String text, User author, Topic topic) {
this.text = text;
this.author = author;
this.topic = topic;
}
public Long getId() {
return id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public User getAuthor() {
return author;
}
public void setAuthor(User author) {
this.author.getPosts().remove(this);
this.author = author;
if (!author.getPosts().contains(this)) {
author.getPosts().add(this);
}
}
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic.getPosts().remove(this);
this.topic = topic;
if (!topic.getPosts().contains(this)) {
topic.getPosts().add(this);
}
}
public List<Comment> getComments() { return comments; }
public void addComment(Comment comment) {
this.comments.add(comment);
if (comment.getPost() != this) {
comment.setPost(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Post post = (Post) o;
return Objects.equals(id, post.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String ToString() {
return "Post{" +
"id=" + id +
", text='" + text + '\'' +
", author='" + author.ToString() + '\'' +
'}';
}
}

View File

@ -0,0 +1,77 @@
package com.webproglabs.lab1.models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Topic {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
@OneToMany(mappedBy = "topic", orphanRemoval = true, fetch = FetchType.EAGER)
private List<Post> posts;
public Topic(String name, String description) {
this.name = name;
this.description = description;
this.posts = new ArrayList<Post>();
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public List<Post> getPosts() {
return posts;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void addPost(Post post) {
this.posts.add(post);
if (post.getTopic() != this) {
post.setTopic(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Topic topic = (Topic) o;
return Objects.equals(id, topic.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String ToString() {
return "Topic{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}

View File

@ -0,0 +1,106 @@
package com.webproglabs.lab1.models;
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 = 64)
private String login;
@Column(nullable = false, length = 64)
@NotBlank
@Size(min = 6, max = 64)
private String password;
private UserRole role;
@OneToMany(mappedBy = "owner", orphanRemoval = true, fetch = FetchType.EAGER)
private List<Comment> comments = new ArrayList<Comment>();
@OneToMany(mappedBy = "author", orphanRemoval = true, fetch = FetchType.EAGER)
private List<Post> posts = new ArrayList<Post>();
public User(){}
public User(String login, String password) {
this.login = login;
this.password=password;
this.role = UserRole.USER;
};
public User(String login, String password, UserRole role) {
this.login = login;
this.password=password;
this.role = role;
};
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<Comment> getComments() { return comments; }
public List<Post> getPosts() {return posts; }
public void addComment(Comment comment) {
this.comments.add(comment);
if (comment.getOwner() != this) {
comment.setOwner(this);
}
}
public void addPost(Post post) {
this.posts.add(post);
if (post.getAuthor() != this) {
post.setAuthor(this);
}
}
@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);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String ToString() {
return "User{" +
"id=" + id +
", login='" + login + '\'' +
", password='" + password + '\'' +
'}';
}
}

View File

@ -0,0 +1,20 @@
package com.webproglabs.lab1.models;
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";
}
}