コード例 #1
0
        public IActionResult AddArticle(NewArticleViewModel articleModel)
        {
            if (!ModelState.IsValid)
            {
                var _errors = new List <string>();
                foreach (var _values in ModelState.Values)
                {
                    foreach (var _error in _values.Errors)
                    {
                        _errors.Add(_error.ErrorMessage);
                    }
                }
                return(Ok(new { check = false, message = string.Join(",  ", _errors) }));
            }

            string url = "";

            if (articleModel.File != null)
            {
                bool check;
                (check, url) = ImageHelper.SaveImage(articleModel.File, _webHostEnvironment);
                if (!check)
                {
                    if (url != null)
                    {
                        _logger.Log(Niche.Core.Enums.LogLevel.ERROR, url);
                    }

                    return(Ok(new { check = false, message = "Image could not be saved, try again later" }));
                }
            }

            try
            {
                var _author  = _authorService.Get(int.Parse(articleModel.AuthorId));
                var _tag     = _tagService.Get(int.Parse(articleModel.TagIds));
                var _article = _articleService.Add(new Article
                {
                    Id        = Support.GetID(),
                    AuthorId  = _author.AuthorId,
                    Body      = articleModel.Content,
                    CreatedOn = DateTime.UtcNow,
                    ImageURl  = url,
                    TagIds    = _tag.TagId,
                    Title     = articleModel.Title
                });

                return(Ok(new { check = true, message = "The article was successfully saved" }));
            }
            catch (Exception ex)
            {
                _logger.Log(Niche.Core.Enums.LogLevel.ERROR, ex.Message + ex.InnerException != null ? $" ,InnerException: {ex.InnerException.Message}" : "");

                return(Ok(new { check = false, message = "Failed to save" }));
            }
        }
コード例 #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 CreateAccount(AccountViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                var _errors = new List <string>();
                foreach (var _values in ModelState.Values)
                {
                    foreach (var _error in _values.Errors)
                    {
                        _errors.Add(_error.ErrorMessage);
                    }
                }
                return(Ok(new { check = false, message = string.Join(",  ", _errors) }));
            }

            if (viewModel.Password != viewModel.ConfirmPassword)
            {
                return(Ok(new { check = false, message = "Passwords not matching" }));
            }
            var _accounts = _accountService.Search(x => x.Username.Contains(viewModel.Username));

            if (_accounts != null && _accounts.Count() > 0)
            {
                return(Ok(new { check = false, message = "User exist already" }));
            }

            var _account = new Account
            {
                CreatedOn = DateTime.UtcNow,
                Id        = Support.GetID(),
                Name      = viewModel.Name,
                Password  = viewModel.Password,
                Username  = viewModel.Username
            };

            try
            {
                _accountService.Add(_account);

                return(Ok(new { check = true, message = "User has been added" }));
            }
            catch (Exception ex)
            {
                _logger.Log(Niche.Core.Enums.LogLevel.ERROR, ex.Message + ex.InnerException != null ? $" ,InnerException: {ex.InnerException.Message}" : "");

                return(Ok(new { check = false, message = "Failed to save" }));
            }
        }
コード例 #4
0
        public IActionResult SaveAbout(AboutViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                string message = "";
                var    _errors = new List <string>();
                foreach (var _values in ModelState.Values)
                {
                    foreach (var _error in _values.Errors)
                    {
                        _errors.Add(_error.ErrorMessage);
                    }
                }
                message = string.Join(", ", _errors);
                return(Ok(new { check = false, message }));
            }

            var _abouts = _aboutService.Search(x => x.IsSelected);

            foreach (var about in _abouts)
            {
                about.IsSelected = false;
                _aboutService.Update(about);
            }


            var _about = new About
            {
                IsSelected = true,
                Id         = Support.GetID(),
                Body       = viewModel.Body,
                Title      = viewModel.Title,
                CreatedOn  = DateTime.UtcNow
            };

            try
            {
                _aboutService.Add(_about);
                return(Ok(new { check = true, message = "About us information has been updated" }));
            }
            catch (Exception ex)
            {
                _logger.Log(Niche.Core.Enums.LogLevel.ERROR, ex.Message + ex.InnerException != null ? $" ,InnerException: {ex.InnerException.Message}" : "");
                return(Ok(new { check = false, message = "Failed, try again" }));
            }
        }
