public async Task <ActionResult <cojReserve> > CreateItem(cojReserve newItem) { try { //check duplicate item id, code, name if (newItem.id != 0) { return(NoContent()); } // newItem.startDate = DateTime.Now.ToString(_culture); newItem.endDate = "31/12/9999 00:00:00"; _context.cojReserves.Add(newItem); await _context.SaveChangesAsync(); newItem.idRef = newItem.id; //initial new item // var _item = await _context.cojReserves.FindAsync (newItem.id); // _item.startDate = DateTime.Now.ToString (_culture); // _item.endDate = "31/12/9999 00:00:00"; // _item.idRef = newItem.id; // _context.Entry (_item).State = EntityState.Modified; // await _context.SaveChangesAsync (); return(CreatedAtAction(nameof(GetItem), new { id = newItem.id }, newItem)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> UpdateItem(long id, cojReserve item) { try { if (id != item.id) { return(NoContent()); } //update endDate // var _item = await _context.cojReserves.FindAsync (id); // _item.endDate = DateTime.Now.ToString (_culture); // _context.Entry (_item).State = EntityState.Modified; // await _context.SaveChangesAsync (); // var _items = await _context.cojReserves.Where (a => a.idRef == item.idRef && a.endDate == "31/12/9999 00:00:00").ToListAsync (); // foreach (var _itm in _items) { // var _item = await _context.cojReserves.FindAsync (_itm.id); // _item.endDate = DateTime.Now.ToString (_culture); // _context.Entry (_item).State = EntityState.Modified; // await _context.SaveChangesAsync (); // } //Add new cojReserve _itemNew = new cojReserve { idRef = item.idRef, itemSort = item.itemSort, cojAgencyId = item.cojAgencyId, cojPeriod = item.cojPeriod, cojFund = item.cojFund, cojBGPlanId = item.cojBGPlanId, cojWorkplanTypeId = item.cojWorkplanTypeId, cojBGWorkplanId = item.cojBGWorkplanId, cojWorkActivityId = item.cojWorkActivityId, cojBGWorkplanName = item.cojBGWorkplanName, cojStgPlanId = item.cojStgPlanId, cojStgId = item.cojStgId, cojReserveNo = item.cojReserveNo, cojReserveDate = item.cojReserveDate, cojReserveDocNo = item.cojReserveDocNo, cojReserveSumA = item.cojReserveSumA, cojReserveSumB = item.cojReserveSumB, cojReserveSumC = item.cojReserveSumC, cojReserveSumAMT = item.cojReserveSumAMT, cojReserveFeeB = item.cojReserveFeeB, cojReserveFeeC = item.cojReserveFeeC, cojReserveFeeAMT = item.cojReserveFeeAMT, cojReserveTransferDocNo = item.cojReserveTransferDocNo, responsibilityAgency = item.responsibilityAgency, procumentAgency = item.procumentAgency, disbursementAgency = item.disbursementAgency, cojReserveType = item.cojReserveType, cojReserveStatus = item.cojReserveStatus, cojBGPlanAllotId = item.cojBGPlanAllotId, cojBGTransferRequestId = item.cojBGTransferRequestId, cojBGTransferId = item.cojBGTransferId, cojWorkActivityItemId = item.cojWorkActivityItemId, cojRequestItemDetail = item.cojRequestItemDetail, remark = item.remark, cojCarryOverItemId = item.cojCarryOverItemId // startDate = DateTime.Now.ToString (_culture), // endDate = "31/12/9999 00:00:00" }; _context.cojReserves.Add(_itemNew); await _context.SaveChangesAsync(); return(Ok(_itemNew)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }