mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
Update file UserController.java
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package com.example.demo.controller;
|
package com.example.demo.controller;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -16,6 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import com.example.demo.dto.UserDto;
|
import com.example.demo.dto.UserDto;
|
||||||
import com.example.demo.entity.User;
|
import com.example.demo.entity.User;
|
||||||
|
import com.example.demo.repository.UserRepo;
|
||||||
import com.example.demo.service.UserService;
|
import com.example.demo.service.UserService;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -23,10 +26,12 @@ import com.example.demo.service.UserService;
|
|||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
private UserRepo userRepo;
|
||||||
|
|
||||||
public UserController(UserService userService) {
|
public UserController(UserService userService, UserRepo userRepo) {
|
||||||
super();
|
super();
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
|
this.userRepo = userRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
// build create user REST API
|
// build create user REST API
|
||||||
@@ -49,6 +54,15 @@ public class UserController {
|
|||||||
return new ResponseEntity<>(users, HttpStatus.OK);
|
return new ResponseEntity<>(users, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// build get current user REST API
|
||||||
|
@GetMapping("/me")
|
||||||
|
public UserDto getLoggedInUser(Principal principal) {
|
||||||
|
if (principal == null) return null;
|
||||||
|
String username = principal.getName();
|
||||||
|
User user = (userRepo.findByUsername(username))
|
||||||
|
.orElse(null);
|
||||||
|
return userService.convertToDto(user);
|
||||||
|
}
|
||||||
|
|
||||||
// build get user by id REST API
|
// build get user by id REST API
|
||||||
// http://localhost:8080/api/users/(id number goes here)
|
// http://localhost:8080/api/users/(id number goes here)
|
||||||
|
|||||||
Reference in New Issue
Block a user