using DatabaseCore; using DatabaseCore.Models.Department; using DepartmentBusinessLogic.BindingModels; using DepartmentBusinessLogic.Interfaces; using DepartmentBusinessLogic.ViewModels; using ModuleTools.BusinessLogics; using ModuleTools.Enums; using ModuleTools.Models; using System; using System.Linq; using System.Threading.Tasks; namespace DepartmentDatabaseImplementation.Implementations { /// /// Реализация IOrderSyncHistoryService /// public class OrderSyncHistoryService : IOrderSyncHistoryService { public async Task CreateAsync(OrderSyncHistorySetBindingModel model) { using var context = DatabaseManager.GetContext; var entity = Mapper.MapToClass(model, true); await context.OrderSyncHistories.AddAsync(entity); await context.SaveChangesAsync(); return OperationResultModel.Success(Mapper.MapToClass(entity, true)); } public async Task DeleteAsync(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); await context.SaveChangesAsync(); var records = context.OrderSyncHistoryRecords.Where(x => x.OrderSyncHistoryId == model.Id); context.OrderSyncHistoryRecords.RemoveRange(records); await context.SaveChangesAsync(); await transaction.CommitAsync(); } catch (Exception) { await transaction.RollbackAsync(); throw; } return OperationResultModel.Success(true); } public async Task ReadAsync(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); return await Task.Run(() => { 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 Task RestoreAsync(OrderSyncHistoryGetBindingModel model) { throw new NotImplementedException(); } public async Task UpdateAsync(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); await context.SaveChangesAsync(); return OperationResultModel.Success(Mapper.MapToClass(entity, true)); } } }