116 lines
3.7 KiB
C#
116 lines
3.7 KiB
C#
using CoreDatabase;
|
|
using CoreDatabase.Models.Department;
|
|
using DepartmentContract.BindingModels;
|
|
using DepartmentContract.Services.IGenericEntityService;
|
|
using DepartmentContract.ViewModels;
|
|
using System;
|
|
using System.Linq;
|
|
using ToolsModule.BusinessLogics;
|
|
using ToolsModule.Enums;
|
|
using ToolsModule.Models;
|
|
|
|
namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService
|
|
{
|
|
/// <summary>
|
|
/// Реализация IOrderSyncHistoryService
|
|
/// </summary>
|
|
public class OrderSyncHistoryService : IOrderSyncHistoryService
|
|
{
|
|
public OperationResultModel Create(OrderSyncHistorySetBindingModel model)
|
|
{
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
var entity = Mapper.MapToClass<OrderSyncHistorySetBindingModel, OrderSyncHistory>(model, true);
|
|
context.OrderSyncHistories.Add(entity);
|
|
context.SaveChanges();
|
|
return OperationResultModel.Success(Mapper.MapToClass<OrderSyncHistory, OrderSyncHistoryViewModel>(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<OrderSyncHistory, OrderSyncHistoryViewModel>(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<OrderSyncHistory, OrderSyncHistoryViewModel>(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<OrderSyncHistory, OrderSyncHistoryViewModel>(entity, true));
|
|
}
|
|
}
|
|
} |