public async Task <IActionResult> Create(SubmissionForm submissionForm) { if (!ModelState.IsValid) { return(RedirectToAction("Index", submissionForm)); } DateTime minimum = DateTime.Now.AddYears(-18); //Check age if (minimum < submissionForm.Age) { return(BadRequest(new { message = "bad request age under 18" })); } var serialNumber = await _mvcDrawContext.SerialNumbers.Where(s => s.SerialNumberValue == submissionForm.SerialNumber).FirstOrDefaultAsync(); //Valid serial number? A part of buisness Logic if (serialNumber == null || serialNumber.IsActive == false) { return(BadRequest(new { message = "bad request INVALID serial number" })); } //Does User Exist? var user = await _mvcDrawContext.Users.Where(u => u.EmailAddress == submissionForm.EmailAddress).FirstOrDefaultAsync(); if (user == null) { user = new UserDraw() { FirstName = submissionForm.FirstName, LastName = submissionForm.LastName, EmailAddress = submissionForm.EmailAddress }; var result = await _mvcDrawContext.Users.AddAsync(user); await _mvcDrawContext.SaveChangesAsync(); } Draw draw = new Draw() { SerialNumberId = serialNumber.Id, UserDrawId = user.Id }; var resultForDraw = await _mvcDrawContext.Draws.AddAsync(draw); serialNumber.AmountUsed += 1; if (serialNumber.AmountUsed >= 2) { serialNumber.IsActive = false; } await _mvcDrawContext.SaveChangesAsync(); submissionForm.Entered = true; return(View("Index", submissionForm)); //return RedirectToAction("Index", submissionForm); }
public async Task <IActionResult> Create([Bind("Id,Title,ReleaseDate,Price")] Draw draw) { if (ModelState.IsValid) { _context.Add(draw); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(draw)); }