public async Task <IActionResult> Post([FromBody] FinanceOrder order)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            // Check
            if (!order.IsValid(this._context))
            {
                throw new BadRequestException("Inputted Object IsValid failed");
            }

            // User
            String usrName = String.Empty;

            try
            {
                usrName = HIHAPIUtility.GetUserID(this);
                if (String.IsNullOrEmpty(usrName))
                {
                    throw new UnauthorizedAccessException();
                }
            }
            catch
            {
                throw new UnauthorizedAccessException();
            }

            // Check whether User assigned with specified Home ID
            var hms = _context.HomeMembers.Where(p => p.HomeID == order.HomeID && p.User == usrName).Count();

            if (hms <= 0)
            {
                throw new UnauthorizedAccessException();
            }

            order.CreatedAt = DateTime.Now;
            order.Createdby = usrName;
            _context.FinanceOrder.Add(order);
            await _context.SaveChangesAsync();

            return(Created(order));
        }
        public async Task <IActionResult> Put([FromODataUri] int key, [FromBody] FinanceOrder update)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            if (key != update.ID)
            {
                throw new BadRequestException("Inputted ID mismatched");
            }

            // User
            String usrName = String.Empty;

            try
            {
                usrName = HIHAPIUtility.GetUserID(this);
                if (String.IsNullOrEmpty(usrName))
                {
                    throw new UnauthorizedAccessException();
                }
            }
            catch
            {
                throw new UnauthorizedAccessException();
            }

            // Check whether User assigned with specified Home ID
            var hms = _context.HomeMembers.Where(p => p.HomeID == update.HomeID && p.User == usrName).Count();

            if (hms <= 0)
            {
                throw new UnauthorizedAccessException();
            }

            if (!update.IsValid(this._context))
            {
                return(BadRequest());
            }

            update.Updatedby             = usrName;
            update.UpdatedAt             = DateTime.Now;
            _context.Entry(update).State = EntityState.Modified;

            // SRules.
            var rulesInDB = _context.FinanceOrderSRule.Where(p => p.OrderID == update.ID).ToList();

            foreach (var rule in update.SRule)
            {
                var itemindb = rulesInDB.Find(p => p.OrderID == update.ID && p.RuleID == rule.RuleID);
                if (itemindb == null)
                {
                    _context.FinanceOrderSRule.Add(rule);
                }
                else
                {
                    // Update
                    _context.Entry(itemindb).State = EntityState.Modified;
                }
            }
            foreach (var rule in rulesInDB)
            {
                var nitem = update.SRule.FirstOrDefault(p => p.OrderID == update.ID && p.RuleID == rule.RuleID);
                if (nitem == null)
                {
                    _context.FinanceOrderSRule.Remove(rule);
                }
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException exp)
            {
                if (!_context.FinanceOrder.Any(p => p.ID == key))
                {
                    return(NotFound());
                }
                else
                {
                    throw new DBOperationException(exp.Message);
                }
            }

            return(Updated(update));
        }