Skip to content

Commit

Permalink
Get User from UserContextService
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdentarikcan committed Dec 16, 2024
1 parent 44e3453 commit 5acc44c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.group1.programminglanguagesforum.DTOs.Responses.QuestionSummaryDto;
import com.group1.programminglanguagesforum.Entities.DifficultyLevel;
import com.group1.programminglanguagesforum.Entities.Question;
import com.group1.programminglanguagesforum.Entities.User;
import com.group1.programminglanguagesforum.Exceptions.ExceptionResponseHandler;
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Services.QuestionDifficultyRateService;
Expand Down Expand Up @@ -125,10 +124,9 @@ public ResponseEntity<GenericApiResponse<Map<String, Object>>> searchQuestions(
@RequestParam(required = false) DifficultyLevel difficulty,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "20") int pageSize,
@RequestParam(defaultValue = "recommended") String sortBy,
@RequestParam(defaultValue = "-1") Long currentUserId) {
@RequestParam(defaultValue = "recommended") String sortBy) {

Page<Question> questionPage = questionService.searchQuestions(query, tags, difficulty, page, pageSize, sortBy, currentUserId);
Page<Question> questionPage = questionService.searchQuestions(query, tags, difficulty, page, pageSize, sortBy);

List<QuestionSummaryDto> questionSummaries = questionPage.getContent().stream()
.map(QuestionService::mapToQuestionSummary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class QuestionService {
private final TagService tagService;
private final BookmarkRepository bookmarkRepository;
private final VoteRepository voteRepository;
private final UserService userService;
private final QuestionDifficultyRateService questionDifficultyRateService;

public Optional<Question> findById(Long id) {
Expand Down Expand Up @@ -221,21 +220,26 @@ public Page<Question> searchQuestions(
DifficultyLevel difficulty,
int page,
int pageSize,
String sortBy,
Long currentUserId) {
String sortBy) {

List<Long> tagIds = null;
if (tagIdsStr != null && !tagIdsStr.isEmpty()) {
tagIds = Arrays.stream(tagIdsStr.split(","))
.map(Long::parseLong)
.collect(Collectors.toList());
}
User currentUser;
try {
currentUser = userContextService.getCurrentUser();
} catch (UnauthorizedAccessException e) {
currentUser = null;
}

PageRequest pageable = PageRequest.of(page - 1, pageSize);
if (Objects.equals(sortBy, "default") || currentUserId == -1) {
if (Objects.equals(sortBy, "default") || Objects.equals(currentUser, null)) {
return questionRepository.searchQuestions(query, tagIds, difficulty, pageable);
} else {
User currentUser = userService.getUserById(currentUserId).get();

List<Long> authorIds = currentUser.getFollowing().stream()
.map(User::getId) // Map each User to its ID
.collect(Collectors.toList()); // Collect the IDs into a List
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -109,9 +108,4 @@ public List<User> getFollowing(User user) {
return user.getFollowing().stream().toList();
}

public List<Long> getFollowingIds(User user) {
return user.getFollowing().stream()
.map(User::getId) // Map each User to its ID
.collect(Collectors.toList()); // Collect the IDs into a List
}
}
8 changes: 0 additions & 8 deletions frontend/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Hash, MessageSquare, Search } from "lucide-react";
import { useEffect, useId, useRef, useState } from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
import { Button } from "./ui/button";
import useAuthStore from "@/services/auth";
import {
DropdownMenu,
DropdownMenuContent,
Expand All @@ -24,7 +23,6 @@ const searchTypes = [
{ id: "questions", label: "Questions", icon: MessageSquare },
] as const;


export const SearchBar = () => {
const id = useId();
const [params] = useSearchParams();
Expand All @@ -37,8 +35,6 @@ export const SearchBar = () => {
const inputRef = useRef<HTMLInputElement>(null);
const navigate = useNavigate();

const { selfProfile } = useAuthStore();

// Get current search type info
const currentSearchType = searchTypes.find((type) => type.id === searchType);
const SearchTypeIcon = currentSearchType?.icon || Hash;
Expand Down Expand Up @@ -101,10 +97,6 @@ export const SearchBar = () => {
const params = new URLSearchParams();
params.append("type", searchType);
params.append("q", search);
// Safely append currentUserId only if selfProfile is available
if (selfProfile?.id) {
params.append("currentUserId", selfProfile.id.toString());
}
params.append("sortBy", "recommended");
navigate("/search?" + params.toString());
};
Expand Down
10 changes: 2 additions & 8 deletions frontend/src/services/api/programmingForumComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1980,16 +1980,10 @@ export type SearchQuestionsQueryParams = {
pageSize?: number;
/**
* Sorting type
*
* @default "recommended"
*
* @default recommended
*/
sortBy?: string;
/**
* Current user Id
*
* @default -1
*/
currentUserId?: number;
};

export type SearchQuestionsError = Fetcher.ErrorWrapper<{
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/services/api/programmingForumSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export type UserProfileUpdate = {
};

/**
* @example {"id":1,"username":"john_doe","reputationPoints":100,"profilePicture":"frontend\src\assets\placeholder_profile.png","name":"John Doe"}
* @example {"id":1,"username":"john_doe","reputationPoints":100,"profilePicture":"https://placehold.co/640x640","name":"John Doe"}
*/
export type UserSummary = {
id: number;
Expand Down
8 changes: 1 addition & 7 deletions swagger/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -938,18 +938,12 @@ paths:
schema:
type: integer
default: 20
- name: sortBy
- name: sortBy
in: query
description: Sorting type
schema:
type: string
default: "recommended"
- name: currentUserId
in: query
description: Current user id
schema:
type: integer
default: -1
responses:
"200":
description: Successful response
Expand Down

0 comments on commit 5acc44c

Please sign in to comment.