Exemplo n.º 1
0
        /// <summary>
        /// Updates a customer
        /// </summary>
        /// <param name="demoCustomer">object containing information about customers</param>
        /// <exception cref="ApplicationException">Customer: [{customer.CustomerID}] is not valid</exception>
        public async Task UpdateDemoCustomerAsync(DemoCustomerDBE demoCustomer)
        {
            // turn off change tracking since we are going to overwite the entity
            // Note: I would not do this if there was a db assigned unique id for the record
            this.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

            var currentCustomer = await this.DemoCustomers.SingleOrDefaultAsync(dc => dc.DemoCustomerId == demoCustomer.DemoCustomerId);

            if (currentCustomer != null)
            {
                this.Update(demoCustomer);

                try
                {
                    await this.SaveChangesAsync();
                }
                catch (DbUpdateException ex)
                {
                    // exception was raised by the db (ex: UK violation)
                    var sqlException = ex.InnerException;

                    // we do this to disconnect the exception that bubbles up from the dbcontext which will be disposed when it leaves this method
                    throw new ApplicationException(sqlException.Message);
                }
                catch (Exception)
                {
                    // rethrow exception
                    throw;
                }
            }
            else
            {
                throw new ApplicationException($"Customer: [{demoCustomer.DemoCustomerId}] is not valid on MerchantID: [{demoCustomer.MerchantId}]");
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <DemoCustomerMBE> > AddDemoCustomer([FromRoute] Guid merchantGuid, [FromBody] NewDemoCustomerMBE newDemoCustomer)
        {
            //trims Customer name so that it doesn't have trailing characters
            newDemoCustomer.CustomerName = newDemoCustomer.CustomerName.Trim();

            // validate request data
            (bool isValidPhoneNo, string formatedPhoneNo, string normalizedPhoneNo) = Utilities.PhoneNoHelpers.NormalizePhoneNo(newDemoCustomer.CustomerPhoneNo);
            if (!isValidPhoneNo)
            {
                ModelState.AddModelError(nameof(newDemoCustomer.CustomerPhoneNo), $"[{newDemoCustomer.CustomerPhoneNo}] is NOT a supported Phone No format.");
                return(BadRequest(ModelState));
            }

            //query the db for the merchant
            var dbMerchant = await _dbContext.GetMerchantAsync(merchantGuid);

            // if we did not find a matching merchant
            if (dbMerchant == null)
            {
                return(BadRequest(new ArgumentException($"MerchantGuid: [{merchantGuid}] not found", nameof(merchantGuid))));
            }
            else
            {
                newDemoCustomer.CustomerPhoneNo = formatedPhoneNo;
            }

            try
            {
                //Store the new customer
                var newDBDemoCustomer = new DemoCustomerDBE()
                {
                    MerchantId      = dbMerchant.MerchantId,
                    CustomerName    = newDemoCustomer.CustomerName,
                    CustomerPhoneNo = newDemoCustomer.CustomerPhoneNo
                };

                await _dbContext.InsertDemoCustomerAsync(newDBDemoCustomer);

                // convert DB entity to the public entity type
                var customer = (DemoCustomerMBE)newDBDemoCustomer;

                // return the response
                return(CreatedAtAction(nameof(GetDemoCustomer), new { merchantGuid = merchantGuid, demoCustomerGuid = customer.CustomerGuid }, customer));
            }
            catch (Exception ex)
            {
                return(BadRequest(new ApplicationException($"Error: [{ex.Message}] trying to add Phone No: [{newDemoCustomer.CustomerPhoneNo}] to merchant: [{merchantGuid}]")));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Inserts new customer into DB (only used by the ResetDB method so we can keep the same guids across reloads).
        /// </summary>
        /// <param name="newDemoCustomer">object containing information for new customer</param>
        /// <returns></returns>
        /// <remarks>
        /// only used by the ResetDB method so we can keep the same guids across reloads.
        /// </remarks>
        internal async Task InsertDemoCustomerAsync(DemoCustomerDBE newDemoCustomer)
        {
            this.DemoCustomers.Add(newDemoCustomer);

            try
            {
                await this.SaveChangesAsync();
            }
            catch (DbUpdateException ex)
            {
                // exception was raised by the db (ex: UK violation)
                var sqlException = ex.InnerException;

                // we do this to disconnect the exception that bubbles up from the dbcontext which will be disposed when it leaves this method
                throw new ApplicationException(sqlException.Message);
            }
            catch (Exception)
            {
                // rethrow exception
                throw;
            }
        }