diff --git a/src/main/java/net/laboulangerie/api/controllers/DonorsController.java b/src/main/java/net/laboulangerie/api/controllers/DonorsController.java index bde7182..5d0ea73 100644 --- a/src/main/java/net/laboulangerie/api/controllers/DonorsController.java +++ b/src/main/java/net/laboulangerie/api/controllers/DonorsController.java @@ -18,6 +18,7 @@ import io.javalin.openapi.OpenApi; import io.javalin.openapi.OpenApiResponse; import io.javalin.openapi.OpenApiContent; +import io.javalin.openapi.OpenApiRequestBody; import net.laboulangerie.api.LaBoulangerieAPI; import net.laboulangerie.api.database.GsonFiles; import net.laboulangerie.api.models.TypedNameUuidModel; @@ -47,7 +48,8 @@ public static void getDonors(Context ctx) { } @OpenApi(description = "Add donor", operationId = "addDonor", path = "/donors", methods = HttpMethod.POST, tags = { - "Donors" }) + "Donors" }, requestBody = @OpenApiRequestBody(content = { + @OpenApiContent(from = TypedNameUuidModel.class) })) public static void addDonor(Context ctx) { DecodedJWT decodedJWT = LaBoulangerieAPI.JWT_MANAGER.getJwtFromContext(ctx); if (decodedJWT == null) @@ -85,19 +87,22 @@ public int compare(TypedNameUuidModel o1, TypedNameUuidModel o2) { } @OpenApi(description = "Delete donor", operationId = "deleteDonor", path = "/donors", methods = HttpMethod.DELETE, tags = { - "Donors", }) + "Donors" }, requestBody = @OpenApiRequestBody(content = { + @OpenApiContent(from = TypedNameUuidModel.class) })) public static void deleteDonor(Context ctx) { DecodedJWT decodedJWT = LaBoulangerieAPI.JWT_MANAGER.getJwtFromContext(ctx); if (decodedJWT == null) return; - String donorUuid = ctx.formParam("uuid"); - List donorsArray = getDonorsArray(); + TypedNameUuidModel donorToDelete = new Gson().fromJson(ctx.body(), TypedNameUuidModel.class); + List donorArray = new ArrayList<>(getDonorsArray()); - donorsArray.removeIf(d -> d.getUuid().toString().equals(donorUuid)); + donorArray.removeIf( + d -> (d.getUuid().equals(donorToDelete.getUuid())) + || (d.getName().equals(donorToDelete.getName()))); try { - GsonFiles.writeArray(donorsFile, donorsArray); + GsonFiles.writeArray(donorsFile, donorArray); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/net/laboulangerie/api/controllers/StaffController.java b/src/main/java/net/laboulangerie/api/controllers/StaffController.java index 7f35e11..0085e2a 100644 --- a/src/main/java/net/laboulangerie/api/controllers/StaffController.java +++ b/src/main/java/net/laboulangerie/api/controllers/StaffController.java @@ -17,6 +17,7 @@ import io.javalin.openapi.OpenApi; import io.javalin.openapi.OpenApiResponse; import io.javalin.openapi.OpenApiContent; +import io.javalin.openapi.OpenApiRequestBody; import net.laboulangerie.api.LaBoulangerieAPI; import net.laboulangerie.api.database.GsonFiles; import net.laboulangerie.api.models.TypedNameUuidModel; @@ -46,7 +47,8 @@ public static void getStaff(Context ctx) { } @OpenApi(description = "Add staff", operationId = "addStaff", path = "/staff", methods = HttpMethod.POST, tags = { - "Staff" }) + "Staff" }, requestBody = @OpenApiRequestBody(content = { + @OpenApiContent(from = TypedNameUuidModel.class) })) public static void addStaff(Context ctx) { DecodedJWT decodedJWT = LaBoulangerieAPI.JWT_MANAGER.getJwtFromContext(ctx); if (decodedJWT == null) @@ -83,16 +85,19 @@ public int compare(TypedNameUuidModel o1, TypedNameUuidModel o2) { } @OpenApi(description = "Delete staff", operationId = "deleteStaff", path = "/staff", methods = HttpMethod.DELETE, tags = { - "Staff", }) + "Staff" }, requestBody = @OpenApiRequestBody(content = { + @OpenApiContent(from = TypedNameUuidModel.class) })) public static void deleteStaff(Context ctx) { DecodedJWT decodedJWT = LaBoulangerieAPI.JWT_MANAGER.getJwtFromContext(ctx); if (decodedJWT == null) return; - String staffUuid = ctx.formParam("uuid"); - List staffArray = getStaffArray(); + TypedNameUuidModel staffToDelete = new Gson().fromJson(ctx.body(), TypedNameUuidModel.class); + List staffArray = new ArrayList<>(getStaffArray()); - staffArray.removeIf(d -> d.getUuid().toString() == staffUuid); + staffArray.removeIf( + d -> (d.getUuid().equals(staffToDelete.getUuid())) + || (d.getName().equals(staffToDelete.getName()))); try { GsonFiles.writeArray(staffFile, staffArray);