public async Task <IActionResult> PutByNumber(int key, [FromBody] InitialRequirePaintingList uInitialRequirePainting) { var Message = "Require painting not been found."; if (uInitialRequirePainting != null) { // set modified uInitialRequirePainting.ModifyDate = DateTime.Now; uInitialRequirePainting.Modifyer = uInitialRequirePainting.Modifyer ?? "Someone"; if (uInitialRequirePainting.RequirePaintingMaster != null) { uInitialRequirePainting.RequirePaintingMaster = null; } // Remove null uInitialRequirePainting.BlastWorkItems.Remove(null); uInitialRequirePainting.PaintWorkItems.Remove(null); if (uInitialRequirePainting.BlastWorkItems != null) { foreach (var uBlastWork in uInitialRequirePainting.BlastWorkItems) { if (uBlastWork == null) { continue; } if (uBlastWork.BlastWorkItemId > 0) { uBlastWork.ModifyDate = uInitialRequirePainting.ModifyDate; uBlastWork.Modifyer = uInitialRequirePainting.Modifyer; } else { uBlastWork.CreateDate = uInitialRequirePainting.ModifyDate; uBlastWork.Creator = uInitialRequirePainting.Modifyer; } uBlastWork.StandradTimeExt = null; uBlastWork.StandradTimeInt = null; uBlastWork.SurfaceTypeExt = null; uBlastWork.SurfaceTypeInt = null; } } if (uInitialRequirePainting.PaintWorkItems != null) { foreach (var uPaintWork in uInitialRequirePainting.PaintWorkItems) { if (uPaintWork == null) { continue; } if (uPaintWork.PaintWorkItemId > 0) { uPaintWork.ModifyDate = uInitialRequirePainting.ModifyDate; uPaintWork.Modifyer = uInitialRequirePainting.Modifyer; } else { uPaintWork.CreateDate = uInitialRequirePainting.ModifyDate; uPaintWork.Creator = uInitialRequirePainting.Modifyer; } uPaintWork.StandradTimeExt = null; uPaintWork.StandradTimeInt = null; uPaintWork.ExtColorItem = null; uPaintWork.IntColorItem = null; } } // update Master not update Detail it need to update Detail directly var updateComplate = await this.repository.UpdateAsync(uInitialRequirePainting, uInitialRequirePainting.InitialRequireId); if (updateComplate != null) { // filter Expression <Func <BlastWorkItem, bool> > condition = m => m.InitialRequireId == updateComplate.InitialRequireId; var dbBlastWorks = this.repositoryBlast.FindAll(condition); Expression <Func <PaintWorkItem, bool> > condition2 = m => m.InitialRequireId == updateComplate.InitialRequireId; var dbPaintWorks = this.repositoryPaint.FindAll(condition2); //Remove BlastWork if edit remove it foreach (var dbBlastWork in dbBlastWorks) { if (!uInitialRequirePainting.BlastWorkItems.Any(x => x.BlastWorkItemId == dbBlastWork.BlastWorkItemId)) { await this.repositoryBlast.DeleteAsync(dbBlastWork.BlastWorkItemId); } } //Remove PaintWork if edit remove it foreach (var dbPaintWork in dbPaintWorks) { if (!uInitialRequirePainting.PaintWorkItems.Any(x => x.PaintWorkItemId == dbPaintWork.PaintWorkItemId)) { await this.repositoryPaint.DeleteAsync(dbPaintWork.PaintWorkItemId); } } //Update BlastWorkItem or New BlastWorkItem foreach (var uBlastWork in uInitialRequirePainting.BlastWorkItems) { if (uBlastWork == null) { continue; } if (uBlastWork.BlastWorkItemId > 0) { await this.repositoryBlast.UpdateAsync(uBlastWork, uBlastWork.BlastWorkItemId); } else { if (uBlastWork.InitialRequireId is null || uBlastWork.InitialRequireId < 1) { uBlastWork.InitialRequireId = uInitialRequirePainting.InitialRequireId; } await this.repositoryBlast.AddAsync(uBlastWork); } } //Update PaintWorkItem or New PaintWorkItem foreach (var uPaintWork in uInitialRequirePainting.PaintWorkItems) { if (uPaintWork == null) { continue; } if (uPaintWork.PaintWorkItemId > 0) { await this.repositoryPaint.UpdateAsync(uPaintWork, uPaintWork.PaintWorkItemId); } else { if (uPaintWork.InitialRequireId is null || uPaintWork.InitialRequireId < 1) { uPaintWork.InitialRequireId = uInitialRequirePainting.InitialRequireId; } await this.repositoryPaint.AddAsync(uPaintWork); } } } return(new JsonResult(await this.repository.UpdateAsync(uInitialRequirePainting, key), this.DefaultJsonSettings)); } return(NotFound(new { Error = Message })); }
public async Task <IActionResult> Post([FromBody] InitialRequirePaintingList nInitialRequirePainting) { var message = "Initial request painting list."; try { if (nInitialRequirePainting != null) { nInitialRequirePainting.CreateDate = DateTime.Now; nInitialRequirePainting.Creator = nInitialRequirePainting.Creator ?? "Someone"; if (nInitialRequirePainting.RequirePaintingMaster != null) { nInitialRequirePainting.RequirePaintingMaster = null; } // Remove null nInitialRequirePainting.BlastWorkItems.Remove(null); nInitialRequirePainting.PaintWorkItems.Remove(null); if (nInitialRequirePainting.BlastWorkItems != null) { // Add BlastWork item foreach (var nBlastWork in nInitialRequirePainting.BlastWorkItems) { if (nBlastWork == null) { continue; } nBlastWork.CreateDate = nInitialRequirePainting.CreateDate; nBlastWork.Creator = nInitialRequirePainting.Creator; // Clear StandradTimeExt if (nBlastWork.StandradTimeExt != null) { nBlastWork.StandradTimeExt = null; } // Clear StandradTimeInt if (nBlastWork.StandradTimeInt != null) { nBlastWork.StandradTimeInt = null; } // Clear SurfaceTypeExt if (nBlastWork.SurfaceTypeExt != null) { nBlastWork.SurfaceTypeExt = null; } // Clear SurfaceTypeInt if (nBlastWork.SurfaceTypeInt != null) { nBlastWork.SurfaceTypeInt = null; } } } if (nInitialRequirePainting.PaintWorkItems != null) { foreach (var nPaintWork in nInitialRequirePainting.PaintWorkItems) { if (nPaintWork == null) { continue; } nPaintWork.CreateDate = nInitialRequirePainting.CreateDate; nPaintWork.Creator = nInitialRequirePainting.Creator; // Clear StandradTimeExt if (nPaintWork.StandradTimeExt != null) { nPaintWork.StandradTimeExt = null; } // Clear StandradTimeInt if (nPaintWork.StandradTimeInt != null) { nPaintWork.StandradTimeInt = null; } // Clear SurfaceTypeExt if (nPaintWork.ExtColorItem != null) { nPaintWork.ExtColorItem = null; } // Clear SurfaceTypeInt if (nPaintWork.IntColorItem != null) { nPaintWork.IntColorItem = null; } } } return(new JsonResult(await this.repository.AddAsync(nInitialRequirePainting), this.DefaultJsonSettings)); } } catch (Exception ex) { message = $"Has error {ex.ToString()}"; } return(NotFound(new { Error = "Not found BlastWork data !!!" })); }