This commit is contained in:
Maciej Maciejewski 2024-12-12 19:48:08 +01:00
parent 5e4a57cb12
commit 43da6a5d93
7 changed files with 188 additions and 3 deletions

View File

@ -63,6 +63,11 @@ namespace FirmTracker_Server.Controllers
{ {
throw new InvalidOperationException("Produkt nie może posiadać ujemnej ceny."); 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); _productCrud.AddProduct(product);
return CreatedAtAction("GetProduct", new { id = product.Id }, product); return CreatedAtAction("GetProduct", new { id = product.Id }, product);

View File

@ -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 });
}
}
} }
} }

12
Models/DayDetailsDto.cs Normal file
View File

@ -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<Workday> WorkdayDetails { get; set; }
}
}

View File

@ -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<Workday> WorkdayDetails { get; set; }
}
}

View File

@ -5,7 +5,7 @@
}, },
"TokenConfig": { "TokenConfig": {
"JwtSecKey": "omgi5Rf4tqg351GQwefw1234567890123456", "JwtSecKey": "omgi5Rf4tqg351GQwefw1234567890123456",
"JwtExpireDays": 30, "JwtExpireDays": 1,
"JwtIssuer": "http://api.graphcom.pl" "JwtIssuer": "http://api.graphcom.pl"
}, },
"profiles": { "profiles": {

View File

@ -1,5 +1,7 @@
using FirmTracker_Server.Entities; using FirmTracker_Server.Entities;
using FirmTracker_Server.nHibernate; using FirmTracker_Server.nHibernate;
using static NHibernate.Engine.Query.CallableParser;
using FirmTracker_Server.Models;
public class WorkdayRepository public class WorkdayRepository
{ {
@ -125,6 +127,131 @@ public class WorkdayRepository
}) })
.ToList(); .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<Workday>()
.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<Workday>()
.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<Workday> GetWorkdaysByLoggedUser(string userId)
{
using (var session = SessionFactory.OpenSession())
{
try
{
int parsedUserId = Int32.Parse(userId);
var workdays = session.Query<Workday>()
.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; return workdays;
} }
catch (Exception ex) catch (Exception ex)

View File

@ -253,7 +253,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
catch (Exception ex) catch (Exception ex)
{ {
transaction.Rollback(); transaction.Rollback();
throw ex; throw;
} }
} }
} }