Skip to content

Commit

Permalink
Update with idempotency
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnwildermuth committed Jan 26, 2024
1 parent 9e636a2 commit e6d383c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 43 deletions.
9 changes: 3 additions & 6 deletions src/Apis/CustomerProjectsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Register(IEndpointRouteBuilder builder)
new SwaggerOperationAttribute(
summary: "Customer's Projects",
description: "Projects that are for a particular customer."));


group.MapGet("", GetAll);
group.MapGet("{id:int}", GetOne).WithName("GetOneCustomerProject");
Expand Down Expand Up @@ -90,12 +90,9 @@ public static async Task<IResult> Update(BillingContext ctx, int customerId, int

model.Adapt(old);

if (await ctx.SaveAllAsync())
{
return Results.Ok(old);
}
await ctx.SaveAllAsync();

return Results.BadRequest("Failed to save new Project.");
return Results.Ok(old);
}
catch (Exception ex)
{
Expand Down
7 changes: 2 additions & 5 deletions src/Apis/CustomersApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,9 @@ public static async Task<IResult> Update(BillingContext ctx, int id, Customer mo

model.Adapt(old);

if (await ctx.SaveAllAsync())
{
return Results.Ok(old);
}
await ctx.SaveAllAsync();

return Results.BadRequest("Failed to save new customer.");
return Results.Ok(old);
}
catch (Exception ex)
{
Expand Down
8 changes: 2 additions & 6 deletions src/Apis/EmployeesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,8 @@ public static async Task<IResult> Update(BillingContext ctx, int id, Employee mo

model.Adapt(old);

if (await ctx.SaveAllAsync())
{
return Results.Ok(old);
}

return Results.BadRequest("Failed to save new employee.");
await ctx.SaveAllAsync();
return Results.Ok(old);
}
catch (Exception ex)
{
Expand Down
17 changes: 7 additions & 10 deletions src/Apis/ProjectTicketsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,15 @@ public static async Task<IResult> Update(BillingContext ctx, int projectId, int

model.Adapt(old);

if (await ctx.SaveAllAsync())
{
var result = await ctx.Tickets
.Include(t => t.Project)
.Include(t => t.Employee)
.Where(t => t.Id == model.Id && t.ProjectId == projectId)
.FirstAsync();
await ctx.SaveAllAsync();

return Results.Ok(result);
}
var result = await ctx.Tickets
.Include(t => t.Project)
.Include(t => t.Employee)
.Where(t => t.Id == model.Id && t.ProjectId == projectId)
.FirstAsync();

return Results.BadRequest("Failed to save new Ticket.");
return Results.Ok(result);
}
catch (Exception ex)
{
Expand Down
8 changes: 2 additions & 6 deletions src/Apis/ProjectsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,8 @@ public static async Task<IResult> Update(BillingContext ctx, int id, Project mod

model.Adapt(old);

if (await ctx.SaveAllAsync())
{
return Results.Ok(old);
}

return Results.BadRequest("Failed to save new Project.");
await ctx.SaveAllAsync();
return Results.Ok(old);
}
catch (Exception ex)
{
Expand Down
15 changes: 5 additions & 10 deletions src/Apis/TicketsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,12 @@ public static async Task<IResult> Update(BillingContext ctx, int id, Ticket mode

model.Adapt(old);

if (await ctx.SaveAllAsync())
{
var result = await ctx.Tickets
.Include(t => t.Project)
.Include(t => t.Employee)
.Where(t => t.Id == model.Id).FirstAsync();

return Results.Ok(result);
}
var result = await ctx.Tickets
.Include(t => t.Project)
.Include(t => t.Employee)
.Where(t => t.Id == model.Id).FirstAsync();

return Results.BadRequest("Failed to save new Ticket.");
return Results.Ok(result);
}
catch (Exception ex)
{
Expand Down

0 comments on commit e6d383c

Please sign in to comment.