Done createPost

Thanh
LEPHUC 5 months ago
parent 5ae9206bd0
commit 5383e90103
  1. BIN
      BE/images/3aee7039-34e3-416e-9ba0-087d323533e5_test+img.jpg
  2. BIN
      BE/images/4d5ef946-53fb-4755-9c96-b6e9c543b553_test+img.jpg
  3. BIN
      BE/images/55ffabae-ba37-4f98-9128-80a3e688fa91_test+img.jpg
  4. BIN
      BE/images/5ff906e0-0a82-431e-a18d-dc2d4a73bd9f_test+img.jpg
  5. BIN
      BE/images/6be654bc-8501-4fa6-a1a8-098e474ac1bb_test+img.jpg
  6. BIN
      BE/images/a23625b0-6fb8-4767-b446-bdd45db17909_test+img.jpg
  7. BIN
      BE/images/a4e3f41f-6597-4a62-b37e-b024da58de56_test+img.jpg
  8. BIN
      BE/images/a93aa807-f085-4e96-830d-c1b20bb5a788_test+img.jpg
  9. BIN
      BE/images/ae709eda-77ce-428c-8a90-8f475cf6ccf1_test+img.jpg
  10. BIN
      BE/images/af18481e-2d9c-4545-becd-f56072559f9c_test+img.jpg
  11. BIN
      BE/images/b4ab1102-65f7-4294-9bad-2f5cad662366_test+img.jpg
  12. BIN
      BE/images/b695d708-7b7d-4ec4-bbf6-902cbffd2cdb_test+img.jpg
  13. BIN
      BE/images/d802196a-5df7-4750-bf3e-194c270b7b1c_test+img.jpg
  14. BIN
      BE/images/test+img.jpg
  15. BIN
      BE/imagestest+img.jpg
  16. 2
      BE/src/main/java/org/example/controllers/PostController.java
  17. 10
      BE/src/main/java/org/example/models/Image.java
  18. 27
      BE/src/main/java/org/example/models/Post.java
  19. 12
      BE/src/main/java/org/example/repositories/PostRepository.java
  20. 1
      BE/src/main/java/org/example/repositories/UserRepository.java
  21. 48
      BE/src/main/java/org/example/services/PostService.java
  22. 1
      BE/src/main/resources/application.properties

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

@ -1,6 +1,7 @@
package org.example.controllers; package org.example.controllers;
//import org.example.models.Author; //import org.example.models.Author;
import org.example.models.Image;
import org.example.models.Post; import org.example.models.Post;
import org.example.models.User; import org.example.models.User;
import org.example.repositories.PostRepository; import org.example.repositories.PostRepository;
@ -30,6 +31,7 @@ public class PostController {
return postService.getAllPost(); return postService.getAllPost();
} }
@PostMapping @PostMapping
public Post createPost( public Post createPost(

@ -7,10 +7,20 @@ import org.springframework.data.neo4j.core.schema.Node;
@Node @Node
public class Image { public class Image {
@Id @Id
@GeneratedValue
private Long id; private Long id;
private String filename; private String filename;
private String contentType; private String contentType;
public Image() {
}
public Image(Long id, String filename, String contentType) {
this.id = id;
this.filename = filename;
this.contentType = contentType;
}
public Long getId() { public Long getId() {
return id; return id;
} }

@ -2,6 +2,8 @@ package org.example.models;
import lombok.Getter; import lombok.Getter;
import org.example.repositories.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.core.schema.GeneratedValue; import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id; import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node; import org.springframework.data.neo4j.core.schema.Node;
@ -14,15 +16,33 @@ import java.util.UUID;
@Getter @Getter
@Node @Node
public class Post { public class Post {
@Id @Id
@GeneratedValue @GeneratedValue
private Long id; private Long id;
private String title; private String title;
private String content; private String content;
private String author;
private String imgUrl;
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Relationship(type = "HAS_IMAGE", direction = Relationship.Direction.OUTGOING) @Relationship(type = "HAS_IMAGE", direction = Relationship.Direction.OUTGOING)
private List<Image> images = new ArrayList<>(); private Image images = new Image();
@Relationship(type = "HAS_VIDEO", direction = Relationship.Direction.OUTGOING) @Relationship(type = "HAS_VIDEO", direction = Relationship.Direction.OUTGOING)
private List<Video> videos = new ArrayList<>(); private List<Video> videos = new ArrayList<>();
@ -51,11 +71,11 @@ public class Post {
this.content = content; this.content = content;
} }
public List<Image> getImages() { public Image getImages() {
return images; return images;
} }
public void setImages(List<Image> images) { public void setImages(Image images) {
this.images = images; this.images = images;
} }
@ -77,5 +97,6 @@ public class Post {
public void setCreator(User creator) { public void setCreator(User creator) {
this.creator = creator; this.creator = creator;
} }
} }

@ -1,6 +1,7 @@
package org.example.repositories; package org.example.repositories;
import org.example.controllers.UserController; import org.example.controllers.UserController;
import org.example.models.Image;
import org.example.models.Post; import org.example.models.Post;
import org.example.queryresults.PostQueryResult; import org.example.queryresults.PostQueryResult;
import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.data.neo4j.repository.Neo4jRepository;
@ -13,8 +14,17 @@ import java.util.UUID;
@Repository @Repository
public interface PostRepository extends Neo4jRepository<Post,Long> { public interface PostRepository extends Neo4jRepository<Post,Long> {
@Query("MATCH (post:Post) return post") @Query("MATCH (post:Post)\n" +
"OPTIONAL MATCH (post)-[:HAS_IMAGE]->(image:Image)\n" +
"OPTIONAL MATCH (post)-[:HAS_VIDEO]->(video:Video)\n" +
"RETURN post, COLLECT(DISTINCT image) AS images, COLLECT(DISTINCT video) AS videos")
List<Post> getAllPost(); List<Post> getAllPost();
@Query("MATCH (post:Post)-[:HAS_IMAGE]-> (image:Image)\n" +
"where ID(post)=$postId \n" +
"return image.filename")
String getImage(@Param("postId") Long postId);
// @Query("MATCH (user:User {username: $username}), (post:Post ) " +" WHERE ID(post)=$postId "+ // @Query("MATCH (user:User {username: $username}), (post:Post ) " +" WHERE ID(post)=$postId "+
// "CREATE (user)-[:createPost]->(post) Return user,post") // "CREATE (user)-[:createPost]->(post) Return user,post")
// void createPostRelationship(@Param("username") String username,@Param("postId") Long postId); // void createPostRelationship(@Param("username") String username,@Param("postId") Long postId);

@ -9,6 +9,7 @@ import java.util.Optional;
public interface UserRepository extends Neo4jRepository<User, Long> { public interface UserRepository extends Neo4jRepository<User, Long> {
Optional<User> findUserByUsername(String username); Optional<User> findUserByUsername(String username);
User findUserBy(String username);
boolean existsByUsername(String username); boolean existsByUsername(String username);

@ -1,6 +1,7 @@
package org.example.services; package org.example.services;
//import org.example.models.Author; //import org.example.models.Author;
import jakarta.transaction.Transactional;
import org.example.controllers.UserController; import org.example.controllers.UserController;
import org.example.models.Image; import org.example.models.Image;
import org.example.models.Post; import org.example.models.Post;
@ -8,6 +9,7 @@ import org.example.models.User;
import org.example.models.Video; import org.example.models.Video;
import org.example.queryresults.PostQueryResult; import org.example.queryresults.PostQueryResult;
import org.example.repositories.PostRepository; import org.example.repositories.PostRepository;
import org.example.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -25,27 +27,45 @@ public class PostService {
private final PostRepository postRepository; private final PostRepository postRepository;
private final UserController userController; private final UserController userController;
private final UserService userService;
private final UserRepository userRepository;
private String username; private String username;
@Autowired @Autowired
public PostService(PostRepository postRepository, UserController userController) { public PostService(PostRepository postRepository, UserController userController, UserService userService, UserRepository userRepository) {
this.postRepository = postRepository; this.postRepository = postRepository;
this.userController = userController; this.userController = userController;
this.userService=userService;
this.userRepository = userRepository;
} }
@Transactional
public List<Post> getAllPost(){ public List<Post> getAllPost(){
return postRepository.getAllPost(); List<Post> getAllPost=postRepository.getAllPost();
}
for (Post post : getAllPost) {
if(postRepository.getImage(post.getId())!=null) {
String imageUrl = postRepository.getImage(post.getId());
post.setImgUrl(imageUrl);
System.out.println("Images: " + post.getImgUrl());
};
// Check if images are populated
}
return getAllPost;
}
@Transactional
public Post createPost(String title, String content, MultipartFile imageFile, MultipartFile videoFile) throws IOException { public Post createPost(String title, String content, MultipartFile imageFile, MultipartFile videoFile) throws IOException {
Post post = new Post(); Post post = new Post();
post.setTitle(title); post.setTitle(title);
post.setContent(content); post.setContent(content);
if (imageFile != null) { if (imageFile != null) {
Image image = saveImageFile(imageFile, "images"); Image image = saveImageFile(imageFile, "C:\\Users\\P R O B O O K\\Documents\\Phuc\\Facebook\\FE/FE/public/picture");
post.getImages().add(image); // post.getImages().add(image);
post.setImages(image);
} }
if (videoFile != null) { if (videoFile != null) {
@ -54,41 +74,41 @@ public class PostService {
} }
username=userController.username; username=userController.username;
post.setAuthor(username);
Post post1= postRepository.save(post); Post post1= postRepository.save(post);
Long postId=post1.getId(); Long postId=post1.getId();
System.out.println(username+" "+post.getContent()+" "+postId);
postRepository.createPostRelationship(username,postId); postRepository.createPostRelationship(username,postId);
return post1; return post1;
} }
private Image saveImageFile(MultipartFile file, String folder) throws IOException { private Image saveImageFile(MultipartFile file, String folder) throws IOException {
// Tạo đường dẫn thư mục nếu nó chưa tồn tại // Ensure directory exists; create if not
Path directoryPath = Paths.get(folder); Path directoryPath = Paths.get(folder);
if (!Files.exists(directoryPath)) { if (!Files.exists(directoryPath)) {
Files.createDirectories(directoryPath); Files.createDirectories(directoryPath);
} }
// Tạo tên tệp an toàn // Generate a safe and unique filename
String originalFilename = file.getOriginalFilename(); String originalFilename = file.getOriginalFilename();
String safeFilename = UUID.randomUUID().toString() + "_" + originalFilename; String safeFilename = UUID.randomUUID().toString() + "_" + originalFilename;
// Đường dẫn đầy đủ đến tệp // Construct full file path
Path filePath = Paths.get(folder, safeFilename); Path filePath = Paths.get(folder, safeFilename);
// Sao chép tệp từ MultipartFile vào đường dẫn // Copy file content to the target path
Files.copy(file.getInputStream(), filePath); Files.copy(file.getInputStream(), filePath);
// Tạo đối tượng Video và lưu thông tin // Create Image object and set metadata
Image image = new Image(); Image image = new Image();
image.setFilename(safeFilename); image.setFilename(safeFilename);
image.setContentType(file.getContentType()); image.setContentType(file.getContentType());
// Lưu video vào hệ thống hoặc cơ sở dữ liệu // Return saved Image object
return image; return image;
} }
private Video saveVideoFile(MultipartFile file, String folder) throws IOException { private Video saveVideoFile(MultipartFile file, String folder) throws IOException {
// Tạo đường dẫn thư mục nếu nó chưa tồn tại // Tạo đường dẫn thư mục nếu nó chưa tồn tại
Path directoryPath = Paths.get(folder); Path directoryPath = Paths.get(folder);

@ -1,3 +1,4 @@
spring.neo4j.uri=neo4j://localhost:7687 spring.neo4j.uri=neo4j://localhost:7687
spring.neo4j.authentication.username=neo4j spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=12345678 spring.neo4j.authentication.password=12345678
file_upload = C:/Users/P R O B O O K/Documents/Facebook/BE/images
Loading…
Cancel
Save