Skip to content

Commit

Permalink
[Back | CRUD] ProductGetList & ObjectStorageGet (#45)
Browse files Browse the repository at this point in the history
* ProductGetList

* ObjectStorageGet
  • Loading branch information
Aloento authored Oct 22, 2023
1 parent 191cf7a commit 787864e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
48 changes: 27 additions & 21 deletions TSystems.LoveOTC/AdminHub/Product/Get.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
namespace TSystems.LoveOTC.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<List<ProductItem>> ProductGetList() {
return new() {
new() {
ProductId = 1,
Cover = Guid.NewGuid(),
Name = "OTC SHIRT - GREY",
Category = "Clothes",
Variant = 2,
Combo = 4,
Stock = 10
},
new() {
ProductId = 2,
Cover = Guid.NewGuid(),
Name = "OTC Cap - Cap and Cap",
Category = "Hat",
Variant = 2,
Combo = 4,
Stock = 20
}
};
var raw = await this.Db.Products
.Select(x => new {
x.ProductId,
Cover = x.Photos
.Where(p => p.Cover == true)
.Select(p => p.ObjectId)
.SingleOrDefault(),
x.Name,
x.Category,
Variant = (byte)x.Variants.Count,
Combo = (byte)x.Combos.Count,
Stock = x.Combos.Select(s => s.Stock).ToArray()
})
.ToListAsync();

return raw.Select(x => new ProductItem {
ProductId = x.ProductId,
Cover = x.Cover,
Name = x.Name,
Category = x.Category?.Name ?? "Pending",
Variant = x.Variant,
Combo = x.Combo,
Stock = x.Stock.Aggregate((uint)0, (prev, curr) => prev + curr)
}).ToList();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion TSystems.LoveOTC/AdminHub/Product/ProductItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ public record ProductItem {

public required byte Combo { get; init; }

public required ushort Stock { get; init; }
public required uint Stock { get; init; }
}
29 changes: 22 additions & 7 deletions TSystems.LoveOTC/Hub/ShopHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace TSystems.LoveOTC.Hub;

using Helpers;
using JetBrains.Annotations;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;

/**
Expand Down Expand Up @@ -40,19 +41,33 @@ public override async Task OnConnectedAsync() {
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 1.0.0
* </remarks>
*/
public async IAsyncEnumerable<byte[]> ObjectStorageGet(Guid objId) {
var imageUrl = "https://source.unsplash.com/random";
using var httpClient = new HttpClient();
using var response = await httpClient.GetAsync(imageUrl, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
var exp = await this.Db.Objects
.Where(x => x.Id == objId)
.Select(x => x.Expires)
.SingleAsync();

await using var stream = await response.Content.ReadAsStreamAsync();
var buffer = new byte[30 * 1024];
if (exp is not null && exp > DateTime.UtcNow) {
await this.Db.Objects.Where(x => x.Id == objId).ExecuteDeleteAsync();
throw new HubException("Object Expired");
}

await using var command = this.Db.Objects
.Where(x => x.Id == objId)
.Select(x => x.Data)
.CreateDbCommand();

await command.Connection!.OpenAsync();
await using var reader = await command.ExecuteReaderAsync();
await reader.ReadAsync();

var buffer = new byte[30 * 1024];
int bytesRead;

await using var stream = reader.GetStream(0);
while ((bytesRead = await stream.ReadAsync(buffer)) > 0)
yield return buffer[..bytesRead];
}
Expand Down
3 changes: 2 additions & 1 deletion TSystems.LoveOTC/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
.WithResolver(ContractlessStandardResolverAllowPrivate.Instance);
});

builder.Host.UseSystemd();
if (!Shared.Dev)
builder.Host.UseSystemd();

var app = builder.Build();

Expand Down

0 comments on commit 787864e

Please sign in to comment.