DepartmentProject/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/EnviromentSettingService.cs

114 lines
3.9 KiB
C#
Raw Normal View History

using DatabaseCore;
using DatabaseCore.Models.Security;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Extensions;
using ModuleTools.Models;
using SecurityBusinessLogic.BindingModels;
using SecurityBusinessLogic.Interfaces;
using SecurityBusinessLogic.ViewModels;
using System;
using System.Linq;
namespace SecurityDatabaseImplementation.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, true);
context.EnviromentSettings.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, true));
}
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, model.HaveRight));
}
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, model.HaveRight));
}
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(x => Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(x, model.HaveRight)).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, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, true));
}
}
}