using DatabaseCore; using DatabaseCore.Models.Department; using DepartmentBusinessLogic.BindingModels; using DepartmentBusinessLogic.Interfaces; using DepartmentBusinessLogic.ViewModels; using ToolsModule.BusinessLogics; using ToolsModule.Enums; using ToolsModule.Models; using System; using System.Linq; namespace DepartmentDatabaseImplementation.Implementations { /// /// Реализация IOrderSyncHistoryService /// public class OrderSyncHistoryService : IOrderSyncHistoryService { public OperationResultModel Create(OrderSyncHistorySetBindingModel model) { using var context = DatabaseManager.GetContext; var entity = Mapper.MapToClass(model, true); context.OrderSyncHistories.Add(entity); context.SaveChanges(); return OperationResultModel.Success(Mapper.MapToClass(entity, true)); } public OperationResultModel Delete(OrderSyncHistoryGetBindingModel model) { using var context = DatabaseManager.GetContext; using var transaction = context.Database.BeginTransaction(); try { var entity = context.OrderSyncHistories.FirstOrDefault(x => x.Id == model.Id); if (entity == null) { return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); } context.OrderSyncHistories.Remove(entity); context.SaveChanges(); var records = context.OrderSyncHistoryRecords.Where(x => x.OrderSyncHistoryId == model.Id); context.OrderSyncHistoryRecords.RemoveRange(records); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } return OperationResultModel.Success(true); } public OperationResultModel Read(OrderSyncHistoryGetBindingModel model) { int countPages = 0; using var context = DatabaseManager.GetContext; // для одной записи if (model.Id.HasValue) { var entity = context.OrderSyncHistories.FirstOrDefault(x => x.Id == model.Id.Value); if (entity == null) { return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); } return OperationResultModel.Success(Mapper.MapToClass(entity, model.HaveRight)); } var query = context.OrderSyncHistories.AsQueryable(); query = query.OrderByDescending(x => x.SyncDate); 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 OrderSyncHistoryListViewModel { MaxCount = countPages, List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() }; return OperationResultModel.Success(result); } public OperationResultModel Restore(OrderSyncHistoryGetBindingModel model) { throw new NotImplementedException(); } public OperationResultModel Update(OrderSyncHistorySetBindingModel model) { using var context = DatabaseManager.GetContext; var entity = context.OrderSyncHistories.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(entity, true)); } } }