package com.kyamran.app.controller; import com.kyamran.app.model.Label; import…
- User
package com.kyamran.app.controller;
import com.kyamran.app.model.Label; import com.kyamran.app.model.Post; import com.kyamran.app.service.LabelService;
import java.util.List;
public class LabelController { LabelService labelService = new LabelService();
public Label save(Label label) { return labelService.save(label); } public Label update(Label label) { return labelService.update(label); } public Label getById(Long Long) { return labelService.getById(Long); } public void deleteById(Long Long) { labelService.deleteById(Long); } public List<Label> getAll() { return labelService.getAll(); } public void savePostLabels(Post post, List<Label> labels) { labelService.savePostLabels(post, labels); }}
package com.kyamran.app.controller;
import com.kyamran.app.model.Post; import com.kyamran.app.service.PostService;
import java.util.List;
public class PostController { PostService postService = new PostService();
public Post save(Post type) { return postService.save(type); } public Post update(Post type) { return postService.update(type); } public Post getById(Long aLong) { return postService.getById(aLong); } public void deleteById(Long aLong) { postService.deleteById(aLong); } public List<Post> getAll() { return postService.getAll(); }}
package com.kyamran.app.model;
import lombok.Builder; import lombok.Getter;
import javax.persistence.*; import java.util.List;
@Getter @Entity @Table(name = "labels") public class Label { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id;
@Column(name = "name") private String name; @ManyToMany(mappedBy = "labels", cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.LAZY) private List<Post> posts; public Label() { } public Label(Long id) { this.id = id; } @Builder public Label(Long id, String name) { this.id = id; this.name = name; } public Label(String name) { this.name = name; } public void setId(Long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setPosts(List<Post> posts) { this.posts = posts; } @Override public String toString() { return "Label{" + "id=" + id + ", name='" + name + '\'' + '}'; }}
package com.kyamran.app.model;
import lombok.Getter;
import javax.persistence.*; import java.time.LocalDate; import java.util.List;
@Entity @Table(name = "posts") public class Post { @Getter @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id;
@Getter @Column(name = "content") private String content; @Getter @Column(name = "created") private LocalDate created; @Getter @Column(name = "updated") private LocalDate updated; @Getter @Column(name = "postStatus") @Enumerated(EnumType.STRING) private PostStatus postStatus; @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.LAZY) @JoinTable( name = "post_labels", joinColumns = {@JoinColumn(name = "PostID")}, inverseJoinColumns = {@JoinColumn(name = "LabelID")} ) private List<Label> labels; @Getter @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "WriterID", nullable = false) private Writer writer; public Post(List<Label> labels) { this.labels = labels; } public Post(String content, List<Label> labels, Writer writer) { this.content = content; this.labels = labels; this.writer = writer; } public Post() { } public void setId(Long id) { this.id = id; } public void setContent(String content) { this.content = content; } public void setCreated(String created) { this.created = LocalDate.parse(created); } public void setUpdated(String updated) { this.updated = LocalDate.parse(updated); } public void setPostStatus(PostStatus postStatus) { this.postStatus = postStatus; } public void setWriter(Writer writer) { this.writer = writer; } @Override public String toString() { return "Post{" + "id=" + id + ", content='" + content + '\'' + ", created=" + created + ", updated=" + updated + ", postStatus=" + postStatus + ", labels=" + labels + ", writer=" + writer.getId() + '}'; } /*public void setLabels(List<Label> labels) { this.labels = labels; }*/ - Assistant