mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2026-09-16 19:54:29 +00:00
Optimize USB product entity so calls to update API get reduced.
This commit is contained in:
3284
Aaru.Server.Database/Migrations/20251229121119_AddUsbVendorIdToProductTable.Designer.cs
generated
Normal file
3284
Aaru.Server.Database/Migrations/20251229121119_AddUsbVendorIdToProductTable.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,34 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Aaru.Server.Database.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddUsbVendorIdToProductTable : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<ushort>(
|
||||
name: "UsbVendorId",
|
||||
table: "UsbProducts",
|
||||
type: "smallint unsigned",
|
||||
nullable: false,
|
||||
defaultValue: (ushort)0);
|
||||
|
||||
migrationBuilder.Sql(
|
||||
"UPDATE UsbProducts p " +
|
||||
"INNER JOIN UsbVendors v ON p.VendorId = v.Id " +
|
||||
"SET p.UsbVendorId = v.VendorId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "UsbVendorId",
|
||||
table: "UsbProducts");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2602,6 +2602,9 @@ namespace Aaru.Server.Database.Migrations
|
||||
b.Property<ushort>("ProductId")
|
||||
.HasColumnType("smallint unsigned");
|
||||
|
||||
b.Property<ushort>("UsbVendorId")
|
||||
.HasColumnType("smallint unsigned");
|
||||
|
||||
b.Property<int>("VendorId")
|
||||
.HasColumnType("int");
|
||||
|
||||
|
||||
@@ -40,10 +40,11 @@ public class UsbProduct : BaseModel<int>
|
||||
|
||||
public UsbProduct(UsbVendor vendor, ushort id, string product)
|
||||
{
|
||||
ProductId = id;
|
||||
Product = product;
|
||||
AddedWhen = ModifiedWhen = DateTime.UtcNow;
|
||||
Vendor = vendor;
|
||||
ProductId = id;
|
||||
Product = product;
|
||||
AddedWhen = ModifiedWhen = DateTime.UtcNow;
|
||||
Vendor = vendor;
|
||||
UsbVendorId = vendor.VendorId;
|
||||
}
|
||||
|
||||
public ushort ProductId { get; set; }
|
||||
@@ -51,6 +52,7 @@ public class UsbProduct : BaseModel<int>
|
||||
public DateTime AddedWhen { get; set; }
|
||||
public DateTime ModifiedWhen { get; set; }
|
||||
public int VendorId { get; set; }
|
||||
public ushort UsbVendorId { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual UsbVendor Vendor { get; set; }
|
||||
|
||||
@@ -32,7 +32,6 @@ 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;
|
||||
|
||||
@@ -63,15 +62,14 @@ public sealed class UpdateController(DbContext ctx) : ControllerBase
|
||||
|
||||
sync.UsbProducts = [];
|
||||
|
||||
foreach(UsbProduct product in ctx.UsbProducts.Include(static p => p.Vendor)
|
||||
.Where(p => p.ModifiedWhen > lastSync))
|
||||
foreach(UsbProduct product in ctx.UsbProducts.Where(p => p.ModifiedWhen > lastSync))
|
||||
{
|
||||
sync.UsbProducts.Add(new UsbProductDto
|
||||
{
|
||||
Id = product.Id,
|
||||
Product = product.Product,
|
||||
ProductId = product.ProductId,
|
||||
VendorId = product.Vendor.VendorId
|
||||
VendorId = product.UsbVendorId
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user