From 43da6a5d93a56a029f6018d0ec451dc0e9e66531 Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Thu, 12 Dec 2024 19:48:08 +0100 Subject: [PATCH] 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; } } }