Compare commits

..

2 Commits

Author SHA1 Message Date
Thanhzxcn 989f94de81 Update Like, ApiResponse 5 months ago
Thanhzxcn 3cbc6d31f8 Update Like, update ApiResponse 5 months ago
  1. 13
      BE/pom.xml
  2. 9
      BE/src/main/java/org/example/controllers/UserController.java
  3. 26
      BE/src/main/java/org/example/models/Like.java
  4. 18
      BE/src/main/java/org/example/objects/ApiResponse.java
  5. 11
      BE/src/main/java/org/example/repositories/UserRepository.java
  6. 15
      BE/src/main/java/org/example/requests/CreateLikeRequest.java
  7. 13
      BE/src/main/java/org/example/services/UserService.java

@ -40,7 +40,18 @@
<groupId>org.neo4j.driver</groupId> <groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId> <artifactId>neo4j-java-driver</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-core</artifactId>
<version>4.0.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-bolt-driver</artifactId>
<version>4.0.10</version>
<scope>runtime</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>

@ -1,8 +1,10 @@
package org.example.controllers; package org.example.controllers;
import org.example.models.User; import org.example.models.User;
import org.example.objects.ApiResponse;
import org.example.objects.UserDTO; import org.example.objects.UserDTO;
import org.example.repositories.UserRepository; import org.example.repositories.UserRepository;
import org.example.requests.CreateLikeRequest;
import org.example.requests.CreateUserRequest; import org.example.requests.CreateUserRequest;
import org.example.services.UserService; import org.example.services.UserService;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -36,4 +38,11 @@ public class UserController {
return new ResponseEntity<>(responseUser, HttpStatus.CREATED); return new ResponseEntity<>(responseUser, HttpStatus.CREATED);
} }
@PostMapping("/createLike")
public ApiResponse<CreateLikeRequest> createLike(@RequestBody CreateLikeRequest request) {
ApiResponse<CreateLikeRequest> response = new ApiResponse<>();
response.setResult(userService.createLike(request));
return response;
}
} }

@ -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;
}

@ -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<T>{
int code = 1000;
String message;
T result;
}

@ -4,6 +4,7 @@ import org.example.models.User;
import org.example.queryresults.PostQueryResult; import org.example.queryresults.PostQueryResult;
import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query; import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import java.util.Optional; import java.util.Optional;
@ -17,6 +18,16 @@ public interface UserRepository extends Neo4jRepository<User, Long> {
"RETURN EXISTS((user)-[:ENROLLED_IN]->(course))") "RETURN EXISTS((user)-[:ENROLLED_IN]->(course))")
Boolean findEnrolmentStatus(String username, String identifier); 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)\n" +
"WHERE id(a) = $id AND id(b) = $idd\n" +
"RETURN COUNT(r) > 0 AS Relationship")
boolean checkLike(@Param("id") Long userId,@Param("idd") Long postId);
// @Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " + // @Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " +
// "CREATE (user)-[:ENROLLED_IN]->(course) RETURN user, course") // "CREATE (user)-[:ENROLLED_IN]->(course) RETURN user, course")
// CourseEnrolmentQueryResult createEnrolmentRelationship(String username, String identifier); // CourseEnrolmentQueryResult createEnrolmentRelationship(String username, String identifier);

@ -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;
}

@ -2,6 +2,7 @@ package org.example.services;
import org.example.models.User; import org.example.models.User;
import org.example.repositories.UserRepository; import org.example.repositories.UserRepository;
import org.example.requests.CreateLikeRequest;
import org.example.requests.CreateUserRequest; import org.example.requests.CreateUserRequest;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -33,4 +34,16 @@ public class UserService {
return user; return user;
} }
public CreateLikeRequest createLike(CreateLikeRequest request) {
Long userId = request.getUserId();
Long postId = request.getPostId();
if(userRepository.checkLike(userId, postId)){
throw new IllegalArgumentException("Already liked");
}
userRepository.createLike(userId, postId);
return request;
}
} }

Loading…
Cancel
Save