using DatabaseCore; using DatabaseCore.Models.Security; using ModelTools.BusinessLogics; using ModelTools.Enums; using ModelTools.Extensions; using ModelTools.Models; using SecurityBusinessLogic.BindingModels; using SecurityBusinessLogic.Interfaces; using SecurityBusinessLogic.ViewModels; using System; using System.Linq; namespace SecurityImplementation.SecurityDatabaseImplementation { /// /// Реализация IEnviromentSettingService /// 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(model); context.EnviromentSettings.Add(entity); context.SaveChanges(); return OperationResultModel.Success(Mapper.MapToClass(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(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(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).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(entity)); } } }