4 lab сделал круды для клиентов(типо регистрации) и в клиенте метод для получения его постов
This commit is contained in:
parent
1335f82494
commit
62d5bd2394
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -3,6 +3,7 @@ package ru.ulstu.is.sbapp.Post.model;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Post.controller.PostDto;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -32,19 +33,22 @@ public class Post {
|
||||
private List<Comment> comments=new ArrayList<>();
|
||||
|
||||
public Post(){}
|
||||
|
||||
public Post(String Heading, String Content)
|
||||
{
|
||||
this.Heading = Heading;
|
||||
this.Content = Content;
|
||||
}
|
||||
|
||||
public Post(String Heading, String Content,byte[] image)
|
||||
{
|
||||
this.Heading = Heading;
|
||||
this.Content = Content;
|
||||
this.image=image;
|
||||
}
|
||||
public Post(PostDto postDto) {
|
||||
this.Heading = postDto.getHeading();
|
||||
this.Content = postDto.getContent();
|
||||
this.image = postDto.getImage().getBytes();
|
||||
}
|
||||
public Long getId()
|
||||
{
|
||||
return id;
|
||||
|
@ -1,6 +1,9 @@
|
||||
package ru.ulstu.is.sbapp.User.controller;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.sbapp.Post.controller.PostDto;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.User.service.UserService;
|
||||
|
||||
import java.util.List;
|
||||
@ -22,6 +25,12 @@ public class UserController {
|
||||
.map(UserDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/{id}/posts")
|
||||
public List<PostDto> getPosts(@PathVariable Long id) {
|
||||
return userService.GetUserPosts(id).stream()
|
||||
.map(PostDto::new)
|
||||
.toList();
|
||||
}
|
||||
@PostMapping
|
||||
public UserDto createUser(@RequestParam("firstName") String firstName,
|
||||
@RequestParam("lastName") String lastname,
|
||||
@ -38,9 +47,8 @@ public class UserController {
|
||||
}
|
||||
@PostMapping("/{id}/Post")
|
||||
public void addPost(@PathVariable Long id,
|
||||
@RequestParam("Heading") String Heading,
|
||||
@RequestParam("Content") String Content) {
|
||||
userService.addNewPost(id, Heading,Content);
|
||||
@RequestBody @Valid PostDto postDto) {
|
||||
userService.addNewPost(id, postDto);
|
||||
}
|
||||
@DeleteMapping("/{id}/Post/{postId}")
|
||||
public void removePost(@PathVariable Long id,
|
||||
|
@ -1,13 +1,18 @@
|
||||
package ru.ulstu.is.sbapp.User.repository;
|
||||
|
||||
import ru.ulstu.is.sbapp.Post.controller.PostDto;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepositoryExtension {
|
||||
Optional<User> safeRemove(Long id);
|
||||
void safeRemoveAll();
|
||||
void addPost(Long id, String Heading, String Content);
|
||||
void addPost(Long id, String Heading, String Content,byte[] image);
|
||||
void addPost(Long id, PostDto post);
|
||||
void removePost(Long id, Long postId);
|
||||
|
||||
List<Post> getUsersPosts(Long id);
|
||||
}
|
||||
|
@ -2,10 +2,12 @@ package ru.ulstu.is.sbapp.User.repository;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Comment.repository.CommentRepository;
|
||||
import ru.ulstu.is.sbapp.Post.controller.PostDto;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.Post.repository.PostRepository;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
@ -58,17 +60,28 @@ public class UserRepositoryImpl implements UserRepositoryExtension {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPost(Long id, String Heading, String Content) {
|
||||
public void addPost(Long id, String Heading, String Content,byte[] image) {
|
||||
Optional<User> currentUser = userRepository.findById(id);
|
||||
if(currentUser.isPresent())
|
||||
{
|
||||
Post post = new Post(Heading, Content);
|
||||
Post post = new Post(Heading, Content,image);
|
||||
post.setUser(currentUser.get());
|
||||
em.merge(post);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPost(Long id, PostDto postdto) {
|
||||
Optional<User> currentUser = userRepository.findById(id);
|
||||
if(currentUser.isPresent())
|
||||
{
|
||||
Post post = new Post(postdto);
|
||||
post.setUser(currentUser.get());
|
||||
em.merge(post);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePost(Long id, Long postId) {
|
||||
em.createQuery("Delete Comment where post.Id = :postId")
|
||||
@ -78,4 +91,12 @@ public class UserRepositoryImpl implements UserRepositoryExtension {
|
||||
.setParameter("postId",postId)
|
||||
.executeUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Post> getUsersPosts(Long id) {
|
||||
TypedQuery<Post> query =
|
||||
em.createQuery("Select p from Post p where user.id = :id",Post.class)
|
||||
.setParameter("id",id);
|
||||
return query.getResultList();
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package ru.ulstu.is.sbapp.User.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.ulstu.is.sbapp.Post.controller.PostDto;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.Post.service.PostNotFoundException;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
@ -63,10 +64,19 @@ public class UserService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addNewPost(Long id, String Heading, String Content) {
|
||||
userRepository.addPost(id,Heading,Content);
|
||||
public void addNewPost(Long id, String Heading, String Content,byte[] img) {
|
||||
userRepository.addPost(id,Heading,Content,img);
|
||||
}
|
||||
@Transactional
|
||||
public void addNewPost(Long id, PostDto post)
|
||||
{
|
||||
userRepository.addPost(id,post);
|
||||
}
|
||||
@Transactional
|
||||
public List<Post> GetUserPosts(Long id)
|
||||
{
|
||||
return userRepository.getUsersPosts(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deletePost(Long id, Long postId) {
|
||||
userRepository.removePost(id,postId);
|
||||
|
@ -1,3 +1,4 @@
|
||||
/*
|
||||
package ru.ulstu.is.sbapp;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
@ -108,3 +109,4 @@ public class JpaClientTests {
|
||||
//посты и коментарии содержащие определенный текст
|
||||
|
||||
}
|
||||
*/
|
||||
|
@ -1,3 +1,4 @@
|
||||
/*
|
||||
package ru.ulstu.is.sbapp;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
@ -82,3 +83,4 @@ public class JpaCommentTest {
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
@ -1,3 +1,4 @@
|
||||
/*
|
||||
package ru.ulstu.is.sbapp;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
@ -151,3 +152,4 @@ public class JpaPostTests {
|
||||
userService.deleteAllUsers();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user