feat(apiv2): post/patch endpoints

This commit is contained in:
spiral 2021-10-14 09:35:20 -04:00
parent fd49e7e4ea
commit e367ed6808
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
4 changed files with 83 additions and 29 deletions

View File

@ -37,12 +37,26 @@ namespace PluralKit.API
} }
[HttpPost("groups")] [HttpPost("groups")]
public async Task<IActionResult> GroupCreate(string group_id) public async Task<IActionResult> GroupCreate([FromBody] JObject data)
{ {
return new ObjectResult("Unimplemented") var system = await ResolveSystem("@me");
{
StatusCode = 501 var patch = GroupPatch.FromJson(data);
}; patch.AssertIsValid();
if (!patch.Name.IsPresent)
patch.Errors.Add(new ValidationError("name", $"Key 'name' is required when creating new group."));
if (patch.Errors.Count > 0)
throw new ModelParseError(patch.Errors);
using var conn = await _db.Obtain();
using var tx = await conn.BeginTransactionAsync();
var newGroup = await _repo.CreateGroup(system.Id, patch.Name.Value, conn);
newGroup = await _repo.UpdateGroup(newGroup.Id, patch, conn);
await tx.CommitAsync();
return Ok(newGroup.ToJson(LookupContext.ByOwner));
} }
[HttpGet("groups/{groupRef}")] [HttpGet("groups/{groupRef}")]
@ -57,13 +71,24 @@ namespace PluralKit.API
return Ok(group.ToJson(this.ContextFor(group), systemStr: system.Hid)); return Ok(group.ToJson(this.ContextFor(group), systemStr: system.Hid));
} }
[HttpPatch("groups/{group_id}")] [HttpPatch("groups/{groupRef}")]
public async Task<IActionResult> GroupPatch(string group_id) public async Task<IActionResult> DoGroupPatch(string groupRef, [FromBody] JObject data)
{ {
return new ObjectResult("Unimplemented") var system = await ResolveSystem("@me");
{ var group = await ResolveGroup(groupRef);
StatusCode = 501 if (group == null)
}; throw Errors.GroupNotFound;
if (group.System != system.Id)
throw Errors.NotOwnGroupError;
var patch = GroupPatch.FromJson(data);
patch.AssertIsValid();
if (patch.Errors.Count > 0)
throw new ModelParseError(patch.Errors);
var newGroup = await _repo.UpdateGroup(group.Id, patch);
return Ok(newGroup.ToJson(LookupContext.ByOwner));
} }
[HttpDelete("groups/{groupRef}")] [HttpDelete("groups/{groupRef}")]

View File

@ -40,10 +40,24 @@ namespace PluralKit.API
[HttpPost("members")] [HttpPost("members")]
public async Task<IActionResult> MemberCreate([FromBody] JObject data) public async Task<IActionResult> MemberCreate([FromBody] JObject data)
{ {
return new ObjectResult("Unimplemented") var patch = MemberPatch.FromJSON(data);
{ patch.AssertIsValid();
StatusCode = 501 if (!patch.Name.IsPresent)
}; patch.Errors.Add(new ValidationError("name", $"Key 'name' is required when creating new member."));
if (patch.Errors.Count > 0)
throw new ModelParseError(patch.Errors);
var system = await ResolveSystem("@me");
using var conn = await _db.Obtain();
using var tx = await conn.BeginTransactionAsync();
var newMember = await _repo.CreateMember(system.Id, patch.Name.Value, conn);
newMember = await _repo.UpdateMember(newMember.Id, patch, conn);
await tx.CommitAsync();
return Ok(newMember.ToJson(LookupContext.ByOwner, v: APIVersion.V2));
} }
[HttpGet("members/{memberRef}")] [HttpGet("members/{memberRef}")]
@ -58,13 +72,24 @@ namespace PluralKit.API
return Ok(member.ToJson(this.ContextFor(member), systemStr: system.Hid, v: APIVersion.V2)); return Ok(member.ToJson(this.ContextFor(member), systemStr: system.Hid, v: APIVersion.V2));
} }
[HttpPatch("members/{member}")] [HttpPatch("members/{memberRef}")]
public async Task<IActionResult> MemberPatch(string member, [FromBody] JObject data) public async Task<IActionResult> DoMemberPatch(string memberRef, [FromBody] JObject data)
{ {
return new ObjectResult("Unimplemented") var system = await ResolveSystem("@me");
{ var member = await ResolveMember(memberRef);
StatusCode = 501 if (member == null)
}; throw Errors.MemberNotFound;
if (member.System != system.Id)
throw Errors.NotOwnMemberError;
var patch = MemberPatch.FromJSON(data, APIVersion.V2);
patch.AssertIsValid();
if (patch.Errors.Count > 0)
throw new ModelParseError(patch.Errors);
var newMember = await _repo.UpdateMember(member.Id, patch);
return Ok(newMember.ToJson(LookupContext.ByOwner, v: APIVersion.V2));
} }
[HttpDelete("members/{memberRef}")] [HttpDelete("members/{memberRef}")]

View File

@ -165,8 +165,7 @@ namespace PluralKit.API
var valueStr = data.Value<string>("timestamp").NullIfEmpty(); var valueStr = data.Value<string>("timestamp").NullIfEmpty();
if (valueStr == null) if (valueStr == null)
// todo throw new ModelParseError(new List<ValidationError>() { new ValidationError("timestamp", $"Key 'timestamp' is required.") });
throw Errors.GenericBadRequest;
var value = Instant.FromDateTimeOffset(DateTime.Parse(valueStr).ToUniversalTime()); var value = Instant.FromDateTimeOffset(DateTime.Parse(valueStr).ToUniversalTime());

View File

@ -24,13 +24,18 @@ namespace PluralKit.API
else return Ok(system.ToJson(this.ContextFor(system), v: APIVersion.V2)); else return Ok(system.ToJson(this.ContextFor(system), v: APIVersion.V2));
} }
[HttpPatch("{system}")] [HttpPatch]
public async Task<IActionResult> SystemPatch(string system, [FromBody] JObject data) public async Task<IActionResult> DoSystemPatch([FromBody] JObject data)
{ {
return new ObjectResult("Unimplemented") var system = await ResolveSystem("@me");
{ var patch = SystemPatch.FromJSON(data, APIVersion.V2);
StatusCode = 501
}; patch.AssertIsValid();
if (patch.Errors.Count > 0)
throw new ModelParseError(patch.Errors);
var newSystem = await _repo.UpdateSystem(system.Id, patch);
return Ok(newSystem.ToJson(LookupContext.ByOwner, v: APIVersion.V2));
} }
} }
} }