Skip to content

Commit

Permalink
fix(backend): tag service not returning any tags without questions
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr committed Dec 13, 2024
1 parent 46c4d5d commit 2aabc8a
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
package com.group1.programminglanguagesforum.Controllers;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.group1.programminglanguagesforum.Constants.EndpointConstants;
import com.group1.programminglanguagesforum.DTOs.Requests.CreateQuestionRequestDto;
import com.group1.programminglanguagesforum.DTOs.Requests.DifficultyLevelRequestDto;
import com.group1.programminglanguagesforum.DTOs.Requests.UpdateQuestionRequestDto;
import com.group1.programminglanguagesforum.DTOs.Responses.*;
import com.group1.programminglanguagesforum.Exceptions.ExceptionResponseHandler;
import com.group1.programminglanguagesforum.DTOs.Responses.CreateQuestionResponseDto;
import com.group1.programminglanguagesforum.DTOs.Responses.ErrorResponse;
import com.group1.programminglanguagesforum.DTOs.Responses.GenericApiResponse;
import com.group1.programminglanguagesforum.DTOs.Responses.GetQuestionDetailsResponseDto;
import com.group1.programminglanguagesforum.DTOs.Responses.QuestionRateResponseDto;
import com.group1.programminglanguagesforum.DTOs.Responses.QuestionSummaryDto;
import com.group1.programminglanguagesforum.Entities.DifficultyLevel;
import com.group1.programminglanguagesforum.Entities.Question;
import com.group1.programminglanguagesforum.Exceptions.ExceptionResponseHandler;
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Services.QuestionDifficultyRateService;
import com.group1.programminglanguagesforum.Services.QuestionService;
import com.group1.programminglanguagesforum.Util.ApiResponseBuilder;
import lombok.RequiredArgsConstructor;

import org.springframework.http.HttpStatus;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/v1")
Expand Down Expand Up @@ -59,7 +73,8 @@ public ResponseEntity<GenericApiResponse<GetQuestionDetailsResponseDto>> getQues
.errorMessage(e.getMessage())
.stackTrace(Arrays.toString(e.getStackTrace()))
.build();
GenericApiResponse<GetQuestionDetailsResponseDto> response = ApiResponseBuilder.buildErrorResponse(GetQuestionDetailsResponseDto.class, e.getMessage(), 404, errorResponse);
GenericApiResponse<GetQuestionDetailsResponseDto> response = ApiResponseBuilder
.buildErrorResponse(GetQuestionDetailsResponseDto.class, e.getMessage(), 404, errorResponse);
return buildResponse(response, org.springframework.http.HttpStatus.NOT_FOUND);
}
}
Expand Down Expand Up @@ -113,7 +128,7 @@ public ResponseEntity<GenericApiResponse<Map<String, Object>>> searchQuestions(
Page<Question> questionPage = questionService.searchQuestions(query, tags, difficulty, page, pageSize);

List<QuestionSummaryDto> questionSummaries = questionPage.getContent().stream()
.map(questionService::mapToQuestionSummary)
.map(QuestionService::mapToQuestionSummary)
.toList();

Map<String, Object> response = new HashMap<>();
Expand Down Expand Up @@ -142,8 +157,7 @@ public ResponseEntity<GenericApiResponse<QuestionRateResponseDto>> rateQuestion(
return buildResponse(response, org.springframework.http.HttpStatus.OK);
} catch (NoSuchElementException e) {
return ExceptionResponseHandler.NoSuchElementException(e);
}
catch (UnauthorizedAccessException e) {
} catch (UnauthorizedAccessException e) {
return ExceptionResponseHandler.UnauthorizedAccessException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package com.group1.programminglanguagesforum.Repositories;

import java.util.List;

import com.group1.programminglanguagesforum.Entities.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import com.group1.programminglanguagesforum.Entities.Tag;

@Repository
public interface TagRepository extends JpaRepository<Tag,Long> {
public interface TagRepository extends JpaRepository<Tag, Long> {
List<Tag> findAllByIdIn(List<Long> ids);

@Query("SELECT t FROM Tag t " +
"JOIN Question q ON t MEMBER OF q.tags " +
"LEFT JOIN Question q ON t MEMBER OF q.tags " +
"WHERE LOWER(t.tagName) LIKE LOWER(CONCAT('%', :tagName, '%')) " +
"GROUP BY t.id " +
"ORDER BY COUNT(q.id) DESC")

Page<Tag> findTagsByTagNameContainingIgnoreCase(String tagName, Pageable pageable);

@Query("SELECT t FROM Tag t JOIN t.followers u WHERE u.id = :userId")
List<Tag> findTagByFollowers(@Param("userId") Long userId);
}
Loading

0 comments on commit 2aabc8a

Please sign in to comment.