[backend/api] Add instance rules endpoints
This commit is contained in:
parent
f35dc27f20
commit
76bb3f1c95
4 changed files with 138 additions and 0 deletions
|
@ -4,12 +4,15 @@ using Iceshrimp.Backend.Controllers.Shared.Attributes;
|
||||||
using Iceshrimp.Backend.Controllers.Web.Renderers;
|
using Iceshrimp.Backend.Controllers.Web.Renderers;
|
||||||
using Iceshrimp.Backend.Core.Configuration;
|
using Iceshrimp.Backend.Core.Configuration;
|
||||||
using Iceshrimp.Backend.Core.Database;
|
using Iceshrimp.Backend.Core.Database;
|
||||||
|
using Iceshrimp.Backend.Core.Database.Tables;
|
||||||
using Iceshrimp.Backend.Core.Extensions;
|
using Iceshrimp.Backend.Core.Extensions;
|
||||||
|
using Iceshrimp.Backend.Core.Helpers;
|
||||||
using Iceshrimp.Backend.Core.Middleware;
|
using Iceshrimp.Backend.Core.Middleware;
|
||||||
using Iceshrimp.Backend.Core.Services;
|
using Iceshrimp.Backend.Core.Services;
|
||||||
using Iceshrimp.Shared.Schemas.Web;
|
using Iceshrimp.Shared.Schemas.Web;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.RateLimiting;
|
using Microsoft.AspNetCore.RateLimiting;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace Iceshrimp.Backend.Controllers.Web;
|
namespace Iceshrimp.Backend.Controllers.Web;
|
||||||
|
@ -42,6 +45,116 @@ public class InstanceController(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("rules")]
|
||||||
|
[ProducesResults(HttpStatusCode.OK)]
|
||||||
|
public async Task<List<RuleResponse>> GetRules()
|
||||||
|
{
|
||||||
|
return await db.Rules
|
||||||
|
.OrderBy(p => p.Order)
|
||||||
|
.ThenBy(p => p.Id)
|
||||||
|
.Select(p => new RuleResponse { Id = p.Id, Text = p.Text, Description = p.Description })
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("rules")]
|
||||||
|
[Authenticate]
|
||||||
|
[Authorize("role:moderator")]
|
||||||
|
[ProducesResults(HttpStatusCode.OK)]
|
||||||
|
public async Task<RuleResponse> CreateRule(RuleCreateRequest request)
|
||||||
|
{
|
||||||
|
var count = await db.Rules.CountAsync();
|
||||||
|
|
||||||
|
var rule = new Rule
|
||||||
|
{
|
||||||
|
Id = IdHelpers.GenerateSnowflakeId(),
|
||||||
|
Order = count + 1,
|
||||||
|
Text = request.Text,
|
||||||
|
Description = request.Description
|
||||||
|
};
|
||||||
|
|
||||||
|
db.Add(rule);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
|
return new RuleResponse { Id = rule.Id, Text = rule.Text, Description = rule.Description };
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPatch("rules/{id}")]
|
||||||
|
[Authenticate]
|
||||||
|
[Authorize("role:moderator")]
|
||||||
|
[ProducesResults(HttpStatusCode.OK)]
|
||||||
|
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||||
|
public async Task<RuleResponse> UpdateRule(string id, RuleUpdateRequest request)
|
||||||
|
{
|
||||||
|
var rule = await db.Rules.FirstOrDefaultAsync(p => p.Id == id)
|
||||||
|
?? throw GracefulException.RecordNotFound();
|
||||||
|
|
||||||
|
var count = await db.Rules.CountAsync();
|
||||||
|
// order is defined here because I don't know why but request.Order is still nullable even if the if statement checks it isn't null
|
||||||
|
var order = request.Order ?? 0;
|
||||||
|
if (order > 0 && order != rule.Order && count != 1)
|
||||||
|
{
|
||||||
|
request.Order = Math.Min(order, count);
|
||||||
|
|
||||||
|
if (order > rule.Order)
|
||||||
|
{
|
||||||
|
var rules = await db.Rules
|
||||||
|
.Where(p => rule.Order < p.Order && p.Order <= order)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
foreach (var r in rules)
|
||||||
|
r.Order -= 1;
|
||||||
|
|
||||||
|
db.UpdateRange(rules);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var rules = await db.Rules
|
||||||
|
.Where(p => order <= p.Order && p.Order < rule.Order)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
foreach (var r in rules)
|
||||||
|
r.Order += 1;
|
||||||
|
|
||||||
|
db.UpdateRange(rules);
|
||||||
|
}
|
||||||
|
|
||||||
|
rule.Order = order;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.Text != null)
|
||||||
|
rule.Text = request.Text.Trim();
|
||||||
|
|
||||||
|
if (request.Description != null)
|
||||||
|
rule.Description = string.IsNullOrWhiteSpace(request.Description) ? null : request.Description.Trim();
|
||||||
|
|
||||||
|
db.Update(rule);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
|
return new RuleResponse { Id = rule.Id, Text = rule.Text, Description = rule.Description };
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("rules/{id}")]
|
||||||
|
[Authenticate]
|
||||||
|
[Authorize("role:moderator")]
|
||||||
|
[ProducesResults(HttpStatusCode.OK)]
|
||||||
|
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||||
|
public async Task DeleteRule(string id)
|
||||||
|
{
|
||||||
|
var rule = await db.Rules.FirstOrDefaultAsync(p => p.Id == id)
|
||||||
|
?? throw GracefulException.RecordNotFound();
|
||||||
|
|
||||||
|
var rules = await db.Rules
|
||||||
|
.Where(p => p.Order > rule.Order)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
db.Remove(rule);
|
||||||
|
|
||||||
|
foreach (var r in rules)
|
||||||
|
r.Order -= 1;
|
||||||
|
db.UpdateRange(rules);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
[HttpGet("staff")]
|
[HttpGet("staff")]
|
||||||
[Authenticate]
|
[Authenticate]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
|
|
7
Iceshrimp.Shared/Schemas/Web/RuleCreateRequest.cs
Normal file
7
Iceshrimp.Shared/Schemas/Web/RuleCreateRequest.cs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
namespace Iceshrimp.Shared.Schemas.Web;
|
||||||
|
|
||||||
|
public class RuleCreateRequest
|
||||||
|
{
|
||||||
|
public required string Text { get; set; }
|
||||||
|
public required string? Description { get; set; }
|
||||||
|
}
|
10
Iceshrimp.Shared/Schemas/Web/RuleResponse.cs
Normal file
10
Iceshrimp.Shared/Schemas/Web/RuleResponse.cs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
using Iceshrimp.Shared.Helpers;
|
||||||
|
|
||||||
|
namespace Iceshrimp.Shared.Schemas.Web;
|
||||||
|
|
||||||
|
public class RuleResponse : IIdentifiable
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string Text { get; set; }
|
||||||
|
public required string? Description { get; set; }
|
||||||
|
}
|
8
Iceshrimp.Shared/Schemas/Web/RuleUpdateRequest.cs
Normal file
8
Iceshrimp.Shared/Schemas/Web/RuleUpdateRequest.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace Iceshrimp.Shared.Schemas.Web;
|
||||||
|
|
||||||
|
public class RuleUpdateRequest
|
||||||
|
{
|
||||||
|
public int? Order { get; set; }
|
||||||
|
public string? Text { get; set; }
|
||||||
|
public string? Description { get; set; }
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue