Thanh
LEPHUC 5 months ago
parent 43faab805b
commit 5ae9206bd0
  1. 124
      BE/.idea/uiDesigner.xml
  2. BIN
      BE/images/0399943a-8183-4c4a-8389-314d94f58cf8_test+img.jpg
  3. 2
      BE/src/main/java/org/example/config/SecurityConfig.java
  4. 44
      BE/src/main/java/org/example/controllers/PostController.java
  5. 7
      BE/src/main/java/org/example/controllers/UserController.java
  6. 0
      BE/src/main/java/org/example/models/Author.java
  7. 37
      BE/src/main/java/org/example/models/Image.java
  8. 81
      BE/src/main/java/org/example/models/Post.java
  9. 3
      BE/src/main/java/org/example/models/User.java
  10. 1
      BE/src/main/java/org/example/models/UserPostResult.java
  11. 37
      BE/src/main/java/org/example/models/Video.java
  12. 18
      BE/src/main/java/org/example/queryresults/PostQueryResult.java
  13. 24
      BE/src/main/java/org/example/repositories/PostRepository.java
  14. 12
      BE/src/main/java/org/example/repositories/UserRepository.java
  15. 119
      BE/src/main/java/org/example/services/PostService.java
  16. 3
      BE/src/main/java/org/example/services/UserService.java
  17. BIN
      BE/videos/4f633c39-e81f-4a2e-b77b-a57e963580b5_Đồng hồ đếm ngược dành cho ai làm video 10s.mp4

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

