3 лаба Я наверное скоро умру
This commit is contained in:
parent
2cb40754c1
commit
071d001e08
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
116
data.trace.db
116
data.trace.db
@ -10,3 +10,119 @@ alter table if exists comment add constraint FK8kcum44fvpupyw6f5baccx25c foreign
|
||||
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]
|
||||
2023-03-27 21:45:28 database: wrong user or password; user: "SA"
|
||||
org.h2.message.DbException: Неверное имя пользователя или пароль
|
||||
Wrong user name or password [28000-210]
|
||||
at org.h2.message.DbException.get(DbException.java:227)
|
||||
at org.h2.message.DbException.get(DbException.java:203)
|
||||
at org.h2.message.DbException.get(DbException.java:192)
|
||||
at org.h2.engine.Engine.openSession(Engine.java:154)
|
||||
at org.h2.engine.Engine.openSession(Engine.java:222)
|
||||
at org.h2.engine.Engine.createSession(Engine.java:201)
|
||||
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:338)
|
||||
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:122)
|
||||
at org.h2.util.JdbcUtils.getConnection(JdbcUtils.java:288)
|
||||
at org.h2.server.web.WebServer.getConnection(WebServer.java:807)
|
||||
at org.h2.server.web.WebApp.login(WebApp.java:1033)
|
||||
at org.h2.server.web.WebApp.process(WebApp.java:226)
|
||||
at org.h2.server.web.WebApp.processRequest(WebApp.java:176)
|
||||
at org.h2.server.web.JakartaWebServlet.doGet(JakartaWebServlet.java:129)
|
||||
at org.h2.server.web.JakartaWebServlet.doPost(JakartaWebServlet.java:166)
|
||||
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:731)
|
||||
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
|
||||
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
|
||||
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
|
||||
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177)
|
||||
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
|
||||
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
|
||||
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119)
|
||||
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
|
||||
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
|
||||
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
|
||||
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:400)
|
||||
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
|
||||
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859)
|
||||
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1734)
|
||||
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
|
||||
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
|
||||
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
|
||||
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
|
||||
at java.base/java.lang.Thread.run(Thread.java:833)
|
||||
Caused by: org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException: Неверное имя пользователя или пароль
|
||||
Wrong user name or password [28000-210]
|
||||
at org.h2.message.DbException.getJdbcSQLException(DbException.java:529)
|
||||
at org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
|
||||
... 50 more
|
||||
2023-03-27 21:45:50 database: wrong user or password; user: "SA"
|
||||
org.h2.message.DbException: Неверное имя пользователя или пароль
|
||||
Wrong user name or password [28000-210]
|
||||
at org.h2.message.DbException.get(DbException.java:227)
|
||||
at org.h2.message.DbException.get(DbException.java:203)
|
||||
at org.h2.message.DbException.get(DbException.java:192)
|
||||
at org.h2.engine.Engine.openSession(Engine.java:154)
|
||||
at org.h2.engine.Engine.openSession(Engine.java:222)
|
||||
at org.h2.engine.Engine.createSession(Engine.java:201)
|
||||
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:338)
|
||||
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:122)
|
||||
at org.h2.util.JdbcUtils.getConnection(JdbcUtils.java:288)
|
||||
at org.h2.server.web.WebServer.getConnection(WebServer.java:807)
|
||||
at org.h2.server.web.WebApp.login(WebApp.java:1033)
|
||||
at org.h2.server.web.WebApp.process(WebApp.java:226)
|
||||
at org.h2.server.web.WebApp.processRequest(WebApp.java:176)
|
||||
at org.h2.server.web.JakartaWebServlet.doGet(JakartaWebServlet.java:129)
|
||||
at org.h2.server.web.JakartaWebServlet.doPost(JakartaWebServlet.java:166)
|
||||
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:731)
|
||||
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
|
||||
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
|
||||
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
|
||||
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
|
||||
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177)
|
||||
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
|
||||
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
|
||||
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119)
|
||||
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
|
||||
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
|
||||
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
|
||||
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:400)
|
||||
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
|
||||
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859)
|
||||
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1734)
|
||||
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
|
||||
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
|
||||
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
|
||||
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
|
||||
at java.base/java.lang.Thread.run(Thread.java:833)
|
||||
Caused by: org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException: Неверное имя пользователя или пароль
|
||||
Wrong user name or password [28000-210]
|
||||
at org.h2.message.DbException.getJdbcSQLException(DbException.java:529)
|
||||
at org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
|
||||
... 50 more
|
||||
|
@ -15,15 +15,12 @@ public class Comment {
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
private Date DateOfAdd;
|
||||
|
||||
private String Text;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE)
|
||||
private Post post;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE)
|
||||
private User user;
|
||||
|
||||
public Comment()
|
||||
@ -31,24 +28,15 @@ public class Comment {
|
||||
|
||||
}
|
||||
|
||||
public Comment(Date DateOfAdd,String text)
|
||||
public Comment(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()
|
||||
{
|
||||
@ -58,29 +46,10 @@ public class Comment {
|
||||
{
|
||||
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) {
|
||||
public void setPost(Post post, User user){
|
||||
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;
|
||||
post.getComments().add(this);
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -101,7 +70,6 @@ public class Comment {
|
||||
public String toString() {
|
||||
return "Comment{" +
|
||||
"id=" + id +
|
||||
", DateOfAdd='" + DateOfAdd + '\'' +
|
||||
", Text='" + Text + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
@ -21,15 +21,11 @@ public class CommentService {
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Comment addComment(Date DateOfAdd, String Text) {
|
||||
if(DateOfAdd==null)
|
||||
{
|
||||
throw new IllegalArgumentException("Date is null or empty");
|
||||
}
|
||||
public Comment addComment(String Text) {
|
||||
if (!StringUtils.hasText(Text)) {
|
||||
throw new IllegalArgumentException("TEXT is null or empty");
|
||||
}
|
||||
final Comment comment = new Comment(DateOfAdd,Text);
|
||||
final Comment comment = new Comment(Text);
|
||||
em.persist(comment);
|
||||
return comment;
|
||||
}
|
||||
@ -49,13 +45,8 @@ public class CommentService {
|
||||
.getResultList();
|
||||
}
|
||||
@Transactional
|
||||
public Comment updateComment(Long id, Date DateOfAdd,String Text) {
|
||||
if(DateOfAdd==null)
|
||||
{
|
||||
throw new IllegalArgumentException("Date is null or empty");
|
||||
}
|
||||
public Comment updateComment(Long id,String Text) {
|
||||
final Comment currentComment = findComment(id);
|
||||
currentComment.setDateOfAdd(DateOfAdd);
|
||||
currentComment.setText(Text);
|
||||
return em.merge(currentComment);
|
||||
}
|
||||
@ -70,31 +61,7 @@ public class CommentService {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@ -21,10 +21,10 @@ public class Post {
|
||||
|
||||
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE)
|
||||
private User user;
|
||||
|
||||
@OneToMany(mappedBy = "post",fetch = FetchType.EAGER)
|
||||
@OneToMany(mappedBy = "post",fetch = FetchType.EAGER,cascade = CascadeType.MERGE)
|
||||
private List<Comment> comments=new ArrayList<>();
|
||||
|
||||
public Post(){}
|
||||
@ -55,27 +55,16 @@ public class Post {
|
||||
}
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
if (!user.getPosts().contains(this)) {
|
||||
user.addNewPost(this);
|
||||
}
|
||||
}
|
||||
public User getUser()
|
||||
{
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
@ -17,6 +17,7 @@ public class PostService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
|
||||
@Transactional
|
||||
public Post addPost(String Heading, String Content) {
|
||||
if (!StringUtils.hasText(Heading) || !StringUtils.hasText(Content) ) {
|
||||
@ -56,38 +57,35 @@ public class PostService {
|
||||
@Transactional
|
||||
public Post deletePost(Long id) {
|
||||
final Post currentPost = findPost(id);
|
||||
em.createQuery("Delete from Comment where post.id = "+id).executeUpdate();
|
||||
em.remove(currentPost);
|
||||
return currentPost;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllTidings() {
|
||||
public void deleteAllPosts() {
|
||||
em.createQuery("delete from Post").executeUpdate();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addUser(Long id, User u) {
|
||||
public Comment addCommentToPost(Long id, User user, String text){
|
||||
final Post post = findPost(id);
|
||||
post.setUser(u);
|
||||
em.merge(post);
|
||||
if(post == null){
|
||||
throw new IllegalArgumentException("Post with id " + id + " not found");
|
||||
}
|
||||
Comment comment=new Comment(text);
|
||||
comment.setPost(post, em.find(User.class, user.getId()));
|
||||
return em.merge(comment);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteUser(Long id) {
|
||||
public void removeCommentFromPost(Long id, Comment comment){
|
||||
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);
|
||||
if(post == null){
|
||||
throw new IllegalArgumentException("Post with id " + id + " not found");
|
||||
}
|
||||
post.getComments().remove(comment);
|
||||
comment.setPost(null, null);
|
||||
em.remove(comment);
|
||||
}
|
||||
}
|
||||
|
@ -20,10 +20,10 @@ public class User {
|
||||
|
||||
private String email;
|
||||
|
||||
@OneToMany(mappedBy ="user",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
|
||||
@OneToMany(mappedBy ="user",cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
|
||||
private List<Post> posts =new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy ="user",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
|
||||
@OneToMany(mappedBy ="user",cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
|
||||
private List<Comment> comments =new ArrayList<>();
|
||||
|
||||
public User() {
|
||||
@ -72,16 +72,6 @@ public class User {
|
||||
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()
|
||||
@ -91,7 +81,13 @@ public class User {
|
||||
public void setEmail(String email){
|
||||
this.email=email;
|
||||
}
|
||||
|
||||
public void removePost(Post post)
|
||||
{
|
||||
if(posts!=null)
|
||||
{
|
||||
posts.remove(post);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
|
@ -63,39 +63,31 @@ public class UserService {
|
||||
@Transactional
|
||||
public User deleteUser(Long id) {
|
||||
final User currentUser = findUser(id);
|
||||
em.createQuery("Delete from Post Where user.id = "+ id).executeUpdate();
|
||||
em.createQuery("Delete from Comment Where user.id = "+id).executeUpdate();
|
||||
em.remove(currentUser);
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllUsers() {
|
||||
em.createQuery("Delete from Post").executeUpdate();
|
||||
em.createQuery("Delete from Comment").executeUpdate();
|
||||
em.createQuery("delete from User").executeUpdate();
|
||||
}
|
||||
@Transactional
|
||||
public void addNewPost(Long id, Post post) {
|
||||
public Post addNewPost(Long id, String Heading,String Content) {
|
||||
User currentUser= findUser(id);
|
||||
currentUser.addNewPost(post);
|
||||
em.merge(currentUser);
|
||||
Post post=new Post(Heading,Content);
|
||||
post.setUser(currentUser);
|
||||
return em.merge(post);
|
||||
}
|
||||
|
||||
@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);
|
||||
em.createQuery("Delete Comment where post.Id = "+ post.getId()).executeUpdate();
|
||||
em.createQuery("Delete from Post").executeUpdate();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteComment(Long id, Comment comment) {
|
||||
User currentUser = findUser(id);
|
||||
currentUser.deleteComment(comment);
|
||||
em.merge(currentUser);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ 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.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.Post.service.PostService;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
import ru.ulstu.is.sbapp.User.service.UserService;
|
||||
|
||||
@ -17,6 +19,8 @@ public class JpaClientTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaClientTests.class);
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Test
|
||||
void testClientCreate() {
|
||||
@ -61,4 +65,27 @@ public class JpaClientTests {
|
||||
log.info("testUserReadAllEmpty: " + users.toString());
|
||||
Assertions.assertEquals(users.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddAndDeletePost()
|
||||
{
|
||||
postService.deleteAllPosts();
|
||||
userService.deleteAllUsers();
|
||||
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
final Post post =userService.addNewPost(user.getId(),"Text1","Text2");
|
||||
final List<Post> posts =postService.findAllPosts();
|
||||
final User user1 = userService.findUser(user.getId());
|
||||
final List<Post> posts1 = user1.getPosts();
|
||||
log.info("testAddAndDeletePost :: ADD " + posts.toString() + " and " + posts1.toString());
|
||||
Assertions.assertEquals(posts.get(0).getUser(), user);
|
||||
Assertions.assertEquals(posts.toString(), posts1.toString());
|
||||
log.info("testAddAndDeletePost :: Delete ");
|
||||
userService.deletePost(user.getId(),post);
|
||||
final User us = userService.findUser(user.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> postService.findPost(post.getId()));
|
||||
|
||||
log.info(us.getPosts().toString());
|
||||
userService.deleteAllUsers();
|
||||
postService.deleteAllPosts();
|
||||
}
|
||||
}
|
||||
|
@ -9,17 +9,15 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
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(JpaTidingTest.class);
|
||||
public class JpaCommentTest {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaCommentTest.class);
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
@Autowired
|
||||
@ -29,8 +27,7 @@ public class JpaTidingTest {
|
||||
void testTidingCreate()
|
||||
{
|
||||
commentService.deleteAllComments();
|
||||
final Date dateofadd= new Date(1212121212121L);
|
||||
final Comment comment=commentService.addComment(dateofadd,"Net");
|
||||
final Comment comment=commentService.addComment("Net");
|
||||
log.info("testCommentCreate: " + comment.toString());
|
||||
Assertions.assertNotNull(comment.getId());
|
||||
commentService.deleteAllComments();
|
||||
@ -39,8 +36,7 @@ public class JpaTidingTest {
|
||||
void testTidingRead()
|
||||
{
|
||||
commentService.deleteAllComments();
|
||||
final Date dateofadd= new Date(1212121212121L);
|
||||
final Comment comment=commentService.addComment(dateofadd,"Net");
|
||||
final Comment comment=commentService.addComment("Net");
|
||||
log.info("testCommentRead[0]: " + comment.toString());
|
||||
final Comment findComment=commentService.findComment(comment.getId());
|
||||
log.info("testCommentRead[1]: " + findComment.toString());
|
||||
@ -57,10 +53,8 @@ public class JpaTidingTest {
|
||||
void testAllTidingRead()
|
||||
{
|
||||
commentService.deleteAllComments();
|
||||
final Date dateofadd1= new Date(112121212121L);
|
||||
final Date dateofadd2= new Date(1212121212121L);
|
||||
commentService.addComment(dateofadd1,"Net");
|
||||
commentService.addComment(dateofadd2,"yep");
|
||||
commentService.addComment("Net");
|
||||
commentService.addComment("yep");
|
||||
final List<Comment> comments = commentService.findAllComments();
|
||||
log.info("testAllCommentRead: " + comments.toString());
|
||||
Assertions.assertEquals(comments.size(), 2);
|
||||
@ -78,34 +72,13 @@ public class JpaTidingTest {
|
||||
void testUpdateTiding()
|
||||
{
|
||||
commentService.deleteAllComments();
|
||||
final Date dateofadd1= new Date(112121212121L);
|
||||
final Date dateofadd2= new Date(1212121212121L);
|
||||
final Comment comment=commentService.addComment(dateofadd1,"Net");
|
||||
final Comment comment=commentService.addComment("Net");
|
||||
log.info("testUpdateComment: " + comment.toString());
|
||||
commentService.updateComment(comment.getId(),dateofadd2,"Ladno");
|
||||
commentService.updateComment(comment.getId(),"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);
|
||||
}
|
||||
|
||||
}
|
97
src/test/java/ru/ulstu/is/sbapp/JpaPostTests.java
Normal file
97
src/test/java/ru/ulstu/is/sbapp/JpaPostTests.java
Normal file
@ -0,0 +1,97 @@
|
||||
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.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Comment.service.CommentService;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.Post.service.PostService;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
import ru.ulstu.is.sbapp.User.service.UserService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaPostTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JpaPostTests.class);
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Test
|
||||
void TestAddPost(){
|
||||
postService.deleteAllPosts();
|
||||
final Post post = postService.addPost("Test","Test");
|
||||
Assertions.assertNotNull(post.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void TestFindPost(){
|
||||
postService.deleteAllPosts();
|
||||
final Post post = postService.addPost("Test","Test");
|
||||
final Post findPost = postService.findPost(post.getId());
|
||||
Assertions.assertEquals(post, findPost);
|
||||
}
|
||||
|
||||
@Test
|
||||
void TestPostReadNotFound(){
|
||||
postService.deleteAllPosts();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> postService.findPost(-1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void TestFindAllPost(){
|
||||
postService.deleteAllPosts();
|
||||
final Post firstPost = postService.addPost("Test1","Test1");
|
||||
final Post secondPost = postService.addPost("Test2","Test2");
|
||||
final List<Post> posts = postService.findAllPosts();
|
||||
Assertions.assertEquals(posts.size(), 2);
|
||||
}
|
||||
@Test
|
||||
void TestPostReadAllEmpty() {
|
||||
postService.deleteAllPosts();
|
||||
final List<Post> posts = postService.findAllPosts();
|
||||
Assertions.assertEquals(posts.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void TestPostUpdate(){
|
||||
postService.deleteAllPosts();
|
||||
Post post = postService.addPost("Test1", "Test1");
|
||||
post = postService.updatePost(post.getId(), "Test2", "Test2");
|
||||
Assertions.assertEquals(post.getHeading(), "Test2");
|
||||
Assertions.assertEquals(post.getContent(), "Test2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void TestDeletePost(){
|
||||
postService.deleteAllPosts();
|
||||
final Post post = postService.addPost("Test","Test");
|
||||
postService.deletePost(post.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> postService.findPost(1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void TestAddComment()
|
||||
{
|
||||
postService.deleteAllPosts();
|
||||
userService.deleteAllUsers();
|
||||
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
final Post post =userService.addNewPost(user.getId(),"Text1","Text2");
|
||||
final Comment comment =postService.addCommentToPost(post.getId(),user,"СОООСИИ УЕБОК ЕБАННЫЙ");
|
||||
final List<Comment> comments = commentService.findAllComments();
|
||||
log.info(comments.toString());
|
||||
final User user1=userService.findUser(user.getId());
|
||||
log.info(user1.getComments().toString());
|
||||
final Post post1=postService.findPost(post.getId());
|
||||
log.info(post1.getComments().toString());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user