From 3cbc6d31f8e2ea761f2a12aa4634ecbc4a419fdb Mon Sep 17 00:00:00 2001 From: Thanhzxcn Date: Thu, 27 Jun 2024 08:54:46 +0700 Subject: [PATCH] Update Like, update ApiResponse --- BE/pom.xml | 13 +++++++++- .../example/controllers/UserController.java | 9 +++++++ BE/src/main/java/org/example/models/Like.java | 26 +++++++++++++++++++ .../java/org/example/objects/ApiResponse.java | 18 +++++++++++++ .../example/repositories/UserRepository.java | 11 ++++++++ .../example/requests/CreateLikeRequest.java | 15 +++++++++++ .../org/example/services/UserService.java | 8 ++++++ 7 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 BE/src/main/java/org/example/models/Like.java create mode 100644 BE/src/main/java/org/example/objects/ApiResponse.java create mode 100644 BE/src/main/java/org/example/requests/CreateLikeRequest.java diff --git a/BE/pom.xml b/BE/pom.xml index 09fad9b..0e736cc 100644 --- a/BE/pom.xml +++ b/BE/pom.xml @@ -40,7 +40,18 @@ org.neo4j.driver neo4j-java-driver - + + org.neo4j + neo4j-ogm-core + 4.0.10 + compile + + + org.neo4j + neo4j-ogm-bolt-driver + 4.0.10 + runtime + org.springframework.boot spring-boot-devtools diff --git a/BE/src/main/java/org/example/controllers/UserController.java b/BE/src/main/java/org/example/controllers/UserController.java index 4044a11..4f43e9b 100644 --- a/BE/src/main/java/org/example/controllers/UserController.java +++ b/BE/src/main/java/org/example/controllers/UserController.java @@ -1,8 +1,10 @@ package org.example.controllers; import org.example.models.User; +import org.example.objects.ApiResponse; import org.example.objects.UserDTO; import org.example.repositories.UserRepository; +import org.example.requests.CreateLikeRequest; import org.example.requests.CreateUserRequest; import org.example.services.UserService; import org.springframework.http.HttpStatus; @@ -36,4 +38,11 @@ public class UserController { return new ResponseEntity<>(responseUser, HttpStatus.CREATED); } + + @PostMapping("/createLike") + public ApiResponse createLike(@RequestBody CreateLikeRequest request) { + ApiResponse response = new ApiResponse<>(); + response.setResult(userService.createLike(request)); + return response; + } } diff --git a/BE/src/main/java/org/example/models/Like.java b/BE/src/main/java/org/example/models/Like.java new file mode 100644 index 0000000..f36c6ea --- /dev/null +++ b/BE/src/main/java/org/example/models/Like.java @@ -0,0 +1,26 @@ +package org.example.models; + + +import lombok.*; +import lombok.experimental.FieldDefaults; +import org.neo4j.ogm.annotation.EndNode; +import org.neo4j.ogm.annotation.RelationshipEntity; +import org.neo4j.ogm.annotation.StartNode; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE) +@RelationshipEntity(type = "LIKE") +public class Like { + Long userId; + Long postId; + String status; + + @StartNode + private User user; + + @EndNode + private Post post; +} diff --git a/BE/src/main/java/org/example/objects/ApiResponse.java b/BE/src/main/java/org/example/objects/ApiResponse.java new file mode 100644 index 0000000..cea6462 --- /dev/null +++ b/BE/src/main/java/org/example/objects/ApiResponse.java @@ -0,0 +1,18 @@ +package org.example.objects; + + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.*; +import lombok.experimental.FieldDefaults; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@FieldDefaults(level = AccessLevel.PRIVATE) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class ApiResponse{ + int code = 1000; + String message; + T result; +} diff --git a/BE/src/main/java/org/example/repositories/UserRepository.java b/BE/src/main/java/org/example/repositories/UserRepository.java index 0b29d02..5f091da 100644 --- a/BE/src/main/java/org/example/repositories/UserRepository.java +++ b/BE/src/main/java/org/example/repositories/UserRepository.java @@ -4,6 +4,7 @@ import org.example.models.User; 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 java.util.Optional; @@ -17,6 +18,16 @@ public interface UserRepository extends Neo4jRepository { "RETURN EXISTS((user)-[:ENROLLED_IN]->(course))") Boolean findEnrolmentStatus(String username, String identifier); + @Query("match (u:User), (p:POST)\n" + + "WHERE id(u) = $nameId AND id(p) = $postId\n" + + "create (u) - [:LIKE] -> (p)") + void createLike(@Param("nameId") Long nameId, @Param("postId") Long postId); + + @Query("match (a:User)-[r:LIKE]-> (b:POST)" + + "WHERE id(a) = $id AND id(b) = $idd" + + "RETURN COUNT(r) > 0 AS Relationship") + boolean checkLike(Long userId, Long postId); + // @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); diff --git a/BE/src/main/java/org/example/requests/CreateLikeRequest.java b/BE/src/main/java/org/example/requests/CreateLikeRequest.java new file mode 100644 index 0000000..30976cb --- /dev/null +++ b/BE/src/main/java/org/example/requests/CreateLikeRequest.java @@ -0,0 +1,15 @@ +package org.example.requests; + + +import lombok.*; +import lombok.experimental.FieldDefaults; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE) +public class CreateLikeRequest { + Long userId; + Long postId; +} diff --git a/BE/src/main/java/org/example/services/UserService.java b/BE/src/main/java/org/example/services/UserService.java index 9ba60f7..ac530d4 100644 --- a/BE/src/main/java/org/example/services/UserService.java +++ b/BE/src/main/java/org/example/services/UserService.java @@ -2,6 +2,7 @@ package org.example.services; import org.example.models.User; import org.example.repositories.UserRepository; +import org.example.requests.CreateLikeRequest; import org.example.requests.CreateUserRequest; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; @@ -33,4 +34,11 @@ public class UserService { return user; } + + public CreateLikeRequest createLike(CreateLikeRequest request) { + Long userId = request.getUserId(); + Long postId = request.getPostId(); + userRepository.createLike(userId, postId); + return request; + } }