public new static async Task <Response <Order> > CreateAsync(Order order, bool refreshFromDb = false, IList <string> navigationProperties = null) { Parallel.ForEach(order.OrderItems.Where(orderItem => orderItem.Id == Guid.Empty), orderItem => { orderItem.Id = Guid.NewGuid(); }); if (!SalesValidator.ValidateOrder(order)) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } order.Date = DateTime.Now; order.Status = OrderStatus.Created.ToInt(); using (var orderRepository = DataLayerUnitOfWork.Repository <OrderRepository>()) { var createdOrder = await orderRepository.CreateAsync(order.CopyTo <DataLayer.Order>(), refreshFromDb, navigationProperties).ConfigureAwait(false); if (createdOrder == null) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.Error)); } return(ResponseFactory <Order> .CreateResponse(true, ResponseCode.Success, createdOrder.CopyTo <Order>())); } }
public static async Task <Response> DeleteAsync(Guid id) { using (var repository = DataLayerUnitOfWork.Repository <TRepo>()) { var result = await repository.DeleteAsync(id).ConfigureAwait(false); return(ResponseFactory.CreateResponse(result, result ? ResponseCode.Success : ResponseCode.ErrorInvalidInput)); } }
public static async Task <Response> DeleteAsync(IList <TModel> modelCollection) { using (var repository = DataLayerUnitOfWork.Repository <TRepo>()) { var entityCollection = modelCollection.CopyTo <TEntity>(); var result = await repository.DeleteAsync(entityCollection).ConfigureAwait(false); return(ResponseFactory.CreateResponse(result, result ? ResponseCode.Success : ResponseCode.ErrorInvalidInput)); } }
public static async Task <Response <IList <TModel> > > GetAllAsync(IList <string> navigationProperties = null) { using (var repository = DataLayerUnitOfWork.Repository <TRepo>()) { var entities = await repository.GetAllAsync(navigationProperties).ConfigureAwait(false); if (entities == null) { ResponseFactory <IList <TModel> > .CreateResponse(false, ResponseCode.Error); } return(ResponseFactory <IList <TModel> > .CreateResponse(true, ResponseCode.Success, entities.CopyTo <TModel>())); } }
public static async Task <Response <IList <Payroll> > > GetAllForClient(Guid clientId, IList <string> navigationProperties = null) { using (var payrollRepository = DataLayerUnitOfWork.Repository <PayrollRepository>()) { var payrolls = await payrollRepository.GetAllForClient(clientId, navigationProperties).ConfigureAwait(false); if (payrolls == null) { return(ResponseFactory <IList <Payroll> > .CreateResponse(false, ResponseCode.ErrorNotFound)); } return(ResponseFactory <IList <Payroll> > .CreateResponse(true, ResponseCode.Success, payrolls.CopyTo <Payroll>())); } }
public static async Task <Response <IList <Order> > > GetAllAsync(string orderBy, bool orderAscending, IList <string> navigationProperties = null) { using (var orderRepository = DataLayerUnitOfWork.Repository <OrderRepository>()) { var orders = await orderRepository.GetAllAsync(navigationProperties).ConfigureAwait(false); if (orders == null || orders.Count == 0) { return(ResponseFactory <IList <Order> > .CreateResponse(true, ResponseCode.SuccessNoContent)); } var sortedOrders = SortOrders(orders.CopyTo <Order>(), orderBy, orderAscending); return(ResponseFactory <IList <Order> > .CreateResponse(true, ResponseCode.Success, sortedOrders)); } }
public static async Task <Response <TModel> > UpdateAsync(TModel model, bool refreshFromDb = false, IList <string> navigationProperties = null) { using (var repository = DataLayerUnitOfWork.Repository <TRepo>()) { var entity = model.CopyTo <TEntity>(); entity = await repository.UpdateAsync(entity, refreshFromDb, navigationProperties).ConfigureAwait(false); if (entity == null) { return(ResponseFactory <TModel> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } return(ResponseFactory <TModel> .CreateResponse(true, ResponseCode.Success, entity.CopyTo <TModel>())); } }
public static async Task <Response <Payroll> > MakePayment(Guid orderId, Guid clientId) { using (var unitOfWork = new DataLayerUnitOfWork()) { var orderRepository = unitOfWork.TrackingRepository <OrderRepository>(); var payrollRepository = unitOfWork.TrackingRepository <PayrollRepository>(); var order = await orderRepository.GetAsync(orderId, new[] { nameof(Order.OrderItems) }).ConfigureAwait(false); if (order == null) { return(ResponseFactory <Payroll> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } var payroll = new DataLayer.Payroll { ClientId = clientId, OrderId = orderId, Date = DateTime.Now, Value = order.OrderItems.Sum(orderItem => orderItem.Price * orderItem.Quantity) }; payroll = await payrollRepository.CreateAsync(payroll, true).ConfigureAwait(false); if (payroll == null) { return(ResponseFactory <Payroll> .CreateResponse(false, ResponseCode.Error)); } order.Status = OrderStatus.Finalized.ToInt(); var updatedOrder = await orderRepository.UpdateAsync(order, true).ConfigureAwait(false); if (updatedOrder == null) { return(ResponseFactory <Payroll> .CreateResponse(false, ResponseCode.Error)); } return(ResponseFactory <Payroll> .CreateResponse(true, ResponseCode.Success, payroll.CopyTo <Payroll>())); } }
public new static async Task <Response <Order> > UpdateAsync(Order order, bool refreshFromDb = false, IList <string> navigationProperties = null) { if (!SalesValidator.ValidateOrder(order, false)) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } using (var orderRepository = DataLayerUnitOfWork.Repository <OrderRepository>()) { var updatedOrder = await orderRepository.UpdateAsync(order.CopyTo <DataLayer.Order>(), refreshFromDb, navigationProperties).ConfigureAwait(false); if (updatedOrder == null) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.Error)); } return(ResponseFactory <Order> .CreateResponse(true, ResponseCode.Success, updatedOrder.CopyTo <Order>())); } }
public static async Task <Response <Order> > Cancel(Order order) { if (!SalesValidator.ValidateOrder(order, false)) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } order.Status = OrderStatus.Cancelled.ToInt(); using (var orderRepository = DataLayerUnitOfWork.Repository <OrderRepository>()) { var dbModel = order.CopyTo <DataLayer.Order>(); var updatedOrder = await orderRepository.UpdateAsync(dbModel).ConfigureAwait(false); if (updatedOrder == null) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.Error)); } return(ResponseFactory <Order> .CreateResponse(true, ResponseCode.Success, order)); } }
public static async Task <Response <OrderItem> > UpdateAsync(OrderItem orderItem) { using (var unitOfWork = new DataLayerUnitOfWork()) { var order = await unitOfWork.TrackingRepository <OrderRepository>().GetAsync(orderItem.OrderId).ConfigureAwait(false); if (!SalesValidator.ValidateOrderItem(orderItem, (SaleType)order.SaleType)) { return(ResponseFactory <OrderItem> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } var dbModel = orderItem.CopyTo <DataLayer.OrderItem>(); var result = await unitOfWork.TrackingRepository <OrderItemRepository>().UpdateAsync(dbModel).ConfigureAwait(false); if (result == null) { return(ResponseFactory <OrderItem> .CreateResponse(false, ResponseCode.Error)); } return(ResponseFactory <OrderItem> .CreateResponse(true, ResponseCode.Success, result.CopyTo <OrderItem>())); } }
private static async Task <Response <Order> > CreatePayroll(Order order, DataLayerUnitOfWork unitOfWork, OrderRepository orderRepository) { var orderWithItems = await orderRepository.GetAsync(order.Id, new[] { nameof(Order.OrderItems) }).ConfigureAwait(false); var payroll = new Payroll { ClientId = order.ClientId, Date = DateTime.Now, OrderId = order.Id, Value = orderWithItems.OrderItems.Sum(orderItem => orderItem.Price * orderItem.Quantity) }; payroll = await unitOfWork.TrackingRepository <PayrollRepository>().CreateAsync(payroll).ConfigureAwait(false); if (payroll == null) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.Error)); } return(ResponseFactory <Order> .CreateResponse(true, ResponseCode.Success, order)); }
public static async Task <Response <Order> > Finalize(Order order) { if (!SalesValidator.ValidateOrder(order, false)) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.ErrorInvalidInput)); } order.Status = OrderStatus.Finalized.ToInt(); using (var unitOfWork = new DataLayerUnitOfWork()) { var orderRepository = unitOfWork.TrackingRepository <OrderRepository>(); var dbModel = order.CopyTo <DataLayer.Order>(); var updatedOrder = await orderRepository.UpdateAsync(dbModel, true).ConfigureAwait(false); if (updatedOrder == null) { return(ResponseFactory <Order> .CreateResponse(false, ResponseCode.Error)); } return(await CreatePayroll(order, unitOfWork, orderRepository)); } }