public async Task <string> UpdateCartItemAsync(ShoppingCartRecord item) { string uri = $"{CartBaseUri}{item.CustomerId}/{item.Id}"; var json = JsonConvert.SerializeObject(item); return(await SubmitPutRequestAsync(uri, json)); }
public async Task <IActionResult> Delete(int customerId, int id, ShoppingCartRecord item) { await _webApiCalls.RemoveCartItemAsync(customerId, id, item.TimeStamp); return(RedirectToAction(nameof(Index), new { customerId })); }
public IActionResult Update(CartRecordViewModel record) { _shoppingCartRepo.Context.CustomerId = ViewBag.CustomerId; ShoppingCartRecord dbItem = _shoppingCartRepo.Find(record.Id); var mapper = _config.CreateMapper(); if (record.TimeStamp != null && dbItem.TimeStamp.SequenceEqual(record.TimeStamp)) { dbItem.Quantity = record.Quantity; dbItem.DateCreated = DateTime.Now; try { _shoppingCartRepo.Update(dbItem); CartRecordWithProductInfo updatedItem = _shoppingCartRepo.GetShoppingCartRecord(dbItem.Id); if (updatedItem == null) { return(new EmptyResult()); } CartRecordViewModel newItem = mapper.Map <CartRecordViewModel>(updatedItem); return(PartialView(newItem)); } catch (Exception) { ModelState.AddModelError(string.Empty, "An error occurred updating the cart. Please reload the page and try again."); var updatedItem = _shoppingCartRepo.GetShoppingCartRecord(dbItem.ProductId); CartRecordViewModel newItem = mapper.Map <CartRecordViewModel>(updatedItem); return(PartialView(newItem)); } } ModelState.AddModelError("", "Another user has updated the record."); var item = _shoppingCartRepo.GetShoppingCartRecord(dbItem.ProductId); CartRecordViewModel vm = mapper.Map <CartRecordViewModel>(dbItem); return(PartialView(vm)); }
public void ShouldDeleteCartRecord() { ShoppingCartRecord item = _repo.Find(0, 32); _repo.Context.Entry(item).State = EntityState.Detached; _repo.Delete(item.Id, item.TimeStamp); Assert.Equal(0, _repo.GetAll().Count()); }
public void ShouldNotDeleteMissingCartRecord() { var item = _repo.Find(1); var recordToDelete = new ShoppingCartRecord { Id = 200, TimeStamp = item.TimeStamp }; Assert.Throws <SpyStoreConcurrencyException>(() => _repo.Delete(recordToDelete)); }
public void ShouldThrowWhenUpdatingTooMuchQuantity() { ShoppingCartRecord item = _repo.Find(0, 32); item.Quantity = 100; item.DateCreated = DateTime.Now; InvalidQuantityException ex = Assert.Throws <InvalidQuantityException>(() => _repo.Update(item)); Assert.Equal("Can't add more product than available in stock", ex.Message); }
public void ShouldDeleteOnUpdateIfQuantityLessThanZero() { ShoppingCartRecord item = _repo.Find(0, 32); item.Quantity = -10; item.DateCreated = DateTime.Now; _repo.Update(item); System.Collections.Generic.List <ShoppingCartRecord> shoppingCartRecords = _repo.GetAll().ToList(); Assert.Equal(0, shoppingCartRecords.Count); }
public IActionResult DeleteCartRecord(int recordId, ShoppingCartRecord item) { if (recordId != item.Id) { return(NotFound()); } _repo.Context.CustomerId = item.CustomerId; _repo.Delete(item); return(NoContent()); }
public async Task <string> UpdateCartItemAsync(ShoppingCartRecord item) { // Change Cart Item(Quantity): http://localhost:40001/api/shoppingcart/{customerId}/{id} HTTPPut // Note: Id, CustomerId, ProductId, TimeStamp, DateCreated, and Quantity in the body //{"Id":0,"CustomerId":0,"ProductId":32,"Quantity":2, "TimeStamp":"AAAAAAAA86s=","DateCreated":"1/20/2016"} //http://localhost:40001/api/shoppingcart/0/AAAAAAAA86s= string uri = $"{CartBaseUri}{item.CustomerId}/{item.Id}"; var json = JsonConvert.SerializeObject(item); return(await SubmitPutRequestAsync(uri, json)); }
public void ShouldUpdateQuantity() { ShoppingCartRecord item = _repo.Find(0, 32); item.Quantity = 5; item.DateCreated = DateTime.Now; _repo.Update(item); System.Collections.Generic.List <ShoppingCartRecord> shoppingCartRecords = _repo.GetAll().ToList(); Assert.Equal(1, shoppingCartRecords.Count); Assert.Equal(5, shoppingCartRecords[0].Quantity); }
[HttpPut("{shoppingCartRecordId}")] //Required even if method name starts with Put public IActionResult Update(int customerId, int shoppingCartRecordId, [FromBody] ShoppingCartRecord item) { if (item == null || item.Id != shoppingCartRecordId || !ModelState.IsValid) { return(BadRequest()); } item.DateCreated = DateTime.Now; Repo.Update(item); //Location: http://localhost:8477/api/ShoppingCart/0 (201) return(CreatedAtRoute("GetShoppingCart", new { customerId = customerId })); }
public void ShouldDeleteOnUpdateIfQuantityLessThanZero() { ShoppingCartRecord item = _repo.Find(0, 32); item.Quantity = -10; item.DateCreated = DateTime.Now; _repo.Update(item); List <ShoppingCartRecord> shoppingCartRecords = _repo.GetAll().ToList(); Assert.Empty(shoppingCartRecords); }
public IActionResult Create(int customerId, [FromBody] ShoppingCartRecord item) { if (item == null || !ModelState.IsValid) { return(BadRequest()); } item.DateCreated = DateTime.Now; item.CustomerId = customerId; Repo.Add(item); return(CreatedAtRoute("GetShoppingCart", new { controler = "ShoppingCart", customerId = customerId })); }
public void ShouldUpdateQuantity() { ShoppingCartRecord item = _repo.Find(0, 32); item.Quantity = 5; item.DateCreated = DateTime.Now; _repo.Update(item); List <ShoppingCartRecord> shoppingCartRecords = _repo.GetAll().ToList(); Assert.Single(shoppingCartRecords); Assert.Equal(5, shoppingCartRecords[0].Quantity); }
public ActionResult UpdateShoppingCartRecord(int recordId, ShoppingCartRecord item) { if (item == null || item.Id != recordId || !ModelState.IsValid) { return(BadRequest()); } item.DateCreated = DateTime.Now; _repo.Context.CustomerId = item.CustomerId; _repo.Update(item); //Location: http://localhost:7940/api/ShoppingCart/0 (201) return(CreatedAtRoute("GetShoppingCartRecord", new { controller = "ShoppingCartRecord", recordId = item.Id }, null)); }
public async Task AddToCartAsync(int customerId, int productId, int quantity) { var record = new ShoppingCartRecord { ProductId = productId, CustomerId = customerId, Quantity = quantity }; var response = await _client.PostAsJsonAsync($"{_settings.CartRecordBaseUri}/{customerId}", record); response.EnsureSuccessStatusCode(); }
public void ShouldDeleteOnAddIfQuantityLessThanZero() { var item = new ShoppingCartRecord() { ProductId = 32, Quantity = -10, DateCreated = DateTime.Now, CustomerId = 0 }; _repo.Add(item); Assert.Equal(0,_repo.Count); }
public void ShouldDeleteOnAddIfQuantityEqualZero() { var item = new ShoppingCartRecord() { ProductId = 30, Quantity = -1, DateCreated = DateTime.Now, CustomerId = 0 }; _repo.Add(item); var shoppingCartRecords = _repo.GetAll().ToList(); Assert.Equal(0,shoppingCartRecords.Count(x => x.ProductId==34)); }
//Delete public async Task RemoveCartItemAsync(int id, ShoppingCartRecord item) { var json = JsonConvert.SerializeObject(item); HttpRequestMessage request = new HttpRequestMessage { Content = new StringContent(json, Encoding.UTF8, "application/json"), Method = HttpMethod.Delete, RequestUri = new Uri($"{_settings.CartRecordBaseUri}/{id}") }; var response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); }
public async Task AddToCartAsync(int customerId, int productId, int quantity) { var record = new ShoppingCartRecord { ProductId = productId, CustomerId = customerId, Quantity = quantity }; var uri = $"{_settings.Uri}{_settings.CartRecordBaseUri}/{customerId}"; var response = await PostAsJson(uri, JsonSerializer.Serialize(record)); response.EnsureSuccessStatusCode(); }
public async Task RemoveCartItemAsync(int id, ShoppingCartRecord item) { var json = JsonSerializer.Serialize(item); HttpRequestMessage request = new HttpRequestMessage { Content = CreateStringContent(json), Method = HttpMethod.Delete, RequestUri = new Uri($"{_settings.Uri}{_settings.CartRecordBaseUri}/{id}") }; var response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); }
public IActionResult Create(int customerId, [FromBody] ShoppingCartRecord item) { if (item == null || !ModelState.IsValid) { return(BadRequest()); //Returns a 400 } item.DateCreated = DateTime.Now; item.CustomerId = customerId; Repo.Add(item); //The header value looks like: http://localhost:8477/api/ShoppingCart/0 (It returns a 201) return(CreatedAtRoute("GetShoppingCart", new { controller = "ShoppingCart", customerId = customerId })); }
public void ShouldThrowWhenAddingToMuchQuantity() { _repo.Context.SaveChanges(); var item = new ShoppingCartRecord() { ProductId = 2, Quantity = 500, DateCreated = DateTime.Now, CustomerId = 2 }; var ex = Assert.Throws<InvalidQuantityException>(() => _repo.Update(item)); Assert.Equal("Can't add more product than available in stock", ex.Message); }
public void ShouldUpdateQuantityOnAddIfAlreadyInCart() { var item = new ShoppingCartRecord() { ProductId = 32, Quantity = 1, DateCreated = DateTime.Now, CustomerId = 0 }; _repo.Add(item); var shoppingCartRecords = _repo.GetAll().ToList(); Assert.Single(shoppingCartRecords); Assert.Equal(2,shoppingCartRecords[0].Quantity); }
public void ShouldAddAnItemToTheCart() { var item = new ShoppingCartRecord() { ProductId = 2, Quantity = 3, DateCreated = DateTime.Now, CustomerId = 0 }; _repo.Add(item); var shoppingCartRecords = _repo.GetAll().ToList(); Assert.Equal(2,shoppingCartRecords.Count); Assert.Equal(2,shoppingCartRecords[0].ProductId); Assert.Equal(3,shoppingCartRecords[0].Quantity); }
public ActionResult AddShoppingCartRecord(int customerId, ShoppingCartRecord record) { if (record == null || customerId != record.CustomerId || !ModelState.IsValid) { return(BadRequest()); } record.DateCreated = DateTime.Now; record.CustomerId = customerId; _repo.Context.CustomerId = customerId; _repo.Add(record); //Location: http://localhost:8477/api/ShoppingCartRecord/1 (201) CreatedAtRouteResult createdAtRouteResult = CreatedAtRoute("GetShoppingCart", new { controller = "ShoppingCart", customerId = customerId }, null); return(createdAtRouteResult); }
public void ShouldUpdateQuantityOnAddIfAlreadyInCart() { ShoppingCartRecord item = new ShoppingCartRecord { ProductId = 32, Quantity = 1, DateCreated = DateTime.Now, CustomerId = 0 }; _repo.Add(item); List <ShoppingCartRecord> shoppingCartRecords = _repo.GetAll().ToList(); Assert.Equal(1, shoppingCartRecords.Count); Assert.Equal(2, shoppingCartRecords[0].Quantity); }
private ShoppingCartRecord CreateCartRecord() { var cart = new ShoppingCartRecord() { Guid = Guid.NewGuid(), ModifiedUtc = _clock.UtcNow }; if (Services.WorkContext.CurrentUser != null) { // Attach cart to user cart.OwnerId = Services.WorkContext.CurrentUser.Id; } // Register cart Services.WorkContext.HttpContext.Session[ShoppingCartKey] = cart.Guid; _shoppingCartRepository.Create(cart); return(cart); }
public async Task <IActionResult> Update(int id, CartRecordViewModel record) { var cartRecord = new ShoppingCartRecord { Id = record.Id, Quantity = record.Quantity, ProductId = record.ProductId, TimeStamp = record.TimeStamp, CustomerId = record.CustomerId }; var item = await _serviceWrapper.UpdateShoppingCartRecordAsync(id, cartRecord); if (item == null) { return(new EmptyResult()); } var mapper = _config.CreateMapper(); CartRecordViewModel vm = mapper.Map <CartRecordViewModel>(item); return(PartialView(vm)); }
private ShoppingCartRecord GetCartRecord(bool CreateIfNull = false) { Guid shoppingCartGuid = Guid.Empty; if (Services.WorkContext.HttpContext.Session[ShoppingCartKey] != null) { shoppingCartGuid = (Guid)Services.WorkContext.HttpContext.Session[ShoppingCartKey]; } ShoppingCartRecord cart = null; if (shoppingCartGuid != null && shoppingCartGuid != Guid.Empty) { // try return session cart cart = GetCartRecord(shoppingCartGuid); } if (cart == null && Services.WorkContext.CurrentUser != null) { // try return user cart cart = GetUserCartRecord(Services.WorkContext.CurrentUser.Id); } else if (cart != null && !cart.OwnerId.HasValue && Services.WorkContext.CurrentUser != null) { // Attach cart to user cart.OwnerId = Services.WorkContext.CurrentUser.Id; } if (cart == null && CreateIfNull) { return(CreateCartRecord()); } else { return(cart); } }