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

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

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

            plan.Createdby = usrName;
            plan.CreatedAt = DateTime.Now;
            _context.FinancePlan.Add(plan);
            await _context.SaveChangesAsync();

            return(Created(plan));
        }
        public async Task <IActionResult> Put([FromODataUri] int key, [FromBody] FinancePlan 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))
            {
                throw new BadRequestException("Inputted Object IsValid failed");
            }

            update.UpdatedAt             = DateTime.Now;
            update.Updatedby             = usrName;
            _context.Entry(update).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException exp)
            {
                if (!_context.FinancePlan.Any(p => p.ID == key))
                {
                    throw new NotFoundException("Inputted ID not found");
                }
                else
                {
                    throw new DBOperationException(exp.Message);
                }
            }

            return(Updated(update));
        }
Ejemplo n.º 3
0
        public void TestModel(int hid, string curr, FinancePlanTypeEnum pty)
        {
            var context = this.fixture.GetCurrentDataContext();

            if (hid == DataSetupUtility.Home1ID)
            {
                fixture.InitHome1TestData(context);
            }
            if (hid == DataSetupUtility.Home2ID)
            {
                fixture.InitHome2TestData(context);
            }
            if (hid == DataSetupUtility.Home3ID)
            {
                fixture.InitHome3TestData(context);
            }
            if (hid == DataSetupUtility.Home4ID)
            {
                fixture.InitHome4TestData(context);
            }
            if (hid == DataSetupUtility.Home5ID)
            {
                fixture.InitHome5TestData(context);
            }

            var plan = new FinancePlan();

            Assert.False(plan.IsValid(context));
            plan.HomeID = hid;
            Assert.False(plan.IsValid(context));
            plan.TranCurr = curr;
            Assert.False(plan.IsValid(context));
            plan.Description = "test";
            Assert.False(plan.IsValid(context));
            plan.StartDate  = DateTime.Today;
            plan.TargetDate = DateTime.Today;
            Assert.False(plan.IsValid(context));
            plan.TargetDate = DateTime.Today.AddDays(100);
            Assert.False(plan.IsValid(context));
            plan.PlanType = pty;
            switch (pty)
            {
            case FinancePlanTypeEnum.Account:
                plan.AccountID = context.FinanceAccount.Where(p => p.HomeID == hid).FirstOrDefault().ID;
                Assert.True(plan.IsValid(context));
                break;

            case FinancePlanTypeEnum.AccountCategory:
                plan.AccountCategoryID = context.FinAccountCategories.FirstOrDefault().ID;
                Assert.True(plan.IsValid(context));
                break;

            case FinancePlanTypeEnum.ControlCenter:
                plan.ControlCenterID = context.FinanceControlCenter.Where(p => p.HomeID == hid).FirstOrDefault().ID;
                Assert.True(plan.IsValid(context));
                break;

            case FinancePlanTypeEnum.TranType:
                plan.TranTypeID = context.FinTransactionType.FirstOrDefault().ID;
                Assert.True(plan.IsValid(context));
                break;

            default:
                break;
            }
        }