/// <summary> /// Updates the merchant. /// </summary> /// <param name="merchant">The merchant.</param> /// <exception cref="ApplicationException">Merchant: [{merchant.MerchantID}] is not valid</exception> internal async Task UpdateMerchantAsync(MerchantDBE merchant) { // 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; // try and find the existing merchant var currentMerchant = await this.Merchants.FirstOrDefaultAsync(m => m.MerchantGuid == merchant.MerchantGuid); if (currentMerchant == null) { throw new ApplicationException($"Merchant: [{merchant.MerchantGuid}] is not valid"); } this.Update(merchant); 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; } }
/// <summary> /// Sets the active merchant for demo. /// </summary> /// <param name="merchantToMakeActive">The merchant to make active.</param> internal async Task SetActiveMerchantForDemoAsync(MerchantDBE merchantToMakeActive) { //gets all merchants who are already active (logically should only be 0 or 1) var allMerchants = await this.GetAllMerchantsAsync(); var merchantsToChange = allMerchants.Where(am => am.IsActive).ToList(); //set other active merchant to inactive foreach (var merchant in merchantsToChange) { merchant.IsActive = false; await this.UpdateMerchantAsync(merchant); } //set merchant to active merchantToMakeActive.IsActive = true; //update merchant in the db await this.UpdateMerchantAsync(merchantToMakeActive); }
/// <summary> /// Inserts the merchant /// </summary> /// <param name="newMerchant">The new merchant.</param> /// <returns>MerchantDBE.</returns> /// <remarks> /// only used by the ResetDB method so we can keep the same guids across reloads. /// </remarks> internal async Task InsertMerchantAsync(MerchantDBE newMerchant) { this.Merchants.Add(newMerchant); 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; } }
public async Task <ActionResult <MerchantMBE> > AddMerchant([FromBody] NewMerchantMBE newMerchant) { //trims merchant name so that it doesn't have trailing characters newMerchant.MerchantName = newMerchant.MerchantName.Trim(); // validate the input params //if (!Uri.IsWellFormedUriString(newMerchant.MerchantUrl.ToString(), UriKind.Absolute)) //{ // return BadRequest(new ArgumentException(nameof(newMerchant.MerchantUrl), @"The merchant url is incorrect. Make sure the url has https://")); //} try { // store the new merchant var newDBMerchant = new MerchantDBE() { MerchantName = newMerchant.MerchantName, IsSupportsTips = newMerchant.IsSupportsTips, MerchantUrl = (Uri.IsWellFormedUriString(newMerchant.MerchantUrl.ToString(), UriKind.Absolute)) ? newMerchant.MerchantUrl : new Uri("https://www.testmerchant.com") }; await _dbContext.InsertMerchantAsync(newDBMerchant); // convert DB entity to the public entity type var merchant = (MerchantMBE)newDBMerchant; // return the response return(CreatedAtAction(nameof(GetMerchant), new { merchantGuid = merchant.MerchantGuid }, merchant)); } catch (Exception ex) { return(BadRequest(new ApplicationException($"Error: [{ex.Message}] Failed trying to add merchant: [{newMerchant.MerchantName}]"))); } }