/// <summary> /// Updates the specified cart item. /// </summary> /// <param name="cart">The cart.</param> /// <param name="userId">The user identifier.</param> /// <returns>Updated infomartion</returns> public async Task <CartResponseDto> Update(CartRequestDto cart, int userId) { CartResponseDto cartItem = null; using (SqlConnection connection = _dbContext.GetConnection()) { SqlCommand command = new SqlCommand("sp_cart_update", connection) { CommandType = System.Data.CommandType.StoredProcedure }; command.Parameters.AddWithValue("@userId", userId); command.Parameters.AddWithValue("@bookId", cart.BookId); command.Parameters.AddWithValue("@quantity", cart.Quantity); await connection.OpenAsync(); using (SqlDataReader reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { cartItem = MapReaderToCartDtoAsync(reader); } } } return(cartItem); }
public async Task <CartResponseDto> GetCart() { CartResponseDto response = new CartResponseDto(); string accessToken = await GetAccessToken(); HttpClient client = new HttpClient(); client.BaseAddress = new Uri("https://localhost:44396"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); for (int i = 1; i < 5; i++) { HttpResponseMessage clientResponse = await client.GetAsync($"/api/Products/{i}"); if (clientResponse.IsSuccessStatusCode) { ProductDetail product = await System.Text.Json.JsonSerializer.DeserializeAsync <ProductDetail>(await clientResponse.Content.ReadAsStreamAsync(), new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); response.items.Add(new CartItem { productDetail = product, quantiy = i }); } } return(response); }
public async Task <IActionResult> AddToCart(CartRequestDto cart) { int userId = Convert.ToInt32(HttpContext.Items["userId"]); CartResponseDto cartItem = await _service.Insert(cart, userId); return(Ok(new Response <CartResponseDto> { StatusCode = (int)HttpStatusCode.OK, Message = ResponseMessage.SUCCESSFUL, Data = cartItem })); }
/// <summary> /// Updates the specified cart. /// </summary> /// <param name="cart">The cart.</param> /// <param name="userId">The user identifier.</param> /// <returns>Updated cart item infomartion</returns> public async Task <CartResponseDto> Update(CartRequestDto cart, int userId) { try { CartResponseDto responseDto = await _repository.Update(cart, userId); await _cacheRepository.UpdateAsync(userId.ToString(), responseDto, responseDto.Book.Id); return(responseDto); } catch (SqlException e) when(e.Number == 50000) { throw new BookstoreException(e.Message); } }
public async Task AddAsync(string key, CartResponseDto cart) { string cachedData = await _service .GetCachedResponseAsync(key); if (cachedData == null) { return; } CartDetailedResponseDto cartDetailed = JsonConvert .DeserializeObject <CartDetailedResponseDto>(cachedData ); cartDetailed.cartItems.Add(cart); await _service.CacheResponseAsync(key, cartDetailed, TimeSpan.FromSeconds(600)); }
public async Task UpdateAsync(string key, CartResponseDto requestDto, int bookId) { CartDetailedResponseDto cartDetailed = JsonConvert .DeserializeObject <CartDetailedResponseDto>(await _service .GetCachedResponseAsync(key) ); if (cartDetailed == null) { return; } foreach (var item in cartDetailed.cartItems.Where(cart => cart.Book.Id == bookId)) { item.ItemQuantity = requestDto.ItemQuantity; } await _service.CacheResponseAsync(key , cartDetailed , TimeSpan.FromSeconds(600)); }
public async Task <IActionResult> UpdateInCart(CartRequestDto cart) { int userId = Convert.ToInt32(HttpContext.Items["userId"]); CartResponseDto cartItem = await _service.Update(cart, userId); if (cartItem == null) { return(BadRequest(new Response <CartResponseDto> { StatusCode = (int)HttpStatusCode.BadRequest, Message = ResponseMessage.CART_ITEM_NOT_FOUND, Data = null })); } return(Ok(new Response <CartResponseDto> { StatusCode = (int)HttpStatusCode.OK, Message = ResponseMessage.SUCCESSFUL, Data = cartItem })); }
/// <summary> /// Inserts the specified cart. /// </summary> /// <param name="cart">The cart.</param> /// <param name="userId">The user identifier.</param> /// <returns>Added item info</returns> /// <exception cref="BookstoreException"> /// Invalid user id! /// or /// Already in cart /// </exception> public async Task <CartResponseDto> Insert(CartRequestDto cart, int userId) { try { CartResponseDto responseDto = await _repository.Insert(cart, userId); await _cacheRepository.AddAsync(userId.ToString(), responseDto); return(responseDto); }catch (SqlException e) when(e.Number == SqlErrorNumbers.CONSTRAINT_VOILATION) { throw new BookstoreException("Invalid user id!"); } catch (SqlException e) when(e.Number == SqlErrorNumbers.DUPLICATEKEY) { throw new BookstoreException("Already in cart"); } catch (SqlException e) when(e.Number == 50000) { throw new BookstoreException(e.Message); } }