Skip to content

Commit

Permalink
Format GET /search/users response.
Browse files Browse the repository at this point in the history
  • Loading branch information
EnesBaserr committed May 14, 2024
1 parent 33c8b60 commit 3264ff6
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.group1.cuisines.controllers;

import com.group1.cuisines.dao.response.ApiResponse;
import com.group1.cuisines.dao.response.ErrorResponse;
import com.group1.cuisines.dao.response.SuccessResponse;
import com.group1.cuisines.dto.DishResponseDto;
import com.group1.cuisines.dto.UserDto;
import com.group1.cuisines.entities.Dish;
import com.group1.cuisines.entities.User;
import com.group1.cuisines.services.SearchService;
import com.group1.cuisines.services.UserService;
import com.group1.cuisines.services.WikidataService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -29,9 +33,18 @@ public ResponseEntity<?> searchUsers(@RequestParam(required = false) String q) {
List<User> users = userService.searchUsers(q);
if (users.isEmpty()) {
// Return a custom message with a "No Content" status when no users are found
return ResponseEntity.status(HttpStatus.NO_CONTENT).body("No users found");
return ResponseEntity.ok(new ErrorResponse(204,"No users found"));
}
return ResponseEntity.ok(users); // Return the list of users when found
return ResponseEntity.ok(new SuccessResponse<>(200, users.stream().map(user -> new UserDto(
user.getId(),
user.getUsername(),
user.getFirstName(),
user.getLastName(),
user.getFollowerCount(),
user.getFollowingCount(),
user.getRecipeCount()
)).collect(Collectors.toList()), "Users fetched successfully"));

}

@GetMapping("/dishes")
Expand Down

0 comments on commit 3264ff6

Please sign in to comment.