use employee api

This commit is contained in:
2026-03-14 20:22:14 -06:00
parent 42f8f568ae
commit 4b024984cd
5 changed files with 137 additions and 19 deletions

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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);
}
}

View File

@@ -16,8 +16,8 @@ import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.example.petshopdesktop.api.dto.user.UserResponse;
import org.example.petshopdesktop.api.endpoints.UserApi;
import org.example.petshopdesktop.api.dto.employee.EmployeeResponse;
import org.example.petshopdesktop.api.endpoints.EmployeeApi;
import org.example.petshopdesktop.auth.UserSession;
import org.example.petshopdesktop.models.StaffAccount;
import org.example.petshopdesktop.util.ActivityLogger;
@@ -116,8 +116,8 @@ public class StaffAccountsController {
new Thread(() -> {
try {
List<UserResponse> users = UserApi.getInstance().listUsers(null);
List<StaffAccount> accounts = users.stream()
List<EmployeeResponse> employees = EmployeeApi.getInstance().listEmployees(null);
List<StaffAccount> accounts = employees.stream()
.map(this::mapToStaffAccount)
.collect(Collectors.toList());
@@ -135,21 +135,22 @@ public class StaffAccountsController {
}).start();
}
private StaffAccount mapToStaffAccount(UserResponse user) {
long id = user.getId() != null ? user.getId() : 0L;
String username = user.getUsername();
String fullName = user.getFullName() != null ? user.getFullName() : "";
private StaffAccount mapToStaffAccount(EmployeeResponse employee) {
long userId = employee.getUserId() != null ? employee.getUserId() : 0L;
long employeeId = employee.getEmployeeId() != null ? employee.getEmployeeId() : 0L;
String username = employee.getUsername();
String fullName = employee.getFullName() != null ? employee.getFullName() : "";
String[] names = splitFullName(fullName);
String firstName = names[0];
String lastName = names[1];
String email = user.getEmail() != null ? user.getEmail() : "";
String phone = user.getPhone() != null ? user.getPhone() : "";
boolean active = user.getActive() != null ? user.getActive() : false;
Timestamp createdAt = user.getCreatedAt() != null
? Timestamp.from(user.getCreatedAt().atZone(ZoneId.systemDefault()).toInstant())
String email = employee.getEmail() != null ? employee.getEmail() : "";
String phone = employee.getPhone() != null ? employee.getPhone() : "";
boolean active = employee.getActive() != null ? employee.getActive() : false;
Timestamp createdAt = employee.getCreatedAt() != null
? Timestamp.from(employee.getCreatedAt().atZone(ZoneId.systemDefault()).toInstant())
: 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) {

View File

@@ -9,8 +9,8 @@ import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.example.petshopdesktop.api.dto.user.UserRequest;
import org.example.petshopdesktop.api.endpoints.UserApi;
import org.example.petshopdesktop.api.dto.employee.EmployeeRequest;
import org.example.petshopdesktop.api.endpoints.EmployeeApi;
import org.example.petshopdesktop.Validator;
import org.example.petshopdesktop.util.ActivityLogger;
@@ -89,16 +89,17 @@ public class StaffRegisterDialogController {
new Thread(() -> {
try {
UserRequest request = new UserRequest();
EmployeeRequest request = new EmployeeRequest();
request.setUsername(username);
request.setPassword(password);
request.setFullName(firstName + " " + lastName);
request.setFirstName(firstName);
request.setLastName(lastName);
request.setEmail(email);
request.setPhone(phone);
request.setRole("STAFF");
request.setActive(true);
UserApi.getInstance().createUser(request);
EmployeeApi.getInstance().createEmployee(request);
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);