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
+4 -1
View File
@@ -24,4 +24,7 @@ Store Catalog
Add to cart wraps text when screen is too small
Program
Probably need to turn on cors at some point
Probably need to turn on cors at some point
ProductController
No way to download products
@@ -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;
}
@@ -1,4 +1,4 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
@@ -1,4 +1,5 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using MistoxWebsite.Shared.DTO.Account;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
@@ -1,4 +1,4 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
@@ -1,6 +1,6 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using MistoxWebsite.Shared.DTO.Session;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace MistoxWebsite.Server.Services.DatabaseService {
@@ -1,4 +1,4 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
@@ -1,4 +1,4 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
@@ -1,4 +1,4 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
@@ -46,8 +46,8 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
return receipts;
}
public async Task<List<ReceiptProduct>> GetAllReceiptsJoinedToProduct( Account account ) {
List<ReceiptProduct> join = new List<ReceiptProduct> ();
public async Task<List<( Receipt, Product )>> GetAllReceiptsJoinedToProduct( Account account ) {
List<( Receipt, Product )> join = new();
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
@@ -78,24 +78,25 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
int _cost = !reader.IsDBNull( "Cost" ) ? reader.GetInt32("Cost") : 0;
string _url = !reader.IsDBNull( "URL" ) ? reader.GetString("URL") : "Something Random That Wont Ever Be In A URL";
join.Add( new ReceiptProduct() {
receipt = new Receipt {
AccountID = _accountid,
ProductID = _gameid,
ReceiptID = _receiptid,
Time = _receiptdate,
TotalCost = _totalcost,
TaxAmount = _taxamount,
LineItem = _lineitem
},
product = new Product() {
ID = _id,
Cost = _cost,
Description = _desc,
Name = _name,
URL = _url
}
} );
Receipt r = new() {
AccountID = _accountid,
ProductID = _gameid,
ReceiptID = _receiptid,
Time = _receiptdate,
TotalCost = _totalcost,
TaxAmount = _taxamount,
LineItem = _lineitem
};
Product p = new() {
ID = _id,
Cost = _cost,
Description = _desc,
Name = _name,
URL = _url
};
join.Add( (r, p) );
}
}
}
@@ -1,4 +1,4 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Shared.Database;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
@@ -1,5 +1,3 @@
using System.Net.Mail;
namespace MistoxWebsite.Server.Services {
public partial class EmailService {
@@ -1,5 +1,3 @@
using System.Net.Mail;
namespace MistoxWebsite.Server.Services {
public partial class EmailService {
@@ -0,0 +1,20 @@
namespace MistoxWebsite.Shared.DTO.Account {
public class UserInventory {
public string Item { get; set; } = string.Empty;
public int Quantity { get; set; }
public string Stats { get; set; } = string.Empty;
}
public class PaymentObject {
public string CardNumber { get; set; } = string.Empty;
public long ExperationMonth { get; set; }
public long ExperationYear { get; set; }
public string CVC { get; set; } = string.Empty;
public string FullName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Zip { get; set; } = string.Empty;
public List<int> productIDs { get; set; } = new List<int>();
}
}
@@ -0,0 +1,21 @@
using MistoxWebsite.Shared.Database;
namespace MistoxWebsite.Shared.DTO.Session {
public class PageLoadObject {
public Database.Account? user { get; set; }
public AccountClaims? claims { get; set; }
public List<Receipt>? receipts { get; set; }
public List<Product>? products { get; set; }
public List<Cart>? Cart { get; set; }
}
public class AccountClaims {
public string UserName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string EmailVerified { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
public string FailedPasswordLock { get; set; } = string.Empty;
}
}
+25 -76
View File
@@ -1,27 +1,6 @@
using System.Diagnostics;
// Reflections of SQL Database objects
// Reflections of SQL Database objects
namespace MistoxWebsite.Shared {
public class PageLoadObject {
public Account? user { get; set; }
public AccountClaims? claims { get; set; }
public List<Receipt>? receipts { get; set; }
public List<Product>? products { get; set; }
public List<Cart>? Cart { get; set; }
}
public class DirObj {
public FileType? Type { get; set; }
public string Path { get; set; } = "";
public DirObj? [] Children { get; set; } = new DirObj?[0];
}
public enum FileType {
File,
Directory
}
namespace MistoxWebsite.Shared.Database {
public class Account {
public int ID { get; set; } // PK
@@ -33,25 +12,6 @@ namespace MistoxWebsite.Shared {
public string Error { get; set; } = "";
}
public class Product {
public int ID { get; set; } // PK
public string Name { get; set; } = "";
public string Description { get; set; } = "";
public int CurShowingIMG = 0;
public List<string> Images { get; set; } = new List<string>();
public int Cost { get; set; }
public string URL { get; set; } = "";
}
public class WebSiteData {
public int AccountID { get; set; } // PK
public bool FailedPasswordLock { get; set; } = false;
public int PasswordAttempts { get; set; } = 5;
public int CurrentPasswordAttempts { get; set; } = 0;
public string Role { get; set; } = "Generic";
public string EmailToken { get; set; } = "";
}
public class AccountInventory {
public int AccountID { get; set; } // PK
public int ProductID { get; set; } // PK
@@ -60,25 +20,14 @@ namespace MistoxWebsite.Shared {
public string Stats { get; set; } = string.Empty;
}
public class UserInventory {
public string Item { get; set; } = string.Empty;
public int Quantity { get; set; }
public string Stats { get; set; } = string.Empty;
}
public class Receipt {
public int AccountID { get; set; } // PK
public int ProductID { get; set; } // PK
public string ReceiptID { get; set; } = string.Empty;
public int LineItem { get; set; }
public int TaxAmount { get; set; }
public int TotalCost { get; set; }
public DateTime Time { get; set; }
}
public class ReceiptProduct {
public Receipt receipt { get; set; } = new Receipt();
public Product product { get; set; } = new Product();
public class Product {
public int ID { get; set; } // PK
public string Name { get; set; } = "";
public string Description { get; set; } = "";
public int CurShowingIMG = 0;
public List<string> Images { get; set; } = new List<string>();
public int Cost { get; set; }
public string URL { get; set; } = "";
}
public class Cart {
@@ -97,23 +46,23 @@ namespace MistoxWebsite.Shared {
public int Deaths { get; set; }
}
public class AccountClaims {
public string UserName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string EmailVerified { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
public string FailedPasswordLock { get; set; } = string.Empty;
public class Receipt {
public int AccountID { get; set; } // PK
public int ProductID { get; set; } // PK
public string ReceiptID { get; set; } = string.Empty;
public int LineItem { get; set; }
public int TaxAmount { get; set; }
public int TotalCost { get; set; }
public DateTime Time { get; set; }
}
public class PaymentObject {
public string CardNumber { get; set; } = string.Empty;
public long ExperationMonth { get; set; }
public long ExperationYear { get; set; }
public string CVC { get; set; } = string.Empty;
public string FullName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Zip { get; set; } = string.Empty;
public List<int> productIDs { get; set; } = new List<int>();
public class WebSiteData {
public int AccountID { get; set; } // PK
public bool FailedPasswordLock { get; set; } = false;
public int PasswordAttempts { get; set; } = 5;
public int CurrentPasswordAttempts { get; set; } = 0;
public string Role { get; set; } = "Generic";
public string EmailToken { get; set; } = "";
}
}