Esempio n. 1
0
        public async Task <ActionResult <Book> > CreateBook(Book book)
        {
            db.Books.Add(book);
            await db.SaveChangesAsync();

            return(Ok(book));
        }
        public async Task <IActionResult> UpdateBookItem(long id, BookItemDTO bookItemDTO)
        {
            if (id != bookItemDTO.Id)
            {
                return(BadRequest());
            }

            var bookItem = await _context.BookItems.FindAsync(id);

            if (bookItem == null)
            {
                return(NotFound());
            }

            bookItem.Name     = bookItemDTO.Name;
            bookItem.Author   = bookItemDTO.Author;
            bookItem.Language = bookItemDTO.Language;
            bookItem.Year     = bookItemDTO.Year;
            bookItem.Pages    = bookItemDTO.Pages;
            bookItem.Synopsis = bookItemDTO.Synopsis;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!BookItemExists(id))
            {
                return(NotFound());
            }

            return(NoContent());
        }
Esempio n. 3
0
        public async Task <IHttpActionResult> PutBook(int id, Book book)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != book.BookID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Esempio n. 4
0
        public async Task <IActionResult> PutReview(int id, Review review)
        {
            if (id != review.ReviewId)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Esempio n. 5
0
        public async Task <ActionResult <Book> > PostBook(Book book)
        {
            _context.Books.Add(book);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetBook", new { id = book.Id }, book));
        }
Esempio n. 6
0
        public async Task <IActionResult> Create(Book book)
        {
            db.Books.Add(book);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> PutBook(int id, Book book)
        {
            if (id != book.ID)
            {
                return(BadRequest());
            }

            if (book.CoverPhoto != null)
            {
                book.GUID       = ImageUpload.WriteFile(book.FileName, book.CoverPhoto); //save new image to static folder
                book.CoverPhoto = null;
                book.FilePath   = _config["ImagePath"] + book.GUID;
            }

            _DBContext.Entry(book).State = EntityState.Modified;

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

            return(NoContent());
        }
Esempio n. 8
0
        public async Task <IHttpActionResult> CreateRef([FromODataUri] int key,
                                                        string navigationProperty, [FromBody] Uri link)
        {
            var product = await db.Photo.SingleOrDefaultAsync(p => p.Id == key);

            if (product == null)
            {
                return(NotFound());
            }
            switch (navigationProperty)
            {
            case "Person":
                // Note: The code for GetKeyFromUri is shown later in this topic.
                var relatedKey = Helpers.GetKeyFromUri <int>(Request, link);
                var supplier   = await db.Person.SingleOrDefaultAsync(f => f.Id == relatedKey);

                if (supplier == null)
                {
                    return(NotFound());
                }

                product.Person = supplier;
                break;

            default:
                return(StatusCode(HttpStatusCode.NotImplemented));
            }
            await db.SaveChangesAsync();

            return(StatusCode(HttpStatusCode.NoContent));
        }
Esempio n. 9
0
        public async Task <IActionResult> PutProductCart(int id, ProductCart productCart)
        {
            if (id != productCart.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <IActionResult> CreateCategory(Category category)
        {
            _context.Categories.Add(category);
            await _context.SaveChangesAsync();

            return(Ok(category));
        }
Esempio n. 11
0
        public async Task <Book> AddAsync(Book model)
        {
            _context.Book.Add(model);
            await _context.SaveChangesAsync();

            return(model);
        }
Esempio n. 12
0
        public async Task <ActionResult <BookItemDTO> > UpdateBookItem(BookItemDTO BookItemDTO)
        {
            var BookItem = await _context.BookItems.FindAsync(BookItemDTO.Id);

            if (BookItem == null)
            {
                return(NotFound());
            }
            BookItem.Bookname = BookItemDTO.Bookname;
            BookItem.Author   = BookItemDTO.Author;
            BookItem.Rating   = BookItemDTO.Rating;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!BookItemExists(BookItemDTO.Id))
            {
                return(NotFound());
            }

            return(CreatedAtAction(
                       nameof(UpdateBookItem),
                       new { id = BookItem.Id },
                       ItemToDTO(BookItem)));
        }
Esempio n. 13
0
        public async Task <IActionResult> CreateBook(Book book)
        {
            _context.Books.Add(book);
            await _context.SaveChangesAsync();

            return(Ok(book));
        }
        public async Task <IActionResult> AboutEdit(About about)
        {
            if (!ModelState.IsValid)
            {
                return(NotFound());
            }

            var aboutFromDb = await _db.Abouts.FirstOrDefaultAsync();

            if (about.Photo != null)
            {
                if (!about.Photo.IsPhoto())
                {
                    ModelState.AddModelError("Photo", "File must be image type.");
                    return(View(aboutFromDb));
                }

                Delete(_env.WebRootPath + aboutFromDb.Image);

                aboutFromDb.Image = await about.Photo.SavePhotoAsync(_env.WebRootPath, "about");
            }

            aboutFromDb.Title       = about.Title;
            aboutFromDb.Description = about.Description;

            _db.Entry(aboutFromDb).State = EntityState.Modified;
            await _db.SaveChangesAsync();

            return(RedirectToAction(nameof(About)));
        }
Esempio n. 15
0
        public async Task <IActionResult> PutBook(int id, Book book)
        {
            if (id != book.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Esempio n. 16
0
        public async Task<IActionResult> PutFavorites(int id, Favorites favorites)
        {
            if (id != favorites.FavId)
            {
                return BadRequest();
            }

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

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

            return NoContent();
        }
Esempio n. 17
0
        public async Task <Book> Create(Book book)
        {
            _context.Books.Add(book);
            await _context.SaveChangesAsync();

            return(book);
        }
Esempio n. 18
0
        public async Task <ActionResult <Book> > PostTodoItem(Book book)
        {
            _context.Books.Add(book);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetTodoItem), new { id = book.Id }, book));
        }
Esempio n. 19
0
        public async Task <IActionResult> PutOrderDetail(int id, OrderDetail orderDetail)
        {
            if (id != orderDetail.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Esempio n. 20
0
        public async Task <IActionResult> Edit(Book phone)
        {
            db.Books.Update(phone);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Esempio n. 21
0
        public async Task <ActionResult <BookItem> > PostBookItem(BookItem item)
        {
            bookContext.BookItems.Add(item);
            await bookContext.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetBookItem), new { isbn = item.isbn }, item));
        }
Esempio n. 22
0
        public async Task <IActionResult> PutBook([FromRoute] int id, [FromBody] Book Book)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != Book.ID)
            {
                return(BadRequest());
            }

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

            try
            {
                await _context.SaveChangesAsync();

                return(Accepted(Book));
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            //return NoContent();
        }
Esempio n. 23
0
        public async Task <IActionResult> PutTodoItem(long id, TextSentence todoItem)
        {
            if (id != todoItem.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Esempio n. 24
0
        public async Task <IActionResult> PutAuthor(int id, Author author)
        {
            if (id != author.AuthorId)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Esempio n. 25
0
        public async Task <ActionResult> ModifyBooksFix2([FromRoute] int count = 100)
        {
            await ParallelizeAsync(async() =>
            {
                // DbContext options can be safely shared between threads,
                // so individual workers can create their own contexts using
                // shared options from DI.
                var newContext = new BookContext(_contextOptions);

                while (Interlocked.Decrement(ref count) >= 0)
                {
                    // Add a new book
                    var author    = await GetRandomAuthorAsync(newContext);
                    var book      = GetRandomBook(author);
                    var addedBook = await newContext.Books.AddAsync(book);
                    await newContext.SaveChangesAsync();

                    // Remove the book
                    newContext.Remove(addedBook.Entity);
                    await newContext.SaveChangesAsync();
                }
            });

            return(Ok());
        }
Esempio n. 26
0
        public async Task DeleteAsync(int id)
        {
            Book book = await db.Books.FindAsync(id);

            db.Books.Remove(book);
            await db.SaveChangesAsync();
        }
Esempio n. 27
0
        public async Task <RedirectResult> NewBook(Book book)
        {
            db.Books.Add(book);

            await db.SaveChangesAsync();

            return(Redirect("Book"));
        }
Esempio n. 28
0
        public async Task <Book> Create(Book book)
        {
            _context.Books.Add(book);
            //will add data to the data base
            await _context.SaveChangesAsync();

            return(book);
        }
Esempio n. 29
0
        public async Task <IActionResult> AddToFavorites(int?bookId)
        {
            var res = new
            {
                code = 404,
                data = "Something goes wrong.",
                icon = "error"
            };

            if (bookId == null)
            {
                return(Json(res));
            }

            var book = await _db.Books.FindAsync(bookId);

            if (book == null)
            {
                return(Json(res));
            }

            var user = await _userManager.GetUserAsync(User);

            var isFavoriteBookAlready = user.Favorites.Any(f => f.BookId == bookId);

            res = new
            {
                code = 204,
                data = "This book already in your favorites.",
                icon = "info"
            };

            if (isFavoriteBookAlready)
            {
                return(Json(res));
            }

            var pivot = new BookFavoritePivot
            {
                BookId  = (int)bookId,
                UserId  = user.Id,
                AddedAt = DateTime.Now
            };

            await _db.BookFavorites.AddAsync(pivot);

            await _db.SaveChangesAsync();

            res = new
            {
                code = 200,
                data = "Book add to favorites",
                icon = "success"
            };

            return(Json(res));
        }
Esempio n. 30
0
        public async Task <T> Create(T entity)
        {
            await _context.Set <T>()
            .AddAsync(entity);

            await _context.SaveChangesAsync();

            return(entity);
        }