Exemplo n.º 1
0
        public PostShowViewModel GetPostWithComments(int postId, SortParametersDto sortParameters)
        {
            BlogContext       db        = new BlogContext();
            PostShowViewModel postModel = new PostShowViewModel
            {
                Post = db.Posts.FirstOrDefault(post => post.Id.Equals(postId))
            };

            postModel.Profile = db.Profiles.FirstOrDefault(pr => pr.ApplicationUser.Equals(postModel.Post.Author));
            if (postModel.Post == null)
            {
                return(null);
            }
            var             store       = new UserStore <ApplicationUser>(new ApplicationDbContext());
            var             userManager = new UserManager <ApplicationUser>(store);
            var             author      = postModel.Post.Author;
            ApplicationUser user        = userManager.FindByIdAsync(author).Result;

            if (user != null)
            {
                postModel.Post.Author = user.UserName;
            }

            var tags = db.Tags.Where(tag => tag.PostId.Equals(postId)).ToList();

            foreach (var tag in tags)
            {
                postModel.Post.PostTags.Add(tag);
            }

            postModel.Comments = _commentsService.GetPagedCommentsByPostId(postId, author, sortParameters);


            return(postModel);
        }
Exemplo n.º 2
0
        public ActionResult Show(int?id, PostShowViewModel POSTdata)  // post d'un commentaire
        {
            if (id == null || id < 1)
            {
                return(View("Error"));
            }
            IDAL dal = new DAL();

            if (!int.TryParse(HttpContext.User.Identity.Name, out int IDloggedUser))
            {
                IDloggedUser = 0;
            }
            PostShowViewModel vm = new PostShowViewModel
            {
                Post         = dal.GetPost((int)id),
                Writers      = dal.GetWritersFromPost((int)id),
                Comments     = dal.GetCommentsPost((int)id),
                LoggedUserID = IDloggedUser,

                Title        = POSTdata.Title,
                Body         = POSTdata.Body,
                IDparentComm = POSTdata.IDparentComm
            };

            if (vm.Post == null)
            {
                return(View("Error"));
            }
            if (ModelState.IsValid)
            {
                dal.AddComment(vm.Title, vm.Body, DateTime.Now, int.Parse(HttpContext.User.Identity.Name), vm.Post.IDpost, vm.IDparentComm);
                return(RedirectToAction("Show/" + vm.Post.IDpost)); // Redirection pour vider le ViewModel et charger le nouveau commentaire
            }
            return(View(vm));
        }
Exemplo n.º 3
0
        public ActionResult Show(int?id)  // Affiche un billet par son ID
        {
            if (id == null || id < 1)
            {
                return(View("Error"));
            }
            IDAL dal = new DAL();

            if (!int.TryParse(HttpContext.User.Identity.Name, out int IDloggedUser))
            {
                IDloggedUser = 0;
            }
            PostShowViewModel vm = new PostShowViewModel
            {
                Post         = dal.GetPost((int)id),
                Writers      = dal.GetWritersFromPost((int)id),
                Comments     = dal.GetCommentsPost((int)id),
                LoggedUserID = IDloggedUser
            };

            if (IDloggedUser > 0)
            {
                vm.LoggedUserRole = dal.GetViewUser(IDloggedUser).Access_lvl;
            }
            if (vm.Post == null)
            {
                return(View("Error"));
            }
            return(View(vm));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Show(int id)
        {
            ViewBag.CurrentUserId = userManager.GetUserId(HttpContext.User);
            var user = await db.People.FindAsync(userManager.GetUserId(HttpContext.User));

            Post post = await GetPostAsync(id);

            var userPostRating = post.Ratings.FirstOrDefault(c => c.UserId == ViewBag.CurrentUserId);

            if (userPostRating == null)
            {
                post.Ratings.Add(new Rating
                {
                    Post  = post,
                    User  = user,
                    Value = 0
                });
                await db.SaveChangesAsync();
            }
            var listComments = post.Comments.ToList();

            foreach (var comment in listComments)
            {
                if (comment.CommentLikes.FirstOrDefault(c => c.User == user) == null) //поиск
                {
                    comment.CommentLikes.Add(new CommentLike
                    {
                        Comment = comment,
                        User    = user,
                        IsLike  = false
                    });
                    comment.LikesCount = LikeCounter(comment);
                    await db.SaveChangesAsync();
                }
            }

            var showPost = new PostShowViewModel
            {
                PostId = post.PostId,
                DateOfCreatedAuthor = post.User.Created,
                AuthorName          = post.User.UserName,
                AuthorAvatar        = post.User.ImagePath,
                Title        = post.Title,
                ImagePath    = post.ImagePath,
                Content      = Markdown.ToHtml(post.Content),
                PostComments = post.Comments.ToList(),
                Tags         = post.PostTags.ToList(),
                Rating       = post.Rating,
                UserRating   = post.Ratings.FirstOrDefault(c => c.UserId == ViewBag.CurrentUserId).Value
            };

            return(View(showPost));
        }
Exemplo n.º 5
0
        public ActionResult GetResForm(int IDpost, int IDparentComm) // Renvoie le formulaire pour poster une réponse à un commentaire
        {
            if (IDpost == 0 || IDparentComm == 0)
            {
                return(null);
            }
            PostShowViewModel vm = new PostShowViewModel
            {
                Post = new Post {
                    IDpost = IDpost
                },
                IDparentComm = IDparentComm
            };

            return(PartialView(vm));
        }
Exemplo n.º 6
0
        public IActionResult Show(long id)
        {
            Post post = postRepo.Get(id);
            PostShowViewModel model = new PostShowViewModel()
            {
                Id            = post.Id,
                Title         = post.Title,
                Text          = post.Text,
                BackwardLink  = "google.com",
                ForwardLink   = "bing.com",
                PublishDate   = post.CreationDate.ToString(),
                ResourceLinks = post.ResourceLinks,
                //RelatedPosts = post.RelatedPosts
            };

            return(View(model));
        }