@ -53,7 +53,7 @@ public class SecurityConfig {
CorsConfiguration configuration = new CorsConfiguration();
// TODO: make sure that the origin list comes from an environment file.
configuration.setAllowedOrigins(Arrays.asList("http://localhost:3001", "http://127.0.0.1:3000"));
configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000", "http://127.0.0.1:3000"));
configuration.setAllowedMethods(Arrays.asList("GET","POST","PATCH", "PUT", "DELETE", "OPTIONS", "HEAD"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(Arrays.asList("Authorization", "Requestor-Type", "Content-Type"));

@ -0,0 +1,44 @@
package org.example.controllers;
//import org.example.models.Author;
import org.example.models.Post;
import org.example.models.User;
import org.example.repositories.PostRepository;
import org.example.services.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/posts")
public class PostController {
private final PostService postService;
private final PostRepository postRepository;
@Autowired
public PostController(PostService postService, PostRepository postRepository) {
this.postService = postService;
this.postRepository = postRepository;
}
@GetMapping("/getAllPosts")
public List<Post> getAllPost(){
return postService.getAllPost();
}
@PostMapping
public Post createPost(
@RequestParam("title") String title,
@RequestParam("content") String content,
@RequestParam(value = "imageFile", required = false) MultipartFile imageFile,
@RequestParam(value = "videoFile", required = false) MultipartFile videoFile) throws IOException {
return postService.createPost(title, content, imageFile, videoFile);
}
}

@ -2,6 +2,7 @@ package org.example.controllers;
import org.example.models.User;
import org.example.objects.UserDTO;
import org.example.repositories.UserRepository;
import org.example.requests.CreateUserRequest;
import org.example.services.UserService;
import org.springframework.http.HttpStatus;
@ -13,13 +14,17 @@ import java.security.Principal;
@RequestMapping("/api/v1/auth")
public class UserController {
private final UserService userService;
private final UserRepository userRepository;
public String username;
public UserController(UserService userService) {
public UserController(UserService userService, UserRepository userRepository) {
this.userService = userService;
this.userRepository = userRepository;
}
@GetMapping("/me")
public String loggedInUserDetails(Principal principal) {
username=principal.getName();
return principal.getName();
}

@ -0,0 +1,37 @@
package org.example.models;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
@Node
public class Image {
@Id
private Long id;
private String filename;
private String contentType;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}

@ -0,0 +1,81 @@
package org.example.models;
import lombok.Getter;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Getter
@Node
public class Post {
@Id
@GeneratedValue
private Long id;
private String title;
private String content;
@Relationship(type = "HAS_IMAGE", direction = Relationship.Direction.OUTGOING)
private List<Image> images = new ArrayList<>();
@Relationship(type = "HAS_VIDEO", direction = Relationship.Direction.OUTGOING)
private List<Video> videos = new ArrayList<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<Image> getImages() {
return images;
}
public void setImages(List<Image> images) {
this.images = images;
}
public List<Video> getVideos() {
return videos;
}
public void setVideos(List<Video> videos) {
this.videos = videos;
}
@Relationship(type = "createPost", direction = Relationship.Direction.INCOMING)
private User creator;
public User getCreator() {
return creator;
}
public void setCreator(User creator) {
this.creator = creator;
}
}

@ -12,7 +12,8 @@ import java.util.Collection;
@Node
public class User implements UserDetails {
@Id @GeneratedValue
@Id
@GeneratedValue
private Long id;
private String name;
private String username;

@ -0,0 +1,37 @@
package org.example.models;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
@Node
public class Video {
@Id
private Long id;
private String filename;
private String contentType;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}

@ -0,0 +1,18 @@
package org.example.queryresults;
import org.example.models.User;
public class PostQueryResult {
private User user;
public PostQueryResult() {
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

@ -0,0 +1,24 @@
package org.example.repositories;
import org.example.controllers.UserController;
import org.example.models.Post;
import org.example.queryresults.PostQueryResult;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.UUID;
@Repository
public interface PostRepository extends Neo4jRepository<Post,Long> {
@Query("MATCH (post:Post) return post")
List<Post> getAllPost();
// @Query("MATCH (user:User {username: $username}), (post:Post ) " +" WHERE ID(post)=$postId "+
// "CREATE (user)-[:createPost]->(post) Return user,post")
// void createPostRelationship(@Param("username") String username,@Param("postId") Long postId);
@Query("MATCH (user:User {username: $username}),(post:Post )"+"where ID(post)=$postId"+ " CREATE (user)-[:createPost ]->(post)"+
"RETURN user, post")
PostQueryResult createPostRelationship(@Param("username") String username,@Param("postId") Long postId);
}

@ -1,7 +1,7 @@
package org.example.repositories;
import org.example.models.User;
import org.example.queryresults.CourseEnrolmentQueryResult;
import org.example.queryresults.PostQueryResult;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
@ -9,12 +9,16 @@ import java.util.Optional;
public interface UserRepository extends Neo4jRepository<User, Long> {
Optional<User> findUserByUsername(String username);
boolean existsByUsername(String username);
@Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " +
"RETURN EXISTS((user)-[:ENROLLED_IN]->(course))")
Boolean findEnrolmentStatus(String username, String identifier);
@Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " +
"CREATE (user)-[:ENROLLED_IN]->(course) RETURN user, course")
CourseEnrolmentQueryResult createEnrolmentRelationship(String username, String identifier);
// @Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " +
// "CREATE (user)-[:ENROLLED_IN]->(course) RETURN user, course")
// CourseEnrolmentQueryResult createEnrolmentRelationship(String username, String identifier);
}

@ -0,0 +1,119 @@
package org.example.services;
//import org.example.models.Author;
import org.example.controllers.UserController;
import org.example.models.Image;
import org.example.models.Post;
import org.example.models.User;
import org.example.models.Video;
import org.example.queryresults.PostQueryResult;
import org.example.repositories.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.Principal;
import java.util.List;
import java.util.UUID;
@Service
public class PostService {
private final PostRepository postRepository;
private final UserController userController;
private String username;
@Autowired
public PostService(PostRepository postRepository, UserController userController) {
this.postRepository = postRepository;
this.userController = userController;
}
public List<Post> getAllPost(){
return postRepository.getAllPost();
}
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);
}
if (videoFile != null) {
Video video = saveVideoFile(videoFile, "videos");
post.getVideos().add(video);
}
username=userController.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
Path directoryPath = Paths.get(folder);
if (!Files.exists(directoryPath)) {
Files.createDirectories(directoryPath);
}
// Tạo tên tệp an toàn
String originalFilename = file.getOriginalFilename();
String safeFilename = UUID.randomUUID().toString() + "_" + originalFilename;
// Đường dẫn đầy đủ đến tệp
Path filePath = Paths.get(folder, safeFilename);
// Sao chép tệp từ MultipartFile vào đường dẫn
Files.copy(file.getInputStream(), filePath);
// Tạo đối tượng Video và lưu thông tin
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 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);
if (!Files.exists(directoryPath)) {
Files.createDirectories(directoryPath);
}
// Tạo tên tệp an toàn
String originalFilename = file.getOriginalFilename();
String safeFilename = UUID.randomUUID().toString() + "_" + originalFilename;
// Đường dẫn đầy đủ đến tệp
Path filePath = Paths.get(folder, safeFilename);
// Sao chép tệp từ MultipartFile vào đường dẫn
Files.copy(file.getInputStream(), filePath);
// Tạo đối tượng Video và lưu thông tin
Video video = new Video();
video.setFilename(safeFilename);
video.setContentType(file.getContentType());
// Lưu video vào hệ thống hoặc cơ sở dữ liệu
return video;
}
}

@ -18,6 +18,9 @@ public class UserService {
}
public User createUser(CreateUserRequest request) {
if (userRepository.existsByUsername(request.getUsername())) {
throw new IllegalArgumentException("Username already exists. Please choose a different username.");
}
User user = new User();
user.setName(request.getName());

Loading…
Cancel
Save