You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

54 lines
1.5 KiB

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using CoviDok.Api;
using NCuid;
namespace CoviDok.BLL.Sessions
{
class SessionHandler
{
private readonly ISessionProvider SessionStore;
public SessionHandler(ISessionProvider Provider)
{
SessionStore = Provider;
}
public async Task<Session> GetSession(string SessionID)
{
string Candidate = SessionStore.Get(SessionID);
if (Candidate == null) return null;
Session session = null;
await Task.Run(() => {
session = JsonSerializer.Deserialize<Session>(Candidate);
session.LastAccess = DateTime.Now;
SessionStore.Set(SessionID, JsonSerializer.Serialize(session));
});
return session;
}
public async Task<string> CreateSession(Role UserType, int UserID)
{
string ID = null;
await Task.Run(() => {
Session session = new Session
{
ID = UserID,
Type = UserType,
LastAccess = DateTime.Now
};
ID = Cuid.Generate();
SessionStore.Set(ID, JsonSerializer.Serialize(session));
});
return ID;
}
public void DeleteSession(string SessionID)
{
SessionStore.Del(SessionID);
}
}
}