public IActionResult Post([FromBody] TblTrainingPrograms nTrainingProgram) { nTrainingProgram.CreateDate = DateTime.Now; nTrainingProgram.Creator = nTrainingProgram.Creator ?? "Someone"; if (nTrainingProgram.TblBasicCourse != null) { foreach (var basic in nTrainingProgram.TblBasicCourse) { basic.CreateDate = nTrainingProgram.CreateDate; basic.Creator = nTrainingProgram.Creator; } } if (nTrainingProgram.TblStandardCourse != null) { foreach (var standard in nTrainingProgram.TblStandardCourse) { standard.CreateDate = nTrainingProgram.CreateDate; standard.Creator = nTrainingProgram.Creator; } } if (nTrainingProgram.TblSupplementCourse != null) { foreach (var suppl in nTrainingProgram.TblSupplementCourse) { suppl.CreateDate = nTrainingProgram.CreateDate; suppl.Creator = nTrainingProgram.Creator; } } if (nTrainingProgram.TblTrainingProgramHasPosition != null) { foreach (var hasPo in nTrainingProgram.TblTrainingProgramHasPosition) { hasPo.CreateDate = nTrainingProgram.CreateDate; hasPo.Creator = nTrainingProgram.Creator; } } if (nTrainingProgram.TblTrainingProgramHasGroup != null) { foreach (var hasGp in nTrainingProgram.TblTrainingProgramHasGroup) { hasGp.CreateDate = nTrainingProgram.CreateDate; hasGp.Creator = nTrainingProgram.Creator; } } return(new JsonResult(this.repository.AddAsync(nTrainingProgram).Result, this.DefaultJsonSettings)); }
public IActionResult Put(int id, [FromBody] TblTrainingPrograms uTrainingProgram) { uTrainingProgram.ModifyDate = DateTime.Now; uTrainingProgram.Modifyer = uTrainingProgram.Modifyer ?? "Someone"; #region Update ModifyDate for child if (uTrainingProgram.TblBasicCourse != null) { foreach (var basic in uTrainingProgram.TblBasicCourse) { if (basic.BasicCourseId > 0) { basic.ModifyDate = uTrainingProgram.ModifyDate; basic.Modifyer = uTrainingProgram.Modifyer; } else { basic.CreateDate = uTrainingProgram.ModifyDate; basic.Creator = uTrainingProgram.Modifyer; } } } if (uTrainingProgram.TblStandardCourse != null) { foreach (var standard in uTrainingProgram.TblStandardCourse) { if (standard.StandardCourseId > 0) { standard.ModifyDate = uTrainingProgram.ModifyDate; standard.Modifyer = uTrainingProgram.Modifyer; } else { standard.CreateDate = uTrainingProgram.ModifyDate; standard.Creator = uTrainingProgram.Modifyer; } } } if (uTrainingProgram.TblSupplementCourse != null) { foreach (var supp in uTrainingProgram.TblSupplementCourse) { if (supp.SupplermentCourseId > 0) { supp.ModifyDate = uTrainingProgram.ModifyDate; supp.Modifyer = uTrainingProgram.Modifyer; } else { supp.CreateDate = uTrainingProgram.ModifyDate; supp.Creator = uTrainingProgram.Modifyer; } } } if (uTrainingProgram.TblTrainingProgramHasPosition != null) { foreach (var hasPo in uTrainingProgram.TblTrainingProgramHasPosition) { if (hasPo.ProgramHasPositionId > 0) { hasPo.ModifyDate = uTrainingProgram.ModifyDate; hasPo.Modifyer = uTrainingProgram.Modifyer; } else { hasPo.CreateDate = uTrainingProgram.ModifyDate; hasPo.Creator = uTrainingProgram.Modifyer; } } } if (uTrainingProgram.TblTrainingProgramHasGroup != null) { foreach (var hasGp in uTrainingProgram.TblTrainingProgramHasGroup) { if (!string.IsNullOrEmpty(hasGp.GroupCode)) { hasGp.ModifyDate = uTrainingProgram.ModifyDate; hasGp.Modifyer = uTrainingProgram.Modifyer; } else { hasGp.CreateDate = uTrainingProgram.ModifyDate; hasGp.Creator = uTrainingProgram.Modifyer; } } } #endregion Update ModifyDate for child var hasData = this.repository.UpdateAsync(uTrainingProgram, id).Result; if (hasData != null) { #region Update and Remove for child #region TblBasicCourse // filter TblBasicCourse Expression <Func <TblBasicCourse, bool> > condition = m => m.TrainingProgramId == id; var items = this.repositoryBasic.FindAllAsync(condition).Result; //Remove TblBasicCourse if edit remove it foreach (var item in items) { if (!uTrainingProgram.TblBasicCourse.Any(x => x.TrainingCousreId == item.TrainingCousreId)) { this.repositoryBasic.Delete(item.BasicCourseId); } } //Update TblBasicCourse foreach (var item in uTrainingProgram.TblBasicCourse) { if (item.BasicCourseId > 0) { this.repositoryBasic.Update(item, item.BasicCourseId); } else { item.TrainingProgramId = id; this.repositoryBasic.Add(item); } } #endregion TblBasicCourse #region TblStandardCourse // filter TblStandardCourse Expression <Func <TblStandardCourse, bool> > condition2 = m => m.TrainingProgramId == id; var item2s = this.repositoryStandard.FindAllAsync(condition2).Result; //Remove TblStandardCourse if edit remove it foreach (var item in item2s) { if (!uTrainingProgram.TblStandardCourse.Any(x => x.TrainingCousreId == item.TrainingCousreId)) { this.repositoryStandard.Delete(item.StandardCourseId); } } //Update TblStandardCourse foreach (var item in uTrainingProgram.TblStandardCourse) { if (item.StandardCourseId > 0) { this.repositoryStandard.Update(item, item.StandardCourseId); } else { item.TrainingProgramId = id; this.repositoryStandard.Add(item); } } #endregion TblStandardCourse #region TblSupplementCourse // filter TblSupplementCourse Expression <Func <TblSupplementCourse, bool> > condition3 = m => m.TrainingProgramId == id; var item3s = this.repositorySupplement.FindAllAsync(condition3).Result; //Remove TblSupplementCourse if edit remove it foreach (var item in item3s) { if (!uTrainingProgram.TblSupplementCourse.Any(x => x.TrainingCousreId == item.TrainingCousreId)) { this.repositorySupplement.Delete(item.SupplermentCourseId); } } //Update TblSupplementCourse foreach (var item in uTrainingProgram.TblSupplementCourse) { if (item.SupplermentCourseId > 0) { this.repositorySupplement.Update(item, item.SupplermentCourseId); } else { item.TrainingProgramId = id; this.repositorySupplement.Add(item); } } #endregion TblSupplementCourse #region TblTrainingProgramHasPosition // filter TblTrainingProgramHasPosition Expression <Func <TblTrainingProgramHasPosition, bool> > condition4 = m => m.TrainingProgramId == id; var item4s = this.repositoryHasPosition.FindAllAsync(condition4).Result; //Remove TblTrainingProgramHasPosition if edit remove it foreach (var item in item4s) { if (!uTrainingProgram.TblTrainingProgramHasPosition.Any(x => x.PositionCode == item.PositionCode)) { this.repositoryHasPosition.Delete(item.ProgramHasPositionId); } } //Update TblTrainingProgramHasPosition foreach (var item in uTrainingProgram.TblTrainingProgramHasPosition) { if (item.ProgramHasPositionId > 0) { this.repositoryHasPosition.Update(item, item.ProgramHasPositionId); } else { item.TrainingProgramId = id; this.repositoryHasPosition.Add(item); } } #endregion TblTrainingProgramHasPosition #region TblTrainingProgramHasGroup // filter TblTrainingProgramHasGroup Expression <Func <TblTrainingProgramHasGroup, bool> > condition5 = m => m.TrainingProgramId == id; var item5s = this.repositoryHasGroup.FindAllAsync(condition5).Result; //Remove TblTrainingProgramHasGroup if edit remove it foreach (var item in item5s) { if (!uTrainingProgram.TblTrainingProgramHasGroup.Any(x => x.GroupCode == item.GroupCode)) { this.repositoryHasGroup.Delete(item.ProgramHasGroupId); } } //Update TblTrainingProgramHasGroup foreach (var item in uTrainingProgram.TblTrainingProgramHasGroup) { if (item.ProgramHasGroupId > 0) { this.repositoryHasGroup.Update(item, item.ProgramHasGroupId); } else { item.TrainingProgramId = id; this.repositoryHasGroup.Add(item); } } #endregion TblTrainingProgramHasGroup #endregion Update and Remove for child } return(new JsonResult(hasData, this.DefaultJsonSettings)); }