Example #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="model"></param>
 /// <param name="lineNumber"></param>
 /// <param name="caller"></param>
 /// <returns></returns>
 public async Task AddAsync(T model, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
 {
     if (string.IsNullOrEmpty(model.Id))
     {
         model.Id = ObjectId.GenerateNewId().ToString();
     }
     _cache?.AddAsync(model.Id, model);
     await _db.InsertOneAsync(model);
 }
Example #2
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);
            }
        }
Example #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="model"></param>
 /// <param name="lineNumber"></param>
 /// <param name="caller"></param>
 /// <returns></returns>
 public Task AddAsync(T model, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
 {
     _cache?.AddAsync(model.Id, model);
     _db.InsertOneAsync(model);
     return(Task.CompletedTask);
 }