コード例 #1
0
        public async Task <ActionResult <Scoldings> > PostScoldings(Scoldings scoldings)
        {
            // Indicate to the database context we want to add this new record
            _context.Scolding.Add(scoldings);
            await _context.SaveChangesAsync();

            // Return a response that indicates the object was created (status code `201`) and some additional
            // headers with details of the newly created object.
            return(CreatedAtAction("GetScoldings", new { id = scoldings.Id }, scoldings));
        }
コード例 #2
0
        public async Task <IActionResult> PutScoldings(int id, Scoldings scoldings)
        {
            // If the ID in the URL does not match the ID in the supplied request body, return a bad request
            if (id != scoldings.Id)
            {
                return(BadRequest());
            }

            // Tell the database to consider everything in scoldings to be _updated_ values. When
            // the save happens the database will _replace_ the values in the database with the ones from scoldings
            _context.Entry(scoldings).State = EntityState.Modified;

            try
            {
                // Try to save these changes.
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                // Ooops, looks like there was an error, so check to see if the record we were
                // updating no longer exists.
                if (!ScoldingsExists(id))
                {
                    // If the record we tried to update was already deleted by someone else,
                    // return a `404` not found
                    return(NotFound());
                }
                else
                {
                    // Otherwise throw the error back, which will cause the request to fail
                    // and generate an error to the client.
                    throw;
                }
            }

            // return NoContent to indicate the update was done. Alternatively you can use the
            // following to send back a copy of the updated data.
            //
            // return Ok(scoldings)
            //
            return(NoContent());
        }
コード例 #3
0
        public async Task <ActionResult <Scoldings> > Scold(int id, Scoldings scolding)
        {
            var pet = await _context.Pets.FindAsync(id);

            if (pet == null)
            {
                return(NotFound());
            }
            pet.HappinessLevel -= 5;
            if (pet.HappinessLevel < 0)
            {
                pet.HappinessLevel = 0;
            }
            scolding.When  = DateTime.Now;
            scolding.PetId = pet.Id;
            _context.Scoldings.Add(scolding);
            await _context.SaveChangesAsync();

            return(Ok(scolding));
        }