dodanie startu dnia pracy użytkownika po ID usera
This commit is contained in:
parent
e50274b736
commit
717eb6ed1f
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<string, DateTime?> WorkStartTimes = new ConcurrentDictionary<string, DateTime?>();
|
||||
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))
|
||||
{
|
||||
if (startTime.HasValue)
|
||||
{
|
||||
return Ok(new { status = "started", startTime = startTime });
|
||||
_workdayCRUD = new WorkdayRepository(); // Instantiate directly (no DI in this example)
|
||||
}
|
||||
else
|
||||
|
||||
// Endpoint to start a workday
|
||||
[HttpPost("start")]
|
||||
[Authorize(Roles = Roles.Admin + "," + Roles.User)]
|
||||
public IActionResult StartWorkday()
|
||||
{
|
||||
return Ok(new { status = "stopped" });
|
||||
}
|
||||
}
|
||||
else
|
||||
try
|
||||
{
|
||||
return NotFound(new { message = "User not found" });
|
||||
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 });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
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] });
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
|
11
Models/EmployeeDto.cs
Normal file
11
Models/EmployeeDto.cs
Normal file
@ -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; }
|
||||
|
||||
}
|
||||
}
|
15
Models/Workday.cs
Normal file
15
Models/Workday.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
@ -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<ErrorHandling>();
|
||||
services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
|
||||
services.AddScoped<IExpenseRepository, ExpenseRepository>();
|
||||
// services.AddScoped<IWorkdayRepository, WorkdayRepository>();
|
||||
services.AddMvc();
|
||||
}
|
||||
|
||||
|
@ -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<Claim>() {
|
||||
new(ClaimTypes.NameIdentifier, user.Id.ToString()),
|
||||
new(ClaimTypes.NameIdentifier, user.UserId.ToString()),
|
||||
new(ClaimTypes.Role, user.Role)
|
||||
};
|
||||
|
||||
|
@ -55,7 +55,8 @@ namespace FirmTracker_Server.nHibernate
|
||||
.AddFromAssemblyOf<Reports.ReportTransactionMapping>()
|
||||
.AddFromAssemblyOf<Reports.ReportExpenseMapping>()
|
||||
.AddFromAssemblyOf<LogsMapping>()
|
||||
.AddFromAssemblyOf<UserMapping>();
|
||||
.AddFromAssemblyOf<UserMapping>()
|
||||
.AddFromAssemblyOf<WorkdayMapping>();
|
||||
|
||||
})
|
||||
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
|
||||
|
@ -7,7 +7,7 @@ public class UserMapping : ClassMap<User>
|
||||
{
|
||||
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);
|
||||
|
12
nHIbernate/Workday.cs
Normal file
12
nHIbernate/Workday.cs
Normal file
@ -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
|
||||
}
|
||||
}
|
15
nHIbernate/WorkdayMapping.cs
Normal file
15
nHIbernate/WorkdayMapping.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using FluentNHibernate.Mapping;
|
||||
namespace FirmTracker_Server.nHibernate
|
||||
{
|
||||
public class WorkdayMapping : ClassMap<Workday>
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
41
nHIbernate/WorkdayRepository.cs
Normal file
41
nHIbernate/WorkdayRepository.cs
Normal file
@ -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<User>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user