Skip to content

Commit

Permalink
feat: getUser and banUser added to UserRestController
Browse files Browse the repository at this point in the history
  • Loading branch information
franchescoURJC committed Mar 18, 2023
1 parent 6d8026f commit 808597e
Showing 1 changed file with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
package net.daw.alist.controllers.rest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jdk.jshell.execution.Util;
import net.daw.alist.models.Post;
import net.daw.alist.models.User;
import net.daw.alist.services.UserService;
import net.daw.alist.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/users")
public class UserRestController {

@Autowired
private UserService userService;

@Autowired
private Utils utils;

@GetMapping("/{username}")
public ResponseEntity<User> getUser(@PathVariable String username) {
Optional<User> optionalUser = userService.findByUsername(username);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
return new ResponseEntity<>(user, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@PutMapping ("/{id}")
public ResponseEntity<User> banUser(Authentication authentication, @PathVariable Long id) {
String userRole = utils.getCurrentUserRole(authentication);
if (!userRole.equals("ADMIN"))
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
Optional<User> optionalUser = userService.findByID(id);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
if (user.isLocked()) {
userService.unbanUser(user.getUsername());
} else {
userService.banUser(user.getUsername());
}
return new ResponseEntity<>(user, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

//TODO: upvotes, followers
}

0 comments on commit 808597e

Please sign in to comment.