Improve handling of context
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
@@ -23,7 +22,7 @@ func (h *Handler) getImportantData(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
budget, err := h.Service.DB.GetBudget(context.Background(), budgetUUID)
|
||||
budget, err := h.Service.DB.GetBudget(c.Request.Context(), budgetUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"git.javil.eu/jacob1123/budgeteer"
|
||||
@@ -23,7 +22,7 @@ func (h *Handler) budget(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
transactions, err := h.Service.DB.GetTransactionsForBudget(context.Background(), budgetUUID)
|
||||
transactions, err := h.Service.DB.GetTransactionsForBudget(c.Request.Context(), budgetUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
@@ -45,7 +44,7 @@ func (h *Handler) newBudget(c *gin.Context) {
|
||||
}
|
||||
|
||||
userID := c.MustGet("token").(budgeteer.Token).GetID()
|
||||
_, err := h.Service.NewBudget(budgetName, userID)
|
||||
_, err := h.Service.NewBudget(c.Request.Context(), budgetName, userID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -59,7 +58,7 @@ func (h *Handler) budgeting(c *gin.Context) {
|
||||
FromDate: firstOfMonth,
|
||||
ToDate: firstOfNextMonth,
|
||||
}
|
||||
categories, err := h.Service.DB.GetCategoriesWithBalance(context.Background(), params)
|
||||
categories, err := h.Service.DB.GetCategoriesWithBalance(c.Request.Context(), params)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
|
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
func (h *Handler) dashboard(c *gin.Context) {
|
||||
userID := c.MustGet("token").(budgeteer.Token).GetID()
|
||||
budgets, err := h.Service.BudgetsForUser(userID)
|
||||
budgets, err := h.Service.DB.GetBudgetsForUser(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@@ -68,7 +68,7 @@ func (h *Handler) loginPost(c *gin.Context) {
|
||||
username, _ := c.GetPostForm("username")
|
||||
password, _ := c.GetPostForm("password")
|
||||
|
||||
user, err := h.Service.DB.GetUserByUsername(context.Background(), username)
|
||||
user, err := h.Service.DB.GetUserByUsername(c.Request.Context(), username)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusUnauthorized, err)
|
||||
return
|
||||
@@ -84,7 +84,8 @@ func (h *Handler) loginPost(c *gin.Context) {
|
||||
c.AbortWithError(http.StatusUnauthorized, err)
|
||||
}
|
||||
|
||||
_, _ = h.Service.DB.UpdateLastLogin(context.Background(), user.ID)
|
||||
go h.Service.DB.UpdateLastLogin(context.Background(), user.ID)
|
||||
|
||||
maxAge := (int)((expiration * time.Hour).Seconds())
|
||||
c.SetCookie(authCookie, t, maxAge, "", "", false, true)
|
||||
c.JSON(http.StatusOK, map[string]string{
|
||||
@@ -97,7 +98,7 @@ func (h *Handler) registerPost(c *gin.Context) {
|
||||
password, _ := c.GetPostForm("password")
|
||||
name, _ := c.GetPostForm("name")
|
||||
|
||||
_, err := h.Service.DB.GetUserByUsername(context.Background(), email)
|
||||
_, err := h.Service.DB.GetUserByUsername(c.Request.Context(), email)
|
||||
if err == nil {
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
return
|
||||
@@ -114,7 +115,7 @@ func (h *Handler) registerPost(c *gin.Context) {
|
||||
Password: hash,
|
||||
Email: email,
|
||||
}
|
||||
_, err = h.Service.DB.CreateUser(context.Background(), createUser)
|
||||
_, err = h.Service.DB.CreateUser(c.Request.Context(), createUser)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
@@ -22,7 +22,7 @@ func (h *Handler) importYNAB(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ynab, err := postgres.NewYNABImport(h.Service.DB, budgetUUID)
|
||||
ynab, err := postgres.NewYNABImport(c.Request.Context(), h.Service.DB, budgetUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
|
Reference in New Issue
Block a user