Skip to content

Instantly share code, notes, and snippets.

@SimonDoy
Last active May 25, 2016 04:57
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 SimonDoy/5dd63ef2e920e4cae47ef336558b5230 to your computer and use it in GitHub Desktop.
Save SimonDoy/5dd63ef2e920e4cae47ef336558b5230 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Itsp365.InvoiceFormApp.Api.Models.Entities;
using ITSP365.InvoiceFormApp.Api.DataAccess;
using Microsoft.ApplicationInsights;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
namespace Itsp365.InvoiceFormApp.Api.Repositories
{
public class InvoiceRepository
{
private static InvoiceRepository _instance = null;
private List<InvoiceForm> _repository;
private InvoiceDataContext _dataContext;
private InvoiceRepository()
{
_repository = new List<InvoiceForm>();
var invoice = new InvoiceForm();
invoice.Id = Guid.NewGuid().ToString();
invoice.Reference = "ACME001";
invoice.CompanyName = "Acme;
invoice.AgencyName = "Acme Agency";
invoice.AgencyContact = "Road Runner";
invoice.CurrencyType = "GBP";
invoice.InvoiceDate = DateTime.UtcNow;
invoice.VatRate = 0.2;
var invoiceLine = new InvoiceLine();
invoiceLine.Description = "SharePoint Project";
invoiceLine.UnitType = "Day";
invoiceLine.UnitValue = 200;
invoiceLine.UnitQuantity = 22;
invoice.InvoiceLines.Add(invoiceLine);
_repository.Add(invoice);
}
private async Task Initialise()
{
_dataContext = await InvoiceDataContext.GetCurrent();
}
public static async Task<InvoiceRepository> GetCurrent()
{
var telemetryClient = new TelemetryClient();
try
{
if (_instance == null || (_instance != null && _instance._dataContext == null))
{
_instance = new InvoiceRepository();
await _instance.Initialise();
}
}
catch (Exception ex)
{
telemetryClient.TrackException(ex);
throw;
}
return _instance;
}
private IOrderedQueryable<InvoiceForm> GetAllQuery()
{
var feedOptions = new FeedOptions { MaxItemCount = -1 };
var documentCollectionUri = new Uri(_dataContext.DocumentCollection.DocumentCollection.AltLink, UriKind.RelativeOrAbsolute);
var query = _dataContext.Database.Client.CreateDocumentQuery<InvoiceForm>(documentCollectionUri, feedOptions);
return query;
}
public IList<InvoiceForm> GetAll()
{
var query = GetAllQuery();
return query.ToList();
}
public InvoiceForm Get(string reference)
{
InvoiceForm form = new InvoiceNotFoundForm();
var result = GetAllQuery().Where(f => f.Reference == reference).ToList();
if (result.Count > 0)
{
form = result.First();
}
return form;
}
public async Task<bool> Add(InvoiceForm form)
{
if (string.IsNullOrEmpty(form.Reference))
{
throw new ArgumentException("invoice does not have a reference field set.");
}
form.Id = Guid.NewGuid().ToString();
var documentCollectionUri = new Uri(_dataContext.DocumentCollection.DocumentCollection.AltLink, UriKind.RelativeOrAbsolute);
var addedDocument = await _dataContext.Database.Client.CreateDocumentAsync(documentCollectionUri, form);
return true;
}
public async Task<bool> Remove(string Id)
{
bool bSuccess = false;
var form = GetAllQuery().FirstOrDefault(f => f.Id == Id);
if (form != null)
{
var documentUri = UriFactory.CreateDocumentUri(_dataContext.Database.Database.Id, _dataContext.DocumentCollection.DocumentCollection.Id, form.Reference);
await _dataContext.Database.Client.DeleteDocumentAsync(documentUri);
bSuccess = true;
}
return bSuccess;
}
public async Task<bool> Update(InvoiceForm updatingForm)
{
bool bSuccess = false;
try
{
}
catch (DocumentClientException clientEx)
{
throw;
}
return bSuccess;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment