Fix issues from golangci

This commit is contained in:
Jan Bader 2022-02-15 11:52:06 +00:00
parent 835a15ec08
commit 38dfa540b4
5 changed files with 33 additions and 70 deletions

View File

@ -24,15 +24,14 @@ type Handler struct {
StaticFS http.FileSystem StaticFS http.FileSystem
} }
const (
expiration = 72
)
// Serve starts the http server // Serve starts the http server
func (h *Handler) Serve() { func (h *Handler) Serve() {
router := gin.Default() router := gin.Default()
h.LoadRoutes(router) h.LoadRoutes(router)
router.Run(":1323") err := router.Run(":1323")
if err != nil {
panic(err)
}
} }
// LoadRoutes initializes all the routes // LoadRoutes initializes all the routes

View File

@ -8,6 +8,7 @@ import (
"git.javil.eu/jacob1123/budgeteer" "git.javil.eu/jacob1123/budgeteer"
"git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
) )
func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, error) { func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, error) {
@ -77,7 +78,7 @@ func (h *Handler) loginPost(c *gin.Context) {
c.AbortWithError(http.StatusUnauthorized, err) c.AbortWithError(http.StatusUnauthorized, err)
} }
go h.Service.UpdateLastLogin(context.Background(), user.ID) go h.UpdateLastLogin(user.ID)
budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), user.ID) budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), user.ID)
if err != nil { if err != nil {
@ -101,14 +102,18 @@ type registerInformation struct {
func (h *Handler) registerPost(c *gin.Context) { func (h *Handler) registerPost(c *gin.Context) {
var register registerInformation var register registerInformation
c.BindJSON(&register) err := c.BindJSON(&register)
if err != nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("parse body: %w", err))
return
}
if register.Email == "" || register.Password == "" || register.Name == "" { if register.Email == "" || register.Password == "" || register.Name == "" {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("e-mail, password and name are required")) c.AbortWithError(http.StatusBadRequest, fmt.Errorf("e-mail, password and name are required"))
return return
} }
_, err := h.Service.GetUserByUsername(c.Request.Context(), register.Email) _, err = h.Service.GetUserByUsername(c.Request.Context(), register.Email)
if err == nil { if err == nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("email is already taken")) c.AbortWithError(http.StatusBadRequest, fmt.Errorf("email is already taken"))
return return
@ -135,7 +140,7 @@ func (h *Handler) registerPost(c *gin.Context) {
c.AbortWithError(http.StatusUnauthorized, err) c.AbortWithError(http.StatusUnauthorized, err)
} }
go h.Service.UpdateLastLogin(context.Background(), user.ID) go h.UpdateLastLogin(user.ID)
budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), user.ID) budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), user.ID)
if err != nil { if err != nil {
@ -144,3 +149,10 @@ func (h *Handler) registerPost(c *gin.Context) {
c.JSON(http.StatusOK, LoginResponse{t, user, budgets}) c.JSON(http.StatusOK, LoginResponse{t, user, budgets})
} }
func (h *Handler) UpdateLastLogin(userID uuid.UUID) {
_, err := h.Service.UpdateLastLogin(context.Background(), userID)
if err != nil {
fmt.Printf("Error updating last login: %s", err)
}
}

View File

@ -31,14 +31,17 @@ func (h *Handler) newTransaction(c *gin.Context) {
var payload NewTransactionPayload var payload NewTransactionPayload
err := c.BindJSON(&payload) err := c.BindJSON(&payload)
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, err) c.AbortWithError(http.StatusBadRequest, err)
return return
} }
fmt.Printf("%v\n", payload) fmt.Printf("%v\n", payload)
amount := postgres.Numeric{} amount := postgres.Numeric{}
amount.Set(payload.Amount) err = amount.Set(payload.Amount)
if err != nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("amount: %w", err))
}
/*transactionUUID, err := getNullUUIDFromParam(c, "transactionid") /*transactionUUID, err := getNullUUIDFromParam(c, "transactionid")
if err != nil { if err != nil {
@ -60,8 +63,6 @@ func (h *Handler) newTransaction(c *gin.Context) {
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err)) c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
} }
return
// } // }
/* /*
_, delete := c.GetPostForm("delete") _, delete := c.GetPostForm("delete")

View File

@ -1,56 +0,0 @@
package http
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func getUUID(c *gin.Context, name string) (uuid.UUID, error) {
value, succ := c.GetPostForm(name)
if !succ {
return uuid.UUID{}, fmt.Errorf("not set")
}
id, err := uuid.Parse(value)
if err != nil {
return uuid.UUID{}, fmt.Errorf("not a valid uuid: %w", err)
}
return id, nil
}
func getNullUUIDFromParam(c *gin.Context, name string) (uuid.NullUUID, error) {
value := c.Param(name)
if value == "" {
return uuid.NullUUID{}, nil
}
id, err := uuid.Parse(value)
if err != nil {
return uuid.NullUUID{}, fmt.Errorf("not a valid uuid: %w", err)
}
return uuid.NullUUID{
UUID: id,
Valid: true,
}, nil
}
func getNullUUIDFromForm(c *gin.Context, name string) (uuid.NullUUID, error) {
value, succ := c.GetPostForm(name)
if !succ || value == "" {
return uuid.NullUUID{}, nil
}
id, err := uuid.Parse(value)
if err != nil {
return uuid.NullUUID{}, fmt.Errorf("not a valid uuid: %w", err)
}
return uuid.NullUUID{
UUID: id,
Valid: true,
}, nil
}

View File

@ -11,6 +11,10 @@ import (
// NewBudget creates a budget and adds it to the current user // NewBudget creates a budget and adds it to the current user
func (s *Database) NewBudget(context context.Context, name string, userID uuid.UUID) (*Budget, error) { func (s *Database) NewBudget(context context.Context, name string, userID uuid.UUID) (*Budget, error) {
tx, err := s.BeginTx(context, &sql.TxOptions{}) tx, err := s.BeginTx(context, &sql.TxOptions{})
if err != nil {
return nil, fmt.Errorf("begin transaction: %w", err)
}
q := s.WithTx(tx) q := s.WithTx(tx)
budget, err := q.CreateBudget(context, CreateBudgetParams{ budget, err := q.CreateBudget(context, CreateBudgetParams{
Name: name, Name: name,
@ -50,7 +54,10 @@ func (s *Database) NewBudget(context context.Context, name string, userID uuid.U
return nil, fmt.Errorf("set inflow category: %w", err) return nil, fmt.Errorf("set inflow category: %w", err)
} }
tx.Commit() err = tx.Commit()
if err != nil {
return nil, fmt.Errorf("commit: %w", err)
}
return &budget, nil return &budget, nil
} }