-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAshaProfileController.java
More file actions
105 lines (80 loc) · 3.78 KB
/
AshaProfileController.java
File metadata and controls
105 lines (80 loc) · 3.78 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.iemr.flw.controller;
import com.iemr.flw.domain.iemr.AshaWorker;
import com.iemr.flw.service.AshaProfileService;
import com.iemr.flw.service.EmployeeMasterInter;
import io.lettuce.core.dynamic.annotation.Param;
import com.iemr.flw.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping(value = "/asha", produces = "application/json")
public class AshaProfileController {
private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
@Autowired
AshaProfileService ashaProfileService;
private Map<String, Object> response = new HashMap<>();
@Autowired
private JwtUtil jwtUtil;
@Autowired
private EmployeeMasterInter employeeMasterInter;
@RequestMapping(value = "editProfile", method = {RequestMethod.POST}, produces = {
"application/json"}, consumes = "application/json")
public ResponseEntity<Map<String, Object>> editEmployee(@RequestBody AshaWorker editEmployee) {
try {
System.out.println(editEmployee.toString());
AshaWorker ashaWorker = ashaProfileService.saveEditData(editEmployee);
response.put("data", ashaWorker);
response.put("statusCode", 200);
response.put("status", "Success");
} catch (Exception e) {
logger.error("Unexpected error:", e);
ResponseEntity.status(500).body(e.getMessage());
}
return ResponseEntity.ok().body(response);
}
@RequestMapping(value = "getProfile", method = RequestMethod.GET)
public ResponseEntity<Map<String, Object>> getProfile(HttpServletRequest request, @Param("employeeId") Integer employeeId) {
Map<String, Object> response = new HashMap<>();
try {
String jwtFromHeader = request.getHeader("JwtToken");
// Validate JWT header presence
// if (jwtFromHeader == null || jwtFromHeader.trim().isEmpty()) {
// response.put("statusCode", 401);
// response.put("status", "Unauthorized");
// response.put("errorMessage", "JWT token is missing");
// return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
// }
// Extract and validate user ID from JWT
// int userId = jwtUtil.extractUserId(jwtFromHeader); // Make sure this returns 0 or throws for invalid token
if (employeeId == 0) {
response.put("statusCode", 401);
response.put("status", "Unauthorized");
response.put("errorMessage", "Invalid JWT token");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
}
// Business logic
AshaWorker ashaWorker = ashaProfileService.getProfileData(employeeId);
logger.info("Asha Profile"+ashaWorker);
response.put("statusCode", 200);
response.put("status", "Success");
response.put("data", ashaWorker);
if (ashaWorker == null) {
response.put("errorMessage", "Asha profile not found");
}
return ResponseEntity.ok().body(response);
} catch (Exception e) {
logger.error("Unexpected error:", e);
response.put("statusCode", 500);
response.put("status", "Error");
response.put("errorMessage", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
}