public async Task <IActionResult> Create([Bind("Id,Discount")] Promotion promotion) { if (DiscountExists(promotion.Discount)) { return(View(promotion) .WithWarning("Promo Exists", "An equivalent promotion exists. Try a different value.")); } if (ModelState.IsValid) { if (promotion.Discount > 0) { _context.Add(promotion); await _context.SaveChangesAsync(); } else { return(View(promotion) .WithWarning("Invalid Promo", "Promo must be a value greater than zero.")); } return(RedirectToAction(nameof(Index)) .WithSuccess("Success", "$" + promotion.Discount + " promotion successfully created."));; } return(View(promotion) .WithWarning("Uh-Oh!", "Something went wrong. Try again.")); }
public async Task <IActionResult> Create([Bind("Id,CustomerId")] Order order) { if (ModelState.IsValid) { _context.Add(order); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CustomerId"] = new SelectList(_context.Customer, "Id", "Email", order.CustomerId); return(View(order)); }
public async Task <IActionResult> Create([Bind("Id,Name,Email,Phone")] Customer customer) { if (ModelState.IsValid) { _context.Add(customer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index)) .WithSuccess("Registration Successful", customer.Name + " was successfully registered as a new customer.")); } return(View(customer) .WithWarning("Uh-Oh!", "Something went wrong. Try again.")); }
public async Task <string> DeleteConfirmed(int id) { try { Role role = await _context.Role.FindAsync(id); _context.Role.Remove(role); await _context.SaveChangesAsync(); return("The " + role.Title + " role was deleted successfully"); } catch (Exception e) { if (!RoleExists(id)) { return("Role Id #" + id + " doesn't exist"); } else { Console.WriteLine(e.Message.ToString()); return("Delete staff associated with this role and try again."); throw; } } }
public async Task <IActionResult> Create([Bind("Id,ChannelName")] Channel channel) { if (ModelState.IsValid) { if (ChannelExists(channel.ChannelName)) { return(View(channel) .WithWarning("Channel Exists", "Channel already exists. Try a different name.")); } _context.Add(channel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index)) .WithSuccess("Success", channel.ChannelName + " channel created successfully")); } return(View(channel)); }
/// <summary> /// Creates staffLink for new staff from user selected values /// </summary> /// <param name="staff_userInput"></param> /// <param name="_newStaff"></param> /// <returns></returns> private async Task createStaffLink(Staff staff_userInput, Staff _newStaff) { Staff newStaff = await UserManager.FindByEmailAsync(_newStaff.Email); Role role = await _context.Role .FirstOrDefaultAsync(r => r.Title == staff_userInput.StaffLink.Role.Title); StaffType staffType = await _context.StaffType .FirstOrDefaultAsync(st => st.Type == staff_userInput.StaffLink.StaffType.Type); newStaff.StaffLink = new StaffLink { StaffId = newStaff.Id, RoleId = role.Id, StaffTypeId = staffType.Id }; _context.StaffLink.Add(newStaff.StaffLink); await _context.SaveChangesAsync(); }
public async Task <IActionResult> Login() { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Total")); } using (StaffController staffController = new StaffController(UserManager, SignInManager, _certitrackContext)) { try { Staff staff = await UserManager.FindByEmailAsync("*****@*****.**").ConfigureAwait(false); if (staff == null) { Role role = await RoleManager.FindByNameAsync("Admin").ConfigureAwait(false); StaffType staffType = await _certitrackContext.StaffType.FirstOrDefaultAsync().ConfigureAwait(false); if (role == null) { role = new Role { Title = "Admin", Description = "Can create, edit, delete anyone and anything. Basically, God mode.", Name = "Admin" }; await RoleManager.CreateAsync(role).ConfigureAwait(false); } if (staffType == null) { staffType = new StaffType { Type = "Head Therapist" }; await _certitrackContext.StaffType.AddAsync(staffType).ConfigureAwait(false); await _certitrackContext.SaveChangesAsync().ConfigureAwait(false); } StaffLink staffLink = new StaffLink { Role = role, StaffType = staffType }; staff = new Staff { UserName = "******", Email = "*****@*****.**", Name = "Admin", Password = "******", StaffLink = staffLink }; if (staff == null) { return(View()); } await staffController.Create(staff).ConfigureAwait(false); IdentityResult result = await UserManager.AddToRoleAsync(staff, "Admin").ConfigureAwait(false); // output result to console - debugging Console.WriteLine("UserManager.AddToRoleAsync - Result: " + result); ViewBag.Message = "DB Seed Successful - Staff: '" + staff.Name + "' was created"; } } catch (DbUpdateConcurrencyException e) { ViewBag.Message = e.Message; } } return(View()); }
public async Task <IActionResult> Edit(int id, [Bind("Customer,Channel,Certificate,Price,Promotion,Staff")] CertificateEditViewModel certificateEditViewModel) { if (certificateEditViewModel == null) { throw new ArgumentNullException(nameof(certificateEditViewModel)); } if (!CertificateExists(id)) { return(RedirectToAction(nameof(Edit)) .WithDanger("Update Not Successful", "Certificate Id: " + id + " doesn't exist")); } if (ModelState.IsValid) { try { Certificate certificate = await _context.Certificate.FindAsync(id); CertificateLink certificateLink = await _context.CertificateLink.FindAsync(id); Channel channel = await _context.Channel .Where(ch => ch.Id == certificateLink.ChannelId) .FirstOrDefaultAsync(); Customer customer = await _context.Customer .Where(c => c.Id == certificateLink.CustomerId) .FirstOrDefaultAsync(); Promotion promotion = await _context.Promotion .Where(p => p.Id == certificateLink.PromotionId) .FirstOrDefaultAsync(); Staff staff = await _context.Staff .Where(s => s.Id == certificateLink.StaffId) .FirstOrDefaultAsync(); certificate.ExpiryDate = certificateEditViewModel.Certificate.ExpiryDate; certificate.DateRedeemed = certificateEditViewModel.Certificate.DateRedeemed; certificate.Price = certificateEditViewModel.Certificate.Price; certificateLink.ChannelId = await _context.Channel .Where(ch => ch.ChannelName == certificateEditViewModel.Channel.ChannelName) .Select(ch => ch.Id) .FirstOrDefaultAsync(); //i believe this customerid is giving me an error on the customer name change. //so i hardcoded the query certificateLink.CustomerId = await _context.Customer .Where(c => c.Name == certificateEditViewModel.Customer.Name) .Select(c => c.Id) .FirstOrDefaultAsync(); certificateLink.PromotionId = await _context.Promotion .Where(p => p.Discount == certificateEditViewModel.Promotion.Discount) .Select(p => p.Id) .FirstOrDefaultAsync(); certificateLink.StaffId = await _context.Staff .Where(s => s.Name == certificateEditViewModel.Staff.Name) .Select(s => s.Id) .FirstOrDefaultAsync(); //if name is not is not equal to the model name if (customer.Name != certificateEditViewModel.Customer.Name) { //there is an error so i used try and catch to redirect to index try { //using fromsql func to create customer update query for name only _context.Customer.FromSql($"UPDATE dbo.customer SET dbo.customer.name = {certificateEditViewModel.Customer.Name} WHERE id = {customer.Id}").FirstOrDefault(); } catch (SqlException) { //there is an id error but it still updates customer so i used the exception like a success message return(RedirectToAction(nameof(Index)).WithSuccess("Cert#" + certificate.CertificateNo, " Successfully Updated! ")); } } _context.UpdateRange(certificate, certificateLink); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } //redirects back to customer details if origin view if (TempData["_ReturnRoute.Update.Controller"] != null) { string message = null; object _ReturnRouteController = TempData["_ReturnRoute.Update.Controller"]; TempData["_ReturnRoute.Update.Controller"] = null; if ((string)_ReturnRouteController == "Certificates" || (string)_ReturnRouteController == "Orders") { message = "Certificate #" + _context.Certificate.FindAsync(id).Result.CertificateNo + " updated successfully"; } return(Redirect("/" + _ReturnRouteController + "/" + TempData["_ReturnRoute.Update.Action"] + "/" + TempData["_ReturnRoute.Update.Id"]) .WithSuccess("Update Successful", message)); } return(RedirectToAction(nameof(Index)) .WithSuccess("Update Successful", "Certificate #" + _context.Certificate.FindAsync(id).Result.CertificateNo + " updated successfully")); } return(RedirectToAction(nameof(Index)) .WithDanger("Update Not Successful", "Something went wrong. Try again.")); }