diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Repositories/QuestionRepository.java b/backend/src/main/java/com/group1/programminglanguagesforum/Repositories/QuestionRepository.java index 58c541d..f306553 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Repositories/QuestionRepository.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Repositories/QuestionRepository.java @@ -1,8 +1,8 @@ package com.group1.programminglanguagesforum.Repositories; -import com.group1.programminglanguagesforum.Entities.DifficultyLevel; -import com.group1.programminglanguagesforum.Entities.Question; -import com.group1.programminglanguagesforum.Entities.User; +import java.util.List; +import java.util.Optional; + import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; @@ -10,36 +10,38 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; -import java.util.List; -import java.util.Optional; +import com.group1.programminglanguagesforum.Entities.DifficultyLevel; +import com.group1.programminglanguagesforum.Entities.Question; +import com.group1.programminglanguagesforum.Entities.User; @Repository public interface QuestionRepository extends JpaRepository { + @Query("SELECT q FROM Question q JOIN q.tags t WHERE t.id = :tagId order by q.likeCount desc ") List findQuestionsByTagId(@Param("tagId") Long tagId); - @Query("SELECT DISTINCT q FROM Question q " + - "LEFT JOIN q.tags t " + - "WHERE (:query IS NULL OR " + - " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR " + - " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) " + - "AND (:tagIds IS NULL OR t.id IN :tagIds) " + - "AND (:difficulty IS NULL OR q.difficulty = :difficulty)") + @Query("SELECT DISTINCT q FROM Question q " + + "LEFT JOIN q.tags t " + + "WHERE (:query IS NULL OR " + + " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR " + + " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) " + + "AND (:tagIds IS NULL OR t.id IN :tagIds) " + + "AND (:difficulty IS NULL OR q.difficulty = :difficulty)") Page searchQuestions( @Param("query") String query, @Param("tagIds") List tagIds, @Param("difficulty") DifficultyLevel difficulty, Pageable pageable); - @Query("SELECT DISTINCT q FROM Question q " + - "LEFT JOIN q.tags t " + - "WHERE (:query IS NULL OR " + - " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR " + - " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) " + - "AND (:tagIds IS NULL OR t.id IN :tagIds) " + - "AND (:difficulty IS NULL OR q.difficulty = :difficulty) " + - "ORDER BY " + - " CASE WHEN :authorIds IS NOT NULL AND q.askedBy.id IN :authorIds THEN 1 ELSE 0 END DESC") + @Query("SELECT DISTINCT q FROM Question q " + + "LEFT JOIN q.tags t " + + "WHERE (:query IS NULL OR " + + " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR " + + " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) " + + "AND (:tagIds IS NULL OR t.id IN :tagIds) " + + "AND (:difficulty IS NULL OR q.difficulty = :difficulty) " + + "ORDER BY " + + " CASE WHEN :authorIds IS NOT NULL AND q.askedBy.id IN :authorIds THEN 1 ELSE 0 END DESC") Page searchQuestionsByRecommended( @Param("query") String query, @Param("authorIds") List authorIds, @@ -52,5 +54,7 @@ Page searchQuestionsByRecommended( @Query("SELECT q.askedBy FROM Question q WHERE q.id = :id") Optional findQuestionOwner(@Param("id") Long questionId); -} + @Query("SELECT q FROM Question q JOIN q.tags t WHERE t.id = :tagId AND q.difficulty = :difficulty order by q.likeCount desc limit 3") + List findQuestionsByDifficultyAndTagId(@Param("difficulty") DifficultyLevel difficulty, @Param("tagId") Long tagId); +} 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 cfb4c18..91a2064 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/TagService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/TagService.java @@ -17,6 +17,7 @@ import com.group1.programminglanguagesforum.DTOs.Responses.SelfProfileResponseDto; import com.group1.programminglanguagesforum.DTOs.Responses.TagDto; import com.group1.programminglanguagesforum.Entities.ComputerScienceTermTag; +import com.group1.programminglanguagesforum.Entities.DifficultyLevel; import com.group1.programminglanguagesforum.Entities.ProgrammingLanguagesTag; import com.group1.programminglanguagesforum.Entities.ProgrammingParadigmTag; import com.group1.programminglanguagesforum.Entities.Question; @@ -35,6 +36,7 @@ @Service @RequiredArgsConstructor public class TagService { + private final TagRepository tagRepository; private final ModelMapper modelMapper; private final QuestionRepository questionRepository; @@ -85,7 +87,7 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) { } Tag tagEntity = tag.get(); TagType tagType = getTagType(tagEntity); - List questions = questionRepository.findQuestionsByTagId(tagId); + List questions = questionRepository.findQuestionsByDifficultyAndTagId(DifficultyLevel.EASY, tagId); List relatedQuestions = questions.stream() .map(QuestionService::mapToQuestionSummary) .toList(); @@ -141,11 +143,11 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) { public List getFollowedTags(Long userId) { return tagRepository.findTagByFollowers(userId).stream() .map(tag -> SelfProfileResponseDto.FollowedTags.builder() - .id(tag.getId()) - .name(tag.getTagName()) - .tagType(getTagType(tag)) - .description(tag.getTagDescription()) - .build()) + .id(tag.getId()) + .name(tag.getTagName()) + .tagType(getTagType(tag)) + .description(tag.getTagDescription()) + .build()) .toList(); } 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 7a6a6b1..4335e5d 100644 --- a/backend/src/test/java/com/group1/programminglanguagesforum/Services/TagServiceTests.java +++ b/backend/src/test/java/com/group1/programminglanguagesforum/Services/TagServiceTests.java @@ -142,6 +142,7 @@ void testGetTagDetails_Success() { when(tagRepository.findById(tagId)).thenReturn(Optional.of(mockTag)); when(questionRepository.findQuestionsByTagId(tagId)).thenReturn(mockQuestions); + when(questionRepository.findQuestionsByDifficultyAndTagId(DifficultyLevel.EASY, tagId)).thenReturn(mockQuestions); // Mocking modelMapper behavior when(modelMapper.map(any(Question.class), eq(GetQuestionWithTagDto.class))) @@ -161,7 +162,7 @@ void testGetTagDetails_Success() { assertEquals("Description1", response.getDescription()); assertEquals(2, response.getRelatedQuestions().size()); verify(tagRepository, times(1)).findById(tagId); - verify(questionRepository, times(1)).findQuestionsByTagId(tagId); + verify(questionRepository, times(1)).findQuestionsByDifficultyAndTagId(DifficultyLevel.EASY, tagId); } @Test