Skip to content

Commit 5bb654b

Browse files
authored
Merge pull request #708 from bounswe/develop
Deploy
2 parents c628604 + 4bae66e commit 5bb654b

File tree

5 files changed

+43
-35
lines changed

5 files changed

+43
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
11
package com.group1.programminglanguagesforum.Repositories;
22

3-
import com.group1.programminglanguagesforum.Entities.DifficultyLevel;
4-
import com.group1.programminglanguagesforum.Entities.Question;
5-
import com.group1.programminglanguagesforum.Entities.User;
3+
import java.util.List;
4+
import java.util.Optional;
5+
66
import org.springframework.data.domain.Page;
77
import org.springframework.data.domain.Pageable;
88
import org.springframework.data.jpa.repository.JpaRepository;
99
import org.springframework.data.jpa.repository.Query;
1010
import org.springframework.data.repository.query.Param;
1111
import org.springframework.stereotype.Repository;
1212

13-
import java.util.List;
14-
import java.util.Optional;
13+
import com.group1.programminglanguagesforum.Entities.DifficultyLevel;
14+
import com.group1.programminglanguagesforum.Entities.Question;
15+
import com.group1.programminglanguagesforum.Entities.User;
1516

1617
@Repository
1718
public interface QuestionRepository extends JpaRepository<Question, Long> {
19+
1820
@Query("SELECT q FROM Question q JOIN q.tags t WHERE t.id = :tagId order by q.likeCount desc ")
1921
List<Question> findQuestionsByTagId(@Param("tagId") Long tagId);
2022

21-
@Query("SELECT DISTINCT q FROM Question q " +
22-
"LEFT JOIN q.tags t " +
23-
"WHERE (:query IS NULL OR " +
24-
" LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR " +
25-
" LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) " +
26-
"AND (:tagIds IS NULL OR t.id IN :tagIds) " +
27-
"AND (:difficulty IS NULL OR q.difficulty = :difficulty)")
23+
@Query("SELECT DISTINCT q FROM Question q "
24+
+ "LEFT JOIN q.tags t "
25+
+ "WHERE (:query IS NULL OR "
26+
+ " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR "
27+
+ " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) "
28+
+ "AND (:tagIds IS NULL OR t.id IN :tagIds) "
29+
+ "AND (:difficulty IS NULL OR q.difficulty = :difficulty)")
2830
Page<Question> searchQuestions(
2931
@Param("query") String query,
3032
@Param("tagIds") List<Long> tagIds,
3133
@Param("difficulty") DifficultyLevel difficulty,
3234
Pageable pageable);
3335

34-
@Query("SELECT DISTINCT q FROM Question q " +
35-
"LEFT JOIN q.tags t " +
36-
"WHERE (:query IS NULL OR " +
37-
" LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR " +
38-
" LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) " +
39-
"AND (:tagIds IS NULL OR t.id IN :tagIds) " +
40-
"AND (:difficulty IS NULL OR q.difficulty = :difficulty) " +
41-
"ORDER BY " +
42-
" CASE WHEN :authorIds IS NOT NULL AND q.askedBy.id IN :authorIds THEN 1 ELSE 0 END DESC")
36+
@Query("SELECT DISTINCT q FROM Question q "
37+
+ "LEFT JOIN q.tags t "
38+
+ "WHERE (:query IS NULL OR "
39+
+ " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR "
40+
+ " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) "
41+
+ "AND (:tagIds IS NULL OR t.id IN :tagIds) "
42+
+ "AND (:difficulty IS NULL OR q.difficulty = :difficulty) "
43+
+ "ORDER BY "
44+
+ " CASE WHEN :authorIds IS NOT NULL AND q.askedBy.id IN :authorIds THEN 1 ELSE 0 END DESC")
4345
Page<Question> searchQuestionsByRecommended(
4446
@Param("query") String query,
4547
@Param("authorIds") List<Long> authorIds,
@@ -52,5 +54,7 @@ Page<Question> searchQuestionsByRecommended(
5254

5355
@Query("SELECT q.askedBy FROM Question q WHERE q.id = :id")
5456
Optional<User> findQuestionOwner(@Param("id") Long questionId);
55-
}
5657

58+
@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")
59+
List<Question> findQuestionsByDifficultyAndTagId(@Param("difficulty") DifficultyLevel difficulty, @Param("tagId") Long tagId);
60+
}

backend/src/main/java/com/group1/programminglanguagesforum/Services/TagService.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.group1.programminglanguagesforum.DTOs.Responses.SelfProfileResponseDto;
1818
import com.group1.programminglanguagesforum.DTOs.Responses.TagDto;
1919
import com.group1.programminglanguagesforum.Entities.ComputerScienceTermTag;
20+
import com.group1.programminglanguagesforum.Entities.DifficultyLevel;
2021
import com.group1.programminglanguagesforum.Entities.ProgrammingLanguagesTag;
2122
import com.group1.programminglanguagesforum.Entities.ProgrammingParadigmTag;
2223
import com.group1.programminglanguagesforum.Entities.Question;
@@ -35,6 +36,7 @@
3536
@Service
3637
@RequiredArgsConstructor
3738
public class TagService {
39+
3840
private final TagRepository tagRepository;
3941
private final ModelMapper modelMapper;
4042
private final QuestionRepository questionRepository;
@@ -85,7 +87,7 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) {
8587
}
8688
Tag tagEntity = tag.get();
8789
TagType tagType = getTagType(tagEntity);
88-
List<Question> questions = questionRepository.findQuestionsByTagId(tagId);
90+
List<Question> questions = questionRepository.findQuestionsByDifficultyAndTagId(DifficultyLevel.EASY, tagId);
8991
List<QuestionSummaryDto> relatedQuestions = questions.stream()
9092
.map(QuestionService::mapToQuestionSummary)
9193
.toList();
@@ -141,11 +143,11 @@ public GetTagDetailsResponseDto getTagDetails(Long tagId) {
141143
public List<SelfProfileResponseDto.FollowedTags> getFollowedTags(Long userId) {
142144
return tagRepository.findTagByFollowers(userId).stream()
143145
.map(tag -> SelfProfileResponseDto.FollowedTags.builder()
144-
.id(tag.getId())
145-
.name(tag.getTagName())
146-
.tagType(getTagType(tag))
147-
.description(tag.getTagDescription())
148-
.build())
146+
.id(tag.getId())
147+
.name(tag.getTagName())
148+
.tagType(getTagType(tag))
149+
.description(tag.getTagDescription())
150+
.build())
149151
.toList();
150152
}
151153

backend/src/test/java/com/group1/programminglanguagesforum/Services/TagServiceTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ void testGetTagDetails_Success() {
142142

143143
when(tagRepository.findById(tagId)).thenReturn(Optional.of(mockTag));
144144
when(questionRepository.findQuestionsByTagId(tagId)).thenReturn(mockQuestions);
145+
when(questionRepository.findQuestionsByDifficultyAndTagId(DifficultyLevel.EASY, tagId)).thenReturn(mockQuestions);
145146

146147
// Mocking modelMapper behavior
147148
when(modelMapper.map(any(Question.class), eq(GetQuestionWithTagDto.class)))
@@ -161,7 +162,7 @@ void testGetTagDetails_Success() {
161162
assertEquals("Description1", response.getDescription());
162163
assertEquals(2, response.getRelatedQuestions().size());
163164
verify(tagRepository, times(1)).findById(tagId);
164-
verify(questionRepository, times(1)).findQuestionsByTagId(tagId);
165+
verify(questionRepository, times(1)).findQuestionsByDifficultyAndTagId(DifficultyLevel.EASY, tagId);
165166
}
166167

167168
@Test

frontend/src/routes/profile.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export default function Profile() {
177177
)
178178
)}
179179
</div>
180-
{profile.followedTags && (
180+
{profile.followedTags && profile.followedTags.length > 0 ? (
181181
<div className="flex flex-col">
182182
<div className="flex flex-wrap gap-2">
183183
<span className="flex items-center gap-2">
@@ -189,13 +189,14 @@ export default function Profile() {
189189
</Link>
190190
))
191191
.slice(0, 3)}
192-
{profile.followedTags?.length &&
193-
profile.followedTags?.length > 3 && (
194-
<span>+ {profile.followedTags?.length - 3} more</span>
195-
)}
192+
{profile.followedTags?.length > 3 && (
193+
<span>+ {profile.followedTags?.length - 3} more</span>
194+
)}
196195
</span>
197196
</div>
198197
</div>
198+
) : (
199+
<div>No followed tags to show.</div>
199200
)}
200201
</div>
201202
<div className="mt-4 flex flex-col gap-4 px-4 py-2">

frontend/src/routes/tag.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export default function TagPage() {
114114
src={tag?.logoImage || "https://placehold.co/400x300"}
115115
alt={`${tag.name} logo`}
116116
title={`alt:The logo image of ${tag.name}`}
117-
className="h-48 w-full rounded-3xl object-contain lg:h-96"
117+
className="h-20 w-full rounded-3xl object-contain lg:h-40"
118118
/>
119119
)}
120120
<div className="mb-4 flex items-center justify-between px-1">

0 commit comments

Comments
 (0)