the api to commit web interface changes to a channel works

This commit is contained in:
Adam R Grey 2024-06-23 20:31:09 -04:00
parent 942b11fcce
commit 0ac28c35fb
5 changed files with 73 additions and 33 deletions

View File

@ -1,13 +1,18 @@
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
using vassago.Models;
#pragma warning disable CA2254
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IHostedService, vassago.ConsoleService>();
builder.Services.AddDbContext<ChattingContext>();
builder.Services.AddControllers().AddNewtonsoftJson();
builder.Services.AddProblemDetails();
builder.Services.Configure<RazorViewEngineOptions>(o => {
o.ViewLocationFormats.Clear();
o.ViewLocationFormats.Add("/WebInterface/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
@ -37,4 +42,12 @@ app.UseSwaggerUI(c =>
c.SwaggerEndpoint("/swagger/v1/swagger.json", "api");
});
app.UseExceptionHandler();
app.UseStatusCodePages();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run();

View File

@ -25,11 +25,20 @@ public class ChannelsController : ControllerBase
return _db.Find<Channel>(id);
}
[HttpPost]
[HttpPatch]
[Produces("application/json")]
public Channel Post([FromBody] Channel channel)
public IActionResult Patch([FromBody] Channel channel)
{
// Write logic to insert employee data
return new Channel();
var fromDb = _db.Channels.Find(channel.Id);
if (fromDb == null)
{
_logger.LogError($"attempt to update channel {channel.Id}, not found"); //ca2254 is moronic. maybe if it wasn't filed under "code quality" and instead was filed under "you didn't include a workaround for the weaknesses of other external junk" i'd be kinder to it ;)
return NotFound();
}
//settable values: lewdness filter level, meanness filter level. maybe i could decorate them...
fromDb.LewdnessFilterLevel = channel.LewdnessFilterLevel;
fromDb.MeannessFilterLevel = channel.MeannessFilterLevel;
_db.SaveChanges();
return Ok(fromDb);
}
}

View File

@ -17,11 +17,13 @@
<tr>
<th scope="row">Lewdness Filter Level</th>
<td>
<select name="LewdnessFilterLevel" id="LewdnessFilterLevel" onchange="postmodelupdate(jsonifyChannel())">
<select name="LewdnessFilterLevel" id="LewdnessFilterLevel" onchange="patchModel(jsonifyChannel())">
<!option value="" @(Model.LewdnessFilterLevel == null ? "selected" : "")>inhereted</!option>
@foreach (Enumerations.LewdnessFilterLevel enumVal in Enum.GetValues(typeof(Enumerations.LewdnessFilterLevel)))
@foreach (Enumerations.LewdnessFilterLevel enumVal in
Enum.GetValues(typeof(Enumerations.LewdnessFilterLevel)))
{
<!option value="@((int)enumVal)" @(Model.LewdnessFilterLevel == enumVal ? "selected" : "")>@(Enumerations.GetDescription<Enumerations.LewdnessFilterLevel>(enumVal))</!option>
<!option value="@((int)enumVal)" @(Model.LewdnessFilterLevel == enumVal ? "selected" : "")>
@(Enumerations.GetDescription<Enumerations.LewdnessFilterLevel>(enumVal))</!option>
}
</select>
</td>
@ -44,7 +46,17 @@
</tr>
<tr>
<th scope="row">Meanness Filter Level</th>
<td>@(Model.MeannessFilterLevel != null ? Enumerations.GetDescription<Enumerations.MeannessFilterLevel>(Model.MeannessFilterLevel.GetValueOrDefault()) : "inherited")</td>
<td>
<select name="MeannessFilterLevel" id="MeannessFilterLevel" onchange="patchModel(jsonifyChannel())">
<!option value="" @(Model.MeannessFilterLevel == null ? "selected" : "")>⤵ inhereted</!option>
@foreach (Enumerations.MeannessFilterLevel enumVal in
Enum.GetValues(typeof(Enumerations.MeannessFilterLevel)))
{
<!option value="@((int)enumVal)" @(Model.MeannessFilterLevel == enumVal ? "selected" : "")>
@(Enumerations.GetDescription<Enumerations.MeannessFilterLevel>(enumVal))</!option>
}
</select>
</td>
</tr>
<tr>
<th scope="row">Messages (count)</th>
@ -79,10 +91,15 @@
});
}
const channelOnLoad = @Html.Raw(modelAsString);
function jsonifyChannel()
{
var channelNow = channelOnLoad;
function jsonifyChannel() {
var channelNow = structuredClone(channelOnLoad);
channelNow.SubChannels = null;
channelNow.ParentChannel = null;
channelNow.Messages = null;
channelNow.Users = null;
channelNow.LewdnessFilterLevel = document.querySelector("#LewdnessFilterLevel").value;
channelNow.MeannessFilterLevel = document.querySelector("#MeannessFilterLevel").value;
console.log(channelNow);
return channelNow;
}

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="bootstrap" Version="5.3.3" />
<PackageReference Include="discord.net" Version="3.10.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>

View File

@ -7,7 +7,7 @@ function testfunct(caller){
console.log("[gibberish]");
console.log(caller);
}
function postmodelupdate(model)
function patchModel(model)
{
//structure the model your (dang) self into a nice object
console.log(model);
@ -28,7 +28,7 @@ function postmodelupdate(model)
console.log("dexter impression: I am now ready to post the following content:");
console.log(JSON.stringify(model));
fetch(apiUrl, {
method: 'POST',
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},