コード例 #1
0
        public async Task <ActionResult <Book> > SaveNewBook(Book newBook)
        {
            _db.Books.Add(newBook);
            await _db.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetBookById), new { id = newBook.Id }, newBook));
        }
コード例 #2
0
        public async Task <IActionResult> PutCategory([FromRoute] int id, [FromBody] Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != category.CategoryId)
            {
                return(BadRequest());
            }

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #3
0
        public async Task <ActionResult <User> > Create([FromBody, BindRequired] User newUser)
        {
            if (NameExists(newUser.Name) || EmailExists(newUser.Email))
            {
                return(UnprocessableEntity(new { error = "Nombre de usuario o correo no disponible." }));
            }

            TryValidateModel(newUser);


            _db.Users.Add(newUser);
            await _db.SaveChangesAsync();

            return(newUser);
        }