Skip to content

Commit

Permalink
Merge pull request #36 from themoment-team/develop
Browse files Browse the repository at this point in the history
Fix - post converter
  • Loading branch information
wwwcomcomcomcom authored Jun 19, 2024
2 parents 8af9052 + b9d4705 commit 26291c1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
34 changes: 19 additions & 15 deletions src/main/java/onboarding/crud/post/LikedUsersConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,33 @@
import jakarta.persistence.AttributeConverter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class LikedUsersConverter implements AttributeConverter<HashSet<Long>, String> {
private static final ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
public class LikedUsersConverter implements AttributeConverter<List<Long>, String> {

private static final String SPLIT_CHAR = ",";

@Override
public String convertToDatabaseColumn(HashSet<Long> attribute) {
try {
return mapper.writeValueAsString(attribute);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException();
public String convertToDatabaseColumn(List<Long> longList) {
if (longList == null || longList.isEmpty()) {
return "";
}
return longList.stream()
.map(String::valueOf)
.collect(Collectors.joining(SPLIT_CHAR));
}

@Override
public HashSet<Long> convertToEntityAttribute(String dbData) {
TypeReference<HashSet<Long>> typeReference = new TypeReference<HashSet<Long>>() {};
try {
return mapper.readValue(dbData, typeReference);
} catch (IOException e) {
throw new IllegalArgumentException();
public List<Long> convertToEntityAttribute(String string) {
if (string == null || string.isEmpty()) {
return new ArrayList<>();
}
return Arrays.stream(string.split(SPLIT_CHAR))
.map(Long::valueOf)
.collect(Collectors.toList());
}
}
4 changes: 2 additions & 2 deletions src/main/java/onboarding/crud/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import onboarding.crud.post.LikedUsersConverter;
import onboarding.crud.comment.entity.Comment;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

Expand All @@ -24,12 +25,11 @@ public class Post {
private String author;
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Comment> comments;
@Setter(AccessLevel.NONE)
private Integer likes;
// Stirng of user ids separated by comma
// ex: 1, 2, 3
@Convert(converter = LikedUsersConverter.class)
private HashSet<Long> likedUsers = new HashSet<>();
private ArrayList<Long> likedUsers = new ArrayList<>();
public void updateLikes() {
this.likes = likedUsers.size();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/onboarding/crud/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -58,7 +59,7 @@ public boolean toggleLikePost(Long postId, Long userId) {
Optional<Post> post = postRepository.findById(postId);
if (post.isPresent()) {
Post likedPost = post.get();
HashSet<Long> likedUsers = likedPost.getLikedUsers();
ArrayList<Long> likedUsers = likedPost.getLikedUsers();
if(likedUsers.contains(userId)){
likedUsers.remove(userId);
likedPost.updateLikes();
Expand Down

0 comments on commit 26291c1

Please sign in to comment.