示例#1
0
        public async Task<IHttpActionResult> PostAuthor(Author author)
        {
            string err = String.Empty;

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //Check for an existing author name
            IQueryable<Author> currentAuthors = GetAuthors();

            var existingAuthor = (from b in currentAuthors
                                  where b.Name == author.Name
                                  select b.Name).ToList();

            if (existingAuthor.Count == 0)
            {
                db.Authors.Add(author);
                await db.SaveChangesAsync();
            }
            else
            {
                return Conflict();
            }

            return CreatedAtRoute("DefaultApi", new { id = author.Id }, author);
        }
        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);
        }
        // PUT api/Authors/5
        public HttpResponseMessage PutAuthor(int id, Author author)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

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

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

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        public async Task<IHttpActionResult> PostAuthor(Author author)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Authors.Add(author);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = author.Id }, author);
        }
        // POST api/Authors
        public HttpResponseMessage PostAuthor(Author author)
        {
            if (ModelState.IsValid)
            {
                db.Authors.Add(author);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, author);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = author.Id }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
示例#6
0
        public static void SeedData(this IApplicationBuilder app)
        {
            var context = app.ApplicationServices.GetService<BookServiceContext>();


            IQueryable<Book> bookQuery = from Book
                                            in context.Books select Book;

            if (bookQuery.Count() > 0)
            {
                return;
            }
            else
            {
                
                Author firstAuth = new Author() { Name = "Jane Austen" };
                Author secondAuth = new Author() { Name = "Charles Dickens" };
                Author thirdAuth = new Author() { Name = "Miguel de Cervantes" };

                context.Authors.Add(firstAuth);
                context.Authors.Add(secondAuth);
                context.Authors.Add(thirdAuth);
                context.SaveChanges();

                //get new Id from Author updates
                IQueryable<int> authorIDQuery = from Author in context.Authors select Author.Id;

                int[] arr = new int[authorIDQuery.Count()];
                int i = 0;

                foreach (var authIdent in authorIDQuery)
                {
                    arr[i] = authIdent;
                    ++i;
                }
                Book firstBook = new Book()
                {
                    Title = "Pride and Prejudice",
                    Year = 1813,
                    AuthorId = arr[0],
                    Price = 9.99M,
                    Genre = "Comedy of manners"
                };
                Book secondBook = new Book()
                {
                    Title = "Northanger Abbey",
                    Year = 1817,
                    AuthorId = arr[0],
                    Price = 12.95M,
                    Genre = "Gothic parody"
                };
                Book thirdBook = new Book()
                {
                    Title = "David Copperfield",
                    Year = 1850,
                    AuthorId = arr[1],
                    Price = 15,
                    Genre = "Bildungsroman"
                };
                Book fourthBook = new Book()
                {

                    Title = "Don Quixote",
                    Year = 1617,
                    AuthorId = arr[2],
                    Price = 8.95M,
                    Genre = "Picaresque"
                };

                context.Books.Add(firstBook);
                context.Books.Add(secondBook);
                context.Books.Add(thirdBook);
                context.Books.Add(fourthBook);

                context.SaveChanges();
            }
        }
        public async Task<IHttpActionResult> PostAuthor(Author author)
        {
            Uri uri = Request.RequestUri;
            SimpleLog.WriteStdOut(uri.ToString());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Authors.Add(author);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = author.Id }, author);
        }