public async Task <IActionResult> Post([FromBody] CommentModel model)
        {
            var comment = model.ToEntity();

            comment.UserId = this.workContext.CurrentUserId;

            if (model.ReportId.HasValue && await this.reportService.GetById(model.ReportId.Value) == null)
            {
                return(this.BadRequest(new AdoptersException(AdopterExceptionCode.RowNotFound), "ReportId"));
            }

            try
            {
                await this.commentService.Insert(comment);
            }
            catch (AdoptersException e)
            {
                return(this.BadRequest(e));
            }

            return(this.Ok(new BaseModel()
            {
                Id = comment.Id
            }));
        }
        public ActionResult Create(CommentModel model)
        {
            if (ModelState.IsValid)
            {
                var comment = model.ToEntity();
                comment.IsNew = false;
                _commentRepository.InsertAndCommit(comment);

                //notification
                SuccessNotification(_localizationService.GetResource("Record.Saved"));
                return(Json(new { Html = this.RenderPartialViewToString("CommentBox", comment) }));
            }
            else
            {
                return(Json(new { Errors = ModelState.SerializeErrors() }));
            }
        }
Ejemplo n.º 3
0
        public async Task <CommentModel> AddCommentAsync(CommentModel model)
        {
            model = model ?? throw new ArgumentNullException(nameof(model));

            try
            {
                Comment entity = await this.repo.AddArticleCommentAsync(model.ToEntity()).ConfigureAwait(false);

                if (entity == null)
                {
                    return(null);
                }

                CommentModel updatedModel = new CommentModel();
                updatedModel.ToModel(entity);

                return(updatedModel);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 4
0
 public async Task Update(Guid id, CommentModel model)
 {
     ValidateModel(model);
     await CommentRepository.Update(id, model.ToEntity());
 }
Ejemplo n.º 5
0
 public async Task Create(CommentModel model)
 {
     ValidateModel(model);
     await CommentRepository.Create(model.ToEntity());
 }
Ejemplo n.º 6
0
        public ActionResult Details(NewsWidgetModel model, FormCollection frm)
        {
            var news = _newsService.GetNewsById(model.Id);
            var user = _userContext.CurrentUser;

            if (user != null)
            {
                model.IsAuthenticated = true;
            }

            if (ModelState.IsValid)
            {
                if (model.IsAuthenticated)
                {
                    string textComments = "";
                    var    keyComment   = frm.AllKeys.FirstOrDefault(x => x.Trim().ToLower() == "txtcomment");
                    if (keyComment != null && !string.IsNullOrEmpty(frm[keyComment].ToString()))
                    {
                        textComments = frm[keyComment].ToString();

                        var comments = _commentService.GetCommentsByNews(model.Id);
                        comments = comments.OrderBy(x => x.DisplayOrder).ToList();

                        var newComment = new CommentModel();
                        newComment.NewsId       = model.Id;
                        newComment.CommentHtml  = textComments;
                        newComment.DisLikes     = newComment.Likes = newComment.ExamId = newComment.HomeworkId = newComment.BlogId = newComment.ProductId = 0;
                        newComment.UserId       = user.Id;
                        newComment.Username     = user.UserName;
                        newComment.DisplayOrder = comments.Count + 1;
                        var commentEntity = newComment.ToEntity();
                        commentEntity.CreatedOn = commentEntity.ModifiedOn = DateTime.Now;
                        _commentService.Insert(commentEntity);

                        if (commentEntity.Id > 0)
                        {
                            news.Comments.Add(commentEntity);
                        }

                        // Update News
                        _newsService.Update(news);
                    }
                }
                else
                {
                    var    newUser      = new UserModel();
                    string textComments = "";
                    var    keyUsername  = frm.AllKeys.FirstOrDefault(x => x.Trim().ToLower() == "username");
                    if (keyUsername != null && !string.IsNullOrEmpty(frm[keyUsername].ToString()))
                    {
                        newUser.Username = frm[keyUsername].ToString();
                    }

                    var keyEmail = frm.AllKeys.FirstOrDefault(x => x.Trim().ToLower() == "email");
                    if (keyEmail != null && !string.IsNullOrEmpty(frm[keyEmail].ToString()))
                    {
                        newUser.Email = frm[keyEmail].ToString();
                    }

                    var keyPassword = frm.AllKeys.FirstOrDefault(x => x.Trim().ToLower() == "password");
                    if (keyPassword != null && !string.IsNullOrEmpty(frm[keyPassword].ToString()))
                    {
                        newUser.Password = frm[keyPassword].ToString();
                    }

                    var keyComment = frm.AllKeys.FirstOrDefault(x => x.Trim().ToLower() == "txtcomment");
                    if (keyComment != null && !string.IsNullOrEmpty(frm[keyComment].ToString()))
                    {
                        textComments = frm[keyComment].ToString();
                    }

                    if (!string.IsNullOrEmpty(newUser.Username) &&
                        !string.IsNullOrEmpty(newUser.Email) &&
                        !string.IsNullOrEmpty(newUser.Password) &&
                        !string.IsNullOrEmpty(textComments))
                    {
                        newUser.IsActive   = true;
                        newUser.IsApproved = true;
                        newUser.IsBlocked  = false;
                        newUser.UserGuid   = Guid.NewGuid();
                        var userEntity = newUser.ToEntity();
                        userEntity.CreatedOn = userEntity.ModifiedOn = DateTime.Now;

                        var defaultRole = _roleService.GetRoleByName("General");
                        userEntity.Roles.Add(defaultRole);
                        _userService.Insert(userEntity);

                        // Impersonate Relation
                        var teacher = _smsService.GetTeacherByImpersonateId(userEntity.Id);
                        var student = _smsService.GetStudentByImpersonateId(userEntity.Id);

                        //sign in customer
                        _authenticationService.SignIn(userEntity, false);
                        _userContext.CurrentUser = userEntity;

                        var comments = _commentService.GetCommentsByNews(model.Id);
                        comments = comments.OrderBy(x => x.DisplayOrder).ToList();

                        var newComment = new CommentModel();
                        newComment.NewsId       = model.Id;
                        newComment.CommentHtml  = textComments;
                        newComment.DisLikes     = newComment.Likes = newComment.ExamId = newComment.HomeworkId = newComment.BlogId = newComment.ProductId = 0;
                        newComment.UserId       = userEntity.Id;
                        newComment.Username     = userEntity.UserName;
                        newComment.DisplayOrder = comments.Count + 1;
                        var commentEntity = newComment.ToEntity();
                        commentEntity.CreatedOn = commentEntity.ModifiedOn = DateTime.Now;
                        _commentService.Insert(commentEntity);

                        if (commentEntity.Id > 0)
                        {
                            news.Comments.Add(commentEntity);
                        }

                        // Update News
                        _newsService.Update(news);
                    }
                }

                SuccessNotification("Comment Added Successfully!");
            }
            else
            {
                if (news != null)
                {
                    model = news.ToWidgetModel();
                    model.IsAuthenticated = false;

                    if (user != null)
                    {
                        model.IsAuthenticated = true;
                    }

                    var newsPictures = _pictureService.GetNewsPictureByNewsId(news.Id).OrderByDescending(x => x.StartDate).ToList();
                    model.HasDefaultPicture = newsPictures.Any(x => x.IsDefault);

                    var newsVideos = _videoService.GetNewsVideosByNewsId(news.Id).OrderByDescending(x => x.StartDate).ToList();
                    model.HasDefaultVideo = newsVideos.Count > 0;

                    if (newsPictures.Count > 0)
                    {
                        model.DefaultPictureSrc = newsPictures.FirstOrDefault(x => model.HasDefaultPicture ? x.IsDefault : true).Picture.PictureSrc;
                        model.Pictures          = newsPictures.Select(x => x.ToModel()).ToList();
                    }

                    if (newsVideos.Count > 0)
                    {
                        model.DefaultVideoSrc = newsVideos.FirstOrDefault().Video.VideoSrc;
                        model.Videos          = newsVideos.Select(x => x.ToModel()).ToList();
                    }

                    model.Reactions = _smsService.SearchReactions(newsid: news.Id).Select(x => x.ToModel()).OrderByDescending(x => x.CreatedOn).ToList();
                    model.Comments  = _commentService.GetCommentsByNews(news.Id).OrderByDescending(x => x.CreatedOn).Select(x => x.ToWidgetModel()).ToList();

                    if (model.Comments.Count > 0)
                    {
                        foreach (var comment in model.Comments)
                        {
                            comment.Replies = _replyService.GetAllRepliesByComment(comment.Id).OrderBy(x => x.DisplayOrder).Select(x => x.ToWidgetModel()).ToList();
                        }
                    }

                    var fromUser = _userService.GetUserById(model.UserId);
                    if (fromUser != null)
                    {
                        model.User     = fromUser.ToModel();
                        model.Username = !string.IsNullOrEmpty(fromUser.FirstName) ? (fromUser.FirstName + (!string.IsNullOrEmpty(fromUser.LastName) ? (" " + fromUser.LastName) : "")) : !string.IsNullOrEmpty(fromUser.UserName) ? fromUser.UserName : fromUser.Email;

                        if (model.User.ProfilePictureId > 0)
                        {
                            var proPicture = _pictureService.GetPictureById(model.User.ProfilePictureId);
                            model.User.ProfilePicture = proPicture.ToModel();
                        }

                        if (model.User.CoverPictureId > 0)
                        {
                            var coverPicture = _pictureService.GetPictureById(model.User.CoverPictureId);
                            model.User.CoverPicture = coverPicture.ToModel();
                        }
                    }
                    var studentUser = _smsService.GetStudentByImpersonatedUser(fromUser.Id);
                    var teacherUser = _smsService.GetTeacherByImpersonatedUser(fromUser.Id);
                    if (studentUser != null)
                    {
                        model.IsStudent = true;
                        model.Student   = studentUser.ToModel();

                        if (model.Student.StudentPictureId > 0)
                        {
                            var proPicture = _pictureService.GetPictureById(model.Student.StudentPictureId);
                            model.User.StudentProfilePicture = proPicture.ToModel();
                        }

                        if (model.Student.CoverPictureId > 0)
                        {
                            var coverPicture = _pictureService.GetPictureById(model.Student.CoverPictureId);
                            model.User.StudentCoverPicture = coverPicture.ToModel();
                        }
                    }

                    if (teacherUser != null)
                    {
                        model.IsTeacher = true;
                        model.Teacher   = teacherUser.ToModel();

                        if (model.Teacher.ProfilePictureId > 0)
                        {
                            var proPicture = _pictureService.GetPictureById(model.Teacher.ProfilePictureId);
                            model.User.TeacherProfilePicture = proPicture.ToModel();
                        }

                        if (model.Teacher.CoverPictureId > 0)
                        {
                            var coverPicture = _pictureService.GetPictureById(model.Teacher.CoverPictureId);
                            model.User.TeacherCoverPicture = coverPicture.ToModel();
                        }
                    }

                    model.LatestPosts = _newsService.GetLatestNews(model.Id).Select(x => x.ToWidgetModel()).OrderByDescending(x => x.CreatedOn).Take(3).ToList();
                    foreach (var post in model.LatestPosts)
                    {
                        var postPictures = _pictureService.GetNewsPictureByNewsId(post.Id).OrderByDescending(x => x.StartDate).ToList();
                        post.HasDefaultPicture = postPictures.Any(x => x.IsDefault);

                        var postVideos = _videoService.GetNewsVideosByNewsId(post.Id).OrderByDescending(x => x.StartDate).ToList();
                        post.HasDefaultVideo = postVideos.Count > 0;

                        if (postPictures.Count > 0)
                        {
                            post.DefaultPictureSrc = postPictures.FirstOrDefault(x => post.HasDefaultPicture ? x.IsDefault : true).Picture.PictureSrc;
                            post.Pictures          = postPictures.Select(x => x.ToModel()).ToList();
                        }

                        if (postVideos.Count > 0)
                        {
                            post.DefaultVideoSrc = postVideos.FirstOrDefault().Video.VideoSrc;
                            post.Videos          = postVideos.Select(x => x.ToModel()).ToList();
                        }
                    }

                    model.OlderPosts = _newsService.GetOlderNews(model.Id).Select(x => x.ToWidgetModel()).OrderByDescending(x => x.CreatedOn).Take(3).ToList();
                    foreach (var post in model.OlderPosts)
                    {
                        var postPictures = _pictureService.GetNewsPictureByNewsId(post.Id).OrderByDescending(x => x.StartDate).ToList();
                        post.HasDefaultPicture = postPictures.Any(x => x.IsDefault);

                        var postVideos = _videoService.GetNewsVideosByNewsId(post.Id).OrderByDescending(x => x.StartDate).ToList();
                        post.HasDefaultVideo = postVideos.Count > 0;

                        if (postPictures.Count > 0)
                        {
                            post.DefaultPictureSrc = postPictures.FirstOrDefault(x => post.HasDefaultPicture ? x.IsDefault : true).Picture.PictureSrc;
                            post.Pictures          = postPictures.Select(x => x.ToModel()).ToList();
                        }

                        if (postVideos.Count > 0)
                        {
                            post.DefaultVideoSrc = postVideos.FirstOrDefault().Video.VideoSrc;
                            post.Videos          = postVideos.Select(x => x.ToModel()).ToList();
                        }
                    }

                    model.Pictures.Clear();
                    foreach (var picture in newsPictures)
                    {
                        if (!model.Pictures.Any(x => x.Id == picture.Id))
                        {
                            var picModel = picture.ToModel();
                            picModel.CreatedOn  = picture.CreatedOn;
                            picModel.ModifiedOn = picture.ModifiedOn;
                            picModel.Picture    = _pictureService.GetPictureById(picture.PictureId).ToModel();
                            model.Pictures.Add(picModel);
                        }
                    }

                    model.Videos.Clear();
                    foreach (var video in newsVideos)
                    {
                        if (!model.Videos.Any(x => x.Id == video.Id))
                        {
                            var vidModel = video.ToModel();
                            vidModel.CreatedOn  = video.CreatedOn;
                            vidModel.ModifiedOn = video.ModifiedOn;
                            vidModel.Video      = _videoService.GetVideoById(video.VideoId).ToModel();
                            model.Videos.Add(vidModel);
                        }
                    }
                }
                else
                {
                    return(RedirectToAction("PageNotFound"));
                }

                return(View(model));
            }

            return(RedirectToRoute("News", new { name = model.SystemName }));
        }