Skip to content

Commit

Permalink
Merge pull request #677 from bounswe/fix/backend/tag-self-following
Browse files Browse the repository at this point in the history
[Backend] Fix Self Following Field in Tag Details
  • Loading branch information
mmtftr authored Dec 13, 2024
2 parents b6afe9b + d543aa9 commit 49005ed
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.group1.programminglanguagesforum.Entities.Tag;
import com.group1.programminglanguagesforum.Entities.TagType;
import com.group1.programminglanguagesforum.Entities.User;
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Repositories.QuestionRepository;
import com.group1.programminglanguagesforum.Repositories.TagRepository;
import com.group1.programminglanguagesforum.Repositories.UserRepository;
Expand All @@ -38,6 +39,7 @@ public class TagService {
private final ModelMapper modelMapper;
private final QuestionRepository questionRepository;
private final UserRepository userRepository;
private final UserContextService userContextService;

public List<Tag> findAllByIdIn(List<Long> tagIds) {
return tagRepository.findAllByIdIn(tagIds);
Expand Down Expand Up @@ -72,6 +74,10 @@ public GetTagDetailsResponseDto createTag(CreateTagRequestDto dto) {
.build();
}

public boolean isTagFollowed(User user, Long tagId) {
return user.getFollowedTags().stream().anyMatch(t -> t.getId().equals(tagId));
}

public GetTagDetailsResponseDto getTagDetails(Long tagId) {
Optional<Tag> tag = tagRepository.findById(tagId);
if (tag.isEmpty()) {
Expand All @@ -85,13 +91,24 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) {
.toList();
Long questionCount = (long) questions.size();

boolean following = false;
try {
// userContextService is null when testing
if (userContextService != null && userContextService.getCurrentUser() != null) {
following = isTagFollowed(userContextService.getCurrentUser(), tagId);
}
} catch (UnauthorizedAccessException e) {
following = false;
}

if (tagType == TagType.PROGRAMMING_LANGUAGE) {
ProgrammingLanguagesTag languageTag = (ProgrammingLanguagesTag) tagEntity;
GetProgrammingLanguageTagResponseDto responseDto = modelMapper.map(languageTag,
GetProgrammingLanguageTagResponseDto.class);
responseDto.setTagType(tagType.toString());
responseDto.setRelatedQuestions(relatedQuestions);
responseDto.setQuestionCount(questionCount);
responseDto.setFollowing(following);
return responseDto;
} else if (tagType == TagType.PROGRAMMING_PARADIGM) {
ProgrammingParadigmTag paradigmTag = (ProgrammingParadigmTag) tagEntity;
Expand All @@ -100,6 +117,7 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) {
responseDto.setTagType(tagType.toString());
responseDto.setRelatedQuestions(relatedQuestions);
responseDto.setQuestionCount(questionCount);
responseDto.setFollowing(following);
return responseDto;
}

Expand All @@ -110,7 +128,7 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) {
.tagType(getTagType(tagEntity).toString())
.relatedQuestions(relatedQuestions)
.questionCount(questionCount)

.following(following)
.build();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
package com.group1.programminglanguagesforum.Services;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.modelmapper.ModelMapper;

import com.group1.programminglanguagesforum.DTOs.Requests.CreateTagRequestDto;
import com.group1.programminglanguagesforum.DTOs.Responses.GetQuestionWithTagDto;
import com.group1.programminglanguagesforum.DTOs.Responses.GetTagDetailsResponseDto;
import com.group1.programminglanguagesforum.Entities.DifficultyLevel;
import com.group1.programminglanguagesforum.Entities.Question;
import com.group1.programminglanguagesforum.Entities.Tag;
import com.group1.programminglanguagesforum.Entities.User;
import com.group1.programminglanguagesforum.Repositories.QuestionRepository;
import com.group1.programminglanguagesforum.Repositories.TagRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.modelmapper.ModelMapper;

import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class TagServiceTest {

Expand Down Expand Up @@ -89,11 +99,45 @@ void testCreateTag() {
@Test
void testGetTagDetails_Success() {
Long tagId = 1L;
Date mockDate = new Date();

// Create a mock user with all required fields
User mockUser = User.builder()
.id(1L)
.username("testUser")
.email("[email protected]") // Add required email
.firstName("Test")
.lastName("User")
.reputationPoints(0L)
.build();

Tag mockTag = new Tag(1L, null, "Tag1", "Description1", null);
List<Question> mockQuestions = Arrays.asList(
new Question(1L, "Question1", "Body1", DifficultyLevel.EASY, 0L, 0L, null, null, null, null, null,null),
new Question(2L, "Question2", "Body2", DifficultyLevel.MEDIUM, 0L, 0L, null, null, null, null, null,null));

Question q1 = Question.builder()
.id(1L)
.title("Question1")
.questionBody("Body1")
.difficulty(DifficultyLevel.EASY)
.likeCount(0L)
.commentCount(0L)
.createdAt(mockDate)
.updatedAt(mockDate)
.askedBy(mockUser)
.build();

Question q2 = Question.builder()
.id(2L)
.title("Question2")
.questionBody("Body2")
.difficulty(DifficultyLevel.MEDIUM)
.likeCount(0L)
.commentCount(0L)
.createdAt(mockDate)
.updatedAt(mockDate)
.askedBy(mockUser)
.build();

List<Question> mockQuestions = Arrays.asList(q1, q2);

when(tagRepository.findById(tagId)).thenReturn(Optional.of(mockTag));
when(questionRepository.findQuestionsByTagId(tagId)).thenReturn(mockQuestions);
Expand Down

0 comments on commit 49005ed

Please sign in to comment.