dodanie absencji, zmiany w endpointach, mały refactor

This commit is contained in:
Maciej Maciejewski 2024-12-05 23:46:08 +01:00
parent d28a692fdd
commit 9efff4d2d0
9 changed files with 202 additions and 18 deletions

View File

@ -34,7 +34,7 @@ namespace FirmTracker_Server.Controllers
public WorkdayController()
{
_workdayCRUD = new WorkdayRepository();
_workdayCRUD = new WorkdayRepository();
}
// Endpoint to start a workday
@ -77,8 +77,24 @@ namespace FirmTracker_Server.Controllers
}
// Endpoint to get all workdays for a user
[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 });
}
}
[HttpGet("user/{userMail}/workdays")]
[Authorize(Roles = Roles.Admin + "," + Roles.User)]
public IActionResult GetWorkdays(string userMail)
@ -93,9 +109,41 @@ namespace FirmTracker_Server.Controllers
return BadRequest(new { message = "An error occurred while fetching workdays.", error = ex.Message });
}
}
[HttpGet("absences")]
[Authorize(Roles = Roles.Admin)]
public IActionResult GetAbsences()
{
try
{
var absences = _workdayCRUD.GetAbsences();
return Ok(absences);
}
catch (Exception ex)
{
return BadRequest(new { message = "An error occurred while fetching absences.", error = ex.Message });
}
}
[HttpPost("absence/add")]
[Authorize(Roles = Roles.Admin + "," + Roles.User)]
public IActionResult AddAbsence([FromBody] AddAbsenceDto dto)
[Authorize(Roles = Roles.Admin)]
public IActionResult AddAbsence([FromBody] string Absence)
{
try
{
_workdayCRUD.AddAbsence(Absence);
return Ok(new { status = "added", Absence});
}
catch (Exception ex)
{
return BadRequest(new { message = "An error occurred while adding the absence.", error = ex.Message });
}
}
[HttpPost("user/absence/add")]
[Authorize(Roles = Roles.Admin)]
public IActionResult AddUserAbsence([FromBody] AddAbsenceDto dto)
{
try
{
@ -117,13 +165,13 @@ namespace FirmTracker_Server.Controllers
}
// Add the absence for the retrieved userId
_workdayCRUD.AddAbsence(userId, dto.AbsenceType, dto.StartTime, dto.EndTime);
_workdayCRUD.AddAbsenceToUser(userId, dto.AbsenceType, dto.StartTime, dto.EndTime);
return Ok(new { status = "added", userId, dto.userEmail, absenceType = dto.AbsenceType });
}
catch (Exception ex)
{
return BadRequest(new { message = "An error occurred while adding the absence.", error = ex.Message });
return BadRequest(new { message = "An error occurred while adding the absence to user.", error = ex.Message });
}
}

View File

@ -121,7 +121,7 @@ namespace FirmTracker_Server
Console.WriteLine("uruchomiono swaggera");
app.UseHttpsRedirection();
}
catch (Exception ex)
catch
{
Console.WriteLine("Nie uda³o siê uruchomiæ swaggera");
}

View File

@ -3,8 +3,6 @@ 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;
@ -13,8 +11,7 @@ using System.Security.Claims;
using System.Text;
using szyfrowanie;
using FirmTracker_Server.nHibernate;
using NHibernate;
using NHibernate.Criterion;
namespace FirmTracker_Server.Services
{

View File

@ -169,6 +169,24 @@ namespace FirmTracker_Server
};
try
{
var nieobecnosc1 = "Urlop / Urlop na żądanie" ;
var nieobecnosc2 = "Zwolnienie chorobowe";
var nieobecnosc3 = "Szkolenie";
var nieobecnosc4 = "Przerwa prywatna";
WorkdayRepository work = new WorkdayRepository();
work.AddAbsence(nieobecnosc1);
work.AddAbsence(nieobecnosc2);
work.AddAbsence(nieobecnosc3);
work.AddAbsence(nieobecnosc4);
}
catch(Exception a)
{
Console.WriteLine("Nie udało się dodać typów nieobecności " + a.Message);
}
try
{
string appDirectory = Directory.GetCurrentDirectory();

View File

@ -1,6 +1,6 @@
{
"AppSettings": {
"ConnectionString": "Server=localhost,1433;Initial Catalog=master;User Id=sa;Password=Rap45tro2;"
"ConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;"
},
"TokenConfig": {
@ -17,7 +17,14 @@
"applicationUrl": "http://localhost:5045"
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7039"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,

10
nHIbernate/Absence.cs Normal file
View File

@ -0,0 +1,10 @@
using FirmTracker_Server.Entities;
namespace FirmTracker_Server.nHibernate
{
public class Absence
{
public virtual int Id { get; set; }
public virtual string AbsenceName { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using FirmTracker_Server.nHibernate;
using FluentNHibernate.Mapping;
namespace FirmTracker_Server.nHIbernate
{
public class AbsenceMapping : ClassMap<Absence>
{
public AbsenceMapping()
{
Table("Absences");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.AbsenceName);
}
}
}

View File

@ -15,6 +15,7 @@
* along with FirmTracker - Server. If not, see <https://www.gnu.org/licenses/>.
*/
using FirmTracker_Server.nHIbernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
@ -56,7 +57,8 @@ namespace FirmTracker_Server.nHibernate
.AddFromAssemblyOf<Reports.ReportExpenseMapping>()
.AddFromAssemblyOf<LogsMapping>()
.AddFromAssemblyOf<UserMapping>()
.AddFromAssemblyOf<WorkdayMapping>();
.AddFromAssemblyOf<WorkdayMapping>()
.AddFromAssemblyOf<AbsenceMapping>();
})
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update

View File

@ -1,5 +1,6 @@
using FirmTracker_Server.Entities;
using FirmTracker_Server.nHibernate;
using static NHibernate.Engine.Query.CallableParser;
public class WorkdayRepository
{
@ -44,8 +45,29 @@ public class WorkdayRepository
}
}
}
public void AddAbsence(int userId, string absenceType, DateTime startTime, DateTime endTime)
public void AddAbsence(string Absence)
{
using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
try
{
var absence = new Absence
{
AbsenceName = Absence,
};
session.Save(absence);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("An error occurred while adding the absence", ex);
}
}
}
public void AddAbsenceToUser(int userId, string absenceType, DateTime startTime, DateTime endTime)
{
using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
@ -70,11 +92,13 @@ public class WorkdayRepository
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("An error occurred while adding the absence", ex);
throw new Exception("An error occurred while adding the absence to user ", ex);
}
}
}
public bool StopWorkday(int userId)
{
using (var session = SessionFactory.OpenSession())
@ -106,7 +130,28 @@ public class WorkdayRepository
}
}
}
public List<Absence> GetAbsences()
{
using (var session = SessionFactory.OpenSession())
{
try
{
var absences = session.Query<Absence>()
.Select(w => new Absence
{
AbsenceName = w.AbsenceName
})
.ToList();
return absences;
}
catch (Exception ex)
{
throw new Exception("An error occurred while fetching workdays", ex);
}
}
}
public List<Workday> GetWorkdaysByUser(string email)
{
using (var session = SessionFactory.OpenSession())
@ -125,6 +170,49 @@ 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 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();
foreach (var workday in workdays)
{
if (workday.Absence != "")
{
workday.WorkedHours = TimeSpan.Zero;
}
}
return workdays;
}
catch (Exception ex)