Compare commits

...

1 Commits

9 changed files with 202 additions and 18 deletions

View File

@ -34,7 +34,7 @@ namespace FirmTracker_Server.Controllers
public WorkdayController() public WorkdayController()
{ {
_workdayCRUD = new WorkdayRepository(); _workdayCRUD = new WorkdayRepository();
} }
// Endpoint to start a workday // 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")] [HttpGet("user/{userMail}/workdays")]
[Authorize(Roles = Roles.Admin + "," + Roles.User)] [Authorize(Roles = Roles.Admin + "," + Roles.User)]
public IActionResult GetWorkdays(string userMail) 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 }); 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")] [HttpPost("absence/add")]
[Authorize(Roles = Roles.Admin + "," + Roles.User)] [Authorize(Roles = Roles.Admin)]
public IActionResult AddAbsence([FromBody] AddAbsenceDto dto) 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 try
{ {
@ -117,13 +165,13 @@ namespace FirmTracker_Server.Controllers
} }
// Add the absence for the retrieved userId // 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 }); return Ok(new { status = "added", userId, dto.userEmail, absenceType = dto.AbsenceType });
} }
catch (Exception ex) 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"); Console.WriteLine("uruchomiono swaggera");
app.UseHttpsRedirection(); app.UseHttpsRedirection();
} }
catch (Exception ex) catch
{ {
Console.WriteLine("Nie uda³o siê uruchomiæ swaggera"); 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.Entities;
using FirmTracker_Server.Exceptions; using FirmTracker_Server.Exceptions;
using FirmTracker_Server.Models; using FirmTracker_Server.Models;
using FirmTracker_Server.Authentication;
using FirmTracker_Server.Exceptions;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using System.Globalization; using System.Globalization;
@ -13,8 +11,7 @@ using System.Security.Claims;
using System.Text; using System.Text;
using szyfrowanie; using szyfrowanie;
using FirmTracker_Server.nHibernate; using FirmTracker_Server.nHibernate;
using NHibernate;
using NHibernate.Criterion;
namespace FirmTracker_Server.Services 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 try
{ {
string appDirectory = Directory.GetCurrentDirectory(); string appDirectory = Directory.GetCurrentDirectory();

View File

@ -1,6 +1,6 @@
{ {
"AppSettings": { "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": { "TokenConfig": {
@ -17,7 +17,14 @@
"applicationUrl": "http://localhost:5045" "applicationUrl": "http://localhost:5045"
}, },
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7039"
},
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",
"launchBrowser": true, "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/>. * along with FirmTracker - Server. If not, see <https://www.gnu.org/licenses/>.
*/ */
using FirmTracker_Server.nHIbernate;
using FluentNHibernate.Cfg; using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db; using FluentNHibernate.Cfg.Db;
using NHibernate; using NHibernate;
@ -56,7 +57,8 @@ namespace FirmTracker_Server.nHibernate
.AddFromAssemblyOf<Reports.ReportExpenseMapping>() .AddFromAssemblyOf<Reports.ReportExpenseMapping>()
.AddFromAssemblyOf<LogsMapping>() .AddFromAssemblyOf<LogsMapping>()
.AddFromAssemblyOf<UserMapping>() .AddFromAssemblyOf<UserMapping>()
.AddFromAssemblyOf<WorkdayMapping>(); .AddFromAssemblyOf<WorkdayMapping>()
.AddFromAssemblyOf<AbsenceMapping>();
}) })
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update .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.Entities;
using FirmTracker_Server.nHibernate; using FirmTracker_Server.nHibernate;
using static NHibernate.Engine.Query.CallableParser;
public class WorkdayRepository public class WorkdayRepository
{ {
@ -44,8 +45,29 @@ public class WorkdayRepository
} }
} }
} }
public void AddAbsence(string Absence)
public void AddAbsence(int userId, string absenceType, DateTime startTime, DateTime endTime) {
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 session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction()) using (var transaction = session.BeginTransaction())
@ -70,11 +92,13 @@ public class WorkdayRepository
catch (Exception ex) catch (Exception ex)
{ {
transaction.Rollback(); 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) public bool StopWorkday(int userId)
{ {
using (var session = SessionFactory.OpenSession()) 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) public List<Workday> GetWorkdaysByUser(string email)
{ {
using (var session = SessionFactory.OpenSession()) using (var session = SessionFactory.OpenSession())
@ -125,6 +170,49 @@ 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 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; return workdays;
} }
catch (Exception ex) catch (Exception ex)