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.
Zero/ZeroLevel.SqLite/SqLiteUserRepository.cs

136 lines
3.5 KiB

5 years ago
using System;
using System.Collections.Generic;
using System.Threading;
using ZeroLevel.Models;
namespace ZeroLevel.SqLite
{
public class SqLiteUserRepository
2 years ago
: BaseSqLiteDB<User>
5 years ago
{
#region Fields
private readonly ReaderWriterLockSlim _rwLock = new ReaderWriterLockSlim();
#endregion Fields
#region Ctor
public SqLiteUserRepository()
2 years ago
: base("users.db")
5 years ago
{
}
#endregion Ctor
public IEnumerable<User> GetAll()
{
var list = new List<User>();
_rwLock.EnterReadLock();
try
{
2 years ago
foreach (var r in SelectAll())
5 years ago
{
2 years ago
list.Add(r);
5 years ago
}
}
catch (Exception ex)
{
Log.Error(ex, "[SqLiteUserRepository] Fault get all users");
}
finally
{
_rwLock.ExitReadLock();
}
return list;
}
public User Get(long id)
{
User user = null;
_rwLock.EnterReadLock();
try
{
2 years ago
user = Single(r => r.Id == id);
5 years ago
}
catch (Exception ex)
{
Log.Error(ex, $"[SqLiteUserRepository] Fault get user by id '{id}'");
}
finally
{
_rwLock.ExitReadLock();
}
return user;
}
public User Get(string username, byte[] hash)
{
User user = null;
_rwLock.EnterReadLock();
try
{
2 years ago
user = Single(r => r.UserName == username && r.PasswordHash == hash);
5 years ago
}
catch (Exception ex)
{
Log.Error(ex, $"[SqLiteUserRepository] Fault get user by username '{username}' and pwdhash");
}
finally
{
_rwLock.ExitReadLock();
}
return user;
}
public InvokeResult<long> SaveUser(User user)
{
long id = -1;
_rwLock.EnterWriteLock();
var creationTime = DateTime.UtcNow.Ticks;
try
{
2 years ago
var count_obj = Count(r => r.UserName == user.UserName);
if (count_obj > 0)
5 years ago
{
2 years ago
return InvokeResult<long>.Fault<long>("Пользователь с таким именем уже существует");
5 years ago
}
2 years ago
id = Append(user).Id;
5 years ago
}
catch (Exception ex)
{
Log.Error(ex, $"[SqLiteUserRepository] Fault insert user in storage.");
InvokeResult<long>.Fault(ex.Message);
}
finally
{
_rwLock.ExitWriteLock();
}
return InvokeResult<long>.Succeeding(id);
}
public InvokeResult RemoveUser(string login)
{
_rwLock.EnterWriteLock();
try
{
2 years ago
Delete(r => r.UserName == login);
5 years ago
return InvokeResult.Succeeding();
}
catch (Exception ex)
{
Log.Error(ex, $"[SqLiteUserRepository] Fault remove user '{login}'");
return InvokeResult.Fault(ex.Message);
}
finally
{
_rwLock.ExitWriteLock();
}
}
2 years ago
protected override void DisposeStorageData()
{
}
5 years ago
}
}

Powered by TurnKey Linux.