-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserV1Controller.java
More file actions
85 lines (71 loc) · 2.92 KB
/
UserV1Controller.java
File metadata and controls
85 lines (71 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package in.newdevpoint.bootcamp.controller;
import in.newdevpoint.bootcamp.api.UsersApi;
import in.newdevpoint.bootcamp.dto.UserReq;
import in.newdevpoint.bootcamp.usecase.UserUseCase;
import in.newdevpoint.bootcamp.utility.RoleConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
// @PreAuthorize(RoleConstants.USER_CRUD)
public class UserV1Controller implements UsersApi {
private final UserUseCase userUseCase;
@Override
@PreAuthorize(RoleConstants.ADMIN_CRUD)
public ResponseEntity<UserReq> getUserById(String userId) {
UserReq userProfile = userUseCase.getUserById(userId);
return ResponseEntity.ok(userProfile);
}
@Override
@PreAuthorize(RoleConstants.USER_CRUD)
public ResponseEntity<UserReq> getUserDetails() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String username = auth.getName();
UserReq userProfile = userUseCase.getUser(username);
return ResponseEntity.ok(userProfile);
}
@Override
@PreAuthorize(RoleConstants.ADMIN_CRUD)
public ResponseEntity<Object> searchUsers(
Integer size, Integer page, String sortDir, String query, String sortBy) {
Page<UserReq> userEntityList = userUseCase.searchUser(size, page, sortDir, query, sortBy);
return ResponseEntity.status(HttpStatus.CREATED).body(userEntityList);
}
@Override
@PreAuthorize(RoleConstants.ADMIN_CRUD)
public ResponseEntity<UserReq> updateUser(@RequestBody UserReq userEntity) {
UserReq updatedUserEntity = userUseCase.updateUser(userEntity);
if (updatedUserEntity != null) {
return ResponseEntity.ok(updatedUserEntity);
} else {
return ResponseEntity.notFound().build();
}
}
@Override
@PreAuthorize(RoleConstants.ADMIN_CRUD)
public ResponseEntity<Void> deleteUser(@PathVariable String id) {
userUseCase.deleteUser(id);
return ResponseEntity.noContent().build();
}
/* @Override
@PreAuthorize(RoleConstants.ADMIN_CRUD)
public ResponseEntity<List<UserReq>> getUsers() {
List<UserReq> userEntityList = userUseCase.getUsers();
return ResponseEntity.status(HttpStatus.OK).body(userEntityList);
}*/
@Override
public ResponseEntity<UserReq> updateUserProfilePhoto(MultipartFile file) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String username = auth.getName();
UserReq updatedUserEntity = userUseCase.uploadUserProfile(file, username);
return ResponseEntity.ok(updatedUserEntity);
}
}