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

            // Check
            if (!ctgy.IsValid(this._context) || !ctgy.HomeID.HasValue)
            {
                throw new BadRequestException("Inputted category 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 == ctgy.HomeID.Value && p.User == usrName).Count();

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

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

            ctgy.Createdby = usrName;
            ctgy.CreatedAt = DateTime.Now;
            _context.FinAssetCategories.Add(ctgy);
            await _context.SaveChangesAsync();

            return(Created(ctgy));
        }
        public async Task TestCase1(int hid, string user)
        {
            var context   = this.fixture.GetCurrentDataContext();
            var ctgyCount = DataSetupUtility.FinanceAssetCategories.Count();

            FinanceAssetCategoriesController control = new FinanceAssetCategoriesController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(user);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = userclaim
                }
            };

            // 1. Read all categories
            var items   = control.Get();
            var itemcnt = items.Count();

            Assert.Equal(ctgyCount, itemcnt);

            // 2. Insert new category
            var ctgy = new FinanceAssetCategory()
            {
                HomeID = hid,
                Name   = "Test 1_UT_" + hid.ToString(),
                Desp   = "Test 1"
            };
            var rst1 = await control.Post(ctgy);

            Assert.NotNull(rst1);
            var rst2 = Assert.IsType <CreatedODataResult <FinanceAssetCategory> >(rst1);

            Assert.Equal(ctgy.Name, rst2.Entity.Name);
            var firstctg = rst2.Entity.ID;

            Assert.True(firstctg > 0);
            Assert.Equal(ctgy.Desp, rst2.Entity.Desp);

            // 3. Read all categories, again
            items   = control.Get();
            itemcnt = items.Count();
            Assert.Equal(ctgyCount + 1, itemcnt);

            // 4. Change it
            ctgy.Name = "Test 2";
            rst1      = await control.Put(firstctg, ctgy);

            var rst3 = Assert.IsType <UpdatedODataResult <FinanceAssetCategory> >(rst1);

            Assert.Equal(ctgy.Name, rst3.Entity.Name);

            // 5. Delete it
            var rst4 = await control.Delete(firstctg);

            Assert.NotNull(rst4);
            var rst6 = Assert.IsType <StatusCodeResult>(rst4);

            Assert.Equal(204, rst6.StatusCode);

            // 6. Read all categories again
            items   = control.Get();
            itemcnt = items.Count();
            Assert.Equal(ctgyCount, itemcnt);

            await context.DisposeAsync();
        }
        public async Task <IActionResult> Put([FromODataUri] int key, [FromBody] FinanceAssetCategory update)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            if (key != update.ID)
            {
                throw new BadRequestException("Key is mismatch");
            }

            // 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.UpdatedAt             = DateTime.Now;
            update.Updatedby             = usrName;
            _context.Entry(update).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException exp)
            {
                if (!_context.FinAssetCategories.Any(p => p.ID == key))
                {
                    return(NotFound());
                }
                else
                {
                    throw new DBOperationException(exp.Message);
                }
            }

            return(Updated(update));
        }