Skip to content

Commit

Permalink
Fix delete body + add openapi docs
Browse files Browse the repository at this point in the history
  • Loading branch information
PainOchoco committed Sep 27, 2023
1 parent c83b622 commit 9a0a783
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<TypedNameUuidModel> donorsArray = getDonorsArray();
TypedNameUuidModel donorToDelete = new Gson().fromJson(ctx.body(), TypedNameUuidModel.class);
List<TypedNameUuidModel> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<TypedNameUuidModel> staffArray = getStaffArray();
TypedNameUuidModel staffToDelete = new Gson().fromJson(ctx.body(), TypedNameUuidModel.class);
List<TypedNameUuidModel> 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);
Expand Down

0 comments on commit 9a0a783

Please sign in to comment.