Skip to content

Commit

Permalink
[Back | CRUD] ProductGetList & ProductGetCategory & ProductGetVariant…
Browse files Browse the repository at this point in the history
…s & ProdGetBasic (#46)

* ProductGetCategory ProductGetName

* ProductGetVariants

* ProdGetCombo

* up
  • Loading branch information
Aloento authored Oct 23, 2023
1 parent 787864e commit e63294f
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 228 deletions.
52 changes: 27 additions & 25 deletions TSystems.LoveOTC/AdminHub/Product/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ internal partial class AdminHub {
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.2.0
* @version 0.2.1
* </remarks>
*/
public async Task<List<ProductItem>> ProductGetList() {
public async Task<ProductItem[]> ProductGetList() {
var raw = await this.Db.Products
.Select(x => new {
x.ProductId,
Expand All @@ -24,7 +24,7 @@ public async Task<List<ProductItem>> ProductGetList() {
Combo = (byte)x.Combos.Count,
Stock = x.Combos.Select(s => s.Stock).ToArray()
})
.ToListAsync();
.ToArrayAsync();

return raw.Select(x => new ProductItem {
ProductId = x.ProductId,
Expand All @@ -34,50 +34,52 @@ public async Task<List<ProductItem>> ProductGetList() {
Variant = x.Variant,
Combo = x.Combo,
Stock = x.Stock.Aggregate((uint)0, (prev, curr) => prev + curr)
}).ToList();
}).ToArray();
}

/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<string> ProductGetName(uint prodId) {
return "OTC SHIRT - GREY";
}
public async Task<string> ProductGetName(uint prodId) =>
await this.Db.Products
.Where(x => x.ProductId == prodId)
.Select(x => x.Name)
.SingleAsync();

/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<string> ProductGetCategory(uint prodId) {
return "Clothes";
var cate = await this.Db.Products
.Where(x => x.ProductId == prodId)
.Select(x => x.Category)
.SingleAsync();

return cate?.Name ?? "Pending";
}

/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<List<VariantItem>> ProductGetVariants(uint prodId) {
return new() {
new() {
VariantId = 1,
Name = "Color",
Types = new() { "White", "Red" }
},
new() {
VariantId = 2,
Name = "Size",
Types = new() { "Big", "Small" }
}
};
}
public async Task<VariantItem[]> ProductGetVariants(uint prodId) =>
await this.Db.Variants
.Where(x => x.ProductId == prodId)
.Select(x => new VariantItem {
VariantId = x.VariantId,
Name = x.Name,
Types = x.Types.Select(t => t.Name).ToArray()
})
.ToArrayAsync();
}
6 changes: 3 additions & 3 deletions TSystems.LoveOTC/AdminHub/Product/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public async Task<uint> ProductPostCombo(uint prodId, Dictionary<string, string>
.ToImmutableSortedDictionary(k => k.Variant.Name, v => v.Name))
.ToImmutableArray();

if (existCombo.Any(x => x.SequenceEqual(reqCombo)))
throw new HubException("Combo already exist");

var reqTypes = new List<Type>();

foreach (var (vari, type) in reqCombo) {
Expand All @@ -235,9 +238,6 @@ public async Task<uint> ProductPostCombo(uint prodId, Dictionary<string, string>
reqTypes.Add(t);
}

if (existCombo.Any(x => x.SequenceEqual(reqCombo)))
throw new HubException("Combo already exist");

var temp = await this.Db.Combos.AddAsync(new() {
ProductId = prodId,
Stock = stock,
Expand Down
2 changes: 1 addition & 1 deletion TSystems.LoveOTC/AdminHub/Product/VariantItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public record VariantItem {

public required string Name { get; init; }

public required List<string> Types { get; init; }
public required ICollection<string> Types { get; init; }
}
2 changes: 1 addition & 1 deletion TSystems.LoveOTC/Hub/Product/ComboItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace TSystems.LoveOTC.Hub;
internal record ComboItem {
public required uint ComboId { get; init; }

public required Dictionary<string, string> Combo { get; init; }
public required IDictionary<string, string> Combo { get; init; }

public required ushort Stock { get; init; }
}
76 changes: 30 additions & 46 deletions TSystems.LoveOTC/Hub/Product/Get.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,60 @@
namespace TSystems.LoveOTC.Hub;

using System.Collections.Immutable;
using Microsoft.EntityFrameworkCore;

internal partial class ShopHub {
/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<ProductInfo> ProdGetBasic(uint prodId) {
return new() {
Cover = $"https://picsum.photos/{Random.Shared.Next(500, 1000)}",
Name = $"Product {prodId}"
};
return await this.Db.Products
.Where(x => x.ProductId == prodId)
.Select(x => new ProductInfo {
Name = x.Name,
Cover = x.Photos
.Where(p => p.Cover == true)
.Select(p => p.Object.Id)
.Single()
})
.SingleAsync();
}

/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
public async Task<byte> ProdGetLimit(uint prodId) {
return (byte)Random.Shared.Next(10);
public Task<byte> ProdGetLimit(uint _) {
return Task.FromResult<byte>(3);
}

/**
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.1.0
* @version 1.0.0
* </remarks>
*/
public async Task<List<ComboItem>> ProdGetCombo(uint prodId) {
if (prodId > 100)
throw new KeyNotFoundException("Product Not Found");
public async Task<ComboItem[]> ProdGetCombo(uint prodId) {
var comboDb = await this.Db.Combos
.Where(x => x.ProductId == prodId)
.Include(x => x.Types)
.ThenInclude(x => x.Variant)
.ToArrayAsync();

return new() {
new() {
ComboId = 1,
Combo = new() {
{"Color", "White"},
{"Size", "Big"}
},
Stock = 8
},
new() {
ComboId = 2,
Combo = new() {
{"Color", "Red"},
{"Size", "Small"}
},
Stock = 6
},
new() {
ComboId = 3,
Combo = new() {
{"Color", "White"},
{"Size", "Big"}
},
Stock = 10
},
new() {
ComboId = 4,
Combo = new() {
{"Color", "Red"},
{"Size", "Small"}
},
Stock = 4
}
};
return comboDb.Select(x => new ComboItem {
ComboId = x.ComboId,
Stock = x.Stock,
Combo = x.Types
.ToImmutableDictionary(k => k.Variant.Name, v => v.Name)
}).ToArray();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion TSystems.LoveOTC/Hub/Product/ProductInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace TSystems.LoveOTC.Hub;
* </remarks>
*/
internal record ProductInfo {
public required string Cover { get; init; }
public required Guid Cover { get; init; }

public required string Name { get; init; }
}
5 changes: 3 additions & 2 deletions TSystems.LoveOTC/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
.WithResolver(ContractlessStandardResolverAllowPrivate.Instance);
});

if (!Shared.Dev)
builder.Host.UseSystemd();
#if RELEASE
builder.Host.UseSystemd();
#endif

var app = builder.Build();

Expand Down
5 changes: 2 additions & 3 deletions TSystems.LoveOTC/ShopContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
namespace TSystems.LoveOTC;

using Entities;
using Microsoft.EntityFrameworkCore;
using Models;

Expand Down Expand Up @@ -69,8 +68,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) {
#endregion

#region MockData

if (!Shared.Dev) return;
#if false

modelBuilder.Entity<User>().HasData(new User {
UserId = Guid.Parse("e2653b80-9be7-41d0-aff0-524ad0e66944"),
Expand Down Expand Up @@ -336,6 +334,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) {

#endregion

#endif
#endregion
}
}
2 changes: 1 addition & 1 deletion TSystems.LoveOTC/TSystems.LoveOTC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0-rc.2.23479.6" Condition="'$(Configuration)'=='Release'" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0-rc.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.2" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="8.0.0-rc.2.23480.1-01" />
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"update": "npx npm-check-updates -u"
},
"dependencies": {
"@fluentui/react-components": "^9.35.1",
"@fluentui/react-components": "^9.36.0",
"@fluentui/react-hooks": "^8.6.30",
"@fluentui/react-icons": "^2.0.221",
"@griffel/react": "^1.5.16",
Expand Down
Loading

0 comments on commit e63294f

Please sign in to comment.