From 5acc44ca369416e6f65643c7ac2f2ac1039ce454 Mon Sep 17 00:00:00 2001 From: ozdentarikcan Date: Mon, 16 Dec 2024 15:42:05 +0300 Subject: [PATCH] Get User from UserContextService --- .../Controllers/QuestionController.java | 6 ++---- .../Services/QuestionService.java | 14 +++++++++----- .../Services/UserService.java | 6 ------ frontend/src/components/SearchBar.tsx | 8 -------- .../src/services/api/programmingForumComponents.ts | 10 ++-------- .../src/services/api/programmingForumSchemas.ts | 2 +- swagger/openapi.yml | 8 +------- 7 files changed, 15 insertions(+), 39 deletions(-) diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java b/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java index 5304b0c0..96ed50ad 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java @@ -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; @@ -125,10 +124,9 @@ public ResponseEntity>> 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 questionPage = questionService.searchQuestions(query, tags, difficulty, page, pageSize, sortBy, currentUserId); + Page questionPage = questionService.searchQuestions(query, tags, difficulty, page, pageSize, sortBy); List questionSummaries = questionPage.getContent().stream() .map(QuestionService::mapToQuestionSummary) diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java b/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java index 2d88d4f4..e4bc9b81 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java @@ -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 findById(Long id) { @@ -221,8 +220,7 @@ public Page searchQuestions( DifficultyLevel difficulty, int page, int pageSize, - String sortBy, - Long currentUserId) { + String sortBy) { List tagIds = null; if (tagIdsStr != null && !tagIdsStr.isEmpty()) { @@ -230,12 +228,18 @@ public Page searchQuestions( .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 authorIds = currentUser.getFollowing().stream() .map(User::getId) // Map each User to its ID .collect(Collectors.toList()); // Collect the IDs into a List diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java b/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java index 46e18d54..118f6a23 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -109,9 +108,4 @@ public List getFollowing(User user) { return user.getFollowing().stream().toList(); } - public List 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 - } } diff --git a/frontend/src/components/SearchBar.tsx b/frontend/src/components/SearchBar.tsx index 8260bfe7..5ef44760 100644 --- a/frontend/src/components/SearchBar.tsx +++ b/frontend/src/components/SearchBar.tsx @@ -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, @@ -24,7 +23,6 @@ const searchTypes = [ { id: "questions", label: "Questions", icon: MessageSquare }, ] as const; - export const SearchBar = () => { const id = useId(); const [params] = useSearchParams(); @@ -37,8 +35,6 @@ export const SearchBar = () => { const inputRef = useRef(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; @@ -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()); }; diff --git a/frontend/src/services/api/programmingForumComponents.ts b/frontend/src/services/api/programmingForumComponents.ts index 16a6d8b4..b05bc956 100644 --- a/frontend/src/services/api/programmingForumComponents.ts +++ b/frontend/src/services/api/programmingForumComponents.ts @@ -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<{ diff --git a/frontend/src/services/api/programmingForumSchemas.ts b/frontend/src/services/api/programmingForumSchemas.ts index 59344b8f..4080d5ec 100644 --- a/frontend/src/services/api/programmingForumSchemas.ts +++ b/frontend/src/services/api/programmingForumSchemas.ts @@ -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; diff --git a/swagger/openapi.yml b/swagger/openapi.yml index 86ed232c..499f1a48 100644 --- a/swagger/openapi.yml +++ b/swagger/openapi.yml @@ -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