Exemple #1
0
        public async Task <IActionResult> Create(int id)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var chapter = ChaptersVM.Chapter;
                    chapter.EditDate          = DateTime.Now;
                    chapter.ApplicationUserId = ChaptersVM.Censor.Id;
                    chapter.BookId            = ChaptersVM.Book.Id;
                    chapter.Approved          = false;
                    _context.Add(chapter);
                    var book = await _context.Books.Where(u => u.Id == ChaptersVM.Book.Id).FirstOrDefaultAsync();

                    var sum = book.BookPrice + chapter.Price;
                    book.BookPrice = (int)Math.Round(sum * 0.90, MidpointRounding.AwayFromZero);
                    await _context.SaveChangesAsync();

                    _iSingleton.LogException("Thêm chương mới của sách Id: " + book.Id);
                    return(RedirectToAction("Create", "Chapters", new { id = id }));
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                _iSingleton.LogException(e.Message);
                return(RedirectToAction("Error", "Log"));
            }
        }
Exemple #2
0
        public async Task <IActionResult> CreatePOST()
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (!SameName(AuthorsVM.Author.Signed, AuthorsVM.Author.CountryId))
                    {
                        _context.Add(AuthorsVM.Author);
                        await _context.SaveChangesAsync();

                        var webRootPath = _hostEnvironment.WebRootPath;
                        var files       = HttpContext.Request.Form.Files;
                        if (files.Count > 0)
                        {
                            var uploads   = Path.Combine(webRootPath, SD.AuthorImageFolder);
                            var extension = Path.GetExtension(files[0].FileName);
                            using (var fileStream = new FileStream(Path.Combine(uploads, AuthorsVM.Author.Id + extension), FileMode.Create))
                            {
                                files[0].CopyTo(fileStream);
                            }
                            AuthorsVM.Author.Avatar = @"\" + SD.AuthorImageFolder + @"\" + AuthorsVM.Author.Id + extension;
                        }
                        else
                        {
                            var upload = Path.Combine(webRootPath, SD.DefaultUserAvatar);
                            System.IO.File.Copy(upload, webRootPath + @"\" + SD.AuthorImageFolder + @"\" + AuthorsVM.Author.Id + ".png");
                            AuthorsVM.Author.Avatar = @"\" + SD.AuthorImageFolder + @"\" + AuthorsVM.Author.Id + ".png";
                        }
                        _iSingleTon.LogException("Thêm tác giả Id:" + AuthorsVM.Author.Id);
                        await _context.SaveChangesAsync();

                        return(RedirectToAction(nameof(Index)));
                    }
                    else
                    {
                        AuthorsVM.Authors = await _context.Authors.ToListAsync();

                        ModelState.AddModelError("SameName", "Trùng kí danh tác giả");
                        return(View(nameof(Index), AuthorsVM));
                    }
                }
                ViewData["CountryId"] = new SelectList(_context.Countries, "Id", "CountryName", AuthorsVM.Author.CountryId);
                return(View(nameof(Index), AuthorsVM));
            }
            catch (Exception e)
            {
                _iSingleTon.LogException(e.Message);
                return(RedirectToAction("Error", "Log"));
            }
        }
        public async Task <IActionResult> CreatePOST(string publisherName, string description, int countryId)
        {
            try
            {
                Publisher publisher = new Publisher()
                {
                    PublisherName = publisherName,
                    Description   = description,
                    CountryId     = countryId
                };
                if (ModelState.IsValid)
                {
                    _context.Add(publisher);
                    await _context.SaveChangesAsync();

                    var webRootPath = _hostEnvironment.WebRootPath;
                    var files       = HttpContext.Request.Form.Files;
                    if (files.Count != 0)
                    {
                        var uploads   = Path.Combine(webRootPath, SD.PublisherImageFolder);
                        var extension = Path.GetExtension(files[0].FileName);
                        using (var fileStream = new FileStream(Path.Combine(uploads, publisher.Id + extension), FileMode.Create))
                        {
                            files[0].CopyTo(fileStream);
                        };
                        publisher.PublisherLogo = @"\" + SD.PublisherImageFolder + @"\" + publisher.Id + extension;
                    }
                    else
                    {
                        var uploads = Path.Combine(webRootPath, SD.DefaultPublisherLogo);
                        System.IO.File.Copy(uploads, webRootPath + @"\" + SD.PublisherImageFolder + @"\" + publisher.Id + ".png");
                        publisher.PublisherLogo = @"\" + SD.PublisherImageFolder + @"\" + publisher.Id + ".png";
                    }
                    await _context.SaveChangesAsync();

                    _iSingleton.LogException("Thêm NXB Id: " + publisher.Id + " " + publisher.PublisherName);
                    return(RedirectToAction(nameof(Index)));
                }
                ViewData["CountryId"] = new SelectList(_context.Countries, "Id", "CountryName", publisher.CountryId);
                return(View(publisher));
            }
            catch (Exception e)
            {
                _iSingleton.LogException(e.Message);
                return(RedirectToAction("Error", "Log"));
            }
        }
Exemple #4
0
        public async Task <IActionResult> Index(int productPage = 1, string q = null, string userId = null)
        {
            try
            {
                ApplicationUserViewModel ApplicationUserVM = new ApplicationUserViewModel()
                {
                    ApplicationUsers = new List <Models.ApplicationUser>()
                };
                StringBuilder param = new StringBuilder();

                param.Append("/Admin/AdminUser?productPage=:");
                ApplicationUserVM.ApplicationUsers = _db.ApplicationUsers.Where(u => u.Email != User.Identity.Name).ToList();

                var count = ApplicationUserVM.ApplicationUsers.Count;
                ApplicationUserVM.ApplicationUsers = ApplicationUserVM.ApplicationUsers.Skip((productPage - 1) * PageSize).Take(PageSize).ToList();
                ApplicationUserVM.PagingInfo       = new PagingInfo
                {
                    CurrentPage  = productPage,
                    ItemsPerPage = PageSize,
                    TotalItems   = count,
                    urlParam     = param.ToString()
                };
                if (q == "Active")
                {
                    ApplicationUserVM.ApplicationUsers = ApplicationUserVM.ApplicationUsers.Where(u => u.LockoutEnd.HasValue == false).ToList();
                }
                if (q == "Deactive")
                {
                    ApplicationUserVM.ApplicationUsers = ApplicationUserVM.ApplicationUsers.Where(u => u.LockoutEnd.HasValue == true).ToList();
                }
                if (userId != null)
                {
                    ApplicationUserVM.ApplicationUser = await _db.ApplicationUsers.Where(u => u.Id == userId).FirstOrDefaultAsync();
                }
                return(View(ApplicationUserVM));
            }
            catch (Exception e)
            {
                _iSingleTon.LogException(e.Message);
                throw;
            }
        }
        public async Task <IActionResult> Create([Bind("Id,CountryName")] Country country)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    await UnitOfWork.Countries.Insert(country);

                    UnitOfWork.Commit();
                    _iSingleton.LogException("Thêm quốc gia: " + country.CountryName);
                    return(RedirectToAction(nameof(Index)));
                }
                return(View(country));
            }
            catch (Exception e)
            {
                _iSingleton.LogException(e.Message);
                throw;
            }
        }
        public async Task <IActionResult> CreatePOST()
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var book = BooksVM.Book;
                    if (SameBookExist(book))
                    {
                        ModelState.AddModelError("SameBook", "Sách đã có trong hệ thống");
                        ViewData["AuthorId"]    = new SelectList(_context.Authors, "Id", "Signed", BooksVM.Book.AuthorId);
                        ViewData["PublisherId"] = new SelectList(_context.Publishers, "Id", "PublisherName", BooksVM.Book.PublisherId);
                        return(View(BooksVM));
                    }
                    _context.Add(BooksVM.Book);
                    await _context.SaveChangesAsync();

                    if (BooksVM.BareTag != null)
                    {
                        List <string> tags = HandingBareTag(BooksVM.BareTag);
                        foreach (var tag in tags)
                        {
                            int id;
                            Tag bTag = new Tag()
                            {
                                Tagname = tag
                            };
                            var getTag = await _context.Tag.Where(u => u.Tagname == bTag.Tagname).FirstOrDefaultAsync();

                            if (getTag == null)
                            {
                                _context.Tag.Add(bTag);
                                await _context.SaveChangesAsync();

                                id = bTag.Id;
                            }
                            else
                            {
                                id = getTag.Id;
                            }
                            BookTag bookTag = new BookTag()
                            {
                                BookId = BooksVM.Book.Id,
                                TagId  = id
                            };
                            _context.BookTags.Add(bookTag);
                            await _context.SaveChangesAsync();
                        }
                    }
                    var bookFromDb  = _context.Books.Find(BooksVM.Book.Id);
                    var webRootPath = _hostEnvironment.WebRootPath;
                    var files       = HttpContext.Request.Form.Files;
                    if (files.Count != 0)
                    {
                        var uploads   = Path.Combine(webRootPath, SD.BookImageFolder);
                        var extension = Path.GetExtension(files[0].FileName);
                        using (var fileStream = new FileStream(Path.Combine(uploads, BooksVM.Book.Id + extension), FileMode.Create))
                        {
                            files[0].CopyTo(fileStream);
                        }
                        bookFromDb.BookImage = @"\" + SD.BookImageFolder + @"\" + BooksVM.Book.Id + extension;
                    }
                    else
                    {
                        var uploads = Path.Combine(webRootPath, SD.BookDefaultImage);
                        System.IO.File.Copy(uploads, webRootPath + @"\" + SD.BookImageFolder + @"\" + BooksVM.Book.Id + ".png");
                        bookFromDb.BookImage = @"\" + SD.BookImageFolder + @"\" + BooksVM.Book.Id + ".png";
                    }
                    if (BooksVM.GenresViewModels != null)
                    {
                        foreach (var genresView in BooksVM.GenresViewModels)
                        {
                            if (genresView.Selected)
                            {
                                BookGenres bookGenres = new BookGenres()
                                {
                                    BookId   = bookFromDb.Id,
                                    GenresId = genresView.Genres.Id
                                };
                                _context.BookGenres.Add(bookGenres);
                            }
                        }
                    }
                    bookFromDb.Approved = false;
                    await _context.SaveChangesAsync();

                    _iSingleton.LogException("Thêm sách Id: " + bookFromDb.Id + " " + bookFromDb.BookName);
                    return(RedirectToAction(nameof(Index)));
                }
                ViewData["AuthorId"]    = new SelectList(_context.Authors, "Id", "Id", BooksVM.Book.AuthorId);
                ViewData["PublisherId"] = new SelectList(_context.Publishers, "Id", "Id", BooksVM.Book.PublisherId);
                return(View(BooksVM.Book));
            }
            catch (Exception e)
            {
                _iSingleton.LogException(e.Message);
                return(RedirectToAction("Error", "Log"));
            }
        }