Example #1
0
        public async Task <IActionResult> Create(BookViewModels bookViewModels)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogError($"Create book model is invalid. Model: {bookViewModels}");
                return(RedirectToAction(nameof(Index)));
            }

            try
            {
                var fileName = await ProcessBookCoverImage(bookViewModels);

                var bookEntity = new Book
                {
                    UserName    = GetUserEmail(),
                    CreateTime  = DateTime.Now,
                    UpdateTime  = DateTime.Now,
                    Description = bookViewModels.Description,
                    Name        = bookViewModels.Name,
                    Quantity    = bookViewModels.Quantity,
                    Image       = fileName
                };

                _context.Add(bookEntity);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Create book errors.");
            }

            return(RedirectToAction(nameof(Index)));
        }
Example #2
0
        public IViewComponentResult Invoke()
        {
            var items = new BookViewModels();

            if (!_cache.TryGetValue("top5download", out items))
            {
                if (items == null)
                {
                    var books = _bookRepository.Top5Download.Where(x => x.Status).ToList();
                    items = new BookViewModels
                    {
                        Books = books
                    };
                }
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetSlidingExpiration(TimeSpan.FromMinutes(3));

                _cache.Set("top5download", items, cacheEntryOptions);
            }
            return(View(items));
        }
Example #3
0
        private async Task <string> ProcessBookCoverImage(BookViewModels bookViewModels)
        {
            if (null == bookViewModels.Files)
            {
                return(string.Empty);
            }
            else
            {
                var filePath = _env.WebRootPath + BookImagePath;
                var fileName = Guid.NewGuid() + Path.GetExtension(bookViewModels.Files.FileName);

                if (bookViewModels.Files.Length > 0)
                {
                    using (var stream = new FileStream(filePath + fileName, FileMode.Create))
                    {
                        await bookViewModels.Files.CopyToAsync(stream);
                    }
                }

                return(fileName);
            }
        }