public async Task <IActionResult> CreateV2([FromBody] BillofMaterialViewModel recordViewModel) { if (recordViewModel == null) { return(BadRequest(new { Error = "Data not been found." })); } var record = this.mapper.Map <BillofMaterialViewModel, BillofMaterial>(recordViewModel); // +7 Hour record = this.helper.AddHourMethod(record); record.CreateDate = DateTime.Now; if (await this.repository.AddAsync(record) == null) { return(BadRequest()); } if (recordViewModel.BomLowLevel != null) { foreach (var item in recordViewModel.BomLowLevel) { if (item == null) { continue; } item.BomParentId = record.BillofMaterialId; item.CreateDate = record.CreateDate; item.Creator = record.Creator; await this.repository.AddAsync(item); } } return(new JsonResult(record, this.DefaultJsonSettings)); }
public async Task <IActionResult> UpdateV2(int key, [FromBody] BillofMaterialViewModel recordViewModel) { if (recordViewModel == null) { return(BadRequest(new { Error = "Data not been found." })); } var record = this.mapper.Map <BillofMaterialViewModel, BillofMaterial>(recordViewModel); // +7 Hour record = this.helper.AddHourMethod(record); // Set date for CrateDate Entity if (record.GetType().GetProperty("ModifyDate") != null) { record.GetType().GetProperty("ModifyDate").SetValue(record, DateTime.Now); } if (await this.repository.UpdateAsync(record, key) == null) { return(BadRequest()); } var dbBomLowLevel = await this.repository.GetToListAsync(x => x, b => b.BomParentId == key); if (recordViewModel.BomLowLevel != null) { // Remove bom low level if remove foreach (var item in dbBomLowLevel) { if (!recordViewModel.BomLowLevel.Any(x => x.BillofMaterialId == item.BillofMaterialId)) { await this.repository.DeleteAsync(item.BillofMaterialId); } } foreach (var item in recordViewModel.BomLowLevel) { if (item.BillofMaterialId > 0) { item.ModifyDate = record.ModifyDate; item.Modifyer = record.Modifyer; item.BomParentId = record.BillofMaterialId; await this.repository.UpdateAsync(item, item.BillofMaterialId); } else { item.CreateDate = record.ModifyDate; item.Creator = record.Modifyer; item.BomParentId = record.BillofMaterialId; await this.repository.AddAsync(item); } } } else //Check if update and remove all bow low level { if (dbBomLowLevel.Any()) { foreach (var item in dbBomLowLevel) { await this.repository.DeleteAsync(item.BillofMaterialId); } } } return(new JsonResult(record, this.DefaultJsonSettings)); }