2022-07-27 18:14:07 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2022-08-09 15:59:40 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
2022-07-27 18:14:07 +05:00
|
|
|
|
{
|
|
|
|
|
public class UserSettingsRepository : IUserSettingsRepository
|
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext context;
|
|
|
|
|
|
|
|
|
|
public UserSettingsRepository(IAsbCloudDbContext context)
|
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 15:59:40 +05:00
|
|
|
|
public Task<System.Text.Json.JsonDocument> GetOrDefaultAsync(int userId, string key, CancellationToken token)
|
2022-07-27 18:14:07 +05:00
|
|
|
|
=> context.Set<UserSetting>()
|
|
|
|
|
.Where(s => s.IdUser == userId && s.Key == key)
|
2022-08-09 15:59:40 +05:00
|
|
|
|
.Select(s => s.Value)
|
2022-07-27 18:14:07 +05:00
|
|
|
|
.FirstOrDefaultAsync(token);
|
2022-08-09 15:59:40 +05:00
|
|
|
|
|
|
|
|
|
public async Task<int> InsertAsync(int userId, string key, System.Text.Json.JsonDocument value, CancellationToken token)
|
2022-07-27 18:14:07 +05:00
|
|
|
|
{
|
|
|
|
|
var set = context.Set<UserSetting>();
|
|
|
|
|
|
2022-08-09 15:59:40 +05:00
|
|
|
|
if (await set.AnyAsync(s => s.IdUser == userId && s.Key == key, token))
|
2022-07-27 18:14:07 +05:00
|
|
|
|
return IUserSettingsRepository.ErrorKeyIsUsed;
|
|
|
|
|
|
|
|
|
|
var entity = new UserSetting
|
|
|
|
|
{
|
|
|
|
|
IdUser = userId,
|
|
|
|
|
Key = key,
|
|
|
|
|
Value = value,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.Set<UserSetting>()
|
|
|
|
|
.Add(entity);
|
|
|
|
|
|
|
|
|
|
return await context.SaveChangesAsync(token);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 15:59:40 +05:00
|
|
|
|
public async Task<int> UpdateAsync(int userId, string key, System.Text.Json.JsonDocument value, CancellationToken token)
|
2022-07-27 18:14:07 +05:00
|
|
|
|
{
|
|
|
|
|
var set = context.Set<UserSetting>();
|
|
|
|
|
var updatingItem = await set
|
|
|
|
|
.FirstOrDefaultAsync(s => s.IdUser == userId && s.Key == key, token);
|
|
|
|
|
|
|
|
|
|
if (updatingItem is null)
|
|
|
|
|
return IUserSettingsRepository.ErrorKeyNotFound;
|
|
|
|
|
|
|
|
|
|
updatingItem.Value = value;
|
|
|
|
|
set.Update(updatingItem);
|
|
|
|
|
|
|
|
|
|
return await context.SaveChangesAsync(token);
|
|
|
|
|
}
|
|
|
|
|
public async Task<int> DeleteAsync(int userId, string key, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var set = context.Set<UserSetting>();
|
|
|
|
|
var removingItem = await set
|
2022-08-09 15:59:40 +05:00
|
|
|
|
.FirstOrDefaultAsync(s => s.IdUser == userId && s.Key == key, token);
|
2022-07-27 18:14:07 +05:00
|
|
|
|
|
2022-08-09 15:59:40 +05:00
|
|
|
|
if (removingItem is null)
|
2022-07-27 18:14:07 +05:00
|
|
|
|
return IUserSettingsRepository.ErrorKeyNotFound;
|
|
|
|
|
|
|
|
|
|
set.Remove(removingItem);
|
|
|
|
|
return await context.SaveChangesAsync(token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|