LANCommander/LANCommander.Server/Services/KeyService.cs

63 lines
1.6 KiB
C#
Raw Normal View History

2024-08-04 18:44:33 -05:00
using LANCommander.Server.Data;
using LANCommander.Server.Data.Models;
2023-01-09 01:05:40 -06:00
2024-08-04 18:44:33 -05:00
namespace LANCommander.Server.Services
2023-01-09 01:05:40 -06:00
{
public class KeyService : BaseDatabaseService<Key>
{
public KeyService(DatabaseContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
}
public async Task<Key> Allocate(Key key, User user)
{
key.ClaimedByUser = user;
2024-02-05 17:20:11 -06:00
key.ClaimedOn = DateTime.Now;
2023-01-09 01:05:40 -06:00
key.AllocationMethod = KeyAllocationMethod.UserAccount;
key = await Update(key);
return key;
}
public async Task<Key> Allocate(Key key, string macAddress)
{
key.ClaimedByMacAddress = macAddress;
2024-02-05 17:20:11 -06:00
key.ClaimedOn = DateTime.Now;
2023-01-09 01:05:40 -06:00
key.AllocationMethod = KeyAllocationMethod.MacAddress;
key = await Update(key);
return key;
}
2023-02-11 21:39:06 -06:00
public async Task<Key> Release(Guid id)
{
var key = await Get(id);
if (key == null)
2023-02-11 21:39:06 -06:00
return null;
2023-02-11 21:39:06 -06:00
return await Release(key);
}
public async Task<Key> Release(Key key)
{
switch (key.AllocationMethod)
{
case KeyAllocationMethod.UserAccount:
key.ClaimedByUser = null;
key.ClaimedOn = null;
break;
case KeyAllocationMethod.MacAddress:
key.ClaimedByMacAddress = "";
key.ClaimedOn = null;
break;
}
2023-02-11 21:39:06 -06:00
return await Update(key);
}
2023-01-09 01:05:40 -06:00
}
}