Пример #1
0
        //[ValidateAntiForgeryToken]
        public async Task <ActionResult> UploadBook(Book book, HttpPostedFileBase file)
        {
            FileStream stream;

            if (file != null && file.ContentLength > 0)
            {
                string path = Path.Combine(Server.MapPath("~/Content/"), file.FileName);
                file.SaveAs(path);
                //var stream = new MemoryStream(Encoding.ASCII.GetBytes(path));
                stream = new FileStream(Path.Combine(path), FileMode.Open);
                await Task.Run(() => BookRepo.Upload(stream, file.FileName));

                book.file = path;
            }

            try
            {
                if (ModelState.IsValid)
                {
                    await BookRepo.Create(book);

                    return(RedirectToAction("Index", "Home"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }
            return(View(book));
        }
        public IActionResult AddOrEdit(Book book)
        {
            if (string.IsNullOrEmpty(book.Id))
            {
                _bookRepo.Create(book);
            }
            else
            {
                _bookRepo.Update(book.Id, book);
            }

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Create([FromBody] CreateListingViewModel createListingViewModel)
        {
            var book    = _mapper.Map <Book>(createListingViewModel);
            var listing = _mapper.Map <Listing>(createListingViewModel);

            if (!await _iBookRepo.Exist(x => x.ISBN10 == book.ISBN10))
            {
                await _iBookRepo.Create(book);
            }
            else
            {
                book = await _iBookRepo.Find(x => x.ISBN10 == book.ISBN10);
            }

            listing.BookId = book.Id;
            var user = await _userManager.GetUserAsync(User);

            listing.UserId = user.Id;
            await _iListingRepo.Create(listing);

            return(Created("GetListing", new { id = listing.Id }));
        }
        public ActionResult <Book> Create(Book book)
        {
            _bookRepo.Create(book);

            return(CreatedAtRoute("GetBook", new { id = book.Id.ToString() }, book));
        }