use employee api
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
package org.example.petshopdesktop.api.dto.employee;
|
||||||
|
|
||||||
|
public class EmployeeRequest {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private String email;
|
||||||
|
private String phone;
|
||||||
|
private String role;
|
||||||
|
private Boolean active;
|
||||||
|
|
||||||
|
public String getUsername() { return username; }
|
||||||
|
public void setUsername(String username) { this.username = username; }
|
||||||
|
public String getPassword() { return password; }
|
||||||
|
public void setPassword(String password) { this.password = password; }
|
||||||
|
public String getFirstName() { return firstName; }
|
||||||
|
public void setFirstName(String firstName) { this.firstName = firstName; }
|
||||||
|
public String getLastName() { return lastName; }
|
||||||
|
public void setLastName(String lastName) { this.lastName = lastName; }
|
||||||
|
public String getEmail() { return email; }
|
||||||
|
public void setEmail(String email) { this.email = email; }
|
||||||
|
public String getPhone() { return phone; }
|
||||||
|
public void setPhone(String phone) { this.phone = phone; }
|
||||||
|
public String getRole() { return role; }
|
||||||
|
public void setRole(String role) { this.role = role; }
|
||||||
|
public Boolean getActive() { return active; }
|
||||||
|
public void setActive(Boolean active) { this.active = active; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package org.example.petshopdesktop.api.dto.employee;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
public class EmployeeResponse {
|
||||||
|
private Long employeeId;
|
||||||
|
private Long userId;
|
||||||
|
private String username;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private String fullName;
|
||||||
|
private String email;
|
||||||
|
private String phone;
|
||||||
|
private String role;
|
||||||
|
private Boolean active;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
public Long getEmployeeId() { return employeeId; }
|
||||||
|
public void setEmployeeId(Long employeeId) { this.employeeId = employeeId; }
|
||||||
|
public Long getUserId() { return userId; }
|
||||||
|
public void setUserId(Long userId) { this.userId = userId; }
|
||||||
|
public String getUsername() { return username; }
|
||||||
|
public void setUsername(String username) { this.username = username; }
|
||||||
|
public String getFirstName() { return firstName; }
|
||||||
|
public void setFirstName(String firstName) { this.firstName = firstName; }
|
||||||
|
public String getLastName() { return lastName; }
|
||||||
|
public void setLastName(String lastName) { this.lastName = lastName; }
|
||||||
|
public String getFullName() { return fullName; }
|
||||||
|
public void setFullName(String fullName) { this.fullName = fullName; }
|
||||||
|
public String getEmail() { return email; }
|
||||||
|
public void setEmail(String email) { this.email = email; }
|
||||||
|
public String getPhone() { return phone; }
|
||||||
|
public void setPhone(String phone) { this.phone = phone; }
|
||||||
|
public String getRole() { return role; }
|
||||||
|
public void setRole(String role) { this.role = role; }
|
||||||
|
public Boolean getActive() { return active; }
|
||||||
|
public void setActive(Boolean active) { this.active = active; }
|
||||||
|
public LocalDateTime getCreatedAt() { return createdAt; }
|
||||||
|
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
|
||||||
|
public LocalDateTime getUpdatedAt() { return updatedAt; }
|
||||||
|
public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package org.example.petshopdesktop.api.endpoints;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import org.example.petshopdesktop.api.ApiClient;
|
||||||
|
import org.example.petshopdesktop.api.dto.common.PageResponse;
|
||||||
|
import org.example.petshopdesktop.api.dto.employee.EmployeeRequest;
|
||||||
|
import org.example.petshopdesktop.api.dto.employee.EmployeeResponse;
|
||||||
|
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EmployeeApi {
|
||||||
|
private static final EmployeeApi INSTANCE = new EmployeeApi();
|
||||||
|
private final ApiClient apiClient;
|
||||||
|
|
||||||
|
private EmployeeApi() {
|
||||||
|
this.apiClient = ApiClient.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static EmployeeApi getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<EmployeeResponse> listEmployees(String query) throws Exception {
|
||||||
|
String path = "/api/v1/employees?page=0&size=1000";
|
||||||
|
if (query != null && !query.isEmpty()) {
|
||||||
|
path += "&q=" + URLEncoder.encode(query, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
String response = apiClient.getRawResponse(path);
|
||||||
|
PageResponse<EmployeeResponse> pageResponse = apiClient.getObjectMapper().readValue(
|
||||||
|
response,
|
||||||
|
new TypeReference<PageResponse<EmployeeResponse>>() {}
|
||||||
|
);
|
||||||
|
if (pageResponse == null) {
|
||||||
|
throw new IllegalStateException("Null response from employees endpoint");
|
||||||
|
}
|
||||||
|
return pageResponse.getContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeResponse createEmployee(EmployeeRequest request) throws Exception {
|
||||||
|
return apiClient.post("/api/v1/employees", request, EmployeeResponse.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,8 +16,8 @@ import javafx.scene.control.TextField;
|
|||||||
import javafx.scene.control.cell.PropertyValueFactory;
|
import javafx.scene.control.cell.PropertyValueFactory;
|
||||||
import javafx.stage.Modality;
|
import javafx.stage.Modality;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import org.example.petshopdesktop.api.dto.user.UserResponse;
|
import org.example.petshopdesktop.api.dto.employee.EmployeeResponse;
|
||||||
import org.example.petshopdesktop.api.endpoints.UserApi;
|
import org.example.petshopdesktop.api.endpoints.EmployeeApi;
|
||||||
import org.example.petshopdesktop.auth.UserSession;
|
import org.example.petshopdesktop.auth.UserSession;
|
||||||
import org.example.petshopdesktop.models.StaffAccount;
|
import org.example.petshopdesktop.models.StaffAccount;
|
||||||
import org.example.petshopdesktop.util.ActivityLogger;
|
import org.example.petshopdesktop.util.ActivityLogger;
|
||||||
@@ -116,8 +116,8 @@ public class StaffAccountsController {
|
|||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
List<UserResponse> users = UserApi.getInstance().listUsers(null);
|
List<EmployeeResponse> employees = EmployeeApi.getInstance().listEmployees(null);
|
||||||
List<StaffAccount> accounts = users.stream()
|
List<StaffAccount> accounts = employees.stream()
|
||||||
.map(this::mapToStaffAccount)
|
.map(this::mapToStaffAccount)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
@@ -135,21 +135,22 @@ public class StaffAccountsController {
|
|||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private StaffAccount mapToStaffAccount(UserResponse user) {
|
private StaffAccount mapToStaffAccount(EmployeeResponse employee) {
|
||||||
long id = user.getId() != null ? user.getId() : 0L;
|
long userId = employee.getUserId() != null ? employee.getUserId() : 0L;
|
||||||
String username = user.getUsername();
|
long employeeId = employee.getEmployeeId() != null ? employee.getEmployeeId() : 0L;
|
||||||
String fullName = user.getFullName() != null ? user.getFullName() : "";
|
String username = employee.getUsername();
|
||||||
|
String fullName = employee.getFullName() != null ? employee.getFullName() : "";
|
||||||
String[] names = splitFullName(fullName);
|
String[] names = splitFullName(fullName);
|
||||||
String firstName = names[0];
|
String firstName = names[0];
|
||||||
String lastName = names[1];
|
String lastName = names[1];
|
||||||
String email = user.getEmail() != null ? user.getEmail() : "";
|
String email = employee.getEmail() != null ? employee.getEmail() : "";
|
||||||
String phone = user.getPhone() != null ? user.getPhone() : "";
|
String phone = employee.getPhone() != null ? employee.getPhone() : "";
|
||||||
boolean active = user.getActive() != null ? user.getActive() : false;
|
boolean active = employee.getActive() != null ? employee.getActive() : false;
|
||||||
Timestamp createdAt = user.getCreatedAt() != null
|
Timestamp createdAt = employee.getCreatedAt() != null
|
||||||
? Timestamp.from(user.getCreatedAt().atZone(ZoneId.systemDefault()).toInstant())
|
? Timestamp.from(employee.getCreatedAt().atZone(ZoneId.systemDefault()).toInstant())
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
return new StaffAccount(id, id, username, firstName, lastName, email, phone, active, createdAt);
|
return new StaffAccount(userId, employeeId, username, firstName, lastName, email, phone, active, createdAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String[] splitFullName(String fullName) {
|
private String[] splitFullName(String fullName) {
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import javafx.scene.control.Label;
|
|||||||
import javafx.scene.control.PasswordField;
|
import javafx.scene.control.PasswordField;
|
||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import org.example.petshopdesktop.api.dto.user.UserRequest;
|
import org.example.petshopdesktop.api.dto.employee.EmployeeRequest;
|
||||||
import org.example.petshopdesktop.api.endpoints.UserApi;
|
import org.example.petshopdesktop.api.endpoints.EmployeeApi;
|
||||||
import org.example.petshopdesktop.Validator;
|
import org.example.petshopdesktop.Validator;
|
||||||
import org.example.petshopdesktop.util.ActivityLogger;
|
import org.example.petshopdesktop.util.ActivityLogger;
|
||||||
|
|
||||||
@@ -89,16 +89,17 @@ public class StaffRegisterDialogController {
|
|||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
UserRequest request = new UserRequest();
|
EmployeeRequest request = new EmployeeRequest();
|
||||||
request.setUsername(username);
|
request.setUsername(username);
|
||||||
request.setPassword(password);
|
request.setPassword(password);
|
||||||
request.setFullName(firstName + " " + lastName);
|
request.setFirstName(firstName);
|
||||||
|
request.setLastName(lastName);
|
||||||
request.setEmail(email);
|
request.setEmail(email);
|
||||||
request.setPhone(phone);
|
request.setPhone(phone);
|
||||||
request.setRole("STAFF");
|
request.setRole("STAFF");
|
||||||
request.setActive(true);
|
request.setActive(true);
|
||||||
|
|
||||||
UserApi.getInstance().createUser(request);
|
EmployeeApi.getInstance().createEmployee(request);
|
||||||
|
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||||
|
|||||||
Reference in New Issue
Block a user