public async Task<IHttpActionResult> Post(SaleHeader sale) { if (!ModelState.IsValid) { return BadRequest(ModelState); } return Created(await CreateAsync(sale)); }
public Task<IHttpActionResult> Put([FromODataUri] Guid id, SaleHeader sale, ODataQueryOptions<SaleHeader> options) { if (!ModelState.IsValid) { return Task.FromResult((IHttpActionResult)BadRequest(ModelState)); } if (id != sale.Id) { return Task.FromResult((IHttpActionResult)BadRequest("The Id of sale does not match the id")); } var sales = this.database.SaleHeaders.Where(r => r.Id == id); return PutOrPatchODataAsync(sales, options, () => CreateAsync(sale), () => UpdateAsync(sale)); }
private Task<SaleHeader> UpdatePatchAsync(SaleHeader entity, Delta<SaleHeader> patch) { patch.Patch(entity); return UpdateAsync(entity); }
private async Task<SaleHeader> UpdateAsync(SaleHeader entity) { var now = DateTimeOffset.UtcNow; var user = Thread.CurrentPrincipal.Identity.Name; if (entity.CustomerContacts != null) { foreach (var contact in entity.CustomerContacts) { contact._SaleHeaderId = entity.Id; contact.Update(this.database, this.database.Contacts, now, user); } } if (entity.Items != null) { foreach (var item in entity.Items) { item._SaleHeaderId = entity.Id; item.Update(this.database, this.database.SaleLineItems, now, user); } } entity.Update(this.database, this.database.SaleHeaders, now, user); await this.database.SaveChangesAsync(); return entity; }
private Task<SaleHeader> CreatePatchAsync(Delta<SaleHeader> patch) { var entity = new SaleHeader(); patch.Patch(entity); return CreateAsync(entity); }
private async Task<SaleHeader> CreateAsync(SaleHeader entity) { var now = DateTimeOffset.UtcNow; var user = Thread.CurrentPrincipal.Identity.Name; var start = new DateTimeOffset(now.Year, now.Month, now.Day, 0, 0, 0, 0, TimeSpan.Zero); var end = new DateTimeOffset(now.Year, now.Month, now.Day, 23, 59, 59, 999, TimeSpan.Zero); var dayNumbers = this.database.SaleHeaders.Where(s => s.Created >= start && s.Created <= end).Select(s => s.DayNumber); if (await dayNumbers.AnyAsync()) { entity.DayNumber = (await dayNumbers.MaxAsync()) + 1; } else { entity.DayNumber = 1; } var totalNumbers = this.database.SaleHeaders.Select(s => s.TotalNumber); if (await totalNumbers.AnyAsync()) { entity.TotalNumber = (await totalNumbers.MaxAsync()) + 1; } else { entity.TotalNumber = 1; } entity.Status = SaleStatus.Ok; entity.Create(this.database.SaleHeaders, now, user); entity.NumberText = entity.GenerateSaleNumber(); if (entity.CustomerContacts != null) { foreach (var contact in entity.CustomerContacts) { contact._SaleHeaderId = entity.Id; contact.Create(this.database.Contacts, now, user); } } if (entity.Items != null) { foreach (var item in entity.Items) { item._SaleHeaderId = entity.Id; item.Status = SaleStatus.Ok; item.Create(this.database.SaleLineItems, now, user); } } await this.database.SaveChangesAsync(); return entity; }
private static void GetSaleAndETag(out SaleHeader purchase, out string etag) { purchase = GetSale(); var tag = default(string); ODataClientHelper.InvokeGet(string.Format("{0}Sales({1})", SalesServiceRootUrl, purchase.Id), null, response => { var json = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); var obj = JObject.Parse(json); tag = (string)obj["@odata.etag"]; return false; }); etag = tag; }
private static void FillCommonValues(SaleHeader purchase, DateTimeOffset time) { FillEntityCommonValues(purchase, time); foreach (var contact in purchase.CustomerContacts) { FillEntityCommonValues(contact, time); contact._SaleHeaderId = purchase.Id; } foreach (var transaction in purchase.Items) { FillEntityCommonValues(transaction, time); transaction._SaleHeaderId = purchase.Id; } }
private static void CompareChanged(SaleHeader purchase, JToken token) { CompareChangedCommon(purchase, token); Assert.AreEqual(purchase.DayNumber, (int)token["DayNumber"]); Assert.AreEqual(purchase.TotalNumber, (int)token["TotalNumber"]); Assert.AreEqual(purchase.NumberText, (string)token["NumberText"]); Assert.AreEqual(purchase.SalesPersonName, (string)token["SalesPersonName"]); Assert.AreEqual(purchase.Status, (SaleStatus)Enum.Parse(typeof(SaleStatus), (string)token["Status"])); Assert.AreNotEqual(purchase.CustomerName, (string)token["CustomerName"]); CompareLazyProperty(() => purchase.Items, token, "Items", Compare); CompareLazyProperty(() => purchase.CustomerContacts, token, "CustomerContacts", Compare); }
public void TestInitialize() { TestCleanup(); var totalNumber = default(int); using (var db = new SaleDbContext()) { if (db.SaleHeaders.Any()) { totalNumber = db.SaleHeaders.Max(s => s.TotalNumber); } } var sales = new List<SaleHeader>(55); for (int i = 0; i < sales.Capacity; i++) { var now = GenerateFakeNow(); var random = new Random(i); var sale = new SaleHeader() { DayNumber = sales.Count(s => s.Created.Date == now.Date) + 1, TotalNumber = totalNumber + i + 1, SalesPersonName = GenerateSalesPersonName(i), CustomerName = string.Format("customer {0}", i + 1), Status = GenerateEnumValue<SaleStatus>(), }; sale.NumberText = sale.GenerateSaleNumber(); sale.CustomerContacts = new[] { new Contact() { Method = ContactMethod.Phone, Value = string.Format("contact method phone number {0}", i + 1), }, new Contact() { Method = GenerateEnumValue<ContactMethod>(), Value = string.Format("contact method {0}", i + 1), }, }; sale.Items = new List<SaleLineItem>(); var length = random.Next(1, 5); for (int j = 0; j < length; j++) { var line = new SaleLineItem() { ProductName = GenerateProductName(), ProductDescription = null, Quantity = random.Next(1, 2), UnitPrice = (decimal)((random.NextDouble() + 0.1) * 10000), Status = GenerateEnumValue<SaleStatus>(), }; if (j % 2 == 0) { line.DynamicProperties.Add("Certificate", GenerateCertificate()); line.DynamicProperties.Add("Cut", GenerateCut()); line.DynamicProperties.Add("Caret", GenerateCaret()); line.DynamicProperties.Add("Color", GenerateColor()); line.DynamicProperties.Add("Clarity", GenerateClarity()); } sale.Items.Add(line); } FillCommonValues(sale, now); sales.Add(sale); } using (var db = new SaleDbContext()) { db.SaleHeaders.AddRange(sales); db.SaleLineItems.AddRange(sales.SelectMany(s => s.Items)); db.Contacts.AddRange(sales.SelectMany(s => s.CustomerContacts)); db.SaveChanges(); } }