|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
package org.example.services; |
|
|
|
|
|
|
|
|
|
//import org.example.models.Author;
|
|
|
|
|
import jakarta.transaction.Transactional; |
|
|
|
|
import org.example.controllers.UserController; |
|
|
|
|
import org.example.models.Image; |
|
|
|
|
import org.example.models.Post; |
|
|
|
@ -8,6 +9,7 @@ import org.example.models.User; |
|
|
|
|
import org.example.models.Video; |
|
|
|
|
import org.example.queryresults.PostQueryResult; |
|
|
|
|
import org.example.repositories.PostRepository; |
|
|
|
|
import org.example.repositories.UserRepository; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
@ -25,27 +27,45 @@ public class PostService { |
|
|
|
|
|
|
|
|
|
private final PostRepository postRepository; |
|
|
|
|
private final UserController userController; |
|
|
|
|
private final UserService userService; |
|
|
|
|
private final UserRepository userRepository; |
|
|
|
|
private String username; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
public PostService(PostRepository postRepository, UserController userController) { |
|
|
|
|
public PostService(PostRepository postRepository, UserController userController, UserService userService, UserRepository userRepository) { |
|
|
|
|
this.postRepository = postRepository; |
|
|
|
|
this.userController = userController; |
|
|
|
|
this.userService=userService; |
|
|
|
|
this.userRepository = userRepository; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Transactional |
|
|
|
|
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 { |
|
|
|
|
Post post = new Post(); |
|
|
|
|
post.setTitle(title); |
|
|
|
|
post.setContent(content); |
|
|
|
|
|
|
|
|
|
if (imageFile != null) { |
|
|
|
|
Image image = saveImageFile(imageFile, "images"); |
|
|
|
|
post.getImages().add(image); |
|
|
|
|
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.setImages(image); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (videoFile != null) { |
|
|
|
@ -54,41 +74,41 @@ public class PostService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
username=userController.username; |
|
|
|
|
|
|
|
|
|
post.setAuthor(username); |
|
|
|
|
Post post1= postRepository.save(post); |
|
|
|
|
Long postId=post1.getId(); |
|
|
|
|
System.out.println(username+" "+post.getContent()+" "+postId); |
|
|
|
|
postRepository.createPostRelationship(username,postId); |
|
|
|
|
|
|
|
|
|
return post1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
if (!Files.exists(directoryPath)) { |
|
|
|
|
Files.createDirectories(directoryPath); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tạo tên tệp an toàn
|
|
|
|
|
// Generate a safe and unique filename
|
|
|
|
|
String originalFilename = file.getOriginalFilename(); |
|
|
|
|
String safeFilename = UUID.randomUUID().toString() + "_" + originalFilename; |
|
|
|
|
|
|
|
|
|
// Đường dẫn đầy đủ đến tệp
|
|
|
|
|
// Construct full file path
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
// Tạo đối tượng Video và lưu thông tin
|
|
|
|
|
// Create Image object and set metadata
|
|
|
|
|
Image image = new Image(); |
|
|
|
|
image.setFilename(safeFilename); |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
Path directoryPath = Paths.get(folder); |
|
|
|
|