public async Task <IActionResult> AddToCart(int menuId, int availabilityDateId, int userProfileId) { try { var canteenSystemDbContext = _context.MealMenus.Where(x => x.Id == menuId && x.MealMenuAvailabilities.Any(y => y.Id == availabilityDateId)).Include(x => x.MealMenuAvailabilities); var mealMenu = await canteenSystemDbContext.FirstOrDefaultAsync(); if (mealMenu != null) { var availableDate = mealMenu.MealMenuAvailabilities. Where(x => x.Id == availabilityDateId).FirstOrDefault().AvailabilityDate; var isExistingOrder = _context.Carts.FirstOrDefault(x => x.MealMenuId == menuId && x.MealAvailableDate == availableDate && x.UserProfileId == userProfileId); if (isExistingOrder == null) { var cart = new Cart { MealMenuId = mealMenu.Id, MealAvailableDate = availableDate, Price = mealMenu.Price, Quantity = 1, CreatedDate = DateTime.Now, UpdatedDate = DateTime.Now, UserProfileId = userProfileId }; _context.Add(cart); _context.SaveChanges(); } else { return(Json(new { Status = false })); } } return(Json(new { Status = true })); } catch (Exception ex) { //jhgh } return(Json(new { Status = false })); }
public async Task <IActionResult> Create([Bind("Id,Name,Description,DiscountPercentage,ValidFromDate,ValidToDate")] Discount discount) { if (ModelState.IsValid) { Context.Add(discount); await Context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(discount)); }
public async Task <IActionResult> Create([Bind("Id,Name")] MealType mealType) { if (ModelState.IsValid) { _context.Add(mealType); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(mealType)); }
public async Task <IActionResult> Create([Bind("Id,OrderReference,UserProfileId,TotalPrice,CreatedDate,UpdatedDate")] Order order) { if (ModelState.IsValid) { _context.Add(order); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["UserProfileId"] = new SelectList(_context.UserProfiles, "Id", "Department", order.UserProfileId); return(View(order)); }
public async Task <IActionResult> Create([Bind("Id,MealMenuId,MealMenuOrderDate,OrderId,Quantity,Price")] OrderItem orderItem) { if (ModelState.IsValid) { _context.Add(orderItem); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["MealMenuId"] = new SelectList(_context.MealMenus, "Id", "MealName", orderItem.MealMenuId); ViewData["OrderId"] = new SelectList(_context.Orders, "Id", "OrderReference", orderItem.OrderId); return(View(orderItem)); }
public async Task <IActionResult> Create([Bind("Id,MealName,MealTypeId,Price,DiscountId,ImageName")] MealMenu mealMenu) { if (ModelState.IsValid) { _context.Add(mealMenu); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["DiscountId"] = new SelectList(_context.Discounts, "Id", "Description", mealMenu.DiscountId); ViewData["MealTypeId"] = new SelectList(_context.MealTypes, "Id", "Name", mealMenu.MealTypeId); return(View(mealMenu)); }
public async Task <IActionResult> Register(RegisterModel model) { if (!ModelState.IsValid) { return(View(model)); } var userExists = await userManager.FindByNameAsync(model.Email); var studentId = 0; if (!model.IsParent) { var existingRoleNumber = _context.UserProfiles.Where(x => x.RollNumber == model.Rollnumber).FirstOrDefault(); if (userExists != null || existingRoleNumber != null) { ModelState.AddModelError("error", "User already exists!"); return(View("Register", model)); } } else { var studentEmailAddress = _context.UserProfiles.Where(x => x.EmailAddress == model.StudentEmail).FirstOrDefault(); if (studentEmailAddress == null) { ModelState.AddModelError("error", "The student email address doesnt exist"); return(View("Register", model)); } studentId = studentEmailAddress.Id; } var applicationUser = new ApplicationUser() { Email = model.Email, SecurityStamp = Guid.NewGuid().ToString(), UserName = model.Email }; var result = await userManager.CreateAsync(applicationUser, model.Password); if (!result.Succeeded) { ModelState.AddModelError("error", "User creation failed! Please check user details and try again."); return(View("Register", model)); } var newlyCreatedUser = await userManager.FindByNameAsync(model.Email); if (newlyCreatedUser != null) { var user = new UserProfile() { Name = model.Firstname + " " + model.Lastname, EmailAddress = model.Email, Department = model.Department, RollNumber = model.Rollnumber, ApplicationUserId = newlyCreatedUser.Id }; Random generator = new Random(); int r = generator.Next(100000, 1000000); _context.Add(user); _context.SaveChanges(); if (!model.IsParent) { var card = new Card() { CardNumber = $"CN{r}SM", IsActive = true, AvailableBalance = 0, UserProfileId = user.Id }; _context.Add(card); } else { var parentMapping = new ParentMapping() { StudentId = studentId, ParentId = user.Id }; _context.Add(parentMapping); } _context.SaveChanges(); } if (!model.IsParent) { if (!await roleManager.RoleExistsAsync(UserRoles.Student)) { await roleManager.CreateAsync(new IdentityRole(UserRoles.Student)); } if (await roleManager.RoleExistsAsync(UserRoles.Student)) { await userManager.AddToRoleAsync(newlyCreatedUser, UserRoles.Student); } } else { if (!await roleManager.RoleExistsAsync(UserRoles.Parents)) { await roleManager.CreateAsync(new IdentityRole(UserRoles.Parents)); } if (await roleManager.RoleExistsAsync(UserRoles.Parents)) { await userManager.AddToRoleAsync(newlyCreatedUser, UserRoles.Parents); } } return(RedirectToAction(nameof(Index), new { message = "User has been created and which needs to be verfied by admin. " + "Please contact admin to verify your registration." })); }