Пример #1
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());
        }
Пример #2
0
        public async Task <IActionResult> PutStore(int id, Store store)
        {
            if (id != store.StoreId)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Пример #3
0
        public async Task <IActionResult> AddRemoveWishList(WishListReq req)
        {
            if (req.WishListed == false)
            {
                BibliotecaOnline.DbModels.WishList wish = _context.WishLists.Where(e => e.BookId == req.BookId && e.BibliotecaUserId == req.UserId).FirstOrDefault();

                if (wish != null)
                {
                    _context.WishLists.Remove(wish);
                }
            }
            else
            {
                _context.WishLists.Add(new BibliotecaOnline.DbModels.WishList()
                {
                    AddDate          = DateTime.Now,
                    BibliotecaUserId = req.UserId,
                    BookId           = req.BookId
                });
            }

            try
            {
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
Пример #4
0
        public async Task <IActionResult> PutBook(int id, Book book)
        {
            if (id != book.BookId)
            {
                return(BadRequest());
            }

            var existingParent = _context.Books
                                 .Where(p => p.BookId == book.BookId)
                                 .Include(p => p.BookGenres)
                                 .Include(p => p.BookAuthors)
                                 .Include(p => p.BookStores)
                                 .SingleOrDefault();

            if (existingParent != null)
            {
                // Update parent
                _context.Entry(existingParent).CurrentValues.SetValues(book);

                // Delete children
                foreach (var existingChild in existingParent.BookGenres.ToList())
                {
                    if (!book.BookGenres.Any(c => c.GenreId == existingChild.GenreId))
                    {
                        _context.BookGenres.Remove(existingChild);
                    }
                }

                foreach (var existingChild in existingParent.BookAuthors.ToList())
                {
                    if (!book.BookGenres.Any(c => c.GenreId == existingChild.AuthorId))
                    {
                        _context.BookAuthors.Remove(existingChild);
                    }
                }

                foreach (var existingChild in existingParent.BookStores.ToList())
                {
                    if (!book.BookStores.Any(c => c.StoreId == existingChild.StoreId))
                    {
                        _context.BookStores.Remove(existingChild);
                    }
                }

                // Update and Insert children
                foreach (var childModel in book.BookGenres)
                {
                    var existingChild = existingParent.BookGenres
                                        .Where(c => c.GenreId == childModel.GenreId)
                                        .SingleOrDefault();

                    if (existingChild != null)
                    {
                        // Update child
                        _context.Entry(existingChild).CurrentValues.SetValues(childModel);
                    }
                    else
                    {
                        existingParent.BookGenres.Add(new BookGenre()
                        {
                            BookId = book.BookId, GenreId = childModel.GenreId
                        });
                    }
                }

                foreach (var childModel in book.BookAuthors)
                {
                    var existingChild = existingParent.BookAuthors
                                        .Where(c => c.AuthorId == childModel.AuthorId)
                                        .SingleOrDefault();

                    if (existingChild != null)
                    {
                        // Update child
                        _context.Entry(existingChild).CurrentValues.SetValues(childModel);
                    }
                    else
                    {
                        existingParent.BookAuthors.Add(new BookAuthor()
                        {
                            BookId = book.BookId, AuthorId = childModel.AuthorId
                        });
                    }
                }

                foreach (var childModel in book.BookStores)
                {
                    var existingChild = existingParent.BookStores
                                        .Where(c => c.StoreId == childModel.StoreId)
                                        .SingleOrDefault();

                    if (existingChild != null)
                    {
                        // Update child
                        _context.Entry(existingChild).CurrentValues.SetValues(childModel);
                    }
                    else
                    {
                        existingParent.BookStores.Add(new BookStore()
                        {
                            BookId = book.BookId, StoreId = childModel.StoreId, Stock = childModel.Stock
                        });
                    }
                }
            }


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                if (!BookExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(BadRequest(ex));
                }
            }

            return(NoContent());
        }