Skip to content

Commit

Permalink
Merge pull request #26 from KB-iGOT/4.8.19-dev-v2
Browse files Browse the repository at this point in the history
4.8.19 dev v2
  • Loading branch information
karthik-tarento authored Nov 26, 2024
2 parents 8ec453e + 0d64e72 commit 8593a5b
Show file tree
Hide file tree
Showing 17 changed files with 545 additions and 77 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@
<artifactId>keycloak-common</artifactId>
<version>18.0.0</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.21.1</version>
</dependency>


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.tarento.commenthub.authentication.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tarento.commenthub.constant.Constants;
import com.tarento.commenthub.transactional.cassandrautils.CassandraOperation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class FetchUserDetails {

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@Autowired
private CassandraOperation cassandraOperation;

ObjectMapper objectMapper = new ObjectMapper();

public List<Object> fetchDataForKeys(List<String> keys) {
// Fetch values for all keys from Redis
List<Object> values = redisTemplate.opsForValue().multiGet(keys);

// Create a map of key-value pairs, converting stringified JSON objects to User objects
return keys.stream()
.filter(key -> values.get(keys.indexOf(key)) != null) // Filter out null values
.map(key -> {
String stringifiedJson = (String) values.get(keys.indexOf(key)); // Cast the value to String
try {
// Convert the stringified JSON to a User object using ObjectMapper
return objectMapper.readValue(stringifiedJson, Object.class); // You can map this to a specific User type if needed
} catch (Exception e) {
// Handle any exceptions during deserialization
e.printStackTrace();
return null; // Return null in case of error
}
})
.collect(Collectors.toList());
}

public List<Object> fetchUserFromprimary(List<String> userIds) {
List<Object> userList = new ArrayList<>();
Map<String, Object> propertyMap = new HashMap<>();
propertyMap.put(Constants.ID, userIds);
List<Map<String, Object>> userInfoList = cassandraOperation.getRecordsByPropertiesWithoutFiltering(
Constants.KEYSPACE_SUNBIRD, Constants.TABLE_USER, propertyMap,
Arrays.asList(Constants.PROFILE_DETAILS, Constants.FIRST_NAME, Constants.ID), null);

userList = userInfoList.stream()
.map(userInfo -> {
Map<String, Object> userMap = new HashMap<>();

// Extract user ID and user name
String userId = (String) userInfo.get(Constants.ID);
String userName = (String) userInfo.get(Constants.FIRST_NAME);

userMap.put(Constants.USER_ID_KEY, userId);
userMap.put(Constants.FIRST_NAME_KEY, userName);

// Process profile details if present
String profileDetails = (String) userInfo.get(Constants.PROFILE_DETAILS);
if (StringUtils.isNotBlank(profileDetails)) {
try {
// Convert JSON profile details to a Map
Map<String, Object> profileDetailsMap = objectMapper.readValue(profileDetails,
new TypeReference<HashMap<String, Object>>() {});

// Check for profile image and add to userMap if available
if (MapUtils.isNotEmpty(profileDetailsMap)) {
if (profileDetailsMap.containsKey(Constants.PROFILE_IMG) && StringUtils.isNotBlank((String) profileDetailsMap.get(Constants.PROFILE_IMG))){
userMap.put(Constants.PROFILE_IMG_KEY, (String) profileDetailsMap.get(Constants.PROFILE_IMG));
}
if (profileDetailsMap.containsKey(Constants.DESIGNATION_KEY) && StringUtils.isNotEmpty((String) profileDetailsMap.get(Constants.DESIGNATION_KEY))) {

userMap.put(Constants.DESIGNATION_KEY, (String) profileDetailsMap.get(Constants.PROFILE_IMG));
}
if(profileDetailsMap.containsKey(Constants.EMPLOYMENT_DETAILS) && MapUtils.isNotEmpty(
(Map<?, ?>) profileDetailsMap.get(Constants.EMPLOYMENT_DETAILS)) && ((Map<?, ?>) profileDetailsMap.get(Constants.EMPLOYMENT_DETAILS)).containsKey(Constants.DEPARTMENT_KEY) && StringUtils.isNotBlank(
(String) ((Map<?, ?>) profileDetailsMap.get(Constants.EMPLOYMENT_DETAILS)).get(Constants.DEPARTMENT_KEY))){
userMap.put(Constants.DEPARTMENT, (String) ((Map<?, ?>) profileDetailsMap.get(Constants.EMPLOYMENT_DETAILS)).get(Constants.DEPARTMENT_KEY));

}

}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

return userMap;
})
.collect(Collectors.toList());
return userList;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/tarento/commenthub/config/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@ public RedisTemplate<String, Comment> redisTemplate(RedisConnectionFactory conne
return redisTemplate;
}

// Bean for RedisTemplate<String, Object>
@Bean
public RedisTemplate<String, Object> redisTemplateObject(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer()); // Configure as needed for Object
return redisTemplate;
}

}

15 changes: 15 additions & 0 deletions src/main/java/com/tarento/commenthub/constant/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,19 @@ private Constants() {
public static final String REPORTED_BY = "reportedBy";
public static final String NOT_SUSPENDED_STATUS = "Only reported coments can be deleted by admin";
public static final String DELETED_BY = "deletedBy";
public static final String TAGGED_USERS = "taggedUsers";
public static final String USER_PREFIX = "user:" ;
public static final String USER_ID_KEY = "user_id";
public static final String FIRST_NAME_KEY = "first_name";
public static final String PROFILE_IMG_KEY = "user_profile_img_url";
public static final String DESIGNATION_KEY = "designation";
public static final String DEPARTMENT_KEY = "departmentName";
public static final String EMPLOYMENT_DETAILS = "employmentDetails";
public static final String DEPARTMENT = "department";
public static final String EXCEPTION_MSG_DELETE = "Exception occurred while deleting record from ";
public static final String COMMENT_LIKE_TABLE = "comment_likes";
public static final String REPORTED_REASON = "reportedDueTo";
public static final String OTHER_REASON = "otherReasons";
public static final String COURSEID = "courseId";
public static final String EMPTY_COURSEID = "CourseId is missing";
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,10 @@ public ResponseEntity<?> delete(@RequestBody Map<String, Object> request,
return new ResponseEntity<>(response, response.getResponseCode());
}

@GetMapping("/v1/likedComments")
public ResponseEntity getCommentsLikedByUser(@RequestParam String courseId, @RequestHeader(Constants.X_AUTH_TOKEN) String token) {
ApiResponse response = commentService.getCommentsLikedByUser(courseId, token);
return new ResponseEntity<>(response, response.getResponseCode());
}

}
15 changes: 15 additions & 0 deletions src/main/java/com/tarento/commenthub/dto/CommentsResoponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class CommentsResoponseDTO {

private int commentCount;

private List<Object> taggedUsers;

public CommentsResoponseDTO(CommentTree commentTree, List<Comment> comments, List<Map<String, Object>> userList) {
this.commentTree = commentTree;
this.comments = comments;
Expand All @@ -35,4 +37,17 @@ public CommentsResoponseDTO(List<Comment> comments, List<Map<String, Object>> us
this.comments = comments;
this.users = userList;
}

public CommentsResoponseDTO(CommentTree commentTree, List<Comment> comments, List<Map<String, Object>> userList, List<Object> taggedUsers) {
this.commentTree = commentTree;
this.comments = comments;
this.users = userList;
this.taggedUsers=taggedUsers;
}

public CommentsResoponseDTO(List<Comment> comments, List<Map<String, Object>> userList, List<Object> taggedUsers) {
this.comments = comments;
this.users = userList;
this.taggedUsers=taggedUsers;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/tarento/commenthub/dto/SearchCriteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public class SearchCriteria {

private boolean overrideCache;

private boolean enrichedUser;

}
54 changes: 54 additions & 0 deletions src/main/java/com/tarento/commenthub/dto/UserCourseCommentsId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.tarento.commenthub.dto;

import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;
import javax.persistence.Embeddable;

@Embeddable
public class UserCourseCommentsId implements Serializable {

private String userId; // Corresponds to user_id in the table
private String courseId; // Corresponds to course_id in the table

// Default Constructor
public UserCourseCommentsId() {}

// Constructor with fields
public UserCourseCommentsId(String userId, String courseId) {
this.userId = userId;
this.courseId = courseId;
}

// Getters and Setters
public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getCourseId() {
return courseId;
}

public void setCourseId(String courseId) {
this.courseId = courseId;
}

// Equals and HashCode
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserCourseCommentsId that = (UserCourseCommentsId) o;
return Objects.equals(userId, that.userId) && Objects.equals(courseId, that.courseId);
}

@Override
public int hashCode() {
return Objects.hash(userId, courseId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.tarento.commenthub.entity;

import com.tarento.commenthub.dto.UserCourseCommentsId;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Type;
import com.vladmihalcea.hibernate.type.array.ListArrayType;
import org.hibernate.annotations.TypeDef;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "user_course_comments_like")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
@Embeddable
public class UserCourseCommentLike implements Serializable {

@EmbeddedId
private UserCourseCommentsId id;

@Column(name = "comment_ids")
@Type(type = "com.vladmihalcea.hibernate.type.array.ListArrayType")
private List<String> commentIds; // Maps to the comment_ids column



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tarento.commenthub.repository;

import com.tarento.commenthub.dto.UserCourseCommentsId;
import com.tarento.commenthub.entity.UserCourseCommentLike;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserCommentLikeRepository extends JpaRepository<UserCourseCommentLike, UserCourseCommentsId> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ Comment deleteCommentById(String commentId, CommentTreeIdentifierDTO commentTree
ApiResponse reportComment(Map<String, Object> request, String token);

ApiResponse deleteReportedComments(Map<String, Object> request, String token);

ApiResponse getCommentsLikedByUser(String courseId, String token);
}
Loading

0 comments on commit 8593a5b

Please sign in to comment.