Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Last active August 21, 2020 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbrailsford/a9cc7768756ec87e30fa4494820bec27 to your computer and use it in GitHub Desktop.
Save mattbrailsford/a9cc7768756ec87e30fa4494820bec27 to your computer and use it in GitHub Desktop.
Vendr extension to get all orders for a customer
using System;
using System.Collections.Generic;
using System.Linq;
using Vendr.Core;
using Vendr.Core.Api;
using Vendr.Core.Models;
using Vendr.Core.Services;
namespace VendrExt
{
public static class OrderServiceExtensions
{
public static IEnumerable<OrderReadOnly> GetAllOrdersForCustomer(this IOrderService orderService, Guid storeId, string customerReference)
{
var orders = new List<OrderReadOnly>();
try
{
using (var uow = VendrApi.Instance.Uow.Create() as IDatabaseUnitOfWork)
{
var orderIds = uow.Database.Fetch<Guid>($"SELECT id FROM {Constants.DatabaseSchema.Tables.Order} WHERE storeId = @0 AND customerReference = @1", storeId, customerReference);
orders = orderService.GetOrders(orderIds.ToArray()).ToList();
uow.Complete();
}
}
catch (Exception ex)
{
VendrApi.Instance.Log.Error<IOrderService>(ex, "Error fetching all orders for customer {CustomerReference}", customerReference);
}
return orders;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment