コード例 #1
0
        public async Task <IActionResult> UpdateItem(long id, cojMasterData item)
        {
            try
            {
                if (id != item.id)
                {
                    return(NoContent());
                }

                //update endDate
                // var _item = await _context.cojMasterDatas.FindAsync(id);
                // _item.endDate = DateTime.Now.ToString(_culture);
                // _context.Entry(_item).State = EntityState.Modified;
                // await _context.SaveChangesAsync();

                // var _items = await _context.cojMasterDatas.Where (a => a.idRef == item.idRef && a.endDate == "31/12/9999 00:00:00").ToListAsync ();

                // foreach (var _itm in _items) {
                //     var _item = await _context.cojMasterDatas.FindAsync (_itm.id);
                //     _item.endDate = DateTime.Now.ToString (_culture);
                //     _context.Entry (_item).State = EntityState.Modified;
                //     await _context.SaveChangesAsync ();
                // }

                //Add new
                cojMasterData _itemNew = new cojMasterData {
                    idRef          = item.idRef,
                    code           = item.code,
                    name           = item.name,
                    idDataCategory = item.idDataCategory,
                    idParent       = item.idParent,
                    updateDate     = DateTime.Now.ToString(_culture),
                    // createDate = DateTime.Now.ToString(_culture),
                    // startDate = DateTime.Now.ToString(_culture),
                    // endDate = "31/12/9999 00:00:00",
                    status          = item.status,
                    userUpdate      = item.userUpdate,
                    userAudit       = item.userAudit,
                    userAuditDate   = item.userAuditDate,
                    userApprove     = item.userApprove,
                    userApproveDate = item.userApproveDate,
                    remark          = item.remark,
                    formData        = item.formData,
                    docData         = item.docData
                };

                _context.cojMasterDatas.Add(_itemNew);
                await _context.SaveChangesAsync();

                return(Ok(_itemNew));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #2
0
        public async Task <ActionResult <cojMasterData> > CreateItem(cojMasterData newItem)
        {
            try
            {
                if (newItem.id != 0)
                {
                    return(NoContent());
                }

                newItem.startDate  = DateTime.Now.ToString(_culture);
                newItem.createDate = DateTime.Now.ToString(_culture);
                newItem.endDate    = "31/12/9999 00:00:00";

                _context.cojMasterDatas.Add(newItem);
                await _context.SaveChangesAsync();

                newItem.idRef = newItem.id;

                //initial new item
                var _item = await _context.cojMasterDatas.FindAsync(newItem.id);

                //ตรวจสอบรหัส ถ้าไม่ระบุให้กำหนดค่าจาก id
                if (newItem.code == null || newItem.code.Length == 0)
                {
                    _item.code = newItem.id.ToString();
                }
                else
                {
                    _item.code = newItem.code;
                }


                // _item.code = newItem.id.ToString();
                // _item.startDate = DateTime.Now.ToString(_culture);
                // _item.createDate = DateTime.Now.ToString(_culture);
                // _item.endDate = "31/12/9999 00:00:00";
                // _item.idRef = newItem.id;
                // _item.idDataCategory = newItem.idDataCategory;
                // _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));
            }
        }