예제 #1
0
        public async Task <IActionResult> PutAuthor(int id, AuthorDTO authorDTO)
        {
            var author = _mapper.Map <Author>(authorDTO);

            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(Ok(_mapper.Map <AuthorDTO>(author)));
        }
        public async Task <IActionResult> PutBook(int id, BookDTO bookDTO)
        {
            var book = _mapper.Map <Book>(bookDTO);

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

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

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

            return(Ok(_mapper.Map <BookDTO>(book)));
        }
예제 #3
0
        public async Task <RedirectToActionResult> ClearCart()
        {
            var cartItems = _context.ShoppingCartItems.Where(c => c.ShoppingCartId == _shoppingCart.ShoppingCartId);

            _context.ShoppingCartItems.RemoveRange(cartItems);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
예제 #4
0
        public async Task <IActionResult> Create([Bind("GenreId,GenreName")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                _context.Add(genre);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(genre));
        }
예제 #5
0
        public async Task <IActionResult> PostAsync([FromBody] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Books.Add(book);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            return(BadRequest());
        }
예제 #6
0
        public async Task <IActionResult> Create([Bind("BranchId,BranchName,City,Address,Latitude,Longitude,PhoneNumber")] Branch branch)
        {
            if (ModelState.IsValid)
            {
                _context.Add(branch);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(branch));
        }
        public async Task <ActionResult <BookSale> > PostBookSale(BookSaleDTO bookSaleDTO)
        {
            var bookSale = _mapper.Map <BookSale>(bookSaleDTO);

            if (ModelState.IsValid)
            {
                _context.BookSales.Add(bookSale);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetBookSale", new { id = bookSale.Id }, bookSale));
            }
            else
            {
                return(BadRequest());
            }
        }
예제 #8
0
        public async Task <ActionResult <Book> > Delete(int id)
        {
            var book = _repository.GetBookById(id);

            if (book == null)
            {
                return(NotFound());
            }
            _ctx.Books.Remove(book);
            await _ctx.SaveChangesAsync();

            return(book);
        }
예제 #9
0
        public async Task <IActionResult> Create(IFormFile file, [Bind("BookId,BookName,Author,Publication,Price,Summary,PictureName,GenreId")] Book book)
        {
            if (ModelState.IsValid)
            {
                if (file == null)
                {
                    ModelState.AddModelError("", "You must enter book picture");
                }
                else if (GetAllBooks.Where(b => b.BookName == book.BookName).Count() == 0)
                {
                    // get the image name and save the path to the saved pictures
                    var filePath = _staticImagesRoute + file.FileName;

                    // save the image name to the pictureName property so we get it later for the view
                    book.PictureName = "/img/" + file.FileName;

                    // save the picture to the static path
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }

                    // save book
                    _context.Add(book);
                    await _context.SaveChangesAsync();

                    FacebookConnection.PostMessage(book);
                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    ModelState.AddModelError("", "The book already exist");
                }
            }
            ViewData["GenreID"] = new SelectList(_context.Genres, "GenreId", "GenreName", book.GenreId);
            return(View(book));
        }