source: src/main/java/finki/it/terapijamkbackend/spring/controllers/UserController.java

Last change on this file was 43c9090, checked in by macagaso <gasoskamarija@…>, 5 weeks ago

Updated version

  • Property mode set to 100644
File size: 6.1 KB
Line 
1package finki.it.terapijamkbackend.spring.controllers;
2
3import finki.it.terapijamkbackend.spring.dto.*;
4import finki.it.terapijamkbackend.spring.dto.CarriedOutInfo;
5import finki.it.terapijamkbackend.spring.entities.User;
6import finki.it.terapijamkbackend.spring.exception.UserNotFoundException;
7import finki.it.terapijamkbackend.spring.services.UserService;
8import jakarta.servlet.http.HttpServletRequest;
9import jakarta.servlet.http.Cookie;
10import org.springframework.beans.factory.annotation.Autowired;
11import org.springframework.http.HttpStatus;
12import org.springframework.http.ResponseEntity;
13import org.springframework.web.bind.annotation.*;
14
15import java.util.HashMap;
16import java.util.List;
17import java.util.Map;
18
19
20@RestController
21@RequestMapping("/api/users")
22public class UserController {
23
24 @Autowired
25 private UserService userService;
26
27 @PostMapping(path = "/signIn")
28 public ResponseEntity<Map<String,String>> createUser(@RequestBody Map<String, String> userData){
29 Map<String, String> response = new HashMap<>();
30 response.put("error", "Error creating user");
31 if(userService.userExists(userData.get("username"))){
32 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
33 }
34
35 try {
36 String name = userData.get("name");
37 String surname = userData.get("surname");
38 String username = userData.get("username");
39 String password = userData.get("password");
40 String age = userData.get("age");
41 String phone = userData.get("phone").replace("-","");
42
43 userService.createUser(name, surname, username, password, age, phone);
44
45 response = new HashMap<>();
46 response.put("message", "User created successfully!");
47 return ResponseEntity.ok(response);
48 } catch (Exception e) {
49 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
50 }
51 }
52
53 @GetMapping("/editUser")
54 public ResponseEntity<User> getUser(@RequestParam String username) {
55 User user = userService.getUserByUsername(username);
56 if (user != null) {
57 return ResponseEntity.ok(user);
58 } else {
59 return ResponseEntity.notFound().build();
60 }
61 }
62
63 @GetMapping("/getUsersByParameter")
64 public ResponseEntity<List<User>> getUsersByParameter(@RequestParam String parameter,@RequestParam String filter)
65 {
66 List<User> users = userService.getUsersByFilter(parameter, filter);
67 return ResponseEntity.ok(users);
68
69 }
70
71
72 @PostMapping("/updateUser")
73 public ResponseEntity<User> updateUser(@RequestBody User updatedUser, HttpServletRequest request) {
74 try {
75 String oldUsername=getCookieValue(request, "username");
76 User user = userService.updateUser(oldUsername, updatedUser);
77 return ResponseEntity.ok(user);
78 } catch (UserNotFoundException e) {
79 return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
80 } catch (Exception e) {
81 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
82 }
83 }
84 private String getCookieValue(HttpServletRequest request, String cookieName) {
85 Cookie[] cookies = request.getCookies();
86 if (cookies != null) {
87 for (Cookie cookie : cookies) {
88 if (cookie.getName().equals(cookieName)) {
89 return cookie.getValue();
90 }
91 }
92 }
93 return null;
94 }
95 @PutMapping("/addTerm")
96 public ResponseEntity<String> addTerm(@RequestBody TermUpdateRequest request)
97 {
98 User user=userService.getUserById(request.getUserId());
99 boolean isUpdated = userService.updateUserTermList(user,request.getTerm(),request.getAdditionalInfo(),request.getStatus());
100 if (isUpdated) {
101 return ResponseEntity.ok("Term updated successfully.");
102 } else {
103 return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found.");
104 }
105 }
106 @PutMapping("/carriedOut")
107 public ResponseEntity<String> addTerm(@RequestBody CarriedOutUpdate request)
108 {
109 User user=userService.getUserById(request.getUserId());
110 boolean isUpdated = userService.updateUserCarriedOutList(user,request.getTerm(),request.getAdditionalInfo(),request.getStatus(),request.getNote());
111 if (isUpdated) {
112 return ResponseEntity.ok("Term updated successfully.");
113 } else {
114 return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found.");
115 }
116 }
117
118 @GetMapping("/blockedStatus")
119 public ResponseEntity<Map<String, Boolean>> getUserAccountStatus(@RequestParam("username") String username) {
120 boolean isBlocked = userService.isUserBlocked(username);
121 Map<String, Boolean> response = new HashMap<>();
122 response.put("isBlocked", isBlocked);
123 return ResponseEntity.ok(response);
124 }
125 @PutMapping("/toggleBlock")
126 public ResponseEntity<ApiResponse> toggleBlockUser(@RequestParam("username") String username) {
127 boolean updated = userService.toggleUserBlockStatus(username);
128 if (updated) {
129 return ResponseEntity.ok(new ApiResponse("User status updated"));
130 } else {
131 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ApiResponse("User not found"));
132 }
133 }
134
135 @GetMapping("/getAllIds")
136 public List<Long> getAllUserIds() {
137 return userService.getAllUserIds();
138 }
139
140 @GetMapping("/listAllCarriedOut")
141 public List<CarriedOutInfo> getAppointmentsByUser(@RequestParam("username") String username) {
142 List<CarriedOutInfo>carriedTerms=userService.findCarriedOutByUsername(username);
143 return carriedTerms;
144 }
145 @PostMapping("/checkDifferentUser")
146 public ResponseEntity<Boolean> checkDifferentUser(@RequestBody Map<String, String> userData) {
147 boolean isDifferentUser = userService.checkDifferentUser(userData);
148 if(isDifferentUser){
149 return ResponseEntity.ok(isDifferentUser);
150 }
151 else{
152 return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(isDifferentUser);
153 }
154
155 }
156
157}
158
159
Note: See TracBrowser for help on using the repository browser.