/// <summary> /// Calculates the total based on TrolleyItem specifications. /// </summary> /// <param name="tItem"></param> /// <returns></returns> public async Task <decimal> GetTrolleyTotalAsync(TrolleyItem tItem) { if (tItem == null) { return(0.0m); } var uri = UriHelper.GeTrolleyCalculatorUri(); var result = await HttpHelper.PostAsync <decimal>(uri, tItem); return(result); }
/// <summary> /// Makes a POST call to an external API /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="tItem">Trolley Item: It specifies the price, quantity and specials</param> /// <returns></returns> public static async Task <T> PostAsync <T>(string url, TrolleyItem tItem) { StringContent cont = new StringContent(JsonConvert.SerializeObject(tItem), Encoding.UTF8, "application/json"); var httpClient = new HttpClient(); var response = await httpClient.PostAsync(new Uri(url), cont); var responseContent = response.Content; var rs = await responseContent.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <T>(rs); return(result); }
public async Task Edit(TrolleyItem _trolleyItem) { var _trolleyItemToEdit = await GetById(_trolleyItem.Id); if (_trolleyItemToEdit != null) { _trolleyItemToEdit.Quantity = _trolleyItem.Quantity; _trolleyItemToEdit.DateCreated = _trolleyItem.DateCreated; _trolleyItemToEdit.ProductId = _trolleyItem.ProductId; _trolleyItemToEdit.TrolleyId = _trolleyItem.TrolleyId; await _context.SaveChangesAsync(); } }
public async Task <decimal> CalculateTrolleyTotal([FromBody] TrolleyItem tItem) { try { _logger.LogInformation("Trolley total was Calculated."); var user = await _trolleyService.GetTrolleyTotalAsync(tItem); return(user); } catch (Exception ex) { _logger.LogError($"Something went wrong: {ex}"); throw; } }
public async Task Create(TrolleyItem _trolleyItem) { await _context.TrolleyItems.AddAsync(_trolleyItem); await _context.SaveChangesAsync(); }