public async Task AddChoice() { var options = new DbContextOptionsBuilder <MoviesContext>() .UseInMemoryDatabase(databaseName: "MRAddChoice") .Options; addPartyChoices(options); using (var context = new MoviesContext(options)) { SQLRepository controller = new SQLRepository(context); PartyChoice choiceToAdd = new PartyChoice { Id = 4, PartyId = 1, MovieId = 5, Score = 1 }; await controller.AddChoice(choiceToAdd); List <PartyChoice> actual = controller.GetAllPartyChoicesForParty(1); Assert.Equal(4, actual.Count); } }
public async Task AddChoice(PartyChoice choice) { PartyChoice choiceFromDb = _context.PartyChoices.Where(c => c.PartyId == choice.PartyId && c.MovieId == choice.MovieId).FirstOrDefault(); if (choiceFromDb != null) { choiceFromDb.Score += choice.Score; await _context.SaveChangesAsync(); } else { await _context.PartyChoices.AddAsync(choice); await _context.SaveChangesAsync(); } }
public async Task <IActionResult> AddChoice(PartyChoice partyChoice) { // TODO: replace with DTO if (ModelState.IsValid == false) { return(BadRequest("Bad request.", ModelState)); } //PartyChoice newChoice = new PartyChoice //{ // PartyId = partyId, // MovieId = movieId, // Score = 1 //}; await _repository.AddChoice(partyChoice); return(Ok()); }