public ValueDataResponse <JobPlan> AddJobPlan(UpsertJobPlan jobPlans)
        {
            ValueDataResponse <JobPlan> response = new ValueDataResponse <JobPlan>();

            try
            {
                JobPlan job    = _mapper.Map <JobPlan>(jobPlans);
                var     result = _appContext.JobPlans.Add(job);
                _appContext.SaveChanges();

                foreach (var jtask in jobPlans.JobPlanTasks)
                {
                    _appContext.JobTasks.Add(new JobTask {
                        Id = jtask.Id, JobPlanId = job.Id, Name = jtask.Name, Duration = jtask.Duration, AstTradeId = jtask.AstTradeId
                    });
                }
                _appContext.SaveChanges();

                if (job != null)
                {
                    response.Result          = job;
                    response.IsSuccess       = true;
                    response.AffectedRecords = 1;
                    response.EndUserMessage  = "Job Plan Added Successfully";
                }
                else
                {
                    response.IsSuccess       = true;
                    response.AffectedRecords = 0;
                    response.EndUserMessage  = "Job Plan Added Failed";
                }
            }
            catch (Exception ex)
            {
                response.IsSuccess       = false;
                response.AffectedRecords = 0;
                response.EndUserMessage  = ex.InnerException == null ? ex.Message : ex.InnerException.Message;
                response.Exception       = ex;
            }
            return(response);
        }
 public ValueDataResponse <JobPlan> UpdateJobPlan(UpsertJobPlan jobPlans)
 {
     return(_unitOfWork.JobPlans.UpdateJobPlan(jobPlans));
 }
        public ValueDataResponse <JobPlan> UpdateJobPlan(UpsertJobPlan jobPlans)
        {
            ValueDataResponse <JobPlan> response = new ValueDataResponse <JobPlan>();

            try
            {
                JobPlan job      = _mapper.Map <JobPlan>(jobPlans);
                var     result   = _appContext.JobPlans.Where(x => x.Id == job.Id).FirstOrDefault();
                var     jobtakss = _appContext.JobTasks.Where(x => x.JobPlanId == job.Id).ToList();
                _appContext.JobTasks.RemoveRange(jobtakss);
                _appContext.SaveChanges();

                foreach (var jtask in jobPlans.JobPlanTasks)
                {
                    _appContext.JobTasks.Add(new JobTask {
                        Id = jtask.Id, JobPlanId = job.Id, Name = jtask.Name, Duration = jtask.Duration, AstTradeId = jtask.AstTradeId
                    });
                }

                if (result != null)
                {
                    result.Name           = jobPlans.Name;
                    result.JobDescription = jobPlans.JobDescription;
                    result.JobReference   = jobPlans.JobReference;
                    result.Duration       = jobPlans.Duration;
                    result.SiteId         = jobPlans.SiteId;
                    result.ProjectId      = jobPlans.ProjectId;
                    result.TechnicianId   = jobPlans.TechnicianId;
                    result.AssetGroupId   = jobPlans.AssetGroupId;
                    result.StatusTypeId   = jobPlans.StatusTypeId;
                    result.CreatedBy      = jobPlans.CreatedBy;
                    result.CreatedDate    = jobPlans.CreatedDate;
                    result.UpdatedBy      = jobPlans.UpdatedBy;
                    result.UpdatedDate    = jobPlans.UpdatedDate;
                    result.IsActive       = jobPlans.IsActive;
                }
                _appContext.SaveChanges();

                if (job != null)
                {
                    response.Result          = job;
                    response.IsSuccess       = true;
                    response.AffectedRecords = 1;
                    response.EndUserMessage  = "Job Plan Updated Successfully";
                }
                else
                {
                    response.IsSuccess       = true;
                    response.AffectedRecords = 0;
                    response.EndUserMessage  = "Job Plan Updation Failed";
                }
            }
            catch (Exception ex)
            {
                response.IsSuccess       = false;
                response.AffectedRecords = 0;
                response.EndUserMessage  = ex.InnerException == null ? ex.Message : ex.InnerException.Message;
                response.Exception       = ex;
            }
            return(response);
        }