3 лаба доделать тесты и функционал
This commit is contained in:
parent
ca9a45d0b8
commit
2cb40754c1
BIN
data.mv.db
Normal file
BIN
data.mv.db
Normal file
Binary file not shown.
12
data.trace.db
Normal file
12
data.trace.db
Normal file
@ -0,0 +1,12 @@
|
||||
2023-03-27 16:52:44 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "create table [*]user (id bigint not null, email varchar(255), first_name varchar(255), last_name varchar(255), primary key (id))"; ожидалось "identifier"
|
||||
Syntax error in SQL statement "create table [*]user (id bigint not null, email varchar(255), first_name varchar(255), last_name varchar(255), primary key (id))"; expected "identifier"; SQL statement:
|
||||
create table user (id bigint not null, email varchar(255), first_name varchar(255), last_name varchar(255), primary key (id)) [42001-210]
|
||||
2023-03-27 16:52:44 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "alter table if exists comment add constraint FK8kcum44fvpupyw6f5baccx25c foreign key (user_id) references [*]user"; ожидалось "identifier"
|
||||
Syntax error in SQL statement "alter table if exists comment add constraint FK8kcum44fvpupyw6f5baccx25c foreign key (user_id) references [*]user"; expected "identifier"; SQL statement:
|
||||
alter table if exists comment add constraint FK8kcum44fvpupyw6f5baccx25c foreign key (user_id) references user [42001-210]
|
||||
2023-03-27 16:52:44 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "alter table if exists post add constraint FK72mt33dhhs48hf9gcqrq4fxte foreign key (user_id) references [*]user"; ожидалось "identifier"
|
||||
Syntax error in SQL statement "alter table if exists post add constraint FK72mt33dhhs48hf9gcqrq4fxte foreign key (user_id) references [*]user"; expected "identifier"; SQL statement:
|
||||
alter table if exists post add constraint FK72mt33dhhs48hf9gcqrq4fxte foreign key (user_id) references user [42001-210]
|
109
src/main/java/ru/ulstu/is/sbapp/Comment/model/Comment.java
Normal file
109
src/main/java/ru/ulstu/is/sbapp/Comment/model/Comment.java
Normal file
@ -0,0 +1,109 @@
|
||||
package ru.ulstu.is.sbapp.Comment.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Comment {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
private Date DateOfAdd;
|
||||
|
||||
private String Text;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
private Post post;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
private User user;
|
||||
|
||||
public Comment()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Comment(Date DateOfAdd,String text)
|
||||
{
|
||||
this.DateOfAdd=DateOfAdd;
|
||||
this.Text=text;
|
||||
}
|
||||
public Long getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
public Date getDateOfAdd()
|
||||
{
|
||||
return DateOfAdd;
|
||||
}
|
||||
public String getText() {return Text;}
|
||||
public void setDateOfAdd(Date DateOfAdd)
|
||||
{
|
||||
this.DateOfAdd=DateOfAdd;
|
||||
}
|
||||
public void setText(String text){this.Text=text;}
|
||||
public User getUser()
|
||||
{
|
||||
return user;
|
||||
}
|
||||
public Post getPost()
|
||||
{
|
||||
return post;
|
||||
}
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
if (!user.getComments().contains(this)) {
|
||||
user.addNewComment(this);
|
||||
}
|
||||
}
|
||||
public void deleteUser() {
|
||||
if (user.getComments().contains(this)) {
|
||||
user.deleteComment(this);
|
||||
}
|
||||
this.user = null;
|
||||
}
|
||||
public void setPost(Post post) {
|
||||
this.post = post;
|
||||
if (!post.getComments().contains(this)) {
|
||||
post.addNewComment(this);
|
||||
}
|
||||
}
|
||||
public void deletePost() {
|
||||
if (post.getComments().contains(this)) {
|
||||
post.deleteComment(this);
|
||||
}
|
||||
this.user = null;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Comment{" +
|
||||
"id=" + id +
|
||||
", DateOfAdd='" + DateOfAdd + '\'' +
|
||||
", Text='" + Text + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package ru.ulstu.is.sbapp.Comment.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CommentService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Comment addComment(Date DateOfAdd, String Text) {
|
||||
if(DateOfAdd==null)
|
||||
{
|
||||
throw new IllegalArgumentException("Date is null or empty");
|
||||
}
|
||||
if (!StringUtils.hasText(Text)) {
|
||||
throw new IllegalArgumentException("TEXT is null or empty");
|
||||
}
|
||||
final Comment comment = new Comment(DateOfAdd,Text);
|
||||
em.persist(comment);
|
||||
return comment;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Comment findComment(Long id) {
|
||||
final Comment comment = em.find(Comment.class, id);
|
||||
if (comment == null) {
|
||||
throw new EntityNotFoundException(String.format("Comment with id [%s] is not found", id));
|
||||
}
|
||||
return comment;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Comment> findAllComments() {
|
||||
return em.createQuery("select c from Comment c", Comment.class)
|
||||
.getResultList();
|
||||
}
|
||||
@Transactional
|
||||
public Comment updateComment(Long id, Date DateOfAdd,String Text) {
|
||||
if(DateOfAdd==null)
|
||||
{
|
||||
throw new IllegalArgumentException("Date is null or empty");
|
||||
}
|
||||
final Comment currentComment = findComment(id);
|
||||
currentComment.setDateOfAdd(DateOfAdd);
|
||||
currentComment.setText(Text);
|
||||
return em.merge(currentComment);
|
||||
}
|
||||
@Transactional
|
||||
public Comment deleteComment(Long id) {
|
||||
final Comment currentComment = findComment(id);
|
||||
em.remove(currentComment);
|
||||
return currentComment;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllComments() {
|
||||
em.createQuery("delete from Comment").executeUpdate();
|
||||
}
|
||||
@Transactional
|
||||
public void addUser(Long id, User u) {
|
||||
final Comment comment = findComment(id);
|
||||
comment.setUser(u);
|
||||
em.merge(comment);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteUser(Long id) {
|
||||
final Comment comment = findComment(id);
|
||||
comment.deleteUser();
|
||||
em.merge(comment);
|
||||
}
|
||||
@Transactional
|
||||
public void addPost(Long id, Post p) {
|
||||
final Comment comment = findComment(id);
|
||||
comment.setPost(p);
|
||||
em.merge(comment);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deletePost(Long id) {
|
||||
final Comment comment = findComment(id);
|
||||
comment.deletePost();
|
||||
em.merge(comment);
|
||||
}
|
||||
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
package ru.ulstu.is.sbapp.Favourite.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import ru.ulstu.is.sbapp.FavouriteTiding.FavouriteTiding;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Favourite {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
Date DateOfAdd;
|
||||
|
||||
@OneToMany(mappedBy = "favourite",fetch = FetchType.EAGER)
|
||||
private List<FavouriteTiding> tidings;
|
||||
|
||||
|
||||
public Favourite()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Favourite(Date DateOfAdd)
|
||||
{
|
||||
this.DateOfAdd=DateOfAdd;
|
||||
}
|
||||
public List<FavouriteTiding> getTidings() {
|
||||
if (tidings == null) {
|
||||
tidings = new ArrayList<>();
|
||||
}
|
||||
return tidings;
|
||||
}
|
||||
public Long getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
public Date getDateOfAdd()
|
||||
{
|
||||
return DateOfAdd;
|
||||
}
|
||||
public void setDateOfAdd(Date DateOfAdd)
|
||||
{
|
||||
this.DateOfAdd=DateOfAdd;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Favourite favourite = (Favourite) o;
|
||||
return Objects.equals(id, favourite.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Favourite{" +
|
||||
"id=" + id +
|
||||
", DateOfAdd='" + DateOfAdd + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
package ru.ulstu.is.sbapp.Favourite.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.ulstu.is.sbapp.FavouriteTiding.FavouriteTiding;
|
||||
import ru.ulstu.is.sbapp.FavouriteTiding.FavouriteTidingId;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import ru.ulstu.is.sbapp.Favourite.model.Favourite;
|
||||
import ru.ulstu.is.sbapp.news.model.Tiding;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class FavouriteService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Favourite addFavourite(Date DateOfAdd) {
|
||||
if(DateOfAdd==null)
|
||||
{
|
||||
throw new IllegalArgumentException("Date is null or empty");
|
||||
}
|
||||
final Favourite favourite = new Favourite(DateOfAdd);
|
||||
em.persist(favourite);
|
||||
return favourite;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Favourite findFavourite(Long id) {
|
||||
final Favourite favourite = em.find(Favourite.class, id);
|
||||
if (favourite == null) {
|
||||
throw new EntityNotFoundException(String.format("Favourite with id [%s] is not found", id));
|
||||
}
|
||||
return favourite;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Favourite> findAllFavourites() {
|
||||
return em.createQuery("select f from Favourite f", Favourite.class)
|
||||
.getResultList();
|
||||
}
|
||||
@Transactional
|
||||
public Favourite updateFavourite(Long id, Date DateOfAdd) {
|
||||
if(DateOfAdd==null)
|
||||
{
|
||||
throw new IllegalArgumentException("Date is null or empty");
|
||||
}
|
||||
final Favourite currentFavourite = findFavourite(id);
|
||||
currentFavourite.setDateOfAdd(DateOfAdd);
|
||||
return em.merge(currentFavourite);
|
||||
}
|
||||
@Transactional
|
||||
public Favourite deleteFavourite(Long id) {
|
||||
final Favourite currentFavourite = findFavourite(id);
|
||||
em.createQuery("update Client set favourite = null where favourite.id = " + id).executeUpdate();
|
||||
em.createQuery("delete from FavouriteTiding ft where ft.favourite.id = " + id).executeUpdate();
|
||||
em.remove(currentFavourite);
|
||||
return currentFavourite;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllFavourites() {
|
||||
em.createQuery("update Client set favourite = null").executeUpdate();
|
||||
em.createQuery("delete from FavouriteTiding").executeUpdate();
|
||||
em.createQuery("delete from Favourite").executeUpdate();
|
||||
}
|
||||
@Transactional
|
||||
public void addTidingInFavourite(Long id, Tiding tiding) {
|
||||
final Favourite favourite = findFavourite(id);
|
||||
FavouriteTiding favouriteTiding = em.find(FavouriteTiding.class, new FavouriteTidingId(favourite.getId(), tiding.getId()));
|
||||
if (favouriteTiding == null) {
|
||||
favouriteTiding = new FavouriteTiding(favourite, tiding);
|
||||
}
|
||||
em.merge(favouriteTiding);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void removeTidingInFavourite(Long id, Tiding tiding) {
|
||||
final Favourite favourite = findFavourite(id);
|
||||
FavouriteTiding favouriteTiding = em.find(FavouriteTiding.class, new FavouriteTidingId(favourite.getId(), tiding.getId()));
|
||||
if (favouriteTiding == null) {
|
||||
return;
|
||||
}
|
||||
em.remove(favouriteTiding);
|
||||
}
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
package ru.ulstu.is.sbapp.FavouriteTiding;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import ru.ulstu.is.sbapp.Favourite.model.Favourite;
|
||||
import ru.ulstu.is.sbapp.news.model.Tiding;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name="favourite_tiding")
|
||||
@IdClass(FavouriteTidingId.class)
|
||||
public class FavouriteTiding implements Serializable {
|
||||
@Id
|
||||
@ManyToOne
|
||||
@JoinColumn(name="favourite_id",referencedColumnName = "id")
|
||||
private Favourite favourite;
|
||||
@Id
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "tiding_id",referencedColumnName = "id")
|
||||
private Tiding tiding;
|
||||
|
||||
|
||||
public FavouriteTiding() {
|
||||
}
|
||||
|
||||
public FavouriteTiding(Favourite favourite, Tiding tiding) {
|
||||
this.favourite = favourite;
|
||||
this.tiding = tiding;
|
||||
}
|
||||
|
||||
// Properties
|
||||
|
||||
public void setFavourite(Favourite favourite) {
|
||||
this.favourite = favourite;
|
||||
}
|
||||
|
||||
public Favourite getFavourite() {
|
||||
return favourite;
|
||||
}
|
||||
|
||||
public void setTiding(Tiding tiding) {
|
||||
this.tiding = tiding;
|
||||
}
|
||||
|
||||
public Tiding getTiding() {
|
||||
return tiding;
|
||||
}
|
||||
|
||||
// ![Properties]
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
FavouriteTiding favouriteTiding = (FavouriteTiding) o;
|
||||
return Objects.equals(favourite.getId(), favouriteTiding.favourite.getId()) &&
|
||||
Objects.equals(tiding.getId(), favouriteTiding.tiding.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(favourite.getId(), tiding.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FavouriteTiding{" +
|
||||
"favouriteId=" + favourite.getId() +
|
||||
", tidingId=" + tiding.getId() +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package ru.ulstu.is.sbapp.FavouriteTiding;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
public class FavouriteTidingId implements Serializable {
|
||||
private Long favourite;
|
||||
private Long tiding;
|
||||
|
||||
public FavouriteTidingId()
|
||||
{
|
||||
|
||||
}
|
||||
public FavouriteTidingId(Long favouriteId, Long tidingId)
|
||||
{
|
||||
this.favourite=favouriteId;
|
||||
this.tiding=tidingId;
|
||||
}
|
||||
public void setFavourite(Long favourite) {
|
||||
this.favourite = favourite;
|
||||
}
|
||||
|
||||
public Long getFavourite() {
|
||||
return favourite;
|
||||
}
|
||||
|
||||
public void setTiding(Long tiding) {
|
||||
this.tiding = tiding;
|
||||
}
|
||||
|
||||
public Long getTiding() {
|
||||
return tiding;
|
||||
}
|
||||
|
||||
// ![Properties]
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
FavouriteTidingId favouriteTidingId = (FavouriteTidingId) o;
|
||||
return Objects.equals(favourite, favouriteTidingId.favourite) &&
|
||||
Objects.equals(tiding, favouriteTidingId.tiding);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(favourite, tiding);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FavouriteTidingId{" +
|
||||
"favourite=" + favourite +
|
||||
", tiding=" + tiding +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -1,31 +1,35 @@
|
||||
package ru.ulstu.is.sbapp.news.model;
|
||||
package ru.ulstu.is.sbapp.Post.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import ru.ulstu.is.sbapp.FavouriteTiding.FavouriteTiding;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Tiding {
|
||||
public class Post {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
String Heading;
|
||||
private String Heading;
|
||||
|
||||
String Content;
|
||||
private String Content;
|
||||
|
||||
|
||||
|
||||
@OneToMany(mappedBy = "tiding",fetch = FetchType.EAGER)
|
||||
private List<FavouriteTiding> favourites;
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
private User user;
|
||||
|
||||
public Tiding(){}
|
||||
@OneToMany(mappedBy = "post",fetch = FetchType.EAGER)
|
||||
private List<Comment> comments=new ArrayList<>();
|
||||
|
||||
public Tiding(String Heading, String Content)
|
||||
public Post(){}
|
||||
|
||||
public Post(String Heading, String Content)
|
||||
{
|
||||
this.Heading = Heading;
|
||||
this.Content = Content;
|
||||
@ -49,18 +53,39 @@ public class Tiding {
|
||||
{
|
||||
this.Content = Content;
|
||||
}
|
||||
public List<FavouriteTiding> getFavourites() {
|
||||
if (favourites == null) {
|
||||
favourites = new ArrayList<>();
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
if (!user.getPosts().contains(this)) {
|
||||
user.addNewPost(this);
|
||||
}
|
||||
return favourites;
|
||||
}
|
||||
|
||||
public void deleteUser() {
|
||||
if (user.getPosts().contains(this)) {
|
||||
user.deletePost(this);
|
||||
}
|
||||
this.user = null;
|
||||
}
|
||||
public void addNewComment(Comment comment)
|
||||
{
|
||||
comments.add(comment);
|
||||
comment.setPost(this);
|
||||
}
|
||||
public void deleteComment(Comment comment)
|
||||
{
|
||||
comments.remove(comment);
|
||||
comment.deletePost();
|
||||
}
|
||||
public List<Comment> getComments()
|
||||
{
|
||||
return comments;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Tiding tiding = (Tiding) o;
|
||||
return Objects.equals(id, tiding.id);
|
||||
Post post = (Post) o;
|
||||
return Objects.equals(id, post.id);
|
||||
}
|
||||
|
||||
@Override
|
@ -0,0 +1,93 @@
|
||||
package ru.ulstu.is.sbapp.Post.service;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class PostService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Post addPost(String Heading, String Content) {
|
||||
if (!StringUtils.hasText(Heading) || !StringUtils.hasText(Content) ) {
|
||||
throw new IllegalArgumentException("Post info is null or empty");
|
||||
}
|
||||
final Post post = new Post(Heading,Content);
|
||||
em.persist(post);
|
||||
return post;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Post findPost(Long id) {
|
||||
final Post post = em.find(Post.class, id);
|
||||
if (post == null) {
|
||||
throw new EntityNotFoundException(String.format("Post with id [%s] is not found", id));
|
||||
}
|
||||
return post;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Post> findAllPosts() {
|
||||
return em.createQuery("select p from Post p", Post.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Post updatePost(Long id, String Heading, String Content) {
|
||||
if (!StringUtils.hasText(Heading) || !StringUtils.hasText(Content) ) {
|
||||
throw new IllegalArgumentException("Post info is null or empty");
|
||||
}
|
||||
final Post currentPost = findPost(id);
|
||||
currentPost.setHeading(Heading);
|
||||
currentPost.setContent(Content);
|
||||
return em.merge(currentPost);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Post deletePost(Long id) {
|
||||
final Post currentPost = findPost(id);
|
||||
em.remove(currentPost);
|
||||
return currentPost;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllTidings() {
|
||||
em.createQuery("delete from Post").executeUpdate();
|
||||
}
|
||||
@Transactional
|
||||
public void addUser(Long id, User u) {
|
||||
final Post post = findPost(id);
|
||||
post.setUser(u);
|
||||
em.merge(post);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteUser(Long id) {
|
||||
final Post post = findPost(id);
|
||||
post.deleteUser();
|
||||
em.merge(post);
|
||||
}
|
||||
@Transactional
|
||||
public void addNewComment(Long id, Comment comment) {
|
||||
Post currentPost= findPost(id);
|
||||
currentPost.addNewComment(comment);
|
||||
em.merge(currentPost);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deletePost(Long id, Comment comment) {
|
||||
Post currentPost= findPost(id);
|
||||
currentPost.deleteComment(comment);
|
||||
em.merge(currentPost);
|
||||
}
|
||||
}
|
@ -1,14 +1,16 @@
|
||||
package ru.ulstu.is.sbapp.client.model;
|
||||
package ru.ulstu.is.sbapp.User.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import ru.ulstu.is.sbapp.Favourite.model.Favourite;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Client {
|
||||
@Table(name="tab_user")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@ -18,14 +20,16 @@ public class Client {
|
||||
|
||||
private String email;
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name="favourite_id")
|
||||
private Favourite favourite;
|
||||
@OneToMany(mappedBy ="user",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
|
||||
private List<Post> posts =new ArrayList<>();
|
||||
|
||||
public Client() {
|
||||
@OneToMany(mappedBy ="user",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
|
||||
private List<Comment> comments =new ArrayList<>();
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public Client(String firstName, String lastName,String email) {
|
||||
public User(String firstName, String lastName, String email) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.email=email;
|
||||
@ -51,6 +55,34 @@ public class Client {
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
public List<Post> getPosts()
|
||||
{
|
||||
return posts;
|
||||
}
|
||||
public List<Comment> getComments()
|
||||
{
|
||||
return comments;
|
||||
}
|
||||
public void addNewPost(Post post) {
|
||||
posts.add(post);
|
||||
post.setUser(this);
|
||||
}
|
||||
|
||||
public void deletePost(Post post) {
|
||||
posts.remove(post);
|
||||
post.deleteUser();
|
||||
}
|
||||
public void addNewComment(Comment comment)
|
||||
{
|
||||
comments.add(comment);
|
||||
comment.setUser(this);
|
||||
}
|
||||
public void deleteComment(Comment comment)
|
||||
{
|
||||
comments.remove(comment);
|
||||
comment.deleteUser();
|
||||
}
|
||||
|
||||
|
||||
public String getEmail()
|
||||
{
|
||||
@ -60,22 +92,14 @@ public class Client {
|
||||
this.email=email;
|
||||
}
|
||||
|
||||
public void setFavourite(Favourite favourite)
|
||||
{
|
||||
this.favourite=favourite;
|
||||
}
|
||||
public Favourite getFavourite()
|
||||
{
|
||||
return favourite;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Client client = (Client) o;
|
||||
return Objects.equals(id, client.id);
|
||||
User user = (User) o;
|
||||
return Objects.equals(id, user.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -90,6 +114,7 @@ public class Client {
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
", email='" + email + '\'' +
|
||||
", posts=" + posts +'\''+
|
||||
'}';
|
||||
}
|
||||
|
101
src/main/java/ru/ulstu/is/sbapp/User/service/UserService.java
Normal file
101
src/main/java/ru/ulstu/is/sbapp/User/service/UserService.java
Normal file
@ -0,0 +1,101 @@
|
||||
package ru.ulstu.is.sbapp.User.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import ru.ulstu.is.sbapp.Comment.service.CommentService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Autowired
|
||||
CommentService commentService;
|
||||
|
||||
@Transactional
|
||||
public User addUser(String firstName, String lastName, String email) {
|
||||
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) ||!StringUtils.hasText(email)) {
|
||||
throw new IllegalArgumentException("Client info is null or empty");
|
||||
}
|
||||
final User user = new User(firstName, lastName,email);
|
||||
em.persist(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public User findUser(Long id) {
|
||||
final User user = em.find(User.class, id);
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException(String.format("User with id [%s] is not found", id));
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<User> findAllUsers() {
|
||||
return em.createQuery("select u from User u", User.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public User updateUser(Long id, String firstName, String lastName, String email) {
|
||||
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) ||!StringUtils.hasText(email)) {
|
||||
throw new IllegalArgumentException("User info is null or empty");
|
||||
}
|
||||
final User currentUser = findUser(id);
|
||||
currentUser.setFirstName(firstName);
|
||||
currentUser.setLastName(lastName);
|
||||
currentUser.setEmail(email);
|
||||
return em.merge(currentUser);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public User deleteUser(Long id) {
|
||||
final User currentUser = findUser(id);
|
||||
em.remove(currentUser);
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllUsers() {
|
||||
em.createQuery("delete from User").executeUpdate();
|
||||
}
|
||||
@Transactional
|
||||
public void addNewPost(Long id, Post post) {
|
||||
User currentUser= findUser(id);
|
||||
currentUser.addNewPost(post);
|
||||
em.merge(currentUser);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deletePost(Long id, Post post) {
|
||||
User currentUser = findUser(id);
|
||||
currentUser.deletePost(post);
|
||||
em.merge(currentUser);
|
||||
}
|
||||
@Transactional
|
||||
public void addNewComment(Long id, Comment comment) {
|
||||
User currentUser= findUser(id);
|
||||
currentUser.addNewComment(comment);
|
||||
em.merge(currentUser);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteComment(Long id, Comment comment) {
|
||||
User currentUser = findUser(id);
|
||||
currentUser.deleteComment(comment);
|
||||
em.merge(currentUser);
|
||||
}
|
||||
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
package ru.ulstu.is.sbapp.client.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.Favourite.model.Favourite;
|
||||
import ru.ulstu.is.sbapp.client.model.Client;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import ru.ulstu.is.sbapp.Favourite.service.FavouriteService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ClientService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Autowired
|
||||
FavouriteService favouriteService;
|
||||
|
||||
@Transactional
|
||||
public Client addClient(String firstName, String lastName,String email) {
|
||||
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) ||!StringUtils.hasText(email)) {
|
||||
throw new IllegalArgumentException("Client info is null or empty");
|
||||
}
|
||||
final Client client = new Client(firstName, lastName,email);
|
||||
em.persist(client);
|
||||
return client;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Client findClient(Long id) {
|
||||
final Client client = em.find(Client.class, id);
|
||||
if (client == null) {
|
||||
throw new EntityNotFoundException(String.format("Client with id [%s] is not found", id));
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Client> findAllClients() {
|
||||
return em.createQuery("select c from Client c", Client.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Client updateClient(Long id, String firstName, String lastName,String email) {
|
||||
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) ||!StringUtils.hasText(email)) {
|
||||
throw new IllegalArgumentException("Client info is null or empty");
|
||||
}
|
||||
final Client currentClient = findClient(id);
|
||||
currentClient.setFirstName(firstName);
|
||||
currentClient.setLastName(lastName);
|
||||
currentClient.setEmail(email);
|
||||
return em.merge(currentClient);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Client deleteClient(Long id) {
|
||||
final Client currentClient = findClient(id);
|
||||
em.remove(currentClient);
|
||||
return currentClient;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllClients() {
|
||||
em.createQuery("delete from Client").executeUpdate();
|
||||
}
|
||||
@Transactional
|
||||
public Client setFavourite(Long id, Favourite favourite)
|
||||
{
|
||||
final Client client=findClient(id);
|
||||
client.setFavourite(favourite);
|
||||
em.merge(client);
|
||||
return client;
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
package ru.ulstu.is.sbapp.news.service;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.news.model.Tiding;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class TidingService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Tiding addTiding(String Heading, String Content) {
|
||||
if (!StringUtils.hasText(Heading) || !StringUtils.hasText(Content) ) {
|
||||
throw new IllegalArgumentException("Tiding info is null or empty");
|
||||
}
|
||||
final Tiding tiding = new Tiding(Heading,Content);
|
||||
em.persist(tiding);
|
||||
return tiding;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Tiding findTiding(Long id) {
|
||||
final Tiding tiding = em.find(Tiding.class, id);
|
||||
if (tiding == null) {
|
||||
throw new EntityNotFoundException(String.format("Tiding with id [%s] is not found", id));
|
||||
}
|
||||
return tiding;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Tiding> findAllTidings() {
|
||||
return em.createQuery("select t from Tiding t", Tiding.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Tiding updateTiding(Long id, String Heading, String Content) {
|
||||
if (!StringUtils.hasText(Heading) || !StringUtils.hasText(Content) ) {
|
||||
throw new IllegalArgumentException("Tiding info is null or empty");
|
||||
}
|
||||
final Tiding currentTiding = findTiding(id);
|
||||
currentTiding.setHeading(Heading);
|
||||
currentTiding.setContent(Content);
|
||||
return em.merge(currentTiding);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Tiding deleteTiding(Long id) {
|
||||
final Tiding currentTiding = findTiding(id);
|
||||
em.createQuery("delete from FavouriteTiding ft where ft.tiding.id = " + id).executeUpdate();
|
||||
em.remove(currentTiding);
|
||||
return currentTiding;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllTidings() {
|
||||
em.createQuery("delete from FavouriteTiding");
|
||||
em.createQuery("delete from Tiding").executeUpdate();
|
||||
}
|
||||
}
|
@ -7,77 +7,58 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.sbapp.Favourite.model.Favourite;
|
||||
import ru.ulstu.is.sbapp.Favourite.service.FavouriteService;
|
||||
import ru.ulstu.is.sbapp.client.model.Client;
|
||||
import ru.ulstu.is.sbapp.client.service.ClientService;
|
||||
import ru.ulstu.is.sbapp.news.service.TidingService;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
import ru.ulstu.is.sbapp.User.service.UserService;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaClientTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaFavouriteTests.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaClientTests.class);
|
||||
@Autowired
|
||||
private ClientService clientService;
|
||||
@Autowired
|
||||
private FavouriteService favouriteService;
|
||||
private UserService userService;
|
||||
|
||||
@Test
|
||||
void testClientCreate() {
|
||||
clientService.deleteAllClients();
|
||||
final Client client = clientService.addClient("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
log.info("testClientCreate: " + client.toString());
|
||||
Assertions.assertNotNull(client.getId());
|
||||
userService.deleteAllUsers();
|
||||
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
log.info("testUserCreate: " + user.toString());
|
||||
Assertions.assertNotNull(user.getId());
|
||||
|
||||
clientService.deleteAllClients();
|
||||
userService.deleteAllUsers();
|
||||
}
|
||||
@Test
|
||||
void testReadClient()
|
||||
{
|
||||
clientService.deleteAllClients();
|
||||
final Client client = clientService.addClient("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
log.info("testClientRead[0]: " + client.toString());
|
||||
final Client curclient=clientService.findClient(client.getId());
|
||||
log.info("testClientRead[1]: " + curclient.toString());
|
||||
Assertions.assertEquals(client, curclient);
|
||||
clientService.deleteAllClients();
|
||||
userService.deleteAllUsers();
|
||||
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
log.info("testUserRead[0]: " + user.toString());
|
||||
final User curuser=userService.findUser(user.getId());
|
||||
log.info("testUserRead[1]: " + curuser.toString());
|
||||
Assertions.assertEquals(user, curuser);
|
||||
userService.deleteAllUsers();
|
||||
}
|
||||
@Test
|
||||
void testClientReadNotFound() {
|
||||
clientService.deleteAllClients();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> clientService.findClient(-1L));
|
||||
userService.deleteAllUsers();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> userService.findUser(-1L));
|
||||
}
|
||||
@Test
|
||||
void testClientReadAll() {
|
||||
clientService.deleteAllClients();
|
||||
clientService.addClient("Pupa","Lupa","sasdfdsf@gmail.com");
|
||||
clientService.addClient("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
final List<Client> clients = clientService.findAllClients();
|
||||
log.info("testClientReadAll: " + clients.toString());
|
||||
Assertions.assertEquals(clients.size(), 2);
|
||||
userService.deleteAllUsers();
|
||||
userService.addUser("Pupa","Lupa","sasdfdsf@gmail.com");
|
||||
userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
final List<User> users = userService.findAllUsers();
|
||||
log.info("testUserReadAll: " + users.toString());
|
||||
Assertions.assertEquals(users.size(), 2);
|
||||
|
||||
clientService.deleteAllClients();
|
||||
userService.deleteAllUsers();
|
||||
}
|
||||
@Test
|
||||
void testClientReadAllEmpty() {
|
||||
clientService.deleteAllClients();
|
||||
final List<Client> clients = clientService.findAllClients();
|
||||
log.info("testClientReadAllEmpty: " + clients.toString());
|
||||
Assertions.assertEquals(clients.size(), 0);
|
||||
}
|
||||
@Test
|
||||
void testSetFavourite() {
|
||||
clientService.deleteAllClients();
|
||||
Client client = clientService.addClient("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
final Date dateofadd2= new Date(121212121121L);
|
||||
final Favourite favourite = favouriteService.addFavourite(dateofadd2);
|
||||
clientService.setFavourite(client.getId(), favourite);
|
||||
client = clientService.findClient(client.getId());
|
||||
Assertions.assertEquals(favourite, client.getFavourite());
|
||||
|
||||
clientService.deleteAllClients();
|
||||
favouriteService.deleteAllFavourites();
|
||||
userService.deleteAllUsers();
|
||||
final List<User> users = userService.findAllUsers();
|
||||
log.info("testUserReadAllEmpty: " + users.toString());
|
||||
Assertions.assertEquals(users.size(), 0);
|
||||
}
|
||||
}
|
||||
|
@ -1,115 +0,0 @@
|
||||
package ru.ulstu.is.sbapp;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.sbapp.Favourite.model.Favourite;
|
||||
import ru.ulstu.is.sbapp.Favourite.service.FavouriteService;
|
||||
import ru.ulstu.is.sbapp.news.model.Tiding;
|
||||
import ru.ulstu.is.sbapp.news.service.TidingService;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaFavouriteTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaFavouriteTests.class);
|
||||
@Autowired
|
||||
private FavouriteService favouriteService;
|
||||
@Autowired
|
||||
private TidingService tidingService;
|
||||
|
||||
@Test
|
||||
void testFavouriteCreate() {
|
||||
favouriteService.deleteAllFavourites();
|
||||
final Date dateofadd= new Date(1212121212121L);
|
||||
final Favourite favourite = favouriteService.addFavourite(dateofadd);
|
||||
log.info("testFavouriteCreate: " + favourite.toString());
|
||||
Assertions.assertNotNull(favourite.getId());
|
||||
|
||||
favouriteService.deleteAllFavourites();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFavouriteRead() {
|
||||
favouriteService.deleteAllFavourites();
|
||||
final Date dateofadd= new Date(1212121212121L);
|
||||
final Favourite favourite = favouriteService.addFavourite(dateofadd);
|
||||
log.info("testFavouriteRead[0]: " + favourite.toString());
|
||||
final Favourite findFavourite = favouriteService.findFavourite(favourite.getId());
|
||||
log.info("testFavouriteRead[1]: " + findFavourite.toString());
|
||||
Assertions.assertEquals(favourite, findFavourite);
|
||||
|
||||
favouriteService.deleteAllFavourites();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFavouriteReadNotFound() {
|
||||
favouriteService.deleteAllFavourites();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> favouriteService.findFavourite(-1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFavouriteReadAll() {
|
||||
favouriteService.deleteAllFavourites();
|
||||
final Date dateofadd1= new Date(1212121212121L);
|
||||
final Date dateofadd2= new Date(121212121121L);
|
||||
favouriteService.addFavourite(dateofadd1);
|
||||
favouriteService.addFavourite(dateofadd2);
|
||||
final List<Favourite> favourites = favouriteService.findAllFavourites();
|
||||
log.info("testFavouriteReadAll: " + favourites.toString());
|
||||
Assertions.assertEquals(favourites.size(), 2);
|
||||
|
||||
favouriteService.deleteAllFavourites();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFavouriteReadAllEmpty() {
|
||||
favouriteService.deleteAllFavourites();
|
||||
final List<Favourite> favourites = favouriteService.findAllFavourites();
|
||||
log.info("testFavouriteReadAllEmpty: " + favourites.toString());
|
||||
Assertions.assertEquals(favourites.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddTidingInFavourite() {
|
||||
favouriteService.deleteAllFavourites();
|
||||
final Date dateofadd1= new Date(1212121212121L);
|
||||
Favourite favourite = favouriteService.addFavourite(dateofadd1);
|
||||
final Tiding tidingOne = tidingService.addTiding("Tiding 1", "abcd");
|
||||
final Tiding tidingTwo = tidingService.addTiding("Tiding 2", "efgh");
|
||||
favouriteService.addTidingInFavourite(favourite.getId(), tidingOne);
|
||||
favouriteService.addTidingInFavourite(favourite.getId(), tidingTwo);
|
||||
favourite = favouriteService.findFavourite(favourite.getId());
|
||||
log.info("testAddTidingInFavourite: " + favourite.getTidings().toString());
|
||||
Assertions.assertEquals(favourite.getTidings().size(), 2);
|
||||
|
||||
favouriteService.deleteAllFavourites();
|
||||
tidingService.deleteAllTidings();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testRemoveTidingFromFavourite() {
|
||||
favouriteService.deleteAllFavourites();
|
||||
final Date dateofadd1= new Date(1212121212121L);
|
||||
Favourite favourite = favouriteService.addFavourite(dateofadd1);
|
||||
final Tiding tidingOne = tidingService.addTiding("Tiding 1", "abcd");
|
||||
final Tiding tidingTwo = tidingService.addTiding("Tiding 2", "efgh");
|
||||
favouriteService.addTidingInFavourite(favourite.getId(), tidingOne);
|
||||
favouriteService.addTidingInFavourite(favourite.getId(), tidingTwo);
|
||||
favouriteService.removeTidingInFavourite(favourite.getId(), tidingOne);
|
||||
favourite = favouriteService.findFavourite(favourite.getId());
|
||||
log.info("testRemoveTidingFromFavourite: " + favourite.getTidings());
|
||||
Assertions.assertEquals(favourite.getTidings().size(), 1);
|
||||
|
||||
favouriteService.deleteAllFavourites();
|
||||
tidingService.deleteAllTidings();
|
||||
}
|
||||
|
||||
}
|
@ -7,76 +7,105 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.sbapp.Favourite.model.Favourite;
|
||||
import ru.ulstu.is.sbapp.Favourite.service.FavouriteService;
|
||||
import ru.ulstu.is.sbapp.news.model.Tiding;
|
||||
import ru.ulstu.is.sbapp.news.service.TidingService;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Comment.service.CommentService;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
import ru.ulstu.is.sbapp.User.service.UserService;
|
||||
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaTidingTest {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaFavouriteTests.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaTidingTest.class);
|
||||
@Autowired
|
||||
private TidingService tidingService;
|
||||
private CommentService commentService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Test
|
||||
void testTidingCreate()
|
||||
{
|
||||
tidingService.deleteAllTidings();
|
||||
final Tiding tiding=tidingService.addTiding("Da","Net");
|
||||
log.info("testTidingCreate: " + tiding.toString());
|
||||
Assertions.assertNotNull(tiding.getId());
|
||||
tidingService.deleteAllTidings();
|
||||
commentService.deleteAllComments();
|
||||
final Date dateofadd= new Date(1212121212121L);
|
||||
final Comment comment=commentService.addComment(dateofadd,"Net");
|
||||
log.info("testCommentCreate: " + comment.toString());
|
||||
Assertions.assertNotNull(comment.getId());
|
||||
commentService.deleteAllComments();
|
||||
}
|
||||
@Test
|
||||
void testTidingRead()
|
||||
{
|
||||
tidingService.deleteAllTidings();
|
||||
final Tiding tiding=tidingService.addTiding("Da","Net");
|
||||
log.info("testTidingRead[0]: " + tiding.toString());
|
||||
final Tiding findTiding=tidingService.findTiding(tiding.getId());
|
||||
log.info("testTidingRead[1]: " + findTiding.toString());
|
||||
Assertions.assertEquals(tiding, findTiding);
|
||||
commentService.deleteAllComments();
|
||||
final Date dateofadd= new Date(1212121212121L);
|
||||
final Comment comment=commentService.addComment(dateofadd,"Net");
|
||||
log.info("testCommentRead[0]: " + comment.toString());
|
||||
final Comment findComment=commentService.findComment(comment.getId());
|
||||
log.info("testCommentRead[1]: " + findComment.toString());
|
||||
Assertions.assertEquals(comment, findComment);
|
||||
|
||||
tidingService.deleteAllTidings();
|
||||
commentService.deleteAllComments();
|
||||
}
|
||||
@Test
|
||||
void testTidingReadNotFound() {
|
||||
tidingService.deleteAllTidings();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> tidingService.findTiding(-1L));
|
||||
commentService.deleteAllComments();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> commentService.findComment(-1L));
|
||||
}
|
||||
@Test
|
||||
void testAllTidingRead()
|
||||
{
|
||||
tidingService.deleteAllTidings();
|
||||
tidingService.addTiding("Da","Net");
|
||||
tidingService.addTiding("Aga","yep");
|
||||
final List<Tiding> tidings = tidingService.findAllTidings();
|
||||
log.info("testAllTidingRead: " + tidings.toString());
|
||||
Assertions.assertEquals(tidings.size(), 2);
|
||||
commentService.deleteAllComments();
|
||||
final Date dateofadd1= new Date(112121212121L);
|
||||
final Date dateofadd2= new Date(1212121212121L);
|
||||
commentService.addComment(dateofadd1,"Net");
|
||||
commentService.addComment(dateofadd2,"yep");
|
||||
final List<Comment> comments = commentService.findAllComments();
|
||||
log.info("testAllCommentRead: " + comments.toString());
|
||||
Assertions.assertEquals(comments.size(), 2);
|
||||
|
||||
tidingService.deleteAllTidings();
|
||||
commentService.deleteAllComments();
|
||||
}
|
||||
@Test
|
||||
void testTidingReadAllEmpty() {
|
||||
tidingService.deleteAllTidings();
|
||||
final List<Tiding> tidings = tidingService.findAllTidings();
|
||||
log.info("testTidingReadAllEmpty: " + tidings.toString());
|
||||
Assertions.assertEquals(tidings.size(), 0);
|
||||
commentService.deleteAllComments();
|
||||
final List<Comment> comments = commentService.findAllComments();
|
||||
log.info("testCommentReadAllEmpty: " + comments.toString());
|
||||
Assertions.assertEquals(comments.size(), 0);
|
||||
}
|
||||
@Test
|
||||
void testUpdateTiding()
|
||||
{
|
||||
tidingService.deleteAllTidings();
|
||||
final Tiding tiding=tidingService.addTiding("Da","Net");
|
||||
log.info("testUpdateTiding: " + tiding.toString());
|
||||
tidingService.updateTiding(tiding.getId(),"Net","Ladno");
|
||||
final Tiding tiding1=tidingService.findTiding(tiding.getId());
|
||||
log.info("testUpdateTiding: " + tiding1.toString());
|
||||
Assertions.assertEquals(tiding1.getHeading(), "Net");
|
||||
Assertions.assertEquals(tiding1.getContent(), "Ladno");
|
||||
tidingService.deleteAllTidings();
|
||||
commentService.deleteAllComments();
|
||||
final Date dateofadd1= new Date(112121212121L);
|
||||
final Date dateofadd2= new Date(1212121212121L);
|
||||
final Comment comment=commentService.addComment(dateofadd1,"Net");
|
||||
log.info("testUpdateComment: " + comment.toString());
|
||||
commentService.updateComment(comment.getId(),dateofadd2,"Ladno");
|
||||
final Comment comment1=commentService.findComment(comment.getId());
|
||||
log.info("testUpdateComment: " + comment1.toString());
|
||||
Assertions.assertEquals(comment1.getDateOfAdd(), dateofadd2);
|
||||
Assertions.assertEquals(comment1.getText(), "Ladno");
|
||||
commentService.deleteAllComments();
|
||||
}
|
||||
@Test
|
||||
void testSetAndDeleteUser()
|
||||
{
|
||||
commentService.deleteAllComments();
|
||||
userService.deleteAllUsers();
|
||||
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
final Date dateofadd1= new Date("12/02/2020");
|
||||
final Comment comment=commentService.addComment(dateofadd1,"Net");
|
||||
commentService.addUser(comment.getId(),user);
|
||||
final User user1=userService.findUser(user.getId());
|
||||
final Comment comment1=commentService.findComment(comment.getId());
|
||||
Assertions.assertEquals(comment1.getUser(), user1);
|
||||
Assertions.assertEquals(user1.getComments().get(0), comment);
|
||||
commentService.deleteUser(user.getId());
|
||||
final User user2=userService.findUser(user.getId());
|
||||
final Comment comment2=commentService.findComment(comment.getId());
|
||||
Assertions.assertEquals(comment2.getUser(), null);
|
||||
Assertions.assertEquals(user2.getComments().size(), 0);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user