1
0
forked from PGL/Clyde

added the post /user Endpoint

This commit is contained in:
2024-03-12 23:08:18 +01:00
parent dae59f67ce
commit 4b0ea8cf40
4 changed files with 101 additions and 22 deletions

View File

@ -1,8 +1,10 @@
package ovh.herisson.Clyde.EndPoints;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
@ -11,7 +13,9 @@ import ovh.herisson.Clyde.Services.UserService;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
@RestController
@ -38,13 +42,9 @@ public class UserController {
@PostMapping("/user")
public ResponseEntity<String> postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){
if (authorization == null) return new UnauthorizedResponse<>(null);
User poster = authServ.getUserFromToken(authorization);
if (poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin)
if (!isSecretaryOrAdmin(authorization))
return new UnauthorizedResponse<>(null);
userService.save(user);
return new ResponseEntity<>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED);
}
@ -52,12 +52,7 @@ public class UserController {
@GetMapping("/users")
public ResponseEntity<Iterable<Object[]>> getAllUsers(@RequestHeader("Authorization") String authorization){
if (authorization == null) return new UnauthorizedResponse<>(null);
User poster = authServ.getUserFromToken(authorization);
if (poster == null) return new UnauthorizedResponse<>(null);
if (poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin)
if (!isSecretaryOrAdmin(authorization))
return new UnauthorizedResponse<>(null);
Iterable<User> users = userService.getAll();
@ -68,15 +63,37 @@ public class UserController {
}
return new ResponseEntity<>(withoutPassword, HttpStatus.OK);
}
@PatchMapping("/user")
public ResponseEntity<String> patchUser(@RequestBody Map<String,Object> updates, @RequestHeader("Authorization") String authorization) {
if (authorization == null) return new UnauthorizedResponse<>(null);
User poster = authServ.getUserFromToken(authorization);
if (poster == null) {return new UnauthorizedResponse<>("bad authorization");}
if (!userService.modifyData(poster, updates, poster))
return new UnauthorizedResponse<>("there was an issue with the updates requested");
return new ResponseEntity<>("data modified", HttpStatus.OK);
}
/** return user's data except password
* @param user the user to return
* @return all the user data without the password
*/
/** return user's data except password
* @param user the user to return
* @return all the user data without the password
*/
private Object[] userWithoutPassword(User user){
return new Object[] {user.getRegNo(),user.getFirstName(),user.getLastName(),user.getBirthDate(),user.getCountry(),user.getAddress(),user.getRole()};
}
private boolean isSecretaryOrAdmin(String authorization){
if (authorization ==null)
return false;
User poster = authServ.getUserFromToken(authorization);
if (poster == null) return false;
return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin;
}
}