public async Task <IActionResult> Verify([Bind("AccountName")] CreateCustomerAccounts test) { CustomerAccount customerAccount = await _context.CustomerAccounts.SingleOrDefaultAsync( c => c.AccountName == test.AccountName); if (customerAccount == null) { return(Json(true)); } return(Json($"{test.AccountName} is already in use!")); }
public async Task <IActionResult> Create([Bind("AccountName,Comments,isVisible,EffectiveStartDate,EffectiveEndDate,RatePerHour")] CreateCustomerAccounts customerAccount) { var loginIdName = _userManager.GetUserName(User); UserInfo currentUser = await _context.UserInfo .Where(userId => userId.LoginUserName == loginIdName) .SingleAsync(); CustomerAccount newCustomer = new CustomerAccount(); newCustomer.AccountName = customerAccount.AccountName; newCustomer.Comments = customerAccount.Comments; newCustomer.IsVisible = customerAccount.isVisible; newCustomer.CreatedAt = DateTime.Now; newCustomer.CreatedById = currentUser.UserInfoId; newCustomer.UpdatedAt = DateTime.Now; newCustomer.UpdatedById = currentUser.UserInfoId; try { _context.Add(newCustomer); await _context.SaveChangesAsync(); // Account rates, the code is initialized after the database is updated so that the database can generate the id for the customer accounts. // TODO: Implement Account Rates, probably with ViewModel, though I'm not sure if I should create ViewModel for everything. var customerId = await _context.CustomerAccounts.Where(c => c.AccountName == customerAccount.AccountName) .Select(x => x.CustomerAccountId) .SingleAsync(); AccountRate accRate = new AccountRate(); accRate.CustomerAccountId = customerId; accRate.EffectiveStartDate = customerAccount.EffectiveStartDate.Date; accRate.RatePerHour = customerAccount.RatePerHour; // TODO: Fix the null checking if possible but it's okay // C# 6.0 Monadic null checking // Credits - https://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated // Fix on CA2 rip //accRate.EffectiveEndDate = customerAccount?.EffectiveEndDate.Value.Date; if (customerAccount.EffectiveEndDate != null) { accRate.EffectiveEndDate = customerAccount.EffectiveEndDate.Value.Date; } else { accRate.EffectiveEndDate = null; } try { _context.Add(accRate); TempData["Success"] = "The Customer Account has been successfully created."; await _context.SaveChangesAsync(); } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists, " + "see your system administrator."); } return(RedirectToAction("Index")); } catch (DbUpdateException) { return(View(nameof(Create))); } }