Compare commits

..

2 Commits

Author SHA1 Message Date
ba4f8a717c aktualizacja appsettings 2024-12-12 20:20:13 +01:00
43da6a5d93 zzmiany 2024-12-12 19:48:08 +01:00
7 changed files with 189 additions and 4 deletions

View File

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

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

@ -1,11 +1,11 @@
{
"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": {
"JwtSecKey": "omgi5Rf4tqg351GQwefw1234567890123456",
"JwtExpireDays": 30,
"JwtExpireDays": 1,
"JwtIssuer": "http://api.graphcom.pl"
},
"profiles": {

View File

@ -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<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;
}
catch (Exception ex)

View File

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