Implement YNAB Import in Vue
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
@@ -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")
|
||||
}
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user