コード例 #5
0
        public IActionResult Add(CommentViewModel comment)
        {
            if (!ModelState.IsValid)
            {
                var errors = new List <string>();
                foreach (var value in ModelState.Values)
                {
                    foreach (var error in value.Errors)
                    {
                        errors.Add(error.ErrorMessage);
                    }
                }
                _logger.Log(Niche.Core.Enums.LogLevel.INFORMATION, string.Join(", ", errors));
                return(Json(new { check = false, message = string.Join(", ", errors) }));
            }

            try
            {
                _commentService.Add(new Comment
                {
                    ArticleId  = _session.GetString("aId"),
                    CreatedOn  = DateTime.UtcNow,
                    Email      = comment.Email,
                    Id         = Support.GetID(),
                    ImageUrl   = "",
                    Message    = comment.Message,
                    Name       = comment.Name,
                    Subject    = comment.Subject,
                    IsApproved = false
                });
            }
            catch (Exception ex)
            {
                _logger.Log(Niche.Core.Enums.LogLevel.ERROR, ex.Message + ex.InnerException != null ? $" ,InnerException: {ex.InnerException.Message}" : "");

                return(Json(new
                {
                    check = false,
                    message = "Failed to save your comment, kindly check your connection and try again."
                }));
            }

            return(Ok(new { check = true, message = "You comment has been saved" }));
        }
コード例 #6
0
        public IActionResult AddNew(string name)
        {
            try
            {
                var _tag = new Tag
                {
                    CreatedOn = DateTime.UtcNow,
                    Name      = name,
                    Id        = Support.GetID()
                };

                _tagService.Add(_tag);

                return(Ok(new { check = true, message = "New tag has been addedd successfully" }));
            }
            catch (Exception ex)
            {
                _logger.Log(Niche.Core.Enums.LogLevel.ERROR, ex.Message + ex.InnerException != null ? $" ,InnerException: {ex.InnerException.Message}" : "");
                return(Ok(new { check = false, message = "Failed, try again" }));
            }
        }
コード例 #7
0
        public IActionResult AddNew(string Firstname, string Lastname)
        {
            try
            {
                var _author = new Author
                {
                    CreatedOn = DateTime.UtcNow,
                    FirstName = Firstname,
                    LastName  = Lastname,
                    Id        = Support.GetID()
                };

                _authorService.Add(_author);

                return(Ok(new { check = true, message = "Author has been addedd successfully" }));
            }
            catch (Exception ex)
            {
                _logger.Log(Niche.Core.Enums.LogLevel.ERROR, ex.Message + ex.InnerException != null ? $" ,InnerException: {ex.InnerException.Message}" : "");
                return(Ok(new { check = false, message = "Failed, try again" }));
            }
        }
コード例 #8
0
        public async Task <IActionResult> AddCategory(string name, string description)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(description))
            {
                return(Ok(new
                {
                    code = -1,
                    message = "Fill in all the fields"
                }));
            }


            var category = new Category
            {
                Description = description,
                Id          = Support.Id,
                Name        = name
            };


            category = await _categoryService.Add(category);

            if (category == default)
            {
                return(Ok(new
                {
                    code = -1,
                    message = "Failed to save"
                }));
            }


            return(Ok(new
            {
                code = 0,
                message = "Save successfully",
                data = category
            }));
        }
コード例 #9
0
        public async Task <IActionResult> SaveAll(string categories)
        {
            var cats = categories.Split(',')
                       .ToList();

            var user = Session.GetString("user")
                       .FromJson <User>();


            var userData = await _userService.Get(user.Id);

            userData.Address     = user.Address;
            userData.Occupation  = user.Occupation;
            userData.DateOfBirth = user.DateOfBirth;
            userData.Education   = user.Education;
            userData.ClassId     = user.ClassId;

            await _userService.Update(userData);

            foreach (var category in cats)
            {
                var following = new Follow
                {
                    Id        = Support.Id,
                    UserId    = userData.Id,
                    CreatedOn = DateTime.UtcNow,
                    LibraryId = category
                };

                await _followService.Add(following);
            }


            return(Ok(new
            {
                code = 0,
                message = "update"
            }));
        }
コード例 #10
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Message = "Fill in all the fields";
                return(View(model));
            }

            var accounts = _accountService.SearchQ(x => x.Email == model.Email)
                           .ToList();

            if (accounts.Count > 0)
            {
                model.Message = "Email not avaible";
                return(View(model));
            }

            if (model.Password != model.RepPassword)
            {
                model.Message = "Paswords not matching";
                return(View(model));
            }

            model.Password = model.Password.ToSHA256();

            var account = new Account
            {
                Id       = Support.Id,
                Email    = model.Email,
                Password = model.Password
            };

            account = await _accountService.Add(account);

            if (account == null)
            {
                model.Message = "Registration failed";
                return(View(model));
            }

            var user = new User
            {
                Id        = Support.Id,
                AccountId = account.Id,
                FirstName = model.FirstName,
                LastName  = model.LastName
            };

            user = await _userService.Add(user);

            if (user == null)
            {
                await _accountService.Delete(account);

                model.Message = "Registration failed";
                return(View(model));
            }
            await RegisterClaimsAsync(user);

            return(RedirectToAction("interim", "home"));
        }
コード例 #11
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"
            }));
        }