Ejemplo n.º 1
0
        public async Task <IActionResult> AddBook(BookViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model == null)
                {
                    return(RedirectToAction("Error"));
                }
                BookDTO newBook = new BookDTO
                {
                    Title       = model.Title.Trim(),
                    AuthorId    = model.AuthorId,
                    Genre       = model.Genre.Trim(),
                    Rate        = model.Rate,
                    Description = model.Description,
                    Year        = model.Year,
                    RatesAmount = model.RatesAmount
                };
                if (model.Image != null && model.FileBook != null)
                {
                    byte[] imageData = null;
                    using (var binaryReader = new BinaryReader(model.Image.OpenReadStream()))
                    {
                        imageData = binaryReader.ReadBytes((int)model.Image.Length);
                    }
                    newBook.Image = imageData;

                    byte[] fileData = null;
                    using (var binaryReader = new BinaryReader(model.FileBook.OpenReadStream()))
                    {
                        fileData = binaryReader.ReadBytes((int)model.FileBook.Length);
                    }
                    newBook.FileBook = fileData;
                }
                else
                {
                    return(RedirectToAction("Error"));
                }

                string postData = JsonConvert.SerializeObject(newBook);

                await _client.PostData("books", postData);
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> RateBook(RateViewModel rateVM)
        {
            if (string.IsNullOrEmpty(rateVM.UserId) || string.IsNullOrEmpty(rateVM.RatedEssenceId) || rateVM.Value < 1 || rateVM.Value > 5)
            {
                return(RedirectToAction("Error"));
            }

            string postData = JsonConvert.SerializeObject(rateVM);
            await _client.PostData("rates/rateBook", postData);

            return(RedirectToAction("GetBookInfo", "Home", new { id = rateVM.RatedEssenceId }));
        }