LANCommander/LANCommander.Server/Services/KeyService.cs
2024-08-04 18:44:33 -05:00

62 lines
1.6 KiB
C#

using LANCommander.Server.Data;
using LANCommander.Server.Data.Models;
namespace LANCommander.Server.Services
{
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;
key.ClaimedOn = DateTime.Now;
key.AllocationMethod = KeyAllocationMethod.UserAccount;
key = await Update(key);
return key;
}
public async Task<Key> Allocate(Key key, string macAddress)
{
key.ClaimedByMacAddress = macAddress;
key.ClaimedOn = DateTime.Now;
key.AllocationMethod = KeyAllocationMethod.MacAddress;
key = await Update(key);
return key;
}
public async Task<Key> Release(Guid id)
{
var key = await Get(id);
if (key == null)
return null;
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;
}
return await Update(key);
}
}
}