Example #1
0
        public IActionResult Load()
        {
            JsonResult       jsonResult;
            BudgetInputTypes inputSectionType;
            string           loadType   = this.Request.Query["type"];
            string           authHeader = this.Request.Headers["Authorization"];

            if (authHeader == null)
            {
                //User not logged in. Load defaults.
                if (loadType == "Taxes")
                {
                    TaxInfo taxInfo = TaxInfo.GetDefaults();
                    jsonResult = new JsonResult(taxInfo);
                    return(Ok(jsonResult));
                }
                else if (Enum.TryParse <BudgetInputTypes>(loadType, out inputSectionType))
                {
                    List <BudgetInputRow> budgetInputRows = BudgetInputRow.GetDefaults(inputSectionType);
                    jsonResult = new JsonResult(budgetInputRows);
                    return(Ok(jsonResult));
                }
            }
            else
            {
                //User is logged in. Load from database.
                string username = getUsername(authHeader);

                if (loadType == "Taxes")
                {
                    TaxInfo taxInfo = _budgetRepository.LoadTaxInfo(username);
                    jsonResult = new JsonResult(taxInfo);
                    return(Ok(jsonResult));
                }
                else if (Enum.TryParse <BudgetInputTypes>(loadType, out inputSectionType))
                {
                    BudgetInputRow[] budgetInputRows = _budgetRepository.LoadBudgetInputs(username, inputSectionType);
                    jsonResult = new JsonResult(budgetInputRows);
                    return(Ok(jsonResult));
                }
            }

            return(Unauthorized());
        }