Beispiel #1
0
        public async Task <IHttpActionResult> PutAuthor(int id, Author author)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != author.Id)
            {
                return(BadRequest());
            }

            db.Entry(author).State = EntityState.Modified;

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
        public async Task <IHttpActionResult> PutBook(int id, BookDetailDto bookDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != bookDto.Id)
            {
                return(BadRequest());
            }



            if (bookDto.AuthorId == 0)
            {
                Author newAu = new Author
                {
                    Name = bookDto.AuthorName
                };
                IHttpActionResult result = await new AuthorsController().PostAuthor(newAu);
                bookDto.AuthorId = newAu.Id;
            }

            Book book = Mapper.Map <BookDetailDto, Book>(bookDto);

            book.Id = bookDto.Id;
            db.Entry(book).State = EntityState.Modified;

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

            return(StatusCode(HttpStatusCode.NoContent));
        }