Esempio n. 1
0
            public async Task <JobCommentDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var job = await _context.Jobs.FindAsync(request.JobId);

                if (job == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Job = "Not found" });
                }

                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == request.Username);

                var comment = new JobComment
                {
                    Author    = user,
                    Job       = job,
                    Body      = request.Body,
                    CreatedAt = DateTime.Now
                };

                // job.Comments.Add(jobcomment);
                job.JobComments.Add(comment);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(_mapper.Map <JobCommentDto>(comment));
                }

                throw new Exception("Problem saving changes");
            }
Esempio n. 2
0
        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(JobComment model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into JobComment(");
            strSql.Append(" MainID,CommentType,Comment,CreateHeadUrl,CreateNickName,CreateUser,CreateTime,IsDelete )");
            strSql.Append(" values (");
            strSql.Append("@MainID,@CommentType,@Comment,@CreateHeadUrl,@CreateNickName,@CreateUser,@CreateTime,@IsDelete)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@MainID",         model.MainID),
                new SqlParameter("@CommentType",    model.CommentType),
                new SqlParameter("@Comment",        model.Comment),
                new SqlParameter("@CreateHeadUrl",  model.CreateHeadUrl),
                new SqlParameter("@CreateNickName", model.CreateNickName),
                new SqlParameter("@CreateUser",     model.CreateUser),
                new SqlParameter("@CreateTime",     model.CreateTime),
                new SqlParameter("@IsDelete",       model.IsDelete),
            };

            object obj = SqlHelper.GetSingle(strSql.ToString(), CommandType.Text, parameters);

            return(obj == null ? 0 : Convert.ToInt32(obj));
        }
Esempio n. 3
0
        /// <summary>
        /// 修改 (可能有其他业务逻辑检查)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResultInfo Update(JobComment model)
        {
            ResultInfo ri = new ResultInfo();

            if (Edit(model))
            {
                ri.Ok  = true;
                ri.Msg = "修改成功";
            }

            return(ri);
        }
Esempio n. 4
0
        /// <summary>
        /// 保存 (可能有其他业务逻辑检查)
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public ResultInfo Create(JobComment model)
        {
            ResultInfo ri = new ResultInfo();

            if (model == null)
            {
                return(ri);
            }

            int result = Add(model);

            if (result > 0)
            {
                ri.Ok  = true;
                ri.Msg = "添加成功";
            }

            return(ri);
        }
Esempio n. 5
0
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(JobComment model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update JobComment set ");
            strSql.Append("MainID=@MainID,CommentType=@CommentType,Comment=@Comment,CreateHeadUrl=@CreateHeadUrl,CreateNickName=@CreateNickName,CreateUser=@CreateUser,CreateTime=@CreateTime,IsDelete=@IsDelete");

            strSql.Append(" where JobCommentID=@JobCommentID");
            SqlParameter[] parameters =
            {
                new SqlParameter("@MainID",         model.MainID),
                new SqlParameter("@CommentType",    model.CommentType),
                new SqlParameter("@Comment",        model.Comment),
                new SqlParameter("@CreateHeadUrl",  model.CreateHeadUrl),
                new SqlParameter("@CreateNickName", model.CreateNickName),
                new SqlParameter("@CreateUser",     model.CreateUser),
                new SqlParameter("@CreateTime",     model.CreateTime),
                new SqlParameter("@IsDelete",       model.IsDelete),

                new SqlParameter("@JobCommentID",   model.JobCommentID)
            };

            return(SqlHelper.ExecuteSql(strSql.ToString(), CommandType.Text, parameters) > 0);
        }
