示例#1
0
        public async Task <ActionResult <Comment> > PostComment(Comment _comment)
        {
            var user = await identityService.GetUserWithToken(HttpContext);

            var comment = new Comment()
            {
                ArticleID = _comment.ArticleID, Text = _comment.Text, DateTime = DateTime.Now, UserID = user.Id
            };

            context.Comment.Add(comment);
            await context.SaveChangesAsync();

            var article = await context.Article.FindAsync(comment.ArticleID);

            var comments = await context.Comment
                           .Where(c => c.ArticleID == article.Id)
                           .Include(c => c.User)
                           .Select(c => mapper.Map <CommentDto>(c))
                           .ToListAsync();

            foreach (var comment_ in comments)
            {
                var owner = await context.Users.FindAsync(comment_.User.Id);

                comment_.User = owner;
            }

            await articleHub.Clients.All.ArticleCommentChange(article.Id, comments);

            logger.Log($"{user.UserName} posted a comment `{comment.Text}` on the article of title `{article.Title}`");

            return(Ok());
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("Name,Position,Category")] Member member)
        {
            ModelState.Remove("Category.Name");
            if (ModelState.IsValid)
            {
                var memberExists = await context.Member.FirstOrDefaultAsync(m => m.Name == member.Name);

                if (memberExists != null)
                {
                    return(ValidationProblem(detail: "Member name already exists."));
                }

                member.Category = (memberService.IsJuniorEditor(member)) ? context.Category
                                  .First(c => c.Name == member.Category.Name) : null;

                context.Add(member);
                await context.SaveChangesAsync();

                logger.Log($"{HttpContext.User.Identity.Name} created member `{member.Name}` with position {member.Position} " +
                           $"(Category = {member.Category.Name}) ");

                return(RedirectToAction(nameof(Index)));
            }
            return(View(member));
        }
示例#3
0
        public async Task <OutlookUser> CreateUser(string username)
        {
            OutlookUser admin = new OutlookUser
            {
                FirstName          = username,
                LastName           = username,
                EmailConfirmed     = true,
                UserName           = username,
                NormalizedUserName = username.ToUpper(),
            };

            var oldAdmin = from member in userManager.Users
                           where member.UserName == admin.UserName
                           select member;

            if (oldAdmin.FirstOrDefault() == null)
            {
                var adminPassword = OutlookSecrets.UserPassword.Development.Admin;
                var addAdmin      = await userManager.CreateAsync(admin, adminPassword);

                if (addAdmin.Succeeded)
                {
                    await context.SaveChangesAsync();

                    await signInManager.SignInAsync(admin, isPersistent : false);
                }
                return(admin);
            }

            return(oldAdmin.FirstOrDefault());
        }
示例#4
0
        public async Task <IActionResult> Create([Bind("Number,FallYear,SpringYear")] Volume volume)
        {
            if (ModelState.IsValid)
            {
                context.Add(volume);
                await context.SaveChangesAsync();

                logger.Log($"{HttpContext.User.Identity.Name} created Volume {volume.Number}.");

                return(RedirectToAction(nameof(Index), "Home"));
            }
            return(View(volume));
        }
示例#5
0
        public async Task <ActionResult> Create([Bind("Name,Position")] Member member)
        {
            if (ModelState.IsValid)
            {
                context.Add(member);
                await context.SaveChangesAsync();

                logger.Log($"{HttpContext.User.Identity.Name} created writer `{member.Name}`");

                return(RedirectToAction(nameof(Index)));
            }
            return(View(member));
        }
        public async Task <IActionResult> Create([Bind("Name,Tag")] Category category)
        {
            if (ModelState.IsValid)
            {
                context.Add(category);
                category.SetLanguage(Regex.IsMatch(category.Name, @"^[a-zA-Z.\-+\s]*$") ? OutlookConstants.Language.English : OutlookConstants.Language.Arabic);
                await context.SaveChangesAsync();

                logger.Log($"{HttpContext.User.Identity.Name} created Category `{category.Name}` and ID `{category.Id}`.");

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
示例#7
0
        public async Task <IActionResult> Create(int?id, [Bind("Number,ArabicTheme,EnglishTheme")] Issue issue)
        {
            if (ModelState.IsValid)
            {
                if (id == null)
                {
                    return(ValidationProblem(detail: "Volume Id cannot be null"));
                }
                issue.Volume = await context.Volume.FindAsync(id);

                context.Add(issue);
                await context.SaveChangesAsync();

                var Volume = await context.Volume.FindAsync(id);

                logger.Log($"{HttpContext.User.Identity.Name} created Issue `{issue.Number}` in Volume {Volume.Number} ");

                return(RedirectToAction(nameof(Index), new { id = id }));
            }
            return(View(issue));
        }
示例#8
0
        public async Task <ActionResult> RateUpArticle(int articleID)
        {
            var user = await identityService.GetUserWithToken(HttpContext);

            var userRateArticle = await context.UserRateArticle.FirstOrDefaultAsync(u => (u.Article.Id == articleID) && (u.User.Id == user.Id));

            var article = await context.Article.FindAsync(articleID);

            if (userRateArticle != null)
            {
                if (userRateArticle.Rate == OutlookConstants.UserRate.Up)
                {
                    return(Accepted("You've already rated up this article."));
                }
                else
                {
                    if (userRateArticle.Rate == OutlookConstants.UserRate.Down)
                    {
                        article.UndoDownVote();
                    }
                    userRateArticle.Rate = OutlookConstants.UserRate.Up;
                }
            }
            else
            {
                // Rate up the article
                context.UserRateArticle.Add(new UserRateArticle {
                    User = user, Article = article, Rate = OutlookConstants.UserRate.Up
                });
            }
            article.UpVote();
            await context.SaveChangesAsync();

            // Notify all clients
            await hubContext.Clients.All.ArticleScoreChange(article.Id, article.Rate, article.NumberOfVotes);

            return(Ok());
        }
示例#9
0
        public async Task <IActionResult> Create([FromRoute] int?id, string NewWriter, IFormFile Picture, [Bind("Language,Category,Writer,Title,Subtitle,Text")] Article article)
        {
            ModelState.Remove("Category.Name");
            ModelState.Remove("Writer.Name");

            if (ModelState.IsValid)
            {
                if (id == null)
                {
                    return(ValidationProblem(detail: "Issue Id cannot be null"));
                }

                article.Issue = await context.Issue.FindAsync(id);

                article.DateTime = DateTime.Now;

                // Assign value to the MemberID that refers to the writer of the article
                article.Category = context.Category
                                   .First(c => c.Name == article.Category.Name);

                articleService.SetArticleWriter(article,
                                                (article.Writer.Name == "+ NEW WRITER") ? NewWriter : article.Writer.Name);

                if (Picture != null)
                {
                    await articleService.AddArticlePicture(article, Picture, env.WebRootPath);
                }

                context.Add(article);
                await context.SaveChangesAsync();

                logger.Log($"{HttpContext.User.Identity.Name} created article of title `{article.Title}` and ID `{article.Id}`");

                return(RedirectToAction(nameof(Index), new { id = id }));
            }

            return(View(article));
        }
示例#10
0
        private async Task AssignUserRole(OutlookUser user, string roleName)
        {
            var Role = from _role in context.Roles
                       where _role.Name == roleName
                       select _role;

            if (Role.FirstOrDefault() != null)
            {
                var userRoleAssigned = from userRole in context.UserRoles
                                       where (userRole.UserId == user.Id) && (userRole.RoleId == Role.FirstOrDefault().Id)
                                       select userRole;

                if (userRoleAssigned.FirstOrDefault() == null)
                {
                    await _userManager.AddToRoleAsync(user, Role.FirstOrDefault().Name);

                    await context.SaveChangesAsync();
                }
            }
        }