diff --git a/src/main/java/com/webproglabs/lab1/models/Comment.java b/src/main/java/com/webproglabs/lab1/models/Comment.java new file mode 100644 index 0000000..1b42c97 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/models/Comment.java @@ -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() + '\'' + + '}'; + } +} diff --git a/src/main/java/com/webproglabs/lab1/models/Post.java b/src/main/java/com/webproglabs/lab1/models/Post.java new file mode 100644 index 0000000..20b20ab --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/models/Post.java @@ -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 comments = new ArrayList(); + + @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 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() + '\'' + + '}'; + } +} diff --git a/src/main/java/com/webproglabs/lab1/models/Topic.java b/src/main/java/com/webproglabs/lab1/models/Topic.java new file mode 100644 index 0000000..5415714 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/models/Topic.java @@ -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 posts; + + public Topic(String name, String description) { + this.name = name; + this.description = description; + this.posts = new ArrayList(); + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public List 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 + '\'' + + '}'; + } +} diff --git a/src/main/java/com/webproglabs/lab1/models/User.java b/src/main/java/com/webproglabs/lab1/models/User.java new file mode 100644 index 0000000..162cece --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/models/User.java @@ -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 comments = new ArrayList(); + + @OneToMany(mappedBy = "author", orphanRemoval = true, fetch = FetchType.EAGER) + private List posts = new ArrayList(); + + 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 getComments() { return comments; } + + public List 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 + '\'' + + '}'; + } +} diff --git a/src/main/java/com/webproglabs/lab1/models/UserRole.java b/src/main/java/com/webproglabs/lab1/models/UserRole.java new file mode 100644 index 0000000..ec4d06a --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/models/UserRole.java @@ -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"; + } +}