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:
@@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -30,22 +31,22 @@ namespace PluralKit.Core
|
||||
return conn.QuerySingleOrDefaultAsync<int>(query.ToString(), new {Id = id, PrivacyFilter = privacyFilter});
|
||||
}
|
||||
|
||||
public async Task<PKGroup> CreateGroup(IPKConnection conn, SystemId system, string name)
|
||||
public async Task<PKGroup> CreateGroup(IPKConnection conn, SystemId system, string name, IDbTransaction? transaction = null)
|
||||
{
|
||||
var group = await conn.QueryFirstAsync<PKGroup>(
|
||||
"insert into groups (hid, system, name) values (find_free_group_hid(), @System, @Name) returning *",
|
||||
new {System = system, Name = name});
|
||||
new {System = system, Name = name}, transaction);
|
||||
_logger.Information("Created group {GroupId} in system {SystemId}: {GroupName}", group.Id, system, name);
|
||||
return group;
|
||||
}
|
||||
|
||||
public Task<PKGroup> UpdateGroup(IPKConnection conn, GroupId id, GroupPatch patch)
|
||||
public Task<PKGroup> UpdateGroup(IPKConnection conn, GroupId id, GroupPatch patch, IDbTransaction? transaction = null)
|
||||
{
|
||||
_logger.Information("Updated {GroupId}: {@GroupPatch}", id, patch);
|
||||
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("groups", "id = @id"))
|
||||
.WithConstant("id", id)
|
||||
.Build("returning *");
|
||||
return conn.QueryFirstAsync<PKGroup>(query, pms);
|
||||
return conn.QueryFirstAsync<PKGroup>(query, pms, transaction);
|
||||
}
|
||||
|
||||
public Task DeleteGroup(IPKConnection conn, GroupId group)
|
||||
|
@@ -1,4 +1,5 @@
|
||||
#nullable enable
|
||||
using System.Data;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
@@ -19,23 +20,23 @@ namespace PluralKit.Core
|
||||
public Task<PKMember?> GetMemberByDisplayName(IPKConnection conn, SystemId system, string name) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(display_name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });
|
||||
|
||||
public async Task<PKMember> CreateMember(IPKConnection conn, SystemId id, string memberName)
|
||||
public async Task<PKMember> CreateMember(IPKConnection conn, SystemId id, string memberName, IDbTransaction? transaction = null)
|
||||
{
|
||||
var member = await conn.QueryFirstAsync<PKMember>(
|
||||
"insert into members (hid, system, name) values (find_free_member_hid(), @SystemId, @Name) returning *",
|
||||
new {SystemId = id, Name = memberName});
|
||||
new {SystemId = id, Name = memberName}, transaction);
|
||||
_logger.Information("Created {MemberId} in {SystemId}: {MemberName}",
|
||||
member.Id, id, memberName);
|
||||
return member;
|
||||
}
|
||||
|
||||
public Task<PKMember> UpdateMember(IPKConnection conn, MemberId id, MemberPatch patch)
|
||||
public Task<PKMember> UpdateMember(IPKConnection conn, MemberId id, MemberPatch patch, IDbTransaction? transaction = null)
|
||||
{
|
||||
_logger.Information("Updated {MemberId}: {@MemberPatch}", id, patch);
|
||||
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("members", "id = @id"))
|
||||
.WithConstant("id", id)
|
||||
.Build("returning *");
|
||||
return conn.QueryFirstAsync<PKMember>(query, pms);
|
||||
return conn.QueryFirstAsync<PKMember>(query, pms, transaction);
|
||||
}
|
||||
|
||||
public Task DeleteMember(IPKConnection conn, MemberId id)
|
||||
|
Reference in New Issue
Block a user