From 43da6a5d93a56a029f6018d0ec451dc0e9e66531 Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Thu, 12 Dec 2024 19:48:08 +0100 Subject: [PATCH 1/6] zzmiany --- Controllers/ProductController.cs | 5 + Controllers/WorkDayController.cs | 31 ++++- Models/DayDetailsDto.cs | 12 ++ Models/DayDetailsLoggedUserDto.cs | 12 ++ appsettings.json | 2 +- nHIbernate/WorkdayRepository.cs | 127 +++++++++++++++++++++ nHibernate/Transactions/TransactionCRUD.cs | 2 +- 7 files changed, 188 insertions(+), 3 deletions(-) create mode 100644 Models/DayDetailsDto.cs create mode 100644 Models/DayDetailsLoggedUserDto.cs diff --git a/Controllers/ProductController.cs b/Controllers/ProductController.cs index 11cc479..58f85f8 100644 --- a/Controllers/ProductController.cs +++ b/Controllers/ProductController.cs @@ -63,6 +63,11 @@ namespace FirmTracker_Server.Controllers { throw new InvalidOperationException("Produkt nie może posiadać ujemnej ceny."); } + var productByName = _productCrud.GetProductByName(product.Name); + if (productByName != null) + { + throw new InvalidOperationException("Produkt o podanej nazwie już istnieje."); + } _productCrud.AddProduct(product); return CreatedAtAction("GetProduct", new { id = product.Id }, product); diff --git a/Controllers/WorkDayController.cs b/Controllers/WorkDayController.cs index ba1a5f9..f233454 100644 --- a/Controllers/WorkDayController.cs +++ b/Controllers/WorkDayController.cs @@ -127,6 +127,35 @@ namespace FirmTracker_Server.Controllers } } - + [HttpGet("user/{userMail}/day/info/{date}")] + [Authorize(Roles = Roles.Admin + "," + Roles.User)] + public IActionResult GetUserDayDetailsByMail(string userMail, DateTime date) + { + try + { + var dayDetails = _workdayCRUD.GetDayDetails(userMail, date); + return Ok(dayDetails); + } + catch (Exception ex) + { + return BadRequest(new { message = "An error occurred while fetching the day's details.", error = ex.Message }); + } + } + [HttpGet("user/day/info/{date}")] + [Authorize(Roles = Roles.Admin + "," + Roles.User)] + public IActionResult GetUserDayDetails(DateTime date) + { + try + { + var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ; + + var dayDetails = _workdayCRUD.GetDayDetailsForLoggedUser(int.Parse(userId), date); + return Ok(dayDetails); + } + catch (Exception ex) + { + return BadRequest(new { message = "An error occurred while fetching the day's details.", error = ex.Message }); + } + } } } diff --git a/Models/DayDetailsDto.cs b/Models/DayDetailsDto.cs new file mode 100644 index 0000000..09f1a01 --- /dev/null +++ b/Models/DayDetailsDto.cs @@ -0,0 +1,12 @@ +using FirmTracker_Server.nHibernate; + +namespace FirmTracker_Server.Models +{ + public class DayDetailsDto + { + public string Email { get; set; } + public DateTime Date { get; set; } + public string TotalWorkedHours { get; set; } + public List WorkdayDetails { get; set; } + } +} diff --git a/Models/DayDetailsLoggedUserDto.cs b/Models/DayDetailsLoggedUserDto.cs new file mode 100644 index 0000000..77f3293 --- /dev/null +++ b/Models/DayDetailsLoggedUserDto.cs @@ -0,0 +1,12 @@ +using FirmTracker_Server.nHibernate; + +namespace FirmTracker_Server.Models +{ + public class DayDetailsLoggedUserDto + { + public int UserId { get; set; } + public DateTime Date { get; set; } + public string TotalWorkedHours { get; set; } + public List WorkdayDetails { get; set; } + } +} diff --git a/appsettings.json b/appsettings.json index 180832f..efdb2c0 100644 --- a/appsettings.json +++ b/appsettings.json @@ -5,7 +5,7 @@ }, "TokenConfig": { "JwtSecKey": "omgi5Rf4tqg351GQwefw1234567890123456", - "JwtExpireDays": 30, + "JwtExpireDays": 1, "JwtIssuer": "http://api.graphcom.pl" }, "profiles": { diff --git a/nHIbernate/WorkdayRepository.cs b/nHIbernate/WorkdayRepository.cs index 2a6dc7d..8d49d92 100644 --- a/nHIbernate/WorkdayRepository.cs +++ b/nHIbernate/WorkdayRepository.cs @@ -1,5 +1,7 @@ using FirmTracker_Server.Entities; using FirmTracker_Server.nHibernate; +using static NHibernate.Engine.Query.CallableParser; +using FirmTracker_Server.Models; public class WorkdayRepository { @@ -125,6 +127,131 @@ public class WorkdayRepository }) .ToList(); + foreach (var workday in workdays) + { + if(workday.Absence!="") + { + workday.WorkedHours = TimeSpan.Zero; + } + } + + return workdays; + } + catch (Exception ex) + { + throw new Exception("An error occurred while fetching workdays", ex); + } + } + } + public DayDetailsDto GetDayDetails(string mail, DateTime date) + { + using (var session = SessionFactory.OpenSession()) + { + try + { + // Fetch workdays for the specified user on the given date + var startOfDay = date.Date; + var endOfDay = startOfDay.AddDays(1); + + var workdays = session.Query() + .Where(w => w.User.Email == mail && w.StartTime >= startOfDay && w.StartTime < endOfDay) + .Select(w => new Workday + { + StartTime = w.StartTime, + EndTime = w.EndTime ?? DateTime.Today.AddHours(17), + Absence = w.Absence, + }) + .ToList(); + + TimeSpan totalWorkedHours = TimeSpan.Zero; + + // Calculate total worked hours and adjust if there's an absence + foreach (var workday in workdays) + { + if (string.IsNullOrEmpty(workday.Absence)) + { + totalWorkedHours += workday.WorkedHours; + } + } + + return new DayDetailsDto + { + Email = mail, + Date = date, + TotalWorkedHours = totalWorkedHours.ToString(@"hh\:mm\:ss"), + WorkdayDetails = workdays + }; + } + catch (Exception ex) + { + throw new Exception("An error occurred while fetching the day's details", ex); + } + } + } + public DayDetailsLoggedUserDto GetDayDetailsForLoggedUser(int userId, DateTime date) + { + using (var session = SessionFactory.OpenSession()) + { + try + { + // Fetch workdays for the specified user on the given date + var startOfDay = date.Date; + var endOfDay = startOfDay.AddDays(1); + + var workdays = session.Query() + .Where(w => w.User.UserId == userId && w.StartTime >= startOfDay && w.StartTime < endOfDay) + .Select(w => new Workday + { + StartTime = w.StartTime, + EndTime = w.EndTime ?? DateTime.Today.AddHours(17), + Absence = w.Absence, + }) + .ToList(); + + TimeSpan totalWorkedHours = TimeSpan.Zero; + + // Calculate total worked hours and adjust if there's an absence + foreach (var workday in workdays) + { + if (string.IsNullOrEmpty(workday.Absence)) + { + totalWorkedHours += workday.WorkedHours; + } + } + + return new DayDetailsLoggedUserDto + { + UserId = userId, + Date = date, + TotalWorkedHours = totalWorkedHours.ToString(@"hh\:mm\:ss"), + WorkdayDetails = workdays + }; + } + catch (Exception ex) + { + throw new Exception("An error occurred while fetching the day's details", ex); + } + } + } + public List GetWorkdaysByLoggedUser(string userId) + { + using (var session = SessionFactory.OpenSession()) + { + try + { + int parsedUserId = Int32.Parse(userId); + var workdays = session.Query() + .Where(w => w.User.UserId == parsedUserId) + .Select(w => new Workday + { + Id = w.Id, + StartTime = w.StartTime, + EndTime = w.EndTime ?? DateTime.Today.AddHours(17), + WorkedHours = (w.EndTime ?? DateTime.Today.AddHours(17)) - w.StartTime, + Absence = w.Absence, + }) + .ToList(); + return workdays; } catch (Exception ex) diff --git a/nHibernate/Transactions/TransactionCRUD.cs b/nHibernate/Transactions/TransactionCRUD.cs index feab4de..01c392f 100644 --- a/nHibernate/Transactions/TransactionCRUD.cs +++ b/nHibernate/Transactions/TransactionCRUD.cs @@ -253,7 +253,7 @@ namespace FirmTracker_Server.nHibernate.Transactions catch (Exception ex) { transaction.Rollback(); - throw ex; + throw; } } } -- 2.20.1 From ba4f8a717cf510899a11355fb62759d73e124207 Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Thu, 12 Dec 2024 20:20:13 +0100 Subject: [PATCH 2/6] aktualizacja appsettings --- appsettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appsettings.json b/appsettings.json index efdb2c0..842ffd3 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,6 +1,6 @@ { "AppSettings": { - "ConnectionString": "Server=localhost,1433;Initial Catalog=master;User Id=sa;Password=Rap45tro2;" + "ConnectionString": "Server=localhost;Initial Catalog=master;User Id=sa;Password=Rap45tro2;" }, "TokenConfig": { -- 2.20.1 From b9dd32ee4d7c64cd49709934796adc9226a3f928 Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Fri, 27 Dec 2024 11:06:14 +0100 Subject: [PATCH 3/6] zmiany --- Authentication/AuthenticationSettings.cs | 4 +- Controllers/PdfController.cs | 55 +++++++++++-------- Controllers/WorkDayController.cs | 2 +- Models/CreateUserDto.cs | 8 +-- Models/DayDetailsDto.cs | 6 +-- Models/UpdatePasswordDto.cs | 8 +++ Program.cs | 1 + Services/UserService.cs | 7 +++ TestClass.cs | 67 ++++++++++++++++++++++++ nHIbernate/PdfData.cs | 19 +++++++ 10 files changed, 146 insertions(+), 31 deletions(-) create mode 100644 Models/UpdatePasswordDto.cs diff --git a/Authentication/AuthenticationSettings.cs b/Authentication/AuthenticationSettings.cs index cd15356..36bd3a2 100644 --- a/Authentication/AuthenticationSettings.cs +++ b/Authentication/AuthenticationSettings.cs @@ -2,8 +2,8 @@ { public class AuthenticationSettings { - public string JwtSecKey { get; set; } + public string JwtSecKey { get; set; } public int JwtExpireDays { get; set; } - public string JwtIssuer { get; set; } + public string JwtIssuer { get; set; } } } diff --git a/Controllers/PdfController.cs b/Controllers/PdfController.cs index bfb24fa..f054a4a 100644 --- a/Controllers/PdfController.cs +++ b/Controllers/PdfController.cs @@ -20,11 +20,13 @@ namespace FirmTracker_Server.Controllers { private readonly IExpenseRepository _expenseRepository; private readonly ITransactionRepository _transactionRepository; + private readonly IProductRepository _productRepository; - public PdfController(IExpenseRepository expenseRepository, ITransactionRepository transactionRepository) + public PdfController(IExpenseRepository expenseRepository, ITransactionRepository transactionRepository, IProductRepository productRepository) { _expenseRepository = expenseRepository; _transactionRepository = transactionRepository; + _productRepository = productRepository; } [HttpGet("download")] @@ -112,8 +114,9 @@ namespace FirmTracker_Server.Controllers // Main header page.Header() .Text("Raport transakcji") - .FontSize(20) + .FontSize(22) .SemiBold() + .FontColor(Colors.Blue.Medium) .AlignCenter(); // Summary section @@ -122,16 +125,18 @@ namespace FirmTracker_Server.Controllers column.Spacing(10); column.Item().Text($"Transakcje od ({startDate:yyyy-MM-dd} do {endDate:yyyy-MM-dd})") - .FontSize(16).Underline(); + .FontSize(16) + .Underline() + .FontColor(Colors.Grey.Medium); // Add table header column.Item().Row(row => { - row.RelativeItem().Text("Data").SemiBold(); - row.RelativeItem().Text("Typ płatności").SemiBold(); - row.RelativeItem().Text("Kwota razem").SemiBold(); - row.RelativeItem().Text("Rabat").SemiBold(); - row.RelativeItem().Text("Opis").SemiBold(); + row.RelativeItem().Text("Data").SemiBold().FontColor(Colors.Blue.Darken1); + row.RelativeItem().Text("Typ płatności").SemiBold().FontColor(Colors.Blue.Darken1); + row.RelativeItem().Text("Kwota razem").SemiBold().FontColor(Colors.Blue.Darken1); + row.RelativeItem().Text("Rabat").SemiBold().FontColor(Colors.Blue.Darken1); + row.RelativeItem().Text("Opis").SemiBold().FontColor(Colors.Blue.Darken1); }); // Populate table rows with transaction data @@ -153,13 +158,15 @@ namespace FirmTracker_Server.Controllers if (products.Any()) { - column.Item().Text("Produkty:").SemiBold(); + column.Item().Text("Produkty:").SemiBold().FontColor(Colors.Blue.Medium); foreach (var product in products) { + var productQuery = _productRepository.GetProduct(product.Id); column.Item().Row(productRow => { - productRow.RelativeItem().Text($"Nazwa produktu: {product.ProductName}"); + productRow.RelativeItem().Text($"Nazwa produktu: {productQuery.Name}"); productRow.RelativeItem().Text($"Ilość: {product.Quantity}"); + productRow.RelativeItem().Text($"Cena 1 szt. bez rabatu: {productQuery.Price.ToString("F2")}"); }); } } @@ -171,8 +178,8 @@ namespace FirmTracker_Server.Controllers .AlignCenter() .Text(text => { - text.Span("Wygenerowano przez automat FT: "); - text.Span(DateTime.Now.ToString("yyyy-MM-dd")).SemiBold(); + text.Span("Wygenerowano przez automat FT: ").FontColor(Colors.Grey.Medium); + text.Span(DateTime.Now.ToString("yyyy-MM-dd")).SemiBold().FontColor(Colors.Grey.Medium); }); }); }).GeneratePdf(ms); @@ -200,8 +207,9 @@ namespace FirmTracker_Server.Controllers // Main header page.Header() .Text("Raport wydatków") - .FontSize(20) + .FontSize(22) .SemiBold() + .FontColor(Colors.Green.Medium) .AlignCenter(); // Summary section @@ -211,18 +219,20 @@ namespace FirmTracker_Server.Controllers column.Item().Row(row => { - row.RelativeItem().Text($"Łączne wydatki: {totalExpenses:C}").FontSize(14).Bold(); - row.RelativeItem().Text($"Średnie wydatki dzienne: {averageExpense:C}").FontSize(14).Bold(); + row.RelativeItem().Text($"Łączne wydatki: {totalExpenses:C}").FontSize(14).Bold().FontColor(Colors.Green.Darken1); + row.RelativeItem().Text($"Średnie wydatki dzienne: {averageExpense:C}").FontSize(14).Bold().FontColor(Colors.Green.Darken1); }); column.Item().Text($"Szczegóły wydatków od ({startDate:yyyy-MM-dd} do {endDate:yyyy-MM-dd})") - .FontSize(16).Underline(); + .FontSize(16) + .Underline() + .FontColor(Colors.Grey.Medium); column.Item().Row(row => { - row.RelativeItem().Text("Data").SemiBold(); - row.RelativeItem().Text("Kwota").SemiBold(); - row.RelativeItem().Text("Opis").SemiBold(); + row.RelativeItem().Text("Data").SemiBold().FontColor(Colors.Green.Darken1); + row.RelativeItem().Text("Kwota").SemiBold().FontColor(Colors.Green.Darken1); + row.RelativeItem().Text("Opis").SemiBold().FontColor(Colors.Green.Darken1); }); foreach (var expense in expenses) @@ -236,12 +246,13 @@ namespace FirmTracker_Server.Controllers } }); + // Footer with generation date page.Footer() .AlignCenter() .Text(text => { - text.Span("Wygenerowano przez automat FT: "); - text.Span(DateTime.Now.ToString("yyyy-MM-dd")).SemiBold(); + text.Span("Wygenerowano przez automat FT: ").FontColor(Colors.Grey.Medium); + text.Span(DateTime.Now.ToString("yyyy-MM-dd")).SemiBold().FontColor(Colors.Grey.Medium); }); }); }).GeneratePdf(ms); @@ -249,5 +260,7 @@ namespace FirmTracker_Server.Controllers return ms.ToArray(); } } + + } } diff --git a/Controllers/WorkDayController.cs b/Controllers/WorkDayController.cs index f233454..9ed9a42 100644 --- a/Controllers/WorkDayController.cs +++ b/Controllers/WorkDayController.cs @@ -147,7 +147,7 @@ namespace FirmTracker_Server.Controllers { try { - var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ; + var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; var dayDetails = _workdayCRUD.GetDayDetailsForLoggedUser(int.Parse(userId), date); return Ok(dayDetails); diff --git a/Models/CreateUserDto.cs b/Models/CreateUserDto.cs index b282e21..41895b9 100644 --- a/Models/CreateUserDto.cs +++ b/Models/CreateUserDto.cs @@ -2,10 +2,10 @@ { public class CreateUserDto { - public string Login { get; set; } - public string Password { get; set; } - public string Email { get; set; } - public string Role { get; set; } + public required string Login { get; set; } + public required string Password { get; set; } + public required string Email { get; set; } + public required string Role { get; set; } public bool NewEncryption { get; set; } = true; } } diff --git a/Models/DayDetailsDto.cs b/Models/DayDetailsDto.cs index 09f1a01..bad9080 100644 --- a/Models/DayDetailsDto.cs +++ b/Models/DayDetailsDto.cs @@ -4,9 +4,9 @@ namespace FirmTracker_Server.Models { public class DayDetailsDto { - public string Email { get; set; } + public required string Email { get; set; } public DateTime Date { get; set; } - public string TotalWorkedHours { get; set; } - public List WorkdayDetails { get; set; } + public required string TotalWorkedHours { get; set; } + public required List WorkdayDetails { get; set; } } } diff --git a/Models/UpdatePasswordDto.cs b/Models/UpdatePasswordDto.cs new file mode 100644 index 0000000..251b9dd --- /dev/null +++ b/Models/UpdatePasswordDto.cs @@ -0,0 +1,8 @@ +namespace FirmTracker_Server.Models +{ + public class UpdatePasswordDto + { + public string Email { get; set; } + public string Password { get; set; } + } +} diff --git a/Program.cs b/Program.cs index 355a24c..3d36ac7 100644 --- a/Program.cs +++ b/Program.cs @@ -179,6 +179,7 @@ namespace FirmTracker_Server services.AddScoped, PasswordHasher>(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); // services.AddScoped(); services.AddMvc(); } diff --git a/Services/UserService.cs b/Services/UserService.cs index 0957662..8e9d3b9 100644 --- a/Services/UserService.cs +++ b/Services/UserService.cs @@ -15,6 +15,8 @@ using szyfrowanie; using FirmTracker_Server.nHibernate; using NHibernate; using NHibernate.Criterion; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using NHibernate.Type; namespace FirmTracker_Server.Services { @@ -24,6 +26,7 @@ namespace FirmTracker_Server.Services int AddUser(CreateUserDto dto); string CreateTokenJwt(LoginDto dto); IEnumerable GetAllUserEmails(); + bool UpdatePassword(UpdatePasswordDto dto); } public class UserService : IUserService @@ -44,6 +47,10 @@ namespace FirmTracker_Server.Services SimplerAES = new SimplerAES(); //SessionFactory = sessionFactory; } + public bool UpdatePassword(UpdatePasswordDto dto) + { + return true; + } public IEnumerable GetAllUserEmails() { using (var session = SessionFactory.OpenSession()) diff --git a/TestClass.cs b/TestClass.cs index 9797a9d..761d907 100644 --- a/TestClass.cs +++ b/TestClass.cs @@ -237,6 +237,73 @@ namespace FirmTracker_Server expenseCrud.AddExpense(expense1); expenseCrud.AddExpense(expense2); expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + expenseCrud.AddExpense(expense3); + List testTransactionProducts = new List { new TransactionProduct { ProductID =17, Quantity = 3 }, diff --git a/nHIbernate/PdfData.cs b/nHIbernate/PdfData.cs index 5792f4b..ee02a54 100644 --- a/nHIbernate/PdfData.cs +++ b/nHIbernate/PdfData.cs @@ -1,11 +1,19 @@ using System.Collections.Generic; using System.Linq; +using System.Transactions; using FirmTracker_Server.nHibernate.Expenses; +using FirmTracker_Server.nHibernate.Products; using FirmTracker_Server.nHibernate.Transactions; using NHibernate; +using Transaction = FirmTracker_Server.nHibernate.Transactions.Transaction; namespace FirmTracker_Server.nHibernate { + public interface IProductRepository + { + Product GetProduct(int id); + } + public interface IExpenseRepository { List GetAllExpenses(); @@ -25,6 +33,17 @@ namespace FirmTracker_Server.nHibernate void DeleteTransaction(int transactionId); List GetTransactionProductsForTransactions(List transactionIds); } + public class ProductRepository : IProductRepository + { + public Product GetProduct(int id) + { + using (var session = SessionFactory.OpenSession()) + { + return session.Get(id); + } + } + } + public class TransactionRepository : ITransactionRepository { // Retrieve all transactions -- 2.20.1 From 30378d95abf1401bc293d202afe55a0a10b3d7f6 Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Thu, 2 Jan 2025 22:48:51 +0100 Subject: [PATCH 4/6] update --- Controllers/PdfController.cs | 19 +++++++- Controllers/UserController.cs | 70 +++++++++++++++++++++++++-- Controllers/WorkDayController.cs | 17 ++++++- FirmTracker-Server.csproj | 6 --- Models/AddAbsenceDtocs.cs | 18 ++++++- Models/ChangeUserPasswordDto.cs | 24 ++++++++++ Models/CreateUserDto.cs | 18 ++++++- Models/DayDetailsDto.cs | 18 ++++++- Models/DayDetailsLoggedUserDto.cs | 18 ++++++- Models/EmployeeDto.cs | 18 ++++++- Models/LoginDtocs.cs | 18 ++++++- Models/UpdateAbsenceDto.cs | 18 ++++++- Models/UpdatePasswordDto.cs | 25 ++++++++-- Models/UserDto.cs | 18 ++++++- Models/Workday.cs | 18 ++++++- Services/UserService.cs | 80 +++++++++++++++++++++++++------ TestClass.cs | 71 ++------------------------- 17 files changed, 369 insertions(+), 105 deletions(-) create mode 100644 Models/ChangeUserPasswordDto.cs diff --git a/Controllers/PdfController.cs b/Controllers/PdfController.cs index f054a4a..c823329 100644 --- a/Controllers/PdfController.cs +++ b/Controllers/PdfController.cs @@ -1,4 +1,21 @@ -using System; +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ + +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index bc4ef8b..c61879f 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -1,4 +1,20 @@ -using FirmTracker_Server.Models; +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +using FirmTracker_Server.Models; using FirmTracker_Server.Services; using FirmTracker_Server; using Microsoft.AspNetCore.Authorization; @@ -6,11 +22,14 @@ using Microsoft.AspNetCore.Mvc; using FirmTracker_Server.Entities; using System.Security.Claims; +using System.Security.Cryptography; +using System.Text; + namespace FirmTracker_Server.Controllers { [Route("api/user")] [ApiController] - [Authorize] + //[Authorize] public class UserController : ControllerBase { private readonly IUserService UserService; @@ -21,7 +40,7 @@ namespace FirmTracker_Server.Controllers } [HttpPost("create")] - [Authorize(Roles = Roles.Admin)] + //[Authorize(Roles = Roles.Admin)] public ActionResult CreateUser([FromBody] CreateUserDto dto) { if (!ModelState.IsValid) @@ -62,6 +81,51 @@ namespace FirmTracker_Server.Controllers return Ok(emails); } + [HttpPost("ChangeUserPassword")] + [Authorize(Roles = Roles.Admin)] + public ActionResult ChangeUserPassword([FromBody] ChangeUserPasswordDto dto) + { + try + { + var result = UserService.ChangeUserPassword(dto); + if (result) + { + return Ok("Password changed successfully."); + } + else + { + return BadRequest("Failed to change the password."); + } + } + catch (Exception ex) + { + return BadRequest($"An error occurred: {ex.Message}"); + } + } + [HttpPost("changePassword")] + [Authorize(Roles = Roles.Admin + "," + Roles.User)] + public ActionResult ChangePassword([FromBody] UpdatePasswordDto dto) + { + try + { + var result = UserService.UpdatePassword(dto); + if (result) + { + var loginDto = new LoginDto { Email = dto.email, Password = dto.newPassword }; + var token = UserService.CreateTokenJwt(loginDto); + return Ok(new { Token = token }); + } + else + { + return BadRequest("Failed to change the password."); + } + } + catch (Exception ex) + { + return BadRequest($"An error occurred: {ex.Message}"); + } + } + // New method to get all users /* [HttpGet("all")] [AllowAnonymous] diff --git a/Controllers/WorkDayController.cs b/Controllers/WorkDayController.cs index 9ed9a42..737d74d 100644 --- a/Controllers/WorkDayController.cs +++ b/Controllers/WorkDayController.cs @@ -76,8 +76,23 @@ namespace FirmTracker_Server.Controllers } } + [HttpGet("user/workdays")] + [Authorize(Roles = Roles.Admin + "," + Roles.User)] + public IActionResult GetWorkdaysLoggedUser() + { + try + { + var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; + + var workdays = _workdayCRUD.GetWorkdaysByLoggedUser(userId); + return Ok(workdays); + } + catch (Exception ex) + { + return BadRequest(new { message = "An error occurred while fetching workdays.", error = ex.Message }); + } + } - // Endpoint to get all workdays for a user [HttpGet("user/{userMail}/workdays")] [Authorize(Roles = Roles.Admin + "," + Roles.User)] diff --git a/FirmTracker-Server.csproj b/FirmTracker-Server.csproj index 3ec9a97..c4c466e 100644 --- a/FirmTracker-Server.csproj +++ b/FirmTracker-Server.csproj @@ -35,12 +35,6 @@ - - - ./szyfrowanie.dll - - - True diff --git a/Models/AddAbsenceDtocs.cs b/Models/AddAbsenceDtocs.cs index ba80d3d..0a00b16 100644 --- a/Models/AddAbsenceDtocs.cs +++ b/Models/AddAbsenceDtocs.cs @@ -1,4 +1,20 @@ -namespace FirmTracker_Server.Models +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +namespace FirmTracker_Server.Models { public class AddAbsenceDto { diff --git a/Models/ChangeUserPasswordDto.cs b/Models/ChangeUserPasswordDto.cs new file mode 100644 index 0000000..b465eec --- /dev/null +++ b/Models/ChangeUserPasswordDto.cs @@ -0,0 +1,24 @@ +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +namespace FirmTracker_Server.Models +{ + public class ChangeUserPasswordDto + { + public string email { get; set; } + public string password { get; set; } + } +} diff --git a/Models/CreateUserDto.cs b/Models/CreateUserDto.cs index 41895b9..68bb2be 100644 --- a/Models/CreateUserDto.cs +++ b/Models/CreateUserDto.cs @@ -1,4 +1,20 @@ -namespace FirmTracker_Server.Models +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +namespace FirmTracker_Server.Models { public class CreateUserDto { diff --git a/Models/DayDetailsDto.cs b/Models/DayDetailsDto.cs index bad9080..11aaabf 100644 --- a/Models/DayDetailsDto.cs +++ b/Models/DayDetailsDto.cs @@ -1,4 +1,20 @@ -using FirmTracker_Server.nHibernate; +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +using FirmTracker_Server.nHibernate; namespace FirmTracker_Server.Models { diff --git a/Models/DayDetailsLoggedUserDto.cs b/Models/DayDetailsLoggedUserDto.cs index 77f3293..45b5f53 100644 --- a/Models/DayDetailsLoggedUserDto.cs +++ b/Models/DayDetailsLoggedUserDto.cs @@ -1,4 +1,20 @@ -using FirmTracker_Server.nHibernate; +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +using FirmTracker_Server.nHibernate; namespace FirmTracker_Server.Models { diff --git a/Models/EmployeeDto.cs b/Models/EmployeeDto.cs index 7a30e5b..08a7e31 100644 --- a/Models/EmployeeDto.cs +++ b/Models/EmployeeDto.cs @@ -1,4 +1,20 @@ -using FirmTracker_Server.Controllers; +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +using FirmTracker_Server.Controllers; namespace FirmTracker_Server.Models { diff --git a/Models/LoginDtocs.cs b/Models/LoginDtocs.cs index 65628d7..0473da4 100644 --- a/Models/LoginDtocs.cs +++ b/Models/LoginDtocs.cs @@ -1,4 +1,20 @@ -namespace FirmTracker_Server.Models +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +namespace FirmTracker_Server.Models { public class LoginDto { diff --git a/Models/UpdateAbsenceDto.cs b/Models/UpdateAbsenceDto.cs index db32c06..4d65372 100644 --- a/Models/UpdateAbsenceDto.cs +++ b/Models/UpdateAbsenceDto.cs @@ -1,4 +1,20 @@ -namespace FirmTracker_Server.Models +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +namespace FirmTracker_Server.Models { public class UpdateAbsenceDto { diff --git a/Models/UpdatePasswordDto.cs b/Models/UpdatePasswordDto.cs index 251b9dd..9a36f0a 100644 --- a/Models/UpdatePasswordDto.cs +++ b/Models/UpdatePasswordDto.cs @@ -1,8 +1,27 @@ -namespace FirmTracker_Server.Models +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +namespace FirmTracker_Server.Models { public class UpdatePasswordDto { - public string Email { get; set; } - public string Password { get; set; } + public string email { get; set; } + public string oldPassword { get; set; } + public string newPassword { get; set; } + + } } diff --git a/Models/UserDto.cs b/Models/UserDto.cs index da941b6..388a624 100644 --- a/Models/UserDto.cs +++ b/Models/UserDto.cs @@ -1,4 +1,20 @@ -using System.ComponentModel.DataAnnotations; +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +using System.ComponentModel.DataAnnotations; namespace FirmTracker_Server.Models { diff --git a/Models/Workday.cs b/Models/Workday.cs index 31313b8..5f0126c 100644 --- a/Models/Workday.cs +++ b/Models/Workday.cs @@ -1,4 +1,20 @@ -using FirmTracker_Server.Entities; +/* + * This file is part of FirmTracker - Server. + * + * FirmTracker - Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FirmTracker - Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FirmTracker - Server. If not, see . + */ +using FirmTracker_Server.Entities; using System; namespace YourNamespace.Models diff --git a/Services/UserService.cs b/Services/UserService.cs index 8e9d3b9..04cc88f 100644 --- a/Services/UserService.cs +++ b/Services/UserService.cs @@ -3,15 +3,12 @@ using FirmTracker_Server.Authentication; using FirmTracker_Server.Entities; using FirmTracker_Server.Exceptions; using FirmTracker_Server.Models; -using FirmTracker_Server.Authentication; -using FirmTracker_Server.Exceptions; using Microsoft.AspNetCore.Identity; using Microsoft.IdentityModel.Tokens; using System.Globalization; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; -using szyfrowanie; using FirmTracker_Server.nHibernate; using NHibernate; using NHibernate.Criterion; @@ -27,29 +24,84 @@ namespace FirmTracker_Server.Services string CreateTokenJwt(LoginDto dto); IEnumerable GetAllUserEmails(); bool UpdatePassword(UpdatePasswordDto dto); + bool ChangeUserPassword(ChangeUserPasswordDto dto); } public class UserService : IUserService { - // private readonly GeneralDbContext DbContext; + // private readonly GeneralDbContext DbContext; private readonly IMapper Mapper; private readonly IPasswordHasher PasswordHasher; private readonly AuthenticationSettings AuthenticationSettings; - private readonly SimplerAES SimplerAES; + // private readonly SimplerAES SimplerAES; //private readonly SessionFactory sessionFactory; - public UserService( IMapper mapper, IPasswordHasher passwordHasher, AuthenticationSettings authenticationSettings) + public UserService(IMapper mapper, IPasswordHasher passwordHasher, AuthenticationSettings authenticationSettings) { - // DbContext = dbContext; + // DbContext = dbContext; Mapper = mapper; PasswordHasher = passwordHasher; AuthenticationSettings = authenticationSettings; - SimplerAES = new SimplerAES(); + ///SimplerAES = new SimplerAES(); //SessionFactory = sessionFactory; } + public bool ChangeUserPassword(ChangeUserPasswordDto dto) + { + using (var session = SessionFactory.OpenSession()) + using (var transaction = session.BeginTransaction()) + { + try + { + var user = session.Query().FirstOrDefault(u => u.Email == dto.email); + if (user == null) + { + throw new Exception("User not found."); + } + + user.PassHash = PasswordHasher.HashPassword(user, dto.password); + session.Update(user); + transaction.Commit(); + + return true; + } + catch + { + transaction.Rollback(); + throw; + } + } + } public bool UpdatePassword(UpdatePasswordDto dto) { - return true; + using (var session = SessionFactory.OpenSession()) + using (var transaction = session.BeginTransaction()) + { + try + { + var user = session.Query().FirstOrDefault(u => u.Email == dto.email); + if (user == null) + { + throw new Exception("User not found."); + } + + var result = PasswordHasher.VerifyHashedPassword(user, user.PassHash, dto.oldPassword); + if (result != PasswordVerificationResult.Success) + { + throw new Exception("Invalid current password."); + } + + user.PassHash = PasswordHasher.HashPassword(user, dto.newPassword); + session.Update(user); + transaction.Commit(); + + return true; + } + catch + { + transaction.Rollback(); + throw; + } + } } public IEnumerable GetAllUserEmails() { @@ -74,7 +126,7 @@ namespace FirmTracker_Server.Services var user = Mapper.Map(dto); // Encrypt or hash the password based on NewEncryption flag - user.PassHash = dto.NewEncryption ? SimplerAES.Encrypt(dto.Password) : PasswordHasher.HashPassword(user, dto.Password); + user.PassHash = dto.NewEncryption ? PasswordHasher.HashPassword(user, dto.Password) : PasswordHasher.HashPassword(user, dto.Password); user.Role = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(dto.Role.ToLower()); using (var session = SessionFactory.OpenSession()) @@ -119,9 +171,9 @@ namespace FirmTracker_Server.Services { try { - Console.WriteLine(SimplerAES.Decrypt(user.PassHash)+" "+SimplerAES.Decrypt(dto.Password)); - var ready = SimplerAES.Decrypt(user.PassHash) == SimplerAES.Decrypt(dto.Password); - if (!ready) + Console.WriteLine(PasswordHasher.HashPassword(user, user.PassHash)); + var ready = PasswordHasher.VerifyHashedPassword(user, user.PassHash, dto.Password); + if (ready == 0) { throw new WrongUserOrPasswordException("Nieprawidłowy login lub hasło."); } @@ -134,7 +186,7 @@ namespace FirmTracker_Server.Services else { var ready = PasswordVerificationResult.Failed; - if (SimplerAES.Decrypt(user.PassHash) == SimplerAES.Decrypt(dto.Password)) { ready = PasswordVerificationResult.Success; } //PasswordHasher.VerifyHashedPassword(user, user.PassHash, dto.Password); + if (PasswordHasher.VerifyHashedPassword(user, user.PassHash, dto.Password) == PasswordVerificationResult.Success) { ready = PasswordVerificationResult.Success; } //PasswordHasher.VerifyHashedPassword(user, user.PassHash, dto.Password); if (ready == PasswordVerificationResult.Failed) { throw new WrongUserOrPasswordException("Nieprawidłowy login lub hasło."); diff --git a/TestClass.cs b/TestClass.cs index 761d907..fbe9a25 100644 --- a/TestClass.cs +++ b/TestClass.cs @@ -186,8 +186,8 @@ namespace FirmTracker_Server //SessionFactory.Init(connectionString); - string queryUser = "insert into Users(Email,PassHash,Role) select '123@wp.pl', 'GOsGemJarMJu8btZKF6Rung27JLZkdO7Wfd4CwLhL1k=','User'"; - string queryAdmin = "insert into Users(Email,PassHash,Role) select '321@wp.pl', 'GOsGemJarMJu8btZKF6Rung27JLZkdO7Wfd4CwLhL1k=','Admin'"; + string queryUser = "insert into Users(Email,PassHash,Role) select '123@wp.pl', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','User'"; + string queryAdmin = "insert into Users(Email,PassHash,Role) select '321@wp.pl', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','Admin'"; SqlConnection connection = new SqlConnection(connectionString); @@ -237,72 +237,7 @@ namespace FirmTracker_Server expenseCrud.AddExpense(expense1); expenseCrud.AddExpense(expense2); expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); - expenseCrud.AddExpense(expense3); + List testTransactionProducts = new List { -- 2.20.1 From 567c3539ad731e8b9065c6fbc03a55f06a9d6e8f Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Thu, 2 Jan 2025 22:57:45 +0100 Subject: [PATCH 5/6] poprawka - autoryzacja w controllerze usera --- Controllers/UserController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index c61879f..42b592b 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -29,7 +29,7 @@ namespace FirmTracker_Server.Controllers { [Route("api/user")] [ApiController] - //[Authorize] + [Authorize] public class UserController : ControllerBase { private readonly IUserService UserService; @@ -40,7 +40,7 @@ namespace FirmTracker_Server.Controllers } [HttpPost("create")] - //[Authorize(Roles = Roles.Admin)] + [Authorize(Roles = Roles.Admin)] public ActionResult CreateUser([FromBody] CreateUserDto dto) { if (!ModelState.IsValid) -- 2.20.1 From 490f915ec7f87c9e9268eac56427354229b4be02 Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Fri, 3 Jan 2025 09:17:08 +0100 Subject: [PATCH 6/6] dodanie testowe --- Controllers/ProductController.cs | 4 +- Controllers/WorkDayController.cs | 2 - Models/AddAbsenceDtocs.cs | 2 +- TestClass.cs | 429 ++++++++++++++++++++++++------- 4 files changed, 346 insertions(+), 91 deletions(-) diff --git a/Controllers/ProductController.cs b/Controllers/ProductController.cs index 58f85f8..5209df7 100644 --- a/Controllers/ProductController.cs +++ b/Controllers/ProductController.cs @@ -42,7 +42,7 @@ namespace FirmTracker_Server.Controllers [HttpPost] [ProducesResponseType(200)] // Created [ProducesResponseType(400)] // Bad Request - [Authorize(Roles = Roles.Admin)] + [Authorize(Roles = Roles.Admin + "," + Roles.User)] public IActionResult CreateProduct([FromBody] Product product) { try @@ -153,7 +153,7 @@ namespace FirmTracker_Server.Controllers [HttpDelete("{id}")] [ProducesResponseType(200)] // Created [ProducesResponseType(400)] // Bad Request - [Authorize(Roles = Roles.Admin)] + [Authorize(Roles = Roles.Admin + "," + Roles.User)] public IActionResult DeleteProduct(int id) { try diff --git a/Controllers/WorkDayController.cs b/Controllers/WorkDayController.cs index 737d74d..301fb52 100644 --- a/Controllers/WorkDayController.cs +++ b/Controllers/WorkDayController.cs @@ -119,7 +119,6 @@ namespace FirmTracker_Server.Controllers return BadRequest(new { message = "User email must be provided." }); } - // Fetch the userId based on the provided email int userId; using (var session = SessionFactory.OpenSession()) { @@ -131,7 +130,6 @@ namespace FirmTracker_Server.Controllers userId = user.UserId; } - // Add the absence for the retrieved userId _workdayCRUD.AddAbsence(userId, dto.AbsenceType, dto.StartTime, dto.EndTime); return Ok(new { status = "added", userId, dto.userEmail, absenceType = dto.AbsenceType }); diff --git a/Models/AddAbsenceDtocs.cs b/Models/AddAbsenceDtocs.cs index 0a00b16..3058b23 100644 --- a/Models/AddAbsenceDtocs.cs +++ b/Models/AddAbsenceDtocs.cs @@ -19,7 +19,7 @@ namespace FirmTracker_Server.Models public class AddAbsenceDto { public string userEmail { get; set; } - public string AbsenceType { get; set; } // e.g., "Sick", "Vacation", etc. + public string AbsenceType { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } diff --git a/TestClass.cs b/TestClass.cs index fbe9a25..7a4f129 100644 --- a/TestClass.cs +++ b/TestClass.cs @@ -60,22 +60,22 @@ namespace FirmTracker_Server var products = new List { - CreateProduct("Tarta_truskawka", "produkt", 31.99m, 1, 10), - CreateProduct("Tarta_czekolada", "produkt", 30.99m, 1, 10), - CreateProduct("Tarta_agrest", "produkt", 32.90m, 1, 8), - CreateProduct("Tarta_pistacja", "produkt", 35.99m, 1, 12), - CreateProduct("Tarta_karmel", "produkt", 32.00m, 1, 12), - CreateProduct("Rolada_beza", "produkt", 21.00m, 1, 5), + CreateProduct("Tarta_truskawka", "produkt", 31.99m, 1, 20), + CreateProduct("Tarta_czekolada", "produkt", 30.99m, 1, 20), + CreateProduct("Tarta_agrest", "produkt", 32.90m, 1, 10), + CreateProduct("Tarta_pistacja", "produkt", 35.99m, 1, 15), + CreateProduct("Tarta_karmel", "produkt", 32.00m, 1, 15), + CreateProduct("Rolada_beza", "produkt", 21.00m, 1, 12), CreateProduct("Rolada_róża", "produkt", 21.90m, 1, 10), - CreateProduct("Kostka_truskawka", "produkt", 12.00m, 1, 11), - CreateProduct("Kostka_lemonCurd", "produkt", 13.99m, 1, 13), - CreateProduct("Kostka_hiszpańska", "produkt", 11.99m, 1, 8), - CreateProduct("Kostka_wiosenna", "produkt", 11.99m, 1, 5), - CreateProduct("Kostka_jabłka", "produkt", 12.00m, 1, 5), - CreateProduct("Kostka_porzeczka", "produkt", 12.99m, 1, 5), - CreateProduct("Kostka_królewska", "produkt", 13.50m, 1, 5), - CreateProduct("Kostka_czekolada", "produkt", 14.50m, 1, 10), - CreateProduct("Kostka_wiśnia", "produkt", 12.50m, 1, 5), + CreateProduct("Kostka_truskawka", "produkt", 12.00m, 1, 15), + CreateProduct("Kostka_lemonCurd", "produkt", 13.99m, 1, 15), + CreateProduct("Kostka_hiszpańska", "produkt", 11.99m, 1, 10), + CreateProduct("Kostka_wiosenna", "produkt", 11.99m, 1, 10), + CreateProduct("Kostka_jabłka", "produkt", 12.00m, 1, 15), + CreateProduct("Kostka_porzeczka", "produkt", 12.99m, 1, 10), + CreateProduct("Kostka_królewska", "produkt", 13.50m, 1, 20), + CreateProduct("Kostka_czekolada", "produkt", 14.50m, 1, 12), + CreateProduct("Kostka_wiśnia", "produkt", 12.50m, 1, 10), CreateProduct("Kostka_beza", "produkt", 13.50m, 1, 20), CreateProduct("Kostka_leśna", "produkt", 12.00m, 1, 20), CreateProduct("Kostka_kawowa", "produkt", 12.00m, 1, 10), @@ -116,38 +116,7 @@ namespace FirmTracker_Server }; - var transaction1 = new Transaction - { - Date = DateTime.Now.AddDays(-2), - Description = "zamówienie telefon", - Discount = 5, - EmployeeId = 1, - PaymentType = "Karta kredytowa", - }; - var transaction2 = new Transaction - { - Date = DateTime.Now.AddDays(-3), - Description = "sprzedaż - kasa", - Discount = 30, - EmployeeId = 2, - PaymentType = "Gotówka", - }; - var transaction3 = new Transaction - { - Date = DateTime.Now, - Description = "sprzedaż - kasa", - Discount = 15, - EmployeeId = 1, - PaymentType = "BLIK", - }; - var transaction4 = new Transaction - { - Date = DateTime.Now, - Description = "zamówienie", - Discount = 15, - EmployeeId = 1, - PaymentType = "BLIK", - }; + var expense1 = new Expense { @@ -186,10 +155,14 @@ namespace FirmTracker_Server //SessionFactory.Init(connectionString); - string queryUser = "insert into Users(Email,PassHash,Role) select '123@wp.pl', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','User'"; - string queryAdmin = "insert into Users(Email,PassHash,Role) select '321@wp.pl', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','Admin'"; + string queryAdmin = "insert into Users(Email,PassHash,Role) select 'julia.c03@wp.pl', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','Admin'"; + string queryUser = "insert into Users(Email,PassHash,Role) select 'sylwia1972@gmail.com', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','User'"; + string queryUser2 = "insert into Users(Email,PassHash,Role) select '123@wp.pl', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','User'"; + string queryUser3 = "insert into Users(Email,PassHash,Role) select '321@wp.pl', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','User'"; + string queryUser4 = "insert into Users(Email,PassHash,Role) select 'magdalena.szwarc75@wp.pl', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','User'"; + string queryUser5 = "insert into Users(Email,PassHash,Role) select 'jac.ziel@gmail.com', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','User'"; + string queryUser6 = "insert into Users(Email,PassHash,Role) select 'renata.zielonka@wp.com', 'AQAAAAIAAYagAAAAEMQUuFPUNAddMmuZpCUAZpaDR31+BqMJhnamIAllDi+aTBJQ7tEtLuEMppgz0oLYyw==','User'"; - SqlConnection connection = new SqlConnection(connectionString); connection.Open(); @@ -207,6 +180,44 @@ namespace FirmTracker_Server command2.ExecuteNonQuery(); connection2.Close(); + SqlConnection connection3 = new SqlConnection(connectionString); + connection.Open(); + + SqlCommand command3 = new SqlCommand(queryUser2, connection); + command2.CommandTimeout = 200; + command2.ExecuteNonQuery(); + connection2.Close(); + SqlConnection connection4 = new SqlConnection(connectionString); + connection.Open(); + + SqlCommand command4 = new SqlCommand(queryUser3, connection); + command2.CommandTimeout = 200; + command2.ExecuteNonQuery(); + connection2.Close(); + + SqlConnection connection5 = new SqlConnection(connectionString); + connection.Open(); + + SqlCommand command5 = new SqlCommand(queryUser4, connection); + command2.CommandTimeout = 200; + command2.ExecuteNonQuery(); + connection2.Close(); + + SqlConnection connection6 = new SqlConnection(connectionString); + connection.Open(); + + SqlCommand command6 = new SqlCommand(queryUser6, connection); + command2.CommandTimeout = 200; + command2.ExecuteNonQuery(); + connection2.Close(); + + SqlConnection connection7 = new SqlConnection(connectionString); + connection.Open(); + + SqlCommand command7 = new SqlCommand(queryUser5, connection); + command2.CommandTimeout = 200; + command2.ExecuteNonQuery(); + connection2.Close(); } } @@ -230,61 +241,307 @@ namespace FirmTracker_Server { productCrud.AddProduct(clientProduct); } - transactionCrud.AddTransaction(transaction1); + /*transactionCrud.AddTransaction(transaction1); transactionCrud.AddTransaction(transaction2); transactionCrud.AddTransaction(transaction3); transactionCrud.AddTransaction(transaction4); + transactionCrud.AddTransaction(transaction5); + transactionCrud.AddTransaction(transaction6); + transactionCrud.AddTransaction(transaction7); + transactionCrud.AddTransaction(transaction8); + transactionCrud.AddTransaction(transaction9); + transactionCrud.AddTransaction(transaction10); + transactionCrud.AddTransaction(transaction11); + transactionCrud.AddTransaction(transaction12); + transactionCrud.AddTransaction(transaction13); + transactionCrud.AddTransaction(transaction14); + transactionCrud.AddTransaction(transaction15); + transactionCrud.AddTransaction(transaction16); + transactionCrud.AddTransaction(transaction17); + transactionCrud.AddTransaction(transaction18); + transactionCrud.AddTransaction(transaction19); + transactionCrud.AddTransaction(transaction20);*/ expenseCrud.AddExpense(expense1); expenseCrud.AddExpense(expense2); expenseCrud.AddExpense(expense3); + var transactions = new List +{ + new Transaction + { + Date = DateTime.Now.AddDays(-1), + Description = "zamówienie", + Discount = 5, + EmployeeId = 1, + PaymentType = "Gotówka" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-2), + Description = "sprzedaż", + Discount = 10, + EmployeeId = 2, + PaymentType = "Karta kredytowa" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-3), + Description = "sprzedaż", + Discount = 15, + EmployeeId = 3, + PaymentType = "BLIK" + }, + new Transaction + { + Date = DateTime.Now, + Description = "sprzedaż", + Discount = 20, + EmployeeId = 4, + PaymentType = "Gotówka" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-5), + Description = "sprzedaż", + Discount = 8, + EmployeeId = 1, + PaymentType = "Gotówka" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-6), + Description = "na telefon", + Discount = 12, + EmployeeId = 2, + PaymentType = "Karta kredytowa" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-7), + Description = "sprzedaż", + Discount = 18, + EmployeeId = 3, + PaymentType = "BLIK" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-8), + Description = "rezerwacja", + Discount = 25, + EmployeeId = 4, + PaymentType = "Gotówka" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-10), + Description = "sprzedaż", + Discount = 9, + EmployeeId = 1, + PaymentType = "Gotówka" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-11), + Description = "zamówienie telefoniczne", + Discount = 14, + EmployeeId = 2, + PaymentType = "Karta kredytowa" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-12), + Description = "sprzedaż w punkcie", + Discount = 17, + EmployeeId = 3, + PaymentType = "BLIK" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-13), + Description = "zamówienie", + Discount = 22, + EmployeeId = 4, + PaymentType = "Gotówka" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-15), + Description = "sprzedaż", + Discount = 7, + EmployeeId = 1, + PaymentType = "Gotówka" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-16), + Description = "zamówienie", + Discount = 13, + EmployeeId = 2, + PaymentType = "Karta kredytowa" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-17), + Description = "sprzedaż", + Discount = 16, + EmployeeId = 3, + PaymentType = "BLIK" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-18), + Description = "na telefon", + Discount = 21, + EmployeeId = 4, + PaymentType = "Gotówka" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-20), + Description = "sprzedaż", + Discount = 10, + EmployeeId = 1, + PaymentType = "Gotówka" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-21), + Description = "zamówienie telefoniczne", + Discount = 12, + EmployeeId = 2, + PaymentType = "Karta kredytowa" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-22), + Description = "sprzedaż w punkcie", + Discount = 14, + EmployeeId = 3, + PaymentType = "BLIK" + }, + new Transaction + { + Date = DateTime.Now.AddDays(-23), + Description = "zamówienie online", + Discount = 18, + EmployeeId = 4, + PaymentType = "Gotówka" + } +}; - List testTransactionProducts = new List { - new TransactionProduct { ProductID =17, Quantity = 3 }, - new TransactionProduct { ProductID = 14, Quantity = 1 }, - new TransactionProduct { ProductID = 1, Quantity = 1 }, - }; - foreach (var transactionProduct in testTransactionProducts) + var transactionProducts = new List<(int TransactionIndex, int ProductID, int Quantity)> +{ + (0, 1, 1), // Transaction 1: Product 1 with quantity 1 + (0, 2, 3), // Transaction 1: Product 2 with quantity 3 + (1, 3, 4), // Transaction 2: Product 3 with quantity 4 + (1, 4, 2), // Transaction 2: Product 4 with quantity 2 + (2, 5, 3), // Transaction 3: Product 5 with quantity 3 + (2, 6, 1), // Transaction 3: Product 6 with quantity 1 + (3, 7, 5), // Transaction 4: Product 7 with quantity 5 + (3, 8, 2), // Transaction 4: Product 8 with quantity 2 + (4, 9, 3), // Transaction 5: Product 9 with quantity 3 + (4, 10, 2), // Transaction 5: Product 10 with quantity 2 + (5, 11, 4), // Transaction 6: Product 11 with quantity 4 + (5, 12, 1), // Transaction 6: Product 12 with quantity 1 + (6, 13, 3), // Transaction 7: Product 13 with quantity 3 + (6, 14, 2), // Transaction 7: Product 14 with quantity 2 + (7, 15, 5), // Transaction 8: Product 15 with quantity 5 + (7, 16, 2), // Transaction 8: Product 16 with quantity 2 + (8, 17, 3), // Transaction 9: Product 17 with quantity 3 + (8, 18, 4), // Transaction 9: Product 18 with quantity 4 + (9, 19, 2), // Transaction 10: Product 19 with quantity 2 + (9, 20, 3), // Transaction 10: Product 20 with quantity 3 + (10, 1, 1), // Transaction 11: Product 1 with quantity 1 + (10, 2, 5), // Transaction 11: Product 2 with quantity 5 + (11, 3, 2), // Transaction 12: Product 3 with quantity 2 + (11, 4, 3), // Transaction 12: Product 4 with quantity 3 + (12, 5, 1), // Transaction 13: Product 5 with quantity 1 + (12, 6, 4), // Transaction 13: Product 6 with quantity 4 + (13, 7, 2), // Transaction 14: Product 7 with quantity 2 + (13, 8, 3), // Transaction 14: Product 8 with quantity 3 + (14, 9, 3), // Transaction 15: Product 9 with quantity 3 + (14, 10, 1), // Transaction 15: Product 10 with quantity 1 + (15, 11, 2), // Transaction 16: Product 11 with quantity 2 + (15, 12, 3), // Transaction 16: Product 12 with quantity 3 + (16, 13, 3), // Transaction 17: Product 13 with quantity 3 + (16, 14, 1), // Transaction 17: Product 14 with quantity 1 + (17, 15, 4), // Transaction 18: Product 15 with quantity 4 + (17, 16, 1), // Transaction 18: Product 16 with quantity 1 + (18, 17, 2), // Transaction 19: Product 17 with quantity 2 + (18, 18, 3), // Transaction 19: Product 18 with quantity 3 + (19, 19, 1), // Transaction 20: Product 19 with quantity 1 + (19, 20, 2), // Transaction 20: Product 20 with quantity 2 +}; + + + // Add transactions + foreach (var transaction in transactions) { - transactionCrud.AddTransactionProductToTransaction(transaction1.Id, transactionProduct); - + transactionCrud.AddTransaction(transaction); } - List testTransactionProducts2 = new List + // Add transaction products + foreach (var transactionProduct in transactionProducts) { - new TransactionProduct { ProductID = 28, Quantity=5}, - new TransactionProduct { ProductID = 22, Quantity=5} - }; - foreach (var transactionProduct in testTransactionProducts2) - { - transactionCrud.AddTransactionProductToTransaction(transaction2.Id, transactionProduct); - + var transactionId = transactions[transactionProduct.TransactionIndex].Id; + transactionCrud.AddTransactionProductToTransaction( + transactionId, + new TransactionProduct + { + ProductID = transactionProduct.ProductID, + Quantity = transactionProduct.Quantity + } + ); } - List testTransactionProducts3 = new List - { - new TransactionProduct { ProductID = 3, Quantity=9}, - new TransactionProduct { ProductID = 2, Quantity=1} - }; - foreach (var transactionProduct in testTransactionProducts3) - { - transactionCrud.AddTransactionProductToTransaction(transaction3.Id, transactionProduct); - } - List testTransactionProducts4 = new List - { - new TransactionProduct { ProductID = 33, Quantity=12}, - new TransactionProduct { ProductID = 12, Quantity=1} - }; - foreach (var transactionProduct in testTransactionProducts4) - { - transactionCrud.AddTransactionProductToTransaction(transaction4.Id, transactionProduct); - } + /* List testTransactionProducts = new List { + new TransactionProduct { ProductID =17, Quantity = 3 }, + new TransactionProduct { ProductID = 14, Quantity = 1 }, + new TransactionProduct { ProductID = 1, Quantity = 1 }, + }; + foreach (var transactionProduct in testTransactionProducts) + { + transactionCrud.AddTransactionProductToTransaction(transaction1.Id, transactionProduct); + + } + + List testTransactionProducts2 = new List + { + new TransactionProduct { ProductID = 28, Quantity=5}, + new TransactionProduct { ProductID = 22, Quantity=5} + }; + foreach (var transactionProduct in testTransactionProducts2) + { + transactionCrud.AddTransactionProductToTransaction(transaction2.Id, transactionProduct); + + } + + List testTransactionProducts3 = new List + { + new TransactionProduct { ProductID = 3, Quantity=9}, + new TransactionProduct { ProductID = 2, Quantity=1} + }; + foreach (var transactionProduct in testTransactionProducts3) + { + transactionCrud.AddTransactionProductToTransaction(transaction3.Id, transactionProduct); + + } + List testTransactionProducts4 = new List + { + new TransactionProduct { ProductID = 33, Quantity=12}, + new TransactionProduct { ProductID = 12, Quantity=1} + }; + foreach (var transactionProduct in testTransactionProducts4) + { + transactionCrud.AddTransactionProductToTransaction(transaction4.Id, transactionProduct); + + }*/ } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine(ex.ToString()); throw; -- 2.20.1