diff --git a/http/always-needed-data.go b/http/always-needed-data.go index bc2e95e..ae2c23a 100644 --- a/http/always-needed-data.go +++ b/http/always-needed-data.go @@ -1,44 +1,10 @@ package http import ( - "fmt" - "net/http" - "git.javil.eu/jacob1123/budgeteer/postgres" - "github.com/gin-gonic/gin" - "github.com/google/uuid" ) type AlwaysNeededData struct { Budget postgres.Budget Accounts []postgres.GetAccountsWithBalanceRow } - -func (h *Handler) getImportantData(c *gin.Context) { - budgetID := c.Param("budgetid") - budgetUUID, err := uuid.Parse(budgetID) - if err != nil { - c.AbortWithError(http.StatusBadRequest, fmt.Errorf("budgetid missing from URL")) - return - } - - budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID) - if err != nil { - c.AbortWithError(http.StatusNotFound, err) - return - } - - accounts, err := h.Service.GetAccountsWithBalance(c.Request.Context(), budgetUUID) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - base := AlwaysNeededData{ - Accounts: accounts, - Budget: budget, - } - - c.Set("data", base) - c.Next() -} diff --git a/http/budgeting.go b/http/budgeting.go index a45742d..2cd6018 100644 --- a/http/budgeting.go +++ b/http/budgeting.go @@ -8,6 +8,7 @@ import ( "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" + "github.com/google/uuid" ) type BudgetingData struct { @@ -60,8 +61,29 @@ func getDate(c *gin.Context) (time.Time, error) { } func (h *Handler) budgeting(c *gin.Context) { - alwaysNeededData := c.MustGet("data").(AlwaysNeededData) - budgetUUID := alwaysNeededData.Budget.ID + budgetID := c.Param("budgetid") + budgetUUID, err := uuid.Parse(budgetID) + if err != nil { + c.AbortWithError(http.StatusBadRequest, fmt.Errorf("budgetid missing from URL")) + return + } + + budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + + accounts, err := h.Service.GetAccountsWithBalance(c.Request.Context(), budgetUUID) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + data := AlwaysNeededData{ + Accounts: accounts, + Budget: budget, + } firstOfMonth, err := getDate(c) if err != nil { @@ -72,7 +94,7 @@ func (h *Handler) budgeting(c *gin.Context) { firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0) d := BudgetingData{ - AlwaysNeededData: alwaysNeededData, + AlwaysNeededData: data, Date: firstOfMonth, Next: firstOfNextMonth, Previous: firstOfPreviousMonth, @@ -87,13 +109,12 @@ func (h *Handler) budgeting(c *gin.Context) { } // skip everything in the future - categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, alwaysNeededData.Budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) + categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, data.Budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) if err != nil { return } d.Categories = categoriesWithBalance - data := c.MustGet("data").(AlwaysNeededData) var availableBalance float64 = 0 for _, cat := range categories { if cat.ID != data.Budget.IncomeCategoryID { diff --git a/http/http.go b/http/http.go index b8d63de..4ec3ee2 100644 --- a/http/http.go +++ b/http/http.go @@ -48,7 +48,6 @@ func (h *Handler) Serve() { withBudget := router.Group("") withBudget.Use(h.verifyLoginWithForbidden) - withBudget.Use(h.getImportantData) withBudget.GET("/budget/:budgetid/:year/:month", h.budgeting) withBudget.GET("/budget/:budgetid/all-accounts", h.allAccounts) withBudget.GET("/budget/:budgetid/settings/clear", h.clearBudget) @@ -69,8 +68,8 @@ func (h *Handler) Serve() { authenticated.GET("/admin/clear-database", h.clearDatabase) withBudget2 := authenticated.Group("") - withBudget2.Use(h.getImportantData) withBudget2.GET("/budget/:budgetid", h.budgeting) + withBudget2.POST("/budget/:budgetid/import/ynab", h.importYNAB) budget := authenticated.Group("/budget") budget.POST("/new", h.newBudget) @@ -78,7 +77,6 @@ func (h *Handler) Serve() { transaction := authenticated.Group("/transaction") transaction.POST("/new", h.newTransaction) transaction.POST("/:transactionid", h.newTransaction) - transaction.POST("/import/ynab", h.importYNAB) router.Run(":1323") } diff --git a/http/ynab-import.go b/http/ynab-import.go index 33c6e93..948b442 100644 --- a/http/ynab-import.go +++ b/http/ynab-import.go @@ -10,7 +10,7 @@ import ( ) func (h *Handler) importYNAB(c *gin.Context) { - budgetID, succ := c.GetPostForm("budget_id") + budgetID, succ := c.Params.Get("budgetid") if !succ { c.AbortWithError(http.StatusBadRequest, fmt.Errorf("no budget_id specified")) return diff --git a/web/src/pages/Settings.vue b/web/src/pages/Settings.vue index b27735f..42622ce 100644 --- a/web/src/pages/Settings.vue +++ b/web/src/pages/Settings.vue @@ -2,15 +2,44 @@ import { TITLE } from "../store/mutation-types" export default { + data() { + return { + transactionsFile: null, + assignmentsFile: null + } + }, + computed: { + hasFiles() { + return this.$data.transactionsFile != null && this.$data.assignmentsFile != null; + } + }, mounted() { this.$store.commit(TITLE, "Settings") }, methods: { + gotAssignments(e){ + this.$data.assignmentsFile = e.target.files[0]; + }, + gotTransactions(e) { + this.$data.transactionsFile = e.target.files[0]; + }, clearBudget() { // Clear budget }, cleanNegative() { // Fix all historic negative category-balances + }, + ynabImport() { + // + let formData = new FormData(); + formData.append("transactions", this.$data.transactionsFile); + formData.append("assignments", this.$data.assignmentsFile); + fetch('/api/v1/budget/'+this.$store.getters.CurrentBudget.ID+"/import/ynab", { + method: "POST", + headers: { + 'Authorization': 'Bearer ' + this.$store.state.Session.Token + }, + body: formData}); } } } @@ -27,18 +56,15 @@ export default {
This restores YNABs functionality, that would substract any overspent categories' balances from next months inflows.