27 lines
2.0 KiB
C#
27 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Data.SqlClient;
|
|
using PlotLine.Data;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
namespace PlotLine.Controllers;
|
|
[Authorize(Policy="AdminOnly"),Route("admin/ai"),ResponseCache(NoStore=true,Location=ResponseCacheLocation.None)]
|
|
public sealed class AiAdministrationController(AiAdministrationRepository repository,ICurrentUserService user):Controller
|
|
{
|
|
[HttpGet("")]
|
|
public async Task<IActionResult> Index()=>View(await repository.Read(user.UserId!.Value));
|
|
[HttpGet("usage/{id:guid}")]
|
|
public async Task<IActionResult> Usage(Guid id)=>View(await repository.Attempts(user.UserId!.Value,id));
|
|
[HttpPost("policy"),ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Save(AiPolicyChange change)
|
|
{
|
|
if(!ModelState.IsValid||change.Action is not("Operation" or "Tier" or "Override" or "RevokeOverride"))return BadRequest("Valid policy values and an administration reason are required.");
|
|
if(change.Action=="Operation"&&(change.OperationID is null||change.Enabled is null||change.CreditCost is null||change.MaxScope is null||change.MaxRequests is null||change.MaxTokens is null||change.MaxOutputTokens is null||change.MaxDurationSeconds is null))return BadRequest("Complete the operation policy.");
|
|
if(change.Action=="Tier"&&(change.SubscriptionLevelID is null||change.Enabled is null||change.MonthlyCredits is null))return BadRequest("Complete the tier policy.");
|
|
if(change.Action is "Override" or "RevokeOverride" &&change.UserID is null)return BadRequest("Choose an account.");
|
|
if(change.Action=="Override"&&(change.MonthlyCredits is null||change.ExpiryUtc is null))return BadRequest("An allowance and expiry are required.");
|
|
try{await repository.Save(user.UserId!.Value,change);TempData["AiMessage"]="AI policy updated.";}catch(SqlException exception)when(exception.Number is >=51020 and <=51026 or 547 or 515){TempData["AiError"]="AI policy could not be saved. Check the values; Draft Desk cannot enable discretionary AI.";}
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
}
|