Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Update product deletion and patching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloento committed Dec 7, 2023
1 parent 51bd49e commit 25a1883
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 42 deletions.
41 changes: 34 additions & 7 deletions SoarCraft.AwaiShop/AdminHub/Product/Delete.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
namespace SoarCraft.AwaiShop.AdminHub;

using Microsoft.EntityFrameworkCore;

internal partial class AdminHub {
/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<bool> ProductDeletePhoto(uint photoId) {
throw new NotImplementedException();
var res = await this.Db.Photos
.Where(x => x.PhotoId == photoId)
.ExecuteDeleteAsync();

return res > 0;
}

/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<bool> ProductDeleteVariant(uint variantId) {
throw new NotImplementedException();
var variant = this.Db.Variants
.Where(x => x.VariantId == variantId);

var any = await variant
.SelectMany(x => x.Types)
.SelectMany(x => x.Combos)
.SelectMany(x => x.Orders)
.AnyAsync();

if (!any)
return await variant.ExecuteDeleteAsync() > 0;

var oldVari = await variant
.Include(x => x.Types)
.ThenInclude(x => x.Combos)
.SingleAsync();

oldVari.IsArchived = true;
archiveTypes(oldVari.Types);

return await this.Db.SaveChangesAsync() > 0;
}

/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<bool> ProductDeleteType(uint variantId, string type) {

Check warning on line 59 in SoarCraft.AwaiShop/AdminHub/Product/Delete.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
Expand All @@ -37,7 +64,7 @@ public async Task<bool> ProductDeleteType(uint variantId, string type) {
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<bool> ProductDeleteCombo(uint comboId) {
Expand All @@ -48,7 +75,7 @@ public async Task<bool> ProductDeleteCombo(uint comboId) {
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<bool> ProductDeleteProduct(uint prodId) {
Expand Down
75 changes: 40 additions & 35 deletions SoarCraft.AwaiShop/AdminHub/Product/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,17 @@ private static List<Type> archiveTypes(ICollection<Type> oldTypes) {
* </remarks>
*/
public async Task<bool> ProductPatchName(uint prodId, string name) {
var prop = typeof(Product).GetProperty(nameof(Product.Name))!;
var valid = prop.GetCustomAttribute<StringLengthAttribute>()!;
var valid = typeof(Product)
.GetProperty(nameof(Product.Name))!
.GetCustomAttribute<StringLengthAttribute>()!;

if (!valid.IsValid(name))
throw new HubException(valid.FormatErrorMessage("Name"));

var row = await this.Db.Products
.Where(x => x.ProductId == prodId)
.ExecuteUpdateAsync(x => x.SetProperty(p => p.Name, name));
.ExecuteUpdateAsync(x =>
x.SetProperty(p => p.Name, name));

return row > 0;
}
Expand All @@ -83,8 +85,9 @@ public async Task<bool> ProductPatchName(uint prodId, string name) {
* </remarks>
*/
public async Task<bool> ProductPatchCategory(uint prodId, string name) {
var prop = typeof(Category).GetProperty(nameof(Category.Name))!;
var valid = prop.GetCustomAttribute<StringLengthAttribute>()!;
var valid = typeof(Category)
.GetProperty(nameof(Category.Name))!
.GetCustomAttribute<StringLengthAttribute>()!;

if (!valid.IsValid(name))
throw new HubException(valid.FormatErrorMessage("Name"));
Expand All @@ -108,9 +111,8 @@ public async Task<bool> ProductPatchCategory(uint prodId, string name) {
this.Db.Categories.Remove(prod.Category!);

prod.Category = newCate;
await this.Db.SaveChangesAsync();

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

/**
Expand Down Expand Up @@ -144,8 +146,9 @@ public async Task<bool> ProductPatchPhoto(uint photoId, IAsyncEnumerable<byte[]>
* </remarks>
*/
public async Task<bool> ProductPatchCaption(uint photoId, string caption) {
var prop = typeof(Photo).GetProperty(nameof(Photo.Caption))!;
var valid = prop.GetCustomAttribute<StringLengthAttribute>()!;
var valid = typeof(Photo)
.GetProperty(nameof(Photo.Caption))!
.GetCustomAttribute<StringLengthAttribute>()!;

if (!valid.IsValid(caption))
throw new HubException(valid.FormatErrorMessage("Caption"));
Expand All @@ -165,24 +168,27 @@ public async Task<bool> ProductPatchCaption(uint photoId, string caption) {
* </remarks>
*/
public async Task<bool> ProductPatchVariantName(uint variantId, string name) {
var prop = typeof(Variant).GetProperty(nameof(Variant.Name))!;
var valid = prop.GetCustomAttribute<StringLengthAttribute>()!;
var valid = typeof(Variant)
.GetProperty(nameof(Variant.Name))!
.GetCustomAttribute<StringLengthAttribute>()!;

if (!valid.IsValid(name))
throw new HubException(valid.FormatErrorMessage("Name"));

var any = await this.Db.Variants
.Where(x => x.VariantId == variantId)
var variant = this.Db.Variants
.Where(x => x.VariantId == variantId);

var any = await variant
.SelectMany(x => x.Types)
.SelectMany(x => x.Combos)
.SelectMany(x => x.Orders)
.AnyAsync();

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

oldVari.IsArchived = true;

Expand All @@ -192,13 +198,12 @@ await this.Db.Variants.AddAsync(new() {
Types = archiveTypes(oldVari.Types)
});

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

var row = await this.Db.Variants
.Where(x => x.VariantId == variantId)
.ExecuteUpdateAsync(x => x.SetProperty(p => p.Name, name));
var row = await variant
.ExecuteUpdateAsync(x =>
x.SetProperty(p => p.Name, name));

return row > 0;
}
Expand All @@ -211,22 +216,25 @@ await this.Db.Variants.AddAsync(new() {
* </remarks>
*/
public async Task<bool> ProductPatchType(uint variantId, string oldName, string newName) {
var prop = typeof(Type).GetProperty(nameof(Type.Name))!;
var valid = prop.GetCustomAttribute<StringLengthAttribute>()!;
var valid = typeof(Type)
.GetProperty(nameof(Type.Name))!
.GetCustomAttribute<StringLengthAttribute>()!;

if (!valid.IsValid(newName))
throw new HubException(valid.FormatErrorMessage("Name"));

var any = await this.Db.Types
.Where(x => x.VariantId == variantId && x.Name == oldName)
var type = this.Db.Types
.Where(x => x.VariantId == variantId && x.Name == oldName);

var any = await type
.SelectMany(x => x.Combos)
.SelectMany(x => x.Orders)
.AnyAsync();

if (any) {
var oldType = await this.Db.Types
var oldType = await type
.Include(x => x.Combos)
.SingleAsync(x => x.VariantId == variantId && x.Name == oldName);
.SingleAsync();

oldType.IsArchived = true;

Expand All @@ -236,13 +244,12 @@ await this.Db.Types.AddAsync(new() {
Combos = archiveCombos(oldType.Combos)
});

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

var row = await this.Db.Types
.Where(x => x.VariantId == variantId && x.Name == oldName)
.ExecuteUpdateAsync(x => x.SetProperty(p => p.Name, newName));
var row = await type
.ExecuteUpdateAsync(x =>
x.SetProperty(p => p.Name, newName));

return row > 0;
}
Expand Down Expand Up @@ -272,8 +279,7 @@ public async Task<bool> ProductPatchCombo(uint comboId, Dictionary<string, strin

if (comboVariType.SequenceEqual(reqCombo)) {
dbCombo.Stock = stock;
await this.Db.SaveChangesAsync();
return true;
return await this.Db.SaveChangesAsync() > 0;
}
}

Expand Down Expand Up @@ -313,7 +319,6 @@ await this.Db.Combos.AddAsync(new() {
} else
dbCombo.Types = reqTypes;

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

0 comments on commit 25a1883

Please sign in to comment.