forked from ddrilling/AsbCloudServer
IUserSettingsRepository replace insert and update to upsert.
This commit is contained in:
parent
facf45ad4c
commit
b95891823d
@ -28,24 +28,14 @@ namespace AsbCloudApp.Services
|
||||
Task<System.Text.Json.JsonDocument> GetOrDefaultAsync(int userId, string key, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Добавить настройки с ключем для пользователя
|
||||
/// Добавить или изменить настройки с ключем для пользователя
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<int> InsertAsync(int userId, string key, System.Text.Json.JsonDocument value, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Отредактировать настройки с ключем для пользователя
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<int> UpdateAsync(int userId, string key, System.Text.Json.JsonDocument value, CancellationToken token);
|
||||
Task<int> UpsertAsync(int userId, string key, System.Text.Json.JsonDocument value, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Удалить настройки с ключем для пользователя
|
||||
|
@ -22,37 +22,27 @@ namespace AsbCloudInfrastructure.Repository
|
||||
.Select(s => s.Value)
|
||||
.FirstOrDefaultAsync(token);
|
||||
|
||||
public async Task<int> InsertAsync(int userId, string key, System.Text.Json.JsonDocument value, CancellationToken token)
|
||||
{
|
||||
var set = context.Set<UserSetting>();
|
||||
|
||||
if (await set.AnyAsync(s => s.IdUser == userId && s.Key == key, token))
|
||||
return IUserSettingsRepository.ErrorKeyIsUsed;
|
||||
|
||||
var entity = new UserSetting
|
||||
{
|
||||
IdUser = userId,
|
||||
Key = key,
|
||||
Value = value,
|
||||
};
|
||||
|
||||
context.Set<UserSetting>()
|
||||
.Add(entity);
|
||||
|
||||
return await context.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
public async Task<int> UpdateAsync(int userId, string key, System.Text.Json.JsonDocument value, CancellationToken token)
|
||||
public async Task<int> UpsertAsync(int userId, string key, System.Text.Json.JsonDocument value, CancellationToken token)
|
||||
{
|
||||
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);
|
||||
{
|
||||
var settings = new UserSetting
|
||||
{
|
||||
IdUser = userId,
|
||||
Key = key,
|
||||
Value = value,
|
||||
};
|
||||
set.Add(settings);
|
||||
}
|
||||
else
|
||||
{
|
||||
updatingItem.Value = value;
|
||||
set.Update(updatingItem);
|
||||
}
|
||||
|
||||
return await context.SaveChangesAsync(token);
|
||||
}
|
||||
|
@ -8,6 +8,9 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Контроллер настроек для пользователя
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
@ -20,6 +23,12 @@ namespace AsbCloudWebApi.Controllers
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить настройки
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{key}")]
|
||||
[ProducesResponseType(typeof(object), (int)System.Net.HttpStatusCode.OK)]
|
||||
[Produces("application/json")]
|
||||
@ -38,32 +47,32 @@ namespace AsbCloudWebApi.Controllers
|
||||
return actionResult;
|
||||
}
|
||||
|
||||
[HttpPost("{key}")]
|
||||
public virtual async Task<ActionResult<int>> InsertAsync(string key, [FromBody] System.Text.Json.JsonDocument value, CancellationToken token)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
if (userId is null)
|
||||
return Forbid();
|
||||
|
||||
var result = await service.InsertAsync((int)userId, key, value, token).ConfigureAwait(false);
|
||||
if (result == IUserSettingsRepository.ErrorKeyIsUsed)
|
||||
return BadRequest(ArgumentInvalidException.MakeValidationError(nameof(key), "is already used"));
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// записать новые или обновить старые
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("{key}")]
|
||||
public virtual async Task<ActionResult<int>> UpdateAsync(string key, [FromBody] System.Text.Json.JsonDocument value, CancellationToken token)
|
||||
public virtual async Task<ActionResult<int>> UpsertAsync(string key, [FromBody] System.Text.Json.JsonDocument value, CancellationToken token)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
if (userId is null)
|
||||
return Forbid();
|
||||
|
||||
var result = await service.UpdateAsync((int)userId, key, value, token).ConfigureAwait(false);
|
||||
var result = await service.UpsertAsync((int)userId, key, value, token).ConfigureAwait(false);
|
||||
if (result < 0)
|
||||
return BadRequest(ArgumentInvalidException.MakeValidationError(nameof(key), "not found"));
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удалить настройки пользователя по ключу
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("{key}")]
|
||||
public virtual async Task<ActionResult<int>> DeleteAsync(string key, CancellationToken token)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user