2021-03-26 20:09:52 +04:00
|
|
|
|
using DatabaseCore;
|
|
|
|
|
using DatabaseCore.Models.Security;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
using ModuleTools.BusinessLogics;
|
|
|
|
|
using ModuleTools.Enums;
|
|
|
|
|
using ModuleTools.Extensions;
|
|
|
|
|
using ModuleTools.Models;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
using SecurityBusinessLogic.BindingModels;
|
|
|
|
|
using SecurityBusinessLogic.Interfaces;
|
|
|
|
|
using SecurityBusinessLogic.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
namespace SecurityDatabaseImplementation.Implementations
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация IEnviromentSettingService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class EnviromentSettingService : IEnviromentSettingService
|
|
|
|
|
{
|
|
|
|
|
public OperationResultModel Create(EnviromentSettingSetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var exsistEntity = context.EnviromentSettings.FirstOrDefault(x => x.Key == model.Key);
|
|
|
|
|
if (exsistEntity == null)
|
|
|
|
|
{
|
2021-03-28 20:26:25 +04:00
|
|
|
|
var entity = Mapper.MapToClass<EnviromentSettingSetBindingModel, EnviromentSetting>(model, true);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
context.EnviromentSettings.Add(entity);
|
|
|
|
|
context.SaveChanges();
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Delete(EnviromentSettingGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var entity = context.EnviromentSettings.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.EnviromentSettings.Remove(entity);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Read(EnviromentSettingGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
int countPages = 0;
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
// для одной записи
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var entity = context.EnviromentSettings.FirstOrDefault(x => x.Id == model.Id.Value);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, model.HaveRight));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.Key.IsNotEmpty())
|
|
|
|
|
{
|
|
|
|
|
var entity = context.EnviromentSettings.FirstOrDefault(x => x.Key == model.Key);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, model.HaveRight));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = context.EnviromentSettings.AsQueryable();
|
|
|
|
|
|
|
|
|
|
query = query.OrderBy(x => x.Key);
|
|
|
|
|
|
|
|
|
|
if (model.PageNumber.HasValue && model.PageSize.HasValue)
|
|
|
|
|
{
|
|
|
|
|
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
|
|
|
|
|
query = query
|
|
|
|
|
.Skip(model.PageSize.Value * model.PageNumber.Value)
|
|
|
|
|
.Take(model.PageSize.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = new EnviromentSettingListViewModel
|
|
|
|
|
{
|
|
|
|
|
MaxCount = countPages,
|
2021-03-28 20:26:25 +04:00
|
|
|
|
List = query.Select(x => Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(x, model.HaveRight)).ToList()
|
2021-03-26 20:09:52 +04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Update(EnviromentSettingSetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var entity = context.EnviromentSettings.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
2021-03-28 20:26:25 +04:00
|
|
|
|
entity = Mapper.MapToClass(model, entity, true);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
2021-03-28 20:26:25 +04:00
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, true));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|