Esempio n. 6
0
        public async Task <bool> AddJobComment(CommentViewModel jobComment)
        {
            try
            {
                var job = await _dbContext.Jobs.Include(c => c.JobComments).Where(j => j.Id == jobComment.JobId).FirstAsync();

                if (!string.IsNullOrEmpty(jobComment.Comment))
                {
                    JobComment comment = new JobComment();
                    comment.Comment         = jobComment.Comment;
                    comment.CommentedUserId = jobComment.UserId;
                    comment.JobId           = jobComment.JobId;
                    comment.CreatedDateTime = DateTime.UtcNow;

                    job.JobComments.Add(comment);
                    await _dbContext.SaveChangesAsync();
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Esempio n. 7
0
 public bool InsertJobComment(JobComment comment)
 {
     _jobCommands.ActiveUser = Models.ApiContext.ActiveUser;
     return(_jobCommands.InsertJobComment(comment));
 }
Esempio n. 8
0
 /// <summary>
 /// 修改一条记录
 /// </summary>
 /// <param name="model">实体对象</param>
 /// <returns></returns>
 public bool Edit(JobComment model)
 {
     return(dal.Update(model));
 }
Esempio n. 9
0
 /// <summary>
 /// 添加一条记录,没有任何逻辑
 /// </summary>
 /// <param name="model">实体对象</param>
 /// <returns></returns>
 public int Add(JobComment model)
 {
     return(dal.Add(model));
 }
Esempio n. 10
0
        private Dictionary <Type, List <EntityInfo> > BeforeSaveEntities(Dictionary <Type, List <EntityInfo> > saveMap)
        {
            #region JobPricing
            List <EntityInfo> entityInfos = saveMap.Where(e => e.Key == typeof(JobPricing)).Select(e => e.Value).FirstOrDefault();
            if (entityInfos != null)
            {
                foreach (EntityInfo entityInfo in entityInfos)
                {
                    JobPricing jobPricing = (JobPricing)(entityInfo.Entity);

                    // single query without lazy loading
                    var temp = (from p in Session.Query <JobTranslation>()
                                where p.Id == jobPricing.JobId
                                select new { JobTranslation = p, p.SourceMaterial.Request }).FirstOrDefault();

                    if (temp != null)
                    {
                        if (!CheckForObjectTypeInSaveMap(temp.JobTranslation, saveMap))
                        {
                            temp.JobTranslation.UpdateDate = DateTime.UtcNow;
                            saveMap.AddCustomEntity(temp.JobTranslation, this);
                        }

                        if (!saveMap.Any(e => e.Key == typeof(Request)))
                        {
                            temp.Request.UpdateDate = DateTime.UtcNow;
                            saveMap.AddCustomEntity(temp.Request, this);
                        }
                    }
                }
            }
            #endregion

            #region JobMaterial
            entityInfos = saveMap.Where(e => e.Key == typeof(JobMaterial)).Select(e => e.Value).FirstOrDefault();
            if (entityInfos != null)
            {
                foreach (EntityInfo entityInfo in entityInfos)
                {
                    JobMaterial jobMaterial = (JobMaterial)(entityInfo.Entity);

                    if (!saveMap.Any(e => e.Key.IsSubclassOf(typeof(Job)) && e.Value.Any(f => ((Job)f.Entity).Id == jobMaterial.JobId)))
                    {
                        // single query without lazy loading
                        var temp = (from p in Session.Query <Job>()
                                    where p.Id == jobMaterial.JobId
                                    select new { Job = p, Request = p.SourceMaterial.Request }).FirstOrDefault();

                        if (temp != null)
                        {
                            temp.Job.UpdateDate = DateTime.UtcNow;
                            saveMap.AddCustomEntity(temp.Job, this);

                            if (!saveMap.Any(e => e.Key == typeof(Request)))
                            {
                                temp.Request.UpdateDate = DateTime.UtcNow;
                                saveMap.AddCustomEntity(temp.Request, this);
                            }
                        }
                    }
                }
            }
            #endregion

            #region Reference
            entityInfos = saveMap.Where(e => e.Key == typeof(Reference)).Select(e => e.Value).FirstOrDefault();
            if (entityInfos != null)
            {
                foreach (EntityInfo entityInfo in entityInfos)
                {
                    Reference reference = (Reference)(entityInfo.Entity);
                    Request   request   = saveMap.Where(e => e.Key == typeof(Request)).SelectMany(e => e.Value).Select(c => (Request)c.Entity).FirstOrDefault();

                    if (request != null)
                    {
                        request.UpdateDate = DateTime.UtcNow;
                    }
                    else
                    {
                        // single query without lazy loading
                        request = (from p in Session.Query <Request>()
                                   where p.ReferenceSet.Id == reference.ReferenceSetId
                                   select p).FirstOrDefault();
                        if (request != null)
                        {
                            request.UpdateDate = DateTime.UtcNow;
                            saveMap.AddCustomEntity(request, this);
                        }
                    }
                }
            }
            #endregion

            #region JobComment
            entityInfos = saveMap.Where(e => e.Key == typeof(JobComment)).Select(e => e.Value).FirstOrDefault();
            if (entityInfos != null)
            {
                foreach (EntityInfo entityInfo in entityInfos)
                {
                    JobComment jobComment = (JobComment)(entityInfo.Entity);

                    // single query without lazy loading
                    var temp = (from p in Session.Query <Job>()
                                where p.Id == jobComment.JobId
                                select new { Job = p, Request = p.SourceMaterial.Request }).First();

                    temp.Job.UpdateDate = DateTime.UtcNow;
                    saveMap.AddCustomEntity(temp.Job, this);

                    if (!saveMap.Any(e => e.Key == typeof(Request)))
                    {
                        temp.Request.UpdateDate = DateTime.UtcNow;
                        saveMap.AddCustomEntity(temp.Request, this);
                    }
                }
            }
            #endregion

            #region Job
            entityInfos = saveMap.Where(e => e.Key.IsSubclassOf(typeof(Job))).Select(e => e.Value).FirstOrDefault();
            if (entityInfos != null)
            {
                foreach (EntityInfo entityInfo in entityInfos)
                {
                    if (!saveMap.Any(e => e.Key == typeof(Request)))
                    {
                        Request request = GetQuery <SourceMaterial>().Where(e => e.Id == ((Job)entityInfo.Entity).SourceMaterialId).Select(e => e.Request).FirstOrDefault();
                        if (request == null)
                        {
                            Guid?requestId = saveMap.Where(e => e.Key == typeof(SourceMaterial))
                                             .SelectMany(e => e.Value)
                                             .Select(x => (SourceMaterial)x.Entity)
                                             .Where(x => x.Id == ((Job)entityInfo.Entity).SourceMaterialId)
                                             .Select(x => x.RequestId)
                                             .SingleOrDefault();

                            if (requestId.HasValue)
                            {
                                request = GetQuery <Request>().FirstOrDefault(e => e.Id == requestId);
                            }
                        }
                        saveMap.AddCustomEntity(request, this);
                    }
                }
            }
            #endregion

            #region Material
            entityInfos = saveMap.Where(e => e.Key == typeof(Material) || e.Key.IsSubclassOf(typeof(Material))).Select(e => e.Value).FirstOrDefault();
            if (entityInfos != null)
            {
                // Update 'UploadedBy' field only for new entities
                foreach (EntityInfo entityInfo in entityInfos.Where(e => e.EntityState == EntityState.Added))
                {
                    Material material = (Material)entityInfo.Entity;
                    if (string.IsNullOrEmpty(material.UploadedBy))
                    {
                        material.UploadedBy = material.CreatedBy;
                    }
                    if (!material.UploaderType.HasValue)
                    {
                        material.UploaderType = UploaderType.Other;
                    }
                }
            }
            #endregion

            var tmp = saveMap.Where(p => p.Key.IsSubclassOf(typeof(Job)))
                      .Select(p => p.Value.Where(x => x.EntityState == EntityState.Deleted).ToList())
                      .FirstOrDefault();
            var jobs = tmp == null ? null : tmp.Select(x => (Job)x.Entity).ToList();

            var requestToChange = saveMap
                                  .Where(p => p.Key.IsAssignableFrom(typeof(Request)))
                                  .Select(p => p.Value)
                                  .FirstOrDefault();
            var smToChange = saveMap
                             .Where(p => p.Key.IsAssignableFrom(typeof(SourceMaterial)))
                             .Select(p => p.Value.Where(x => x.EntityState == EntityState.Added).ToList())
                             .FirstOrDefault();

            if (requestToChange != null)
            {
                var rq = ((Request)requestToChange.First().Entity);
                List <SourceMaterial> sms = new List <SourceMaterial>();
                sms = Session.Query <SourceMaterial>().Where(x => x.RequestId == rq.Id).Where(x => !x.Jobs.Any()).Distinct().ToList();
                if (jobs != null)
                {
                    foreach (var job in jobs)
                    {
                        sms.AddRange(Session.Query <SourceMaterial>().Where(x => x.RequestId == rq.Id).Where(v => v.Jobs.Contains(job)).Distinct().ToList());
                    }
                }

                if (rq.RequestTemplateId != null)
                {
                    foreach (var sm in sms)
                    {
                        if (!CheckForObjectTypeInSaveMap(sm, saveMap))
                        {
                            sm.UpdateDate = DateTime.UtcNow;
                            saveMap.AddCustomEntity <SourceMaterial>(sm, this);
                        }
                    }
                }
            }

            Session.Clear();
            return(saveMap);
        }