Exemple #1
0
        /// <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);
        }
Exemple #2
0
        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
            }));
        }
Exemple #3
0
        /// <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);
            }
        }
Exemple #4
0
        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
            }));
        }
Exemple #5
0
        /// <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);
            }
        }