Class UserController

java.lang.Object
com.needleandstitch.pavuk.controller.UserController

@Controller @RequestMapping("/users") @CrossOrigin public class UserController extends Object
REST controller for managing User objects.

This class provides endpoints for handling user-related operations such as retrieving users, signing up new users, handling authentication, logging out users, updating existing users, and deleting users.

Endpoints:

  • GET /users Retrieve all roles
  • GET /users/{id} Retrieve a role by its ID
  • GET /users/current Retrive currently authenticated user
  • POST /users/sign-up Register a new user
  • POST /users/sign-in Sign in a user
  • POST /users/sign-out Log out current user
  • PUT /users/{id} Update an existing role
  • DELETE /users/{id} Delete a role
Since:
15.12.2024
Version:
1.0.0
Author:
Needle & Stitch
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.http.ResponseEntity<Void>
    createUser(User user, jakarta.servlet.http.HttpServletResponse response)
    Endpoint to register a new user.
    org.springframework.http.ResponseEntity<Void>
    Endpoint to delete a user by its ID.
    org.springframework.http.ResponseEntity<List<User>>
    Endpoint to retrieve all users.
    org.springframework.http.ResponseEntity<User>
    getCurrentUser(jakarta.servlet.http.HttpServletRequest request)
    Endpoint to retrieve the currently authenticated user based on the JWT token from cookies.
    org.springframework.http.ResponseEntity<User>
    Endpoint to retrieve a clothing item by its ID.
    org.springframework.http.ResponseEntity<User>
    signIn(User user, jakarta.servlet.http.HttpServletResponse response)
    Endpoint to authenticate a user and return their information.
    org.springframework.http.ResponseEntity<Void>
    signOut(jakarta.servlet.http.HttpServletResponse response)
    Endpoint to log the user out by removing the JWT cookie.
    org.springframework.http.ResponseEntity<Void>
    updateUser(Long id, User userDetails)
    Endpoint to update an existing user by its ID.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • UserController

      public UserController()
  • Method Details

    • getAllUsers

      @GetMapping public org.springframework.http.ResponseEntity<List<User>> getAllUsers()
      Endpoint to retrieve all users.
      Returns:
      A ResponseEntity containing a list of all User objects.
    • getUserById

      @GetMapping("/{id}") public org.springframework.http.ResponseEntity<User> getUserById(@PathVariable Long id)
      Endpoint to retrieve a clothing item by its ID.
      Parameters:
      id - The ID of the user.
      Returns:
      A ResponseEntity containing the User object if found or a 404 Not Found status if the object does not exist.
    • getCurrentUser

      @GetMapping("/current") public org.springframework.http.ResponseEntity<User> getCurrentUser(jakarta.servlet.http.HttpServletRequest request)
      Endpoint to retrieve the currently authenticated user based on the JWT token from cookies.
      Parameters:
      request - The HTTP request containing the cookies
      Returns:
      A ResponseEntity containing the current user and a status of OK (200) if authenticated, or UNAUTHORIZED (401) if not
    • createUser

      @PostMapping("/sign-up") public org.springframework.http.ResponseEntity<Void> createUser(@RequestBody User user, jakarta.servlet.http.HttpServletResponse response)
      Endpoint to register a new user.
      Parameters:
      user - The user information to be used for the registration
      response - The HTTP response for setting the JWT cookie
      Returns:
      A ResponseEntity with a CREATED (201) status
    • signIn

      @PostMapping("/sign-in") public org.springframework.http.ResponseEntity<User> signIn(@RequestBody User user, jakarta.servlet.http.HttpServletResponse response)
      Endpoint to authenticate a user and return their information.
      Parameters:
      user - The user credentials (email and password)
      response - The HTTP response for setting the JWT cookie
      Returns:
      A ResponseEntity containing the authenticated user and a status of OK (200) if successful, or UNAUTHORIZED (401) if authentication fails.
    • signOut

      @PostMapping("/sign-out") public org.springframework.http.ResponseEntity<Void> signOut(jakarta.servlet.http.HttpServletResponse response)
      Endpoint to log the user out by removing the JWT cookie.
      Parameters:
      response - The HTTP response for removing the JWT cookie
      Returns:
      A ResponseEntity with a status of OK (200).
    • updateUser

      @PutMapping("/{id}") public org.springframework.http.ResponseEntity<Void> updateUser(@PathVariable Long id, @RequestBody User userDetails)
      Endpoint to update an existing user by its ID.
      Parameters:
      id - The ID of the user to update.
      userDetails - The updated User object.
      Returns:
      A ResponseEntity with a 200 OK status if successful, or a 404 Not Found status if the object does not exist.
    • deleteUser

      @DeleteMapping("/{id}") public org.springframework.http.ResponseEntity<Void> deleteUser(@PathVariable Long id)
      Endpoint to delete a user by its ID.
      Parameters:
      id - The ID of the user to delete.
      Returns:
      A ResponseEntity with a 204 No Content status if successful, or a 404 Not Found status if not.