API patch improvements

- add PatchObject.CheckIsValid
- use transaction when creating member, as to not create a member if the
patch is invalid
- return edited system in `PATCH /s` endpoint
This commit is contained in:
spiral
2021-04-21 22:57:19 +01:00
parent a2d2036851
commit b34ed5c4c0
10 changed files with 103 additions and 22 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -140,13 +141,18 @@ namespace PluralKit.API
try
{
patch = JsonModelExt.ToSystemPatch(changes);
patch.CheckIsValid();
}
catch (JsonModelParseError e)
{
return BadRequest(e.Message);
}
catch (InvalidPatchException e)
{
return BadRequest($"Request field '{e.Message}' is invalid.");
}
await _repo.UpdateSystem(conn, system!.Id, patch);
system = await _repo.UpdateSystem(conn, system!.Id, patch);
return Ok(system.ToJson(User.ContextFor(system)));
}