Split apart the DTO from the Database Objects

This commit is contained in:
2025-06-19 10:40:26 -07:00
parent e88c7111e6
commit 412624a93c
20 changed files with 118 additions and 201 deletions
@@ -1,10 +1,11 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using MistoxWebsite.Shared;
using System.Security.Claims;
using MistoxWebsite.Server.Services;
using MistoxWebsite.Server.Services.DatabaseService;
using Microsoft.AspNetCore.Authentication.Cookies;
using MistoxWebsite.Shared.Database;
using MistoxWebsite.Shared.DTO.Session;
namespace MistoxWebsite.Server.Controllers {
[ApiController]
@@ -1,6 +1,6 @@
using MistoxWebsite.Server.Services.DatabaseService;
using System.Security.Claims;
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.DTO.Session;
using Microsoft.AspNetCore.Mvc;
namespace MistoxWebsite.Server.Controllers {
@@ -1,12 +1,8 @@
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using MistoxWebsite.Server.Controllers.Payment;
using MistoxWebsite.Server.Services.DatabaseService;
using MistoxWebsite.Shared;
using Newtonsoft.Json;
using MistoxWebsite.Shared.Database;
using Stripe;
using Stripe.Climate;
using Stripe.Tax;
namespace MistoxWebsite.Server.Controllers {
[ApiController]
@@ -24,7 +20,7 @@ namespace MistoxWebsite.Server.Controllers {
public async Task<string> GetPaymentKey( [FromQuery] string userID ) {
string OrderNumber = Guid.NewGuid().ToString().Substring(0,10);
Shared.Account? acc = await _databaseService.GetAccount(userID);
Shared.Database.Account? acc = await _databaseService.GetAccount(userID);
if (acc != null) {
List<Cart> cart = await _databaseService.GetCart(acc);
@@ -84,7 +80,7 @@ namespace MistoxWebsite.Server.Controllers {
}
// Clear the cart
Shared.Account account = new Shared.Account{
Shared.Database.Account account = new() {
ID = userID
};
await _databaseService.ClearCart( account );
@@ -1,4 +1,4 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
namespace MistoxWebsite.Server.Controllers.Payment {
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MistoxWebsite.Server.Controllers.Payment;
using MistoxWebsite.Server.Services.DatabaseService;
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using Stripe;
using Stripe.Tax;
@@ -17,7 +14,7 @@ namespace MistoxWebsite.Server.Controllers {
_databaseService = databaseService;
}
public async Task<(bool, string)> Purchase(string OrderNumber, Shared.Account user, List<Cart> cart) {
public async Task<(bool, string)> Purchase(string OrderNumber, Shared.Database.Account user, List<Cart> cart) {
try {
// build Recipt and calculate Tax
var options = new CalculationCreateOptions {
@@ -34,7 +31,7 @@ namespace MistoxWebsite.Server.Controllers {
// Add items to receipt
int subtotal = 0;
foreach (Cart items in cart) {
Shared.Product? product = await _databaseService.GetProduct(items.ProductID);
Shared.Database.Product? product = await _databaseService.GetProduct(items.ProductID);
if (product != null) {
prods.Add(product.ID);
if (product != null) {
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using MistoxWebsite.Server.Services.DatabaseService;
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using Newtonsoft.Json;
using System.Security.Claims;
using System.Text;
@@ -128,74 +128,6 @@ namespace MistoxWebsite.Server.Controllers {
}
}
DirObj RecursiveBuild( DirObj DirObj, string workingPath, List<ReceiptProduct> purchased ) {
string[] files = Directory.GetFiles(workingPath);
string[] directories = Directory.GetDirectories(workingPath);
List<DirObj> building = new List<DirObj>();
// Get File Names
Parallel.For( 0, files.Length, ( i ) => {
string fileName = files[i].Substring(workingPath.Length, files[i].Length - (workingPath.Length));
building.Add( new DirObj {
Type = FileType.File,
Path = fileName
});
} );
// Get Path Names
Parallel.For( 0, directories.Length, ( i ) => {
foreach( ReceiptProduct cur in purchased ) {
string dirName = directories[i].Substring(workingPath.Length, directories[i].Length - (workingPath.Length));
if( contains( dirName, cur.product.URL ) ) {
DirObj dir = new DirObj {
Type = FileType.Directory,
Path = dirName,
};
building.Add( dir );
RecursiveBuild( dir, directories [i], purchased );
}
}
} );
DirObj.Children = building.ToArray();
return DirObj;
}
string _FolderRoot = "/home/downloads/";
[Route( "api/product/showdownloads" )]
[HttpPost]
public async Task<IActionResult> ShowDownloads() {
try {
if( User.Identity != null && User.Identity.IsAuthenticated ) {
List<Claim> userClaims = User.Claims.ToList();
int UserID = -1;
foreach( Claim claim in userClaims ) {
if( claim.Type == "ID" ) {
UserID = Convert.ToInt32( claim.Value );
break;
}
}
List<ReceiptProduct> purchased = await _databaseService.GetAllReceiptsJoinedToProduct( new Account{ ID = UserID } );
byte[] datapacket = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(RecursiveBuild(new DirObj {
Path = @"\",
Type = FileType.Directory,
}, _FolderRoot, purchased)));
return new FileContentResult( datapacket, "text/html" );
}
return Unauthorized();
} catch {
return NotFound();
}
}
bool contains( string outer, string inner ) {
if ( outer.Length >= inner.Length ) {
for ( int i=0; i<outer.Length-inner.Length; i++ ) {
@@ -221,10 +153,10 @@ namespace MistoxWebsite.Server.Controllers {
if ( contains( Product, product.URL ) ) {
Receipt? receipt = await _databaseService.GetReceipt(user, product);
if( receipt != null ) {
FileStream fileStream = new FileStream(_FolderRoot + Product, FileMode.Open, FileAccess.Read);
return new FileStreamResult( fileStream, "application/octet-stream" ) {
FileDownloadName = fileStream.Name
};
//FileStream fileStream = new FileStream(_FolderRoot + Product, FileMode.Open, FileAccess.Read);
//return new FileStreamResult( fileStream, "application/octet-stream" ) {
// FileDownloadName = fileStream.Name
//};
}
break;
}