From aac6b999c74d89edf6fae6365a26e1355be3f970 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 11 May 2024 01:32:52 +0100 Subject: [PATCH] Add update controller. --- Aaru.Server.New/Aaru.Server.New.csproj | 1 + .../Controllers/UpdateController.cs | 106 ++++++++++++++++++ Aaru.Server.New/Program.cs | 5 + 3 files changed, 112 insertions(+) create mode 100644 Aaru.Server.New/Controllers/UpdateController.cs diff --git a/Aaru.Server.New/Aaru.Server.New.csproj b/Aaru.Server.New/Aaru.Server.New.csproj index c3877dc4..09bf102e 100644 --- a/Aaru.Server.New/Aaru.Server.New.csproj +++ b/Aaru.Server.New/Aaru.Server.New.csproj @@ -10,6 +10,7 @@ + diff --git a/Aaru.Server.New/Controllers/UpdateController.cs b/Aaru.Server.New/Controllers/UpdateController.cs new file mode 100644 index 00000000..989fe690 --- /dev/null +++ b/Aaru.Server.New/Controllers/UpdateController.cs @@ -0,0 +1,106 @@ +using System.Net; +using Aaru.CommonTypes.Metadata; +using Aaru.Dto; +using Aaru.Helpers; +using Aaru.Server.Database.Models; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; +using DbContext = Aaru.Server.Database.DbContext; + +namespace Aaru.Server.New.Controllers; + +[Controller] +public sealed class UpdateController(DbContext ctx) : ControllerBase +{ + /// Receives a report from Aaru.Core, verifies it's in the correct format and stores it on the server + /// HTTP response + [Route("api/update")] + [HttpGet] + public ActionResult Update(long timestamp) + { + var sync = new SyncDto(); + DateTime lastSync = DateHandlers.UnixToDateTime(timestamp); + + sync.UsbVendors = []; + + foreach(UsbVendor vendor in ctx.UsbVendors.Where(v => v.ModifiedWhen > lastSync)) + { + sync.UsbVendors.Add(new UsbVendorDto + { + VendorId = vendor.VendorId, + Vendor = vendor.Vendor + }); + } + + sync.UsbProducts = []; + + foreach(UsbProduct product in ctx.UsbProducts.Include(static p => p.Vendor) + .Where(p => p.ModifiedWhen > lastSync)) + { + sync.UsbProducts.Add(new UsbProductDto + { + Id = product.Id, + Product = product.Product, + ProductId = product.ProductId, + VendorId = product.Vendor.VendorId + }); + } + + sync.Offsets = []; + + foreach(CompactDiscOffset offset in ctx.CdOffsets.Where(o => o.ModifiedWhen > lastSync)) + sync.Offsets.Add(new CdOffsetDto(offset, offset.Id)); + + sync.Devices = []; + + foreach(Device device in ctx.Devices.Where(d => d.ModifiedWhen > lastSync).ToList()) + { + sync.Devices.Add(new + DeviceDto(JsonConvert + .DeserializeObject(JsonConvert.SerializeObject(device, + Formatting.None, + new JsonSerializerSettings + { + ReferenceLoopHandling = + ReferenceLoopHandling.Ignore + })), + device.Id, + device.OptimalMultipleSectorsRead, + device.CanReadGdRomUsingSwapDisc)); + } + + sync.NesHeaders = []; + + foreach(NesHeaderInfo header in ctx.NesHeaders.Where(v => v.ModifiedWhen > lastSync)) + { + sync.NesHeaders.Add(new NesHeaderDto + { + Id = header.Id, + BatteryPresent = header.BatteryPresent, + ConsoleType = header.ConsoleType, + DefaultExpansionDevice = header.DefaultExpansionDevice, + ExtendedConsoleType = header.ExtendedConsoleType, + FourScreenMode = header.FourScreenMode, + Mapper = header.Mapper, + NametableMirroring = header.NametableMirroring, + Sha256 = header.Sha256, + Submapper = header.Submapper, + TimingMode = header.TimingMode, + VsHardwareType = header.VsHardwareType, + VsPpuType = header.VsPpuType + }); + } + + var js = JsonSerializer.Create(); + var sw = new StringWriter(); + js.Serialize(sw, sync); + + return new ContentResult + { + StatusCode = (int)HttpStatusCode.OK, + Content = sw.ToString(), + ContentType = "application/json" + }; + } +} \ No newline at end of file diff --git a/Aaru.Server.New/Program.cs b/Aaru.Server.New/Program.cs index a187f06e..6e84f24a 100644 --- a/Aaru.Server.New/Program.cs +++ b/Aaru.Server.New/Program.cs @@ -122,6 +122,9 @@ builder.Services.AddBlazorise(static options => { options.Immediate = true; }) .AddBootstrap5Providers() .AddFontAwesomeIcons(); +// Add services to the container. +builder.Services.AddControllers(); + WebApplication app = builder.Build(); // Configure the HTTP request pipeline. @@ -137,6 +140,7 @@ else app.UseHttpsRedirection(); +app.UseAuthorization(); app.UseStaticFiles(); app.UseAntiforgery(); @@ -144,6 +148,7 @@ app.MapRazorComponents().AddInteractiveServerRenderMode(); // Add additional endpoints required by the Identity /Account Razor components. app.MapAdditionalIdentityEndpoints(); +app.MapControllers(); using(IServiceScope scope = app.Services.CreateScope()) {