diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Services/TagService.java b/backend/src/main/java/com/group1/programminglanguagesforum/Services/TagService.java index ece5fe82..3647c828 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/TagService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/TagService.java @@ -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; @@ -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 findAllByIdIn(List tagIds) { return tagRepository.findAllByIdIn(tagIds); @@ -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 = tagRepository.findById(tagId); if (tag.isEmpty()) { @@ -85,6 +91,16 @@ 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, @@ -92,6 +108,7 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) { 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; @@ -100,6 +117,7 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) { responseDto.setTagType(tagType.toString()); responseDto.setRelatedQuestions(relatedQuestions); responseDto.setQuestionCount(questionCount); + responseDto.setFollowing(following); return responseDto; } @@ -110,7 +128,7 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) { .tagType(getTagType(tagEntity).toString()) .relatedQuestions(relatedQuestions) .questionCount(questionCount) - + .following(following) .build(); } diff --git a/backend/src/test/java/com/group1/programminglanguagesforum/Services/TagServiceTests.java b/backend/src/test/java/com/group1/programminglanguagesforum/Services/TagServiceTests.java index d1bef799..4b8fbf91 100644 --- a/backend/src/test/java/com/group1/programminglanguagesforum/Services/TagServiceTests.java +++ b/backend/src/test/java/com/group1/programminglanguagesforum/Services/TagServiceTests.java @@ -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 { @@ -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("test@example.com") // Add required email + .firstName("Test") + .lastName("User") + .reputationPoints(0L) + .build(); Tag mockTag = new Tag(1L, null, "Tag1", "Description1", null); - List 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 mockQuestions = Arrays.asList(q1, q2); when(tagRepository.findById(tagId)).thenReturn(Optional.of(mockTag)); when(questionRepository.findQuestionsByTagId(tagId)).thenReturn(mockQuestions);