public IActionResult Comment([FromBody] ApplyCommentDataModel model)
        {
            ApplyComment m = null;

            if (model?.Id != Guid.Empty)
            {
                m = context.ApplyCommentsDb.FirstOrDefault(i => i.Id == model.Id);
            }
            var actionUser = currentUserService.CurrentUser;

            if (actionUser == null)
            {
                return(new JsonResult(ActionStatusMessage.Account.Auth.Invalid.NotLogin));
            }
            var apply = model.IsRemove?null:  applyService.GetById(Guid.Parse(model.Apply));
            //if (apply == null&&!model.IsRemove) return new JsonResult(ActionStatusMessage.ApplyMessage.NotExist);
            var act = model.IsRemove ? "删除" : "添加";
            var ua  = userActionServices.Log(UserOperation.AttachInfoToApply, actionUser.Id, $"{act}假期{apply?.Id.ToString()??m?.Apply}的评论");

            if (m == null)
            {
                if (model.IsRemove)
                {
                    return(new JsonResult(userActionServices.LogNewActionInfo(ua, ActionStatusMessage.StaticMessage.ResourceNotExist)));
                }
                else
                {
                    m = new ApplyComment()
                    {
                        Content = model.Content,
                        From    = actionUser,
                        Apply   = model.Apply,
                        Create  = DateTime.Now
                    };
                    context.ApplyComments.Add(m);
                }
            }
            else
            {
                var permission = actionUser.Id == m.FromId || userActionServices.Permission(actionUser.Application.Permission, DictionaryAllPermission.Apply.AttachInfo, model.IsRemove ? Operation.Remove : Operation.Update, actionUser.Id, m.From.CompanyInfo.CompanyCode);
                if (permission)
                {
                    m.LastModify = DateTime.Now;
                    m.ModifyBy   = actionUser;
                    if (model.IsRemove)
                    {
                        m.Remove();
                    }
                    else
                    {
                        m.Content = model.Content;
                        context.ApplyComments.Update(m);
                    }
                }
            }
            context.SaveChanges();
            userActionServices.Status(ua, true);
            return(new JsonResult(new EntityViewModel <ApplyCommentVDataModel>(m.ToDataModel(context.ApplyCommentLikes, actionUser.Id))));
        }
示例#2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="i"></param>
 /// <param name="likesDb">点赞库</param>
 /// <param name="currentUser">当前登录的用户</param>
 /// <returns></returns>
 public static ApplyCommentVDataModel ToDataModel(this ApplyComment i, IQueryable <ApplyCommentLike> likesDb, string currentUser) =>
 new ApplyCommentVDataModel()
 {
     Id         = i.Id,
     Apply      = i.Apply,
     Content    = i.Content,
     Create     = i.Create,
     From       = i.From.ToSummaryDto(),
     LastModify = i.LastModify,
     ModifyBy   = i.ModifyBy.ToSummaryDto(),
     Like       = i.Likes,
     MyLike     = currentUser == null ? false : likesDb.Where(like => like.CommentId == i.Id).Any(like => like.CreateById == currentUser)
 };