Skip to content

Commit

Permalink
[Back | CURD] ProductPatchVariantName & ProductPatchCombo (#59)
Browse files Browse the repository at this point in the history
* Update dependencies in package.json

* Refactor product patching and add combo validation

* Update archive functionality to use IsArchived
instead of IsRemoved

* Refactor code to archive types and combos

* Update dependencies in package.json
  • Loading branch information
Aloento authored Nov 10, 2023
1 parent 7e34bf2 commit d35931b
Show file tree
Hide file tree
Showing 8 changed files with 831 additions and 732 deletions.
103 changes: 101 additions & 2 deletions TSystems.LoveOTC/AdminHub/Product/Patch.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
namespace TSystems.LoveOTC.AdminHub;

using System.Collections.Immutable;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Models;

internal partial class AdminHub {
/**
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* </remarks>
*/
private static List<Combo> archiveCombos(ICollection<Combo> oldCombos) {
var newTypes = new List<Combo>(oldCombos.Count);

foreach (var oldCombo in oldCombos) {
oldCombo.IsArchived = true;

newTypes.Add(new() {
ProductId = oldCombo.ProductId,
Stock = oldCombo.Stock,
});
}

return newTypes;
}

/**
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* </remarks>
*/
private static List<Type> archiveTypes(ICollection<Type> oldTypes) {
var newTypes = new List<Type>(oldTypes.Count);

foreach (var oldType in oldTypes) {
oldType.IsArchived = true;

var newType = new Type {
Name = oldType.Name,
VariantId = oldType.VariantId,
Combos = archiveCombos(oldType.Combos)
};
newTypes.Add(newType);
}

return newTypes;
}

/**
* <remarks>
* @author Aloento
Expand Down Expand Up @@ -102,7 +149,7 @@ public async Task<bool> ProductPatchCaption(uint photoId, string caption) {
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.2.0
* @version 1.0.0
* </remarks>
*/
public async Task<bool> ProductPatchVariantName(uint variantId, string name) {
Expand All @@ -112,6 +159,31 @@ public async Task<bool> ProductPatchVariantName(uint variantId, string name) {
if (!valid.IsValid(name))
throw new HubException(valid.FormatErrorMessage("Name"));

var any = await this.Db.Variants
.Where(x => x.VariantId == variantId)
.SelectMany(x => x.Types)
.SelectMany(x => x.Combos)
.SelectMany(x => x.Orders)
.AnyAsync();

if (any) {
var oldVari = await this.Db.Variants
.Include(x => x.Types)
.ThenInclude(x => x.Combos)
.SingleAsync(x => x.VariantId == variantId);

oldVari.IsArchived = true;

await this.Db.Variants.AddAsync(new() {
Name = oldVari.Name,
ProductId = oldVari.ProductId,
Types = archiveTypes(oldVari.Types)
});

await this.Db.SaveChangesAsync();
return true;
}

var row = await this.Db.Variants
.Where(x => x.VariantId == variantId)
.ExecuteUpdateAsync(x => x.SetProperty(p => p.Name, name));
Expand All @@ -121,6 +193,7 @@ public async Task<bool> ProductPatchVariantName(uint variantId, string name) {

/**
* <remarks>
* TODO
* @author Aloento
* @since 0.1.0
* @version 0.2.0
Expand All @@ -144,10 +217,36 @@ public async Task<bool> ProductPatchType(uint variantId, string oldName, string
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<bool> ProductPatchCombo(uint comboId, Dictionary<string, string> combo, byte stock) {
var variTypes = (await this.Db.Variants
.Where(x => x.ProductId == this.Db.Combos
.Where(c => c.ComboId == comboId)
.Select(c => c.ProductId)
.Single())
.Include(x => x.Types)
.Select(x => new {
x.Name,
Types = x.Types.Select(t => t.Name).ToImmutableArray()
})
.ToDictionaryAsync(k => k.Name, v => v.Types))
.ToImmutableSortedDictionary();

var reqCombo = combo.ToImmutableSortedDictionary();

if (reqCombo.Count != variTypes.Count)
throw new HubException($"Mismatched: {variTypes.Count} Variants, got {reqCombo.Count} in Combos");

foreach (var (vari, type) in reqCombo) {
if (!variTypes.TryGetValue(vari, out var types))
throw new HubException($"Variant {vari} not found");

if (!types.Any(x => x == type))
throw new HubException($"Type {type} not found in variant {vari}");
}

throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
* @version 0.1.0
* </remarks>
*/
internal interface IRemoveHolder {
public bool? IsRemoved { get; set; }
internal interface IArchive {
public bool? IsArchived { get; set; }
}
4 changes: 2 additions & 2 deletions TSystems.LoveOTC/Models/Combo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace TSystems.LoveOTC.Models;
* @version 0.1.0
* </remarks>
*/
public class Combo : Concurrency, IRemoveHolder {
public class Combo : Concurrency, IArchive {
public uint ComboId { get; set; }

public ushort Stock { get; set; }
Expand All @@ -20,7 +20,7 @@ public class Combo : Concurrency, IRemoveHolder {

public virtual Product Product { get; set; }

public bool? IsRemoved { get; set; }
public bool? IsArchived { get; set; }

public virtual ICollection<Type> Types { get; init; }

Expand Down
4 changes: 2 additions & 2 deletions TSystems.LoveOTC/Models/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace TSystems.LoveOTC.Models;
* </remarks>
*/
[Index(nameof(Name), IsUnique = true)]
public class Product : Concurrency, IRemoveHolder {
public class Product : Concurrency, IArchive {
public uint ProductId { get; set; }

[StringLength(50, MinimumLength = 1)]
Expand All @@ -29,7 +29,7 @@ public class Product : Concurrency, IRemoveHolder {

public JsonElement? Description { get; set; }

public bool? IsRemoved { get; set; }
public bool? IsArchived { get; set; }

public virtual ICollection<Variant> Variants { get; }

Expand Down
4 changes: 2 additions & 2 deletions TSystems.LoveOTC/Models/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace TSystems.LoveOTC.Models;
* </remarks>
*/
[Index(nameof(VariantId), nameof(Name), IsUnique = true)]
public class Type : Concurrency, IRemoveHolder {
public class Type : Concurrency, IArchive {
public uint TypeId { get; set; }

[StringLength(15, MinimumLength = 1)]
Expand All @@ -24,7 +24,7 @@ public class Type : Concurrency, IRemoveHolder {

public virtual Variant Variant { get; set; }

public bool? IsRemoved { get; set; }
public bool? IsArchived { get; set; }

public virtual ICollection<Combo> Combos { get; init; }

Expand Down
6 changes: 3 additions & 3 deletions TSystems.LoveOTC/Models/Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ namespace TSystems.LoveOTC.Models;
* </remarks>
*/
[Index(nameof(ProductId), nameof(Name), IsUnique = true)]
public class Variant : Concurrency, IRemoveHolder {
public class Variant : Concurrency, IArchive {
public uint VariantId { get; set; }

[StringLength(15, MinimumLength = 1)]
public string Name { get; set; }

public virtual ICollection<Type> Types { get; }
public virtual ICollection<Type> Types { get; init; }

public uint ProductId { get; set; }

public virtual Product Product { get; set; }

public bool? IsRemoved { get; set; }
public bool? IsArchived { get; set; }
}
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"update": "npx npm-check-updates -u"
},
"dependencies": {
"@fluentui/react-components": "^9.37.3",
"@fluentui/react-hooks": "^8.6.32",
"@fluentui/react-icons": "^2.0.221",
"@griffel/react": "^1.5.17",
"@fluentui/react-components": "^9.39.0",
"@fluentui/react-hooks": "^8.6.33",
"@fluentui/react-icons": "^2.0.222",
"@griffel/react": "^1.5.18",
"@lexical/clipboard": "^0.12.2",
"@lexical/code": "^0.12.2",
"@lexical/file": "^0.12.2",
Expand Down Expand Up @@ -49,9 +49,9 @@
"rxjs": "^7.8.1"
},
"devDependencies": {
"@types/lodash-es": "^4.17.10",
"@types/react": "^18.2.34",
"@types/react-dom": "^18.2.14",
"@types/lodash-es": "^4.17.11",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"@vitejs/plugin-react-swc": "^3.4.1",
"typescript": "^5.2.2",
"vite": "^4.5.0"
Expand Down
Loading

0 comments on commit d35931b

Please sign in to comment.