public async void NoDuplicateSoldProduct() { Guid serial = Guid.NewGuid(); SoldProduct product = new SoldProduct { SerialNumber = serial }; RaffleDbContext context = getDb("NoDupe"); context.SoldProducts.Add(product); await context.SaveChangesAsync(); RaffleEntry entry = getEntry(); entry.SoldProduct.SerialNumber = serial; RaffleApiController controller = new RaffleApiController( context, new EntryValidator()); IStatusCodeActionResult result = await controller.PostRaffleEntry(entry); StatusCodeResult statusResult = Assert.IsType <StatusCodeResult>(result); Assert.Equal((int)HttpStatusCode.OK, statusResult.StatusCode); Assert.Equal(1, await context.SoldProducts.CountAsync()); }
public async Task <IActionResult> Create([Bind("ID,Firstname,Lastname,Email,Age,Number")] Raffle raffle) { if (ModelState.IsValid) { if (_context.Serialnumbers.Any(s => s.Number == raffle.Number)) { if (_context.Raffle.Where(r => r.Number.Equals(raffle.Number)).Count() < 2) { _context.Add(raffle); await _context.SaveChangesAsync(); return(View("Entered")); } } } return(View()); }
public async Task <StatusCodeResult> PostRaffleEntry([Bind("FirstName,LastName,Email,Age,SoldProduct")] RaffleEntry entry) { if (!ModelState.IsValid) { return(StatusCode((int)HttpStatusCode.BadRequest)); } bool validEntry = _validator.ValidateEntry(_context.SoldProducts.AsQueryable(), _context.Entries.AsQueryable(), entry); if (validEntry) { _context.Entries.Add(entry); await _context.SaveChangesAsync(); return(StatusCode((int)HttpStatusCode.OK)); } return(StatusCode((int)HttpStatusCode.BadRequest)); }
public async Task <IActionResult> Post(Person person) { _logger.LogInformation($"Request: {JsonConvert.SerializeObject(person)}."); _logger.LogInformation($"ModelState: {JsonConvert.SerializeObject(ModelState)}."); if (ModelState.IsValid) { if ((await _raffleDbContext.People.AnyAsync(p => p.Name == person.Name))) { ModelState.TryAddModelError("Name", "There is a record with this name."); return(View("Index", person)); } await _raffleDbContext.People.AddAsync(person); await _raffleDbContext.SaveChangesAsync(); return(View("Thanks")); } return(View("Index", person)); }