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);
}
}
}