public async Task <IActionResult> PutQualitySleepCounter(int id, SleepCounter sleepCounterFromTheClient) { // Log what we got from the client Console.WriteLine($"Hey, got a {sleepCounterFromTheClient.QualityRating} from the client."); // Find the sleep counter with this id — this will give us a SleepCounter or a null var sleepCounterFromTheDatabase = await _context.SleepCounters.FindAsync(id); // If we get a null, return NotFound() if (sleepCounterFromTheDatabase == null) { return(NotFound()); } // Otherwise, set the value of the found sleepCounter QualityRating to the sleep score _context.Entry(sleepCounterFromTheDatabase).State = EntityState.Modified; // Pull out the qualityRating from the object we received from the client // and assign that to the object we got from the database. (e.g. copying // the quality rating from the client input to what we are going to update // in the database) sleepCounterFromTheDatabase.QualityRating = sleepCounterFromTheClient.QualityRating; // Save that sleepCounter try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SleepCounterExists(id)) { return(NotFound()); } else { throw; } } // Return either NoContent() or Ok(sleepCounter) — whichever feels best to you. return(Ok(sleepCounterFromTheDatabase)); }
public async Task <ActionResult <SleepCounter> > PreSleepCounter() { // first request // Makes a new SleepCounter SleepCounter sleepCounter = new SleepCounter() { // Set the TimeStart to now TimeStart = DateTime.UtcNow, // And make sure the TimeEnd is null TimeEnd = null }; // second request make a update field time end // third request update quality _context.SleepCounters.Add(sleepCounter); await _context.SaveChangesAsync(); return(CreatedAtAction("GetSleepCounter", new { id = sleepCounter.Id }, sleepCounter)); }