diff --git a/Controllers/WorkDayController.cs b/Controllers/WorkDayController.cs
index 1abfed7..4c4cc6e 100644
--- a/Controllers/WorkDayController.cs
+++ b/Controllers/WorkDayController.cs
@@ -1,55 +1,58 @@
-using Microsoft.AspNetCore.Mvc;
-using System;
-using System.Collections.Concurrent;
-using System.Linq;
+/*
+ * This file is part of FirmTracker - Server.
+ *
+ * FirmTracker - Server is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * FirmTracker - Server is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with FirmTracker - Server. If not, see .
+ */
-namespace YourNamespace.Controllers
+using FirmTracker_Server.nHibernate;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Security.Claims;
+
+namespace FirmTracker_Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
+ [Authorize]
public class WorkdayController : ControllerBase
{
- // In-memory storage for simplicity, where the key is the userId.
- private static readonly ConcurrentDictionary WorkStartTimes = new ConcurrentDictionary();
+ private readonly WorkdayRepository _workdayCRUD;
- // Get the current status of the user's workday (started or not)
- [HttpGet("status/{userId}")]
- public IActionResult GetWorkdayStatus(string userId)
+ public WorkdayController()
{
- if (WorkStartTimes.TryGetValue(userId, out DateTime? startTime))
+ _workdayCRUD = new WorkdayRepository(); // Instantiate directly (no DI in this example)
+ }
+
+ // Endpoint to start a workday
+ [HttpPost("start")]
+ [Authorize(Roles = Roles.Admin + "," + Roles.User)]
+ public IActionResult StartWorkday()
+ {
+ try
{
- if (startTime.HasValue)
- {
- return Ok(new { status = "started", startTime = startTime });
- }
- else
- {
- return Ok(new { status = "stopped" });
- }
+ var userIdString = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
+ int userId = int.Parse(userIdString);
+ _workdayCRUD.StartWorkday(userId);
+ return Ok(new { status = "started", userId });
}
- else
+ catch (Exception ex)
{
- return NotFound(new { message = "User not found" });
+ return BadRequest(new { message = "An error occurred while starting the workday.", error = ex.Message });
}
}
- // Start or stop the user's workday by toggling the start/stop state
- [HttpPost("toggle/{userId}")]
- public IActionResult ToggleWorkday(string userId)
- {
- // If the workday has already started, stop it, otherwise start it
- if (WorkStartTimes.ContainsKey(userId) && WorkStartTimes[userId].HasValue)
- {
- // Stop the workday
- WorkStartTimes[userId] = null;
- return Ok(new { status = "stopped" });
- }
- else
- {
- // Start the workday
- WorkStartTimes[userId] = DateTime.Now;
- return Ok(new { status = "started", startTime = WorkStartTimes[userId] });
- }
- }
+
}
}
diff --git a/Entities/User.cs b/Entities/User.cs
index 1282962..6e2619c 100644
--- a/Entities/User.cs
+++ b/Entities/User.cs
@@ -2,7 +2,7 @@
{
public class User
{
- public virtual int Id { get; set; }
+ public virtual int UserId { get; set; }
public virtual string Login { get; set; }
public virtual string Email { get; set; }
public virtual string Role { get; set; } = "User";
diff --git a/Models/EmployeeDto.cs b/Models/EmployeeDto.cs
new file mode 100644
index 0000000..7a30e5b
--- /dev/null
+++ b/Models/EmployeeDto.cs
@@ -0,0 +1,11 @@
+using FirmTracker_Server.Controllers;
+
+namespace FirmTracker_Server.Models
+{
+ public class EmployeeDto
+ {
+ public virtual int Id { get; set; }
+ public virtual string email { get; set; }
+
+ }
+}
diff --git a/Models/Workday.cs b/Models/Workday.cs
new file mode 100644
index 0000000..a93638d
--- /dev/null
+++ b/Models/Workday.cs
@@ -0,0 +1,15 @@
+using FirmTracker_Server.Entities;
+using System;
+
+namespace YourNamespace.Models
+{
+ public class Workday
+ {
+ public virtual int Id { get; set; }
+ public virtual DateTime? StartTime { get; set; }
+ public virtual DateTime? EndTime { get; set; }
+
+ // Many-to-One relationship to the User entity
+ public virtual User User { get; set; }
+ }
+}
diff --git a/Program.cs b/Program.cs
index adedab0..4c75fbc 100644
--- a/Program.cs
+++ b/Program.cs
@@ -40,6 +40,7 @@ using FirmTracker_Server.Mappings;
using NuGet.Packaging;
+
namespace FirmTracker_Server
{
internal static class Program
@@ -176,6 +177,7 @@ namespace FirmTracker_Server
services.AddScoped();
services.AddScoped, PasswordHasher>();
services.AddScoped();
+ // services.AddScoped();
services.AddMvc();
}
diff --git a/Services/UserService.cs b/Services/UserService.cs
index 4b5cc53..a846b49 100644
--- a/Services/UserService.cs
+++ b/Services/UserService.cs
@@ -69,7 +69,7 @@ namespace FirmTracker_Server.Services
{
session.Save(user);
transaction.Commit();
- return user.Id;
+ return user.UserId;
}
catch
{
@@ -128,7 +128,7 @@ namespace FirmTracker_Server.Services
// Generate JWT token
var claims = new List() {
- new(ClaimTypes.NameIdentifier, user.Id.ToString()),
+ new(ClaimTypes.NameIdentifier, user.UserId.ToString()),
new(ClaimTypes.Role, user.Role)
};
diff --git a/nHIbernate/SessionFactory.cs b/nHIbernate/SessionFactory.cs
index 6218b36..1a3deff 100644
--- a/nHIbernate/SessionFactory.cs
+++ b/nHIbernate/SessionFactory.cs
@@ -55,7 +55,8 @@ namespace FirmTracker_Server.nHibernate
.AddFromAssemblyOf()
.AddFromAssemblyOf()
.AddFromAssemblyOf()
- .AddFromAssemblyOf();
+ .AddFromAssemblyOf()
+ .AddFromAssemblyOf();
})
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
diff --git a/nHIbernate/UserMapping.cs b/nHIbernate/UserMapping.cs
index 44c7e7b..8f1487e 100644
--- a/nHIbernate/UserMapping.cs
+++ b/nHIbernate/UserMapping.cs
@@ -7,7 +7,7 @@ public class UserMapping : ClassMap
{
Table("Users"); // The name of your table in the database
- Id(x => x.Id); // Mapping the Id property
+ Id(x => x.UserId); // Mapping the Id property
Map(x => x.Email); // Mapping other properties
Map(x => x.PassHash);
Map(x => x.Role);
diff --git a/nHIbernate/Workday.cs b/nHIbernate/Workday.cs
new file mode 100644
index 0000000..7f819cc
--- /dev/null
+++ b/nHIbernate/Workday.cs
@@ -0,0 +1,12 @@
+using FirmTracker_Server.Entities;
+
+namespace FirmTracker_Server.nHibernate
+{
+ public class Workday
+ {
+ public virtual int Id { get; set; }
+ public virtual DateTime StartTime { get; set; }
+ public virtual DateTime? EndTime { get; set; } // Nullable EndTime, if not finished
+ public virtual User User { get; set; } // Assuming a relationship to a User entity
+ }
+}
diff --git a/nHIbernate/WorkdayMapping.cs b/nHIbernate/WorkdayMapping.cs
new file mode 100644
index 0000000..5cdbfde
--- /dev/null
+++ b/nHIbernate/WorkdayMapping.cs
@@ -0,0 +1,15 @@
+using FluentNHibernate.Mapping;
+namespace FirmTracker_Server.nHibernate
+{
+ public class WorkdayMapping : ClassMap
+ {
+ public WorkdayMapping()
+ {
+ Table("Workdays"); // Make sure the table name matches the one in the database
+ Id(x => x.Id).GeneratedBy.Identity();
+ Map(x => x.StartTime);
+ Map(x => x.EndTime);
+ References(x => x.User).Column("UserId"); // Assuming Workday is related to a User
+ }
+ }
+}
diff --git a/nHIbernate/WorkdayRepository.cs b/nHIbernate/WorkdayRepository.cs
new file mode 100644
index 0000000..6c2e992
--- /dev/null
+++ b/nHIbernate/WorkdayRepository.cs
@@ -0,0 +1,41 @@
+using FirmTracker_Server.Entities;
+using NHibernate;
+using System;
+
+namespace FirmTracker_Server.nHibernate
+{
+ public class WorkdayRepository
+ {
+ public void StartWorkday(int userId)
+ {
+ using (var session = SessionFactory.OpenSession())
+ using (var transaction = session.BeginTransaction())
+ {
+ try
+ {
+ // Fetch the user entity by its ID
+ var user = session.Get(userId); // Assuming User is a mapped entity
+ if (user == null)
+ {
+ throw new Exception("User not found");
+ }
+
+ // Create a new Workday and assign the User reference
+ var workday = new Workday
+ {
+ StartTime = DateTime.Now,
+ User = user // Set the User reference here
+ };
+
+ session.Save(workday);
+ transaction.Commit();
+ }
+ catch (Exception ex)
+ {
+ transaction.Rollback();
+ throw new Exception("An error occurred while starting the workday", ex);
+ }
+ }
+ }
+ }
+}