コード例 #1
0
        public IActionResult GetCategories()
        {
            var categories = _categoryService.SearchQ(x => true)
                             .OrderByDescending(x => x.CreatedOn)
                             .ToList();

            return(Ok(categories));
        }
コード例 #2
0
        public async Task <IActionResult> AddClass(Education level, string name, string description)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(description) || level == default)
            {
                return(Ok(new
                {
                    code = -1,
                    message = "Fill in all the fields"
                }));
            }

            var classes = _classService.SearchQ(x => x.Name == name && x.EducationLevel == level)
                          .ToList();

            if (classes.Count > 0)
            {
                return(Ok(new
                {
                    code = -1,
                    message = "Name already in use"
                }));
            }

            var @class = new Class
            {
                CreatedOn      = DateTime.UtcNow,
                EducationLevel = level,
                Description    = description,
                Id             = Support.Id,
                Name           = name
            };

            @class = await _classService.Add(@class);

            if (@class == default)
            {
                return(Ok(new
                {
                    code = -1,
                    message = "Failed to add class"
                }));
            }

            return(Ok(new
            {
                code = 0,
                message = "Class added",
                data = @class
            }));
        }
コード例 #3
0
        public IActionResult LoadLibrary(int page)
        {
            var books = _libraryService.SearchQ(x => x.EducationLevel == User.GetEducation())
                        .OrderByDescending(x => x.CreatedOn)
                        .Skip(page * 10)
                        .Take(10)
                        .ToList();

            if (User.GetEducation() != Education.STAY_AT_HOME)
            {
                books = books.Where(x => x.ClassId == User.GetClass())
                        .ToList();
            }

            var bookList = new List <dynamic>();

            foreach (var book in books)
            {
                var _book = new
                {
                    book.Name,
                    book.Id,
                    book.Description,
                    book.ThumbnailUrl,
                    book.ResourceUrl
                };

                bookList.Add(_book);
            }

            return(Ok(bookList));
        }
コード例 #4
0
        public IActionResult GetClass(Education lvl)
        {
            var classes = _classService.SearchQ(x => x.EducationLevel == lvl)
                          .OrderBy(x => x.CreatedOn)
                          .ToList();

            return(Ok(classes));
        }
コード例 #5
0
        public async Task <IActionResult> Index(LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Message = "Fill in all the fields";
                return(View(model));
            }

            var password = model.Password.ToSHA256();

            var account = _accountService.SearchQ(x => x.Email == model.Email && x.Password == password)
                          .FirstOrDefault();

            if (account == null)
            {
                model.Message = "Wrong email or password";
                return(View(model));
            }


            var user = _userService.SearchQ(x => x.AccountId == account.Id)
                       .FirstOrDefault();

            if (user == null)
            {
                model.Message = "Wrong email or password";
                return(View(model));
            }

            var follow = _followService.SearchQ(x => x.UserId == user.Id).ToList();


            await RegisterClaimsAsync(user);

            if (account.Type == Data.Enums.AccountStatus.ADMIN)
            {
                return(RedirectToAction("index", "dashboard"));
            }
            if (follow.Count == 0)
            {
                return(RedirectToAction("interim", "home"));
            }

            return(RedirectToAction("index", "home"));
        }
コード例 #6
0
        public IActionResult GetBooks()
        {
            var booksFromLibrary = _libraryService
                                   .SearchQ(x => x.LibraryType == LibraryType.EPUB || x.LibraryType == LibraryType.PDF)
                                   .OrderByDescending(x => x.CreatedOn)
                                   .ToList();

            var categories = _categoryService
                             .SearchQ(x => true)
                             .ToList();

            var bookList = new List <dynamic>();
            var index    = 1;

            foreach (var bookFromLibrary in booksFromLibrary)
            {
                var libraryCategories = _categoryLibraryService
                                        .SearchQ(x => x.LibraryId == bookFromLibrary.Id)
                                        .ToList();

                var bookCategories = categories
                                     .Where(x => libraryCategories.Any(y => y.CategoryId == x.Id))
                                     .Select(x => x.Name)
                                     .ToList();

                var book = new
                {
                    bookFromLibrary.Id,
                    Position = index,
                    Title    = bookFromLibrary.Name,
                    bookFromLibrary.Description,
                    Categories = string.Join(", ", bookCategories)
                };
                index++;


                bookList.Add(book);
            }

            return(Ok(bookList));
        }
コード例 #7
0
        public async Task <IActionResult> AddBook(BookViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Ok(new
                {
                    code = -1,
                    message = "Fill in the required fields and provide image"
                }));
            }

            var libraries = _libraryService.SearchQ(x => x.Name == model.Title)
                            .ToList();

            if (libraries.Count > 0)
            {
                return(Ok(new
                {
                    code = -1,
                    message = "Book exist"
                }));
            }

            var uploads = Path.Combine(_webHostEnvironment.WebRootPath, "assets/images/books");

            var filePath = Path.Combine(uploads, model.ThumbnailUrl.FileName);

            using var stream = new FileStream(filePath, FileMode.Create);
            await model.ThumbnailUrl.CopyToAsync(stream);

            var library = new Library
            {
                CreatedOn      = DateTime.UtcNow,
                Description    = model.Description,
                EducationLevel = model.educationLevel,
                LibraryType    = LibraryType.PDF,
                Id             = Support.Id,
                Name           = model.Title,
                ClassId        = model.ClassLevel
            };

            library.ThumbnailUrl = "/assets/images/books/" + model.ThumbnailUrl.FileName;

            filePath = Path.Combine(uploads, model.Resource.FileName);

            using var fileStream = new FileStream(filePath, FileMode.Create);
            await model.Resource.CopyToAsync(fileStream);

            library.ResourceUrl = "/assets/images/books/" + model.Resource.FileName;;

            var categories = model.Categories.Split(new char[] { ',' });

            library.LibraryType = Path.GetExtension(model.Resource.FileName).Contains("pdf") ? LibraryType.PDF : LibraryType.EPUB;

            library = await _libraryService.Add(library);

            foreach (var cat in categories)
            {
                var libraryCateogies = new CategoryLibrary
                {
                    Id         = Support.Id,
                    CategoryId = cat,
                    LibraryId  = library.Id
                };

                libraryCateogies = await _categoryLibraryService.Add(libraryCateogies);
            }

            return(Ok(new
            {
                code = 0,
                message = "Save book"
            }));
        }