114 lines
3.8 KiB
C#
114 lines
3.8 KiB
C#
|
using DatabaseCore;
|
|||
|
using DatabaseCore.Models.Security;
|
|||
|
using ModelTools.BusinessLogics;
|
|||
|
using ModelTools.Enums;
|
|||
|
using ModelTools.Extensions;
|
|||
|
using ModelTools.OperationResultModels;
|
|||
|
using SecurityBusinessLogic.BindingModels;
|
|||
|
using SecurityBusinessLogic.Interfaces;
|
|||
|
using SecurityBusinessLogic.ViewModels;
|
|||
|
using System;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
namespace SecurityImplementation.Implementations
|
|||
|
{
|
|||
|
/// <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)
|
|||
|
{
|
|||
|
var entity = Mapper.MapToClass<EnviromentSettingSetBindingModel, EnviromentSetting>(model);
|
|||
|
context.EnviromentSettings.Add(entity);
|
|||
|
context.SaveChanges();
|
|||
|
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity));
|
|||
|
}
|
|||
|
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);
|
|||
|
}
|
|||
|
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity));
|
|||
|
}
|
|||
|
|
|||
|
if (model.Key.IsNotEmpty())
|
|||
|
{
|
|||
|
var entity = context.EnviromentSettings.FirstOrDefault(x => x.Key == model.Key);
|
|||
|
if (entity == null)
|
|||
|
{
|
|||
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|||
|
}
|
|||
|
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity));
|
|||
|
}
|
|||
|
|
|||
|
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,
|
|||
|
List = query.Select(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>).ToList()
|
|||
|
};
|
|||
|
|
|||
|
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);
|
|||
|
}
|
|||
|
entity = Mapper.MapToClass(model, entity);
|
|||
|
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|