We got some auth going
This commit is contained in:
@@ -33,3 +33,6 @@ test:
|
|||||||
|
|
||||||
build_db_image:
|
build_db_image:
|
||||||
make -C ./pgImage build
|
make -C ./pgImage build
|
||||||
|
|
||||||
|
my-test:
|
||||||
|
@echo hello
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
const (
|
||||||
|
APIPath = "/api/v1/"
|
||||||
|
)
|
||||||
|
|
||||||
|
func APILoginHandler(c *gin.Context) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
exp "expenses/expenses"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getAllCategories(c *gin.Context) {
|
||||||
|
// TODO
|
||||||
|
// What user is logged in?
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// TODO - only get the categories of the user
|
||||||
|
// create roles tho, so an admin or someone can get way more categories
|
||||||
|
categories, err := exp.App.GetAllCategories(ctx)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"categories": categories})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RouteCategories(router *gin.RouterGroup) {
|
||||||
|
router.GET("categories", getAllCategories)
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
exp "expenses/expenses"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getAllExpenses(c *gin.Context) {
|
||||||
|
ctx := context.Background()
|
||||||
|
// TODO - only get the expenses of the user
|
||||||
|
// create roles tho, so an admin or someone can get way more expenses
|
||||||
|
expenses, err := exp.App.GetAllExpenses(ctx)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"expenses": expenses})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RouteExpenses(router *gin.RouterGroup) {
|
||||||
|
router.GET("expenses", getAllExpenses)
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
exp "expenses/expenses"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getAllStores(c *gin.Context) {
|
||||||
|
// TODO - we need to set the current user from the JWT token we got
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// TODO - only get the stores of the user
|
||||||
|
// create roles tho, so an admin or someone can get way more stores
|
||||||
|
stores, err := exp.App.GetAllStores(ctx)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"users": stores})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RouteStores(router *gin.RouterGroup) {
|
||||||
|
router.GET("stores", getAllStores)
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
exp "expenses/expenses"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getAllTypes(c *gin.Context) {
|
||||||
|
// TODO - we need to set the current user from the JWT token we got
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// TODO - only get the types of the user
|
||||||
|
// create roles tho, so an admin or someone can get way more types
|
||||||
|
types, err := exp.App.GetAllTypes(ctx)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"users": types})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RouteTypes(router *gin.RouterGroup) {
|
||||||
|
router.GET("types", getAllTypes)
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
exp "expenses/expenses"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getAllUsers(c *gin.Context) {
|
||||||
|
// TODO - stuff
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// TODO - only get the users of the user
|
||||||
|
// create roles tho, so an admin or someone can get way more users
|
||||||
|
users, err := exp.App.GetAllUsers(ctx)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"users": users})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RouteUsers(router *gin.RouterGroup) {
|
||||||
|
router.GET("users", getAllUsers)
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
expauth "expenses/expenses/auth"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TokenData struct {
|
||||||
|
Token string `json:"JWTToken"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func APIAuthMiddleWare() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
auth := c.GetHeader("Authorization")
|
||||||
|
if !strings.HasPrefix(auth, "Bearer ") {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "missing token"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenStr := strings.TrimPrefix(auth, "Bearer ")
|
||||||
|
validateToken, err := expauth.ValidateToken(tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !validateToken {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "token is not valid"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
exp "expenses/expenses"
|
||||||
|
"expenses/expenses/auth"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LoginData struct {
|
||||||
|
Name string `json:"username"`
|
||||||
|
Pass string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func login(c *gin.Context) {
|
||||||
|
var loginData LoginData
|
||||||
|
err := c.ShouldBindJSON(&loginData)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request - " + err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("user:", loginData.Name)
|
||||||
|
log.Println("pass:", loginData.Pass)
|
||||||
|
|
||||||
|
user, err := exp.App.ValidateCredentials(loginData.Name, loginData.Pass)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error validating user: ", err)
|
||||||
|
// TODO - go to an error page or login failed or something
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
token, err := auth.GenerateToken(user.UserName)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error generating token: ", err)
|
||||||
|
// TODO - get error return
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"token": token})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RouteLogin(eng *gin.Engine) {
|
||||||
|
eng.POST("login", login)
|
||||||
|
}
|
||||||
@@ -135,7 +135,7 @@
|
|||||||
|| evt.detail.xhr.responseType === "text") {
|
|| evt.detail.xhr.responseType === "text") {
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(evt.detail.xhr.responseText);
|
const data = JSON.parse(evt.detail.xhr.responseText);
|
||||||
console.log(data);
|
console.log("Data: " + data);
|
||||||
|
|
||||||
document.getElementById("expense-date").value = data["date"];
|
document.getElementById("expense-date").value = data["date"];
|
||||||
document.getElementById("expense-value").value = data["total"];
|
document.getElementById("expense-value").value = data["total"];
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"expenses/api"
|
||||||
"expenses/config"
|
"expenses/config"
|
||||||
"expenses/expenses/auth"
|
"expenses/expenses/auth"
|
||||||
"expenses/pages"
|
"expenses/pages"
|
||||||
@@ -67,6 +68,14 @@ func setRoutes(router *gin.RouterGroup, eng *gin.Engine) {
|
|||||||
pages.RouteServerError(eng)
|
pages.RouteServerError(eng)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setAPI(router *gin.RouterGroup) {
|
||||||
|
api.RouteExpenses(router)
|
||||||
|
api.RouteUsers(router)
|
||||||
|
api.RouteStores(router)
|
||||||
|
api.RouteTypes(router)
|
||||||
|
api.RouteCategories(router)
|
||||||
|
}
|
||||||
|
|
||||||
func startWebserver() {
|
func startWebserver() {
|
||||||
cfg := config.GetInstance()
|
cfg := config.GetInstance()
|
||||||
|
|
||||||
@@ -83,6 +92,14 @@ func startWebserver() {
|
|||||||
setRoutes(authGroup, router)
|
setRoutes(authGroup, router)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO - this also needs authentication
|
||||||
|
apiGroup := router.Group(api.APIPath)
|
||||||
|
{
|
||||||
|
apiAuth := apiGroup.Group("/")
|
||||||
|
apiAuth.Use(api.APIAuthMiddleWare())
|
||||||
|
setAPI(apiAuth)
|
||||||
|
}
|
||||||
|
|
||||||
err := router.Run(":" + cfg.Port)
|
err := router.Run(":" + cfg.Port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to launch application: \n %s\n", err)
|
log.Printf("Failed to launch application: \n %s\n", err)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func AuthMiddleware() gin.HandlerFunc {
|
|||||||
uid := session.Get("user_id")
|
uid := session.Get("user_id")
|
||||||
log.Println("UID is: ", uid)
|
log.Println("UID is: ", uid)
|
||||||
if uid != nil {
|
if uid != nil {
|
||||||
|
log.Println("Got UUID: ", uid)
|
||||||
user, err = exp.App.GetUser(uid.(int32))
|
user, err = exp.App.GetUser(uid.(int32))
|
||||||
log.Println("Got User: ", user)
|
log.Println("Got User: ", user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -31,15 +32,6 @@ func AuthMiddleware() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we have an authorization header
|
|
||||||
// if user == nil {
|
|
||||||
// authHeader := c.GetHeader("Authorization")
|
|
||||||
// if strings.HasPrefix(authHeader, "Bearer ") {
|
|
||||||
// tokenStr := strings.TrimPrefix(authHeader, "Bearer ")
|
|
||||||
// claims, err := validateJWT(tokenStr)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
if user == nil {
|
if user == nil {
|
||||||
log.Println("No User: ", user)
|
log.Println("No User: ", user)
|
||||||
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO - also allow asymetrical keys to be used
|
||||||
|
func GenerateToken(sub string) (string, error) {
|
||||||
|
// TODO - check if we can or should use JTI
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
|
"iss": "expenseswebapp",
|
||||||
|
"sub": sub,
|
||||||
|
"aud": sub,
|
||||||
|
// "nbf": time.Now().UTC(),
|
||||||
|
"iat": time.Now().UTC(),
|
||||||
|
"exp": time.Now().Add(15 * time.Minute).UTC(),
|
||||||
|
})
|
||||||
|
|
||||||
|
tokenStr, err := token.SignedString([]byte("any?"))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tokenStr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValidateToken(tStr string) (bool, error) {
|
||||||
|
token, err := jwt.Parse(tStr, func(token *jwt.Token) (any, error) {
|
||||||
|
return []byte("any?"), nil
|
||||||
|
}, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
||||||
|
log.Println(claims)
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
@@ -5,8 +5,7 @@ import (
|
|||||||
mod "expenses/expenses/models"
|
mod "expenses/expenses/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *ExpensesApp) GetAllCategories() ([]mod.Category, error) {
|
func (a *ExpensesApp) GetAllCategories(ctx context.Context) ([]mod.Category, error) {
|
||||||
ctx := context.Background()
|
|
||||||
return a.CategoryRepo.GetAll(ctx)
|
return a.CategoryRepo.GetAll(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *ExpensesApp) GetAllExpenses() ([]mod.Expense, error) {
|
func (a *ExpensesApp) GetAllExpenses(ctx context.Context) ([]mod.Expense, error) {
|
||||||
ctx := context.Background()
|
user := *ctx.Value("user").(*mod.User)
|
||||||
|
|
||||||
expenses, err := a.ExpRepo.GetAll(ctx)
|
expenses, err := a.ExpRepo.GetAll(ctx, user.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []mod.Expense{}, err
|
return []mod.Expense{}, err
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,8 @@ func (a *ExpensesApp) GetAllExpenses() ([]mod.Expense, error) {
|
|||||||
return expenses, nil
|
return expenses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) GetExpensesRanged(startDate string, endDate string) ([]mod.Expense, error) {
|
func (a *ExpensesApp) GetExpensesRanged(
|
||||||
|
ctx context.Context, startDate string, endDate string) ([]mod.Expense, error) {
|
||||||
startDateTime, err := time.ParseInLocation("02-Jan-2006 15:04:05", startDate, time.UTC)
|
startDateTime, err := time.ParseInLocation("02-Jan-2006 15:04:05", startDate, time.UTC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error startDate: %v", err)
|
log.Printf("error startDate: %v", err)
|
||||||
@@ -32,9 +33,8 @@ func (a *ExpensesApp) GetExpensesRanged(startDate string, endDate string) ([]mod
|
|||||||
return []mod.Expense{}, nil
|
return []mod.Expense{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
user := *ctx.Value("user").(*mod.User)
|
||||||
|
return a.ExpRepo.GetExpensesRange(ctx, startDateTime, endDateTime, user.UserID)
|
||||||
return a.ExpRepo.GetExpensesRange(ctx, startDateTime, endDateTime)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) GetExpense(id int32) (mod.Expense, error) {
|
func (a *ExpensesApp) GetExpense(id int32) (mod.Expense, error) {
|
||||||
@@ -137,9 +137,7 @@ func (a *ExpensesApp) analyzeExpense(e *mod.Expense) {
|
|||||||
a.NormalizeShares(e)
|
a.NormalizeShares(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) NewExpense(exp mod.Expense) error {
|
func (a *ExpensesApp) NewExpense(ctx context.Context, exp mod.Expense) error {
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
a.analyzeExpense(&exp)
|
a.analyzeExpense(&exp)
|
||||||
|
|
||||||
return a.ExpRepo.Insert(ctx, exp)
|
return a.ExpRepo.Insert(ctx, exp)
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ func (a *ExpensesApp) prepareExpense(exp *mod.Expense) error {
|
|||||||
|
|
||||||
func (a *ExpensesApp) luaGetAllExpenses(L *lua.LState) int {
|
func (a *ExpensesApp) luaGetAllExpenses(L *lua.LState) int {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
expenses, err := a.ExpRepo.GetAll(ctx)
|
// TODO - what do we do about the LUA API
|
||||||
|
expenses, err := a.ExpRepo.GetAll(ctx, 1)
|
||||||
|
|
||||||
tbl := L.NewTable()
|
tbl := L.NewTable()
|
||||||
for _, e := range expenses {
|
for _, e := range expenses {
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
type ExpenseRepository interface {
|
type ExpenseRepository interface {
|
||||||
// Direct expense methods
|
// Direct expense methods
|
||||||
Get(ctx context.Context, id int32) (mod.Expense, error)
|
Get(ctx context.Context, id int32) (mod.Expense, error)
|
||||||
GetAll(ctx context.Context) (mod.Expenses, error)
|
GetAll(ctx context.Context, uId int32) (mod.Expenses, error)
|
||||||
GetExpensesRange(ctx context.Context, start time.Time,
|
GetExpensesRange(ctx context.Context, start time.Time,
|
||||||
end time.Time) (mod.Expenses, error)
|
end time.Time, uId int32) (mod.Expenses, error)
|
||||||
Update(ctx context.Context, exp mod.Expense) error
|
Update(ctx context.Context, exp mod.Expense) error
|
||||||
Insert(ctx context.Context, exp mod.Expense) error
|
Insert(ctx context.Context, exp mod.Expense) error
|
||||||
Delete(ctx context.Context, id int32) error
|
Delete(ctx context.Context, id int32) error
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ func withTx(
|
|||||||
return tx.Commit(ctx)
|
return tx.Commit(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PgExpRepo) getAll(ctx context.Context, q *pgsqlc.Queries) (mod.Expenses, error) {
|
func (p *PgExpRepo) getAll(
|
||||||
|
ctx context.Context, q *pgsqlc.Queries, uId int32) (mod.Expenses, error) {
|
||||||
// We want to select all here, no timestamps
|
// We want to select all here, no timestamps
|
||||||
var start pgtype.Timestamp
|
var start pgtype.Timestamp
|
||||||
start.Valid = false
|
start.Valid = false
|
||||||
@@ -55,6 +56,7 @@ func (p *PgExpRepo) getAll(ctx context.Context, q *pgsqlc.Queries) (mod.Expenses
|
|||||||
expenses, err := q.GetExpenses(ctx, pgsqlc.GetExpensesParams{
|
expenses, err := q.GetExpenses(ctx, pgsqlc.GetExpensesParams{
|
||||||
Startdate: start,
|
Startdate: start,
|
||||||
Enddate: end,
|
Enddate: end,
|
||||||
|
UserID: uId,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []mod.Expense{}, err
|
return []mod.Expense{}, err
|
||||||
@@ -63,8 +65,8 @@ func (p *PgExpRepo) getAll(ctx context.Context, q *pgsqlc.Queries) (mod.Expenses
|
|||||||
return mapRepoGetExpensesRows(expenses), err
|
return mapRepoGetExpensesRows(expenses), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PgExpRepo) GetAll(ctx context.Context) (mod.Expenses, error) {
|
func (p PgExpRepo) GetAll(ctx context.Context, uId int32) (mod.Expenses, error) {
|
||||||
return p.getAll(ctx, pgsqlc.New(p.DB))
|
return p.getAll(ctx, pgsqlc.New(p.DB), uId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PgExpRepo) get(ctx context.Context, q *pgsqlc.Queries, id int32) (mod.Expense, error) {
|
func (p *PgExpRepo) get(ctx context.Context, q *pgsqlc.Queries, id int32) (mod.Expense, error) {
|
||||||
@@ -237,7 +239,10 @@ func (p PgExpRepo) Delete(ctx context.Context, id int32) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p PgExpRepo) GetExpensesRange(
|
func (p PgExpRepo) GetExpensesRange(
|
||||||
ctx context.Context, start time.Time, end time.Time) (mod.Expenses, error) {
|
ctx context.Context,
|
||||||
|
start time.Time,
|
||||||
|
end time.Time,
|
||||||
|
uId int32) (mod.Expenses, error) {
|
||||||
|
|
||||||
startPg, err := timeToTimestamp(start)
|
startPg, err := timeToTimestamp(start)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -253,6 +258,7 @@ func (p PgExpRepo) GetExpensesRange(
|
|||||||
expenses, err := queries.GetExpenses(ctx, pgsqlc.GetExpensesParams{
|
expenses, err := queries.GetExpenses(ctx, pgsqlc.GetExpensesParams{
|
||||||
Startdate: startPg,
|
Startdate: startPg,
|
||||||
Enddate: endPg,
|
Enddate: endPg,
|
||||||
|
UserID: uId,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mod.Expenses{}, err
|
return mod.Expenses{}, err
|
||||||
|
|||||||
@@ -101,12 +101,15 @@ JOIN
|
|||||||
JOIN
|
JOIN
|
||||||
"expenseTypes" as types ON types."TypeID" = expenses."TypeID"
|
"expenseTypes" as types ON types."TypeID" = expenses."TypeID"
|
||||||
WHERE
|
WHERE
|
||||||
($1::timestamp IS NULL OR expenses."ExpDate" >= $1::timestamp)
|
expenses."ExpID" IN (SELECT "ExpID" from "expensesShares" WHERE "expensesShares"."UserID" = $1)
|
||||||
|
AND
|
||||||
|
($2::timestamp IS NULL OR expenses."ExpDate" >= $2::timestamp)
|
||||||
AND
|
AND
|
||||||
($2::timestamp IS NULL OR expenses."ExpDate" <= $2::timestamp)
|
($3::timestamp IS NULL OR expenses."ExpDate" <= $3::timestamp)
|
||||||
`
|
`
|
||||||
|
|
||||||
type GetExpensesParams struct {
|
type GetExpensesParams struct {
|
||||||
|
UserID int32
|
||||||
Startdate pgtype.Timestamp
|
Startdate pgtype.Timestamp
|
||||||
Enddate pgtype.Timestamp
|
Enddate pgtype.Timestamp
|
||||||
}
|
}
|
||||||
@@ -120,7 +123,7 @@ type GetExpensesRow struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) GetExpenses(ctx context.Context, arg GetExpensesParams) ([]GetExpensesRow, error) {
|
func (q *Queries) GetExpenses(ctx context.Context, arg GetExpensesParams) ([]GetExpensesRow, error) {
|
||||||
rows, err := q.db.Query(ctx, getExpenses, arg.Startdate, arg.Enddate)
|
rows, err := q.db.Query(ctx, getExpenses, arg.UserID, arg.Startdate, arg.Enddate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ JOIN
|
|||||||
JOIN
|
JOIN
|
||||||
"expenseTypes" as types ON types."TypeID" = expenses."TypeID"
|
"expenseTypes" as types ON types."TypeID" = expenses."TypeID"
|
||||||
WHERE
|
WHERE
|
||||||
|
expenses."ExpID" IN (SELECT "ExpID" from "expensesShares" WHERE "expensesShares"."UserID" = $1)
|
||||||
|
AND
|
||||||
(sqlc.narg(StartDate)::timestamp IS NULL OR expenses."ExpDate" >= sqlc.narg(StartDate)::timestamp)
|
(sqlc.narg(StartDate)::timestamp IS NULL OR expenses."ExpDate" >= sqlc.narg(StartDate)::timestamp)
|
||||||
AND
|
AND
|
||||||
(sqlc.narg(EndDate)::timestamp IS NULL OR expenses."ExpDate" <= sqlc.narg(EndDate)::timestamp);
|
(sqlc.narg(EndDate)::timestamp IS NULL OR expenses."ExpDate" <= sqlc.narg(EndDate)::timestamp);
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ import (
|
|||||||
mod "expenses/expenses/models"
|
mod "expenses/expenses/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *ExpensesApp) GetAllStores() ([]mod.Store, error) {
|
func (a *ExpensesApp) GetAllStores(ctx context.Context) ([]mod.Store, error) {
|
||||||
ctx := context.Background()
|
|
||||||
return a.StoreRepo.GetAll(ctx)
|
return a.StoreRepo.GetAll(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ import (
|
|||||||
mod "expenses/expenses/models"
|
mod "expenses/expenses/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *ExpensesApp) GetAllTypes() (mod.Types, error) {
|
func (a *ExpensesApp) GetAllTypes(ctx context.Context) (mod.Types, error) {
|
||||||
ctx := context.Background()
|
|
||||||
return a.TypeRepo.GetAll(ctx)
|
return a.TypeRepo.GetAll(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+18
-2
@@ -3,6 +3,7 @@ package expenses
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
mod "expenses/expenses/models"
|
mod "expenses/expenses/models"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *ExpensesApp) GetUser(id int32) (*mod.User, error) {
|
func (a *ExpensesApp) GetUser(id int32) (*mod.User, error) {
|
||||||
@@ -15,7 +16,22 @@ func (a *ExpensesApp) GetUserByName(name string) (*mod.User, error) {
|
|||||||
return a.UserRepo.GetByName(ctx, name)
|
return a.UserRepo.GetByName(ctx, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) GetAllUsers() ([]mod.User, error) {
|
func (a *ExpensesApp) GetAllUsers(ctx context.Context) ([]mod.User, error) {
|
||||||
ctx := context.Background()
|
|
||||||
return a.UserRepo.GetAll(ctx)
|
return a.UserRepo.GetAll(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *ExpensesApp) ValidateCredentials(name string, pass string) (*mod.User, error) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
user, err := a.UserRepo.GetByName(ctx, name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(pass))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|||||||
+2
-1
@@ -6,12 +6,14 @@ require (
|
|||||||
github.com/ESilva15/gofatqr v1.0.0
|
github.com/ESilva15/gofatqr v1.0.0
|
||||||
github.com/gin-contrib/sessions v1.0.4
|
github.com/gin-contrib/sessions v1.0.4
|
||||||
github.com/gin-gonic/gin v1.10.1
|
github.com/gin-gonic/gin v1.10.1
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||||
github.com/golang-migrate/migrate/v4 v4.18.3
|
github.com/golang-migrate/migrate/v4 v4.18.3
|
||||||
github.com/jackc/pgx/v5 v5.5.4
|
github.com/jackc/pgx/v5 v5.5.4
|
||||||
github.com/layeh/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
|
github.com/layeh/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
|
||||||
github.com/shopspring/decimal v1.4.0
|
github.com/shopspring/decimal v1.4.0
|
||||||
github.com/spf13/cobra v1.9.1
|
github.com/spf13/cobra v1.9.1
|
||||||
github.com/yuin/gopher-lua v1.1.1
|
github.com/yuin/gopher-lua v1.1.1
|
||||||
|
golang.org/x/crypto v0.37.0
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,7 +49,6 @@ require (
|
|||||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
go.uber.org/atomic v1.7.0 // indirect
|
go.uber.org/atomic v1.7.0 // indirect
|
||||||
golang.org/x/arch v0.16.0 // indirect
|
golang.org/x/arch v0.16.0 // indirect
|
||||||
golang.org/x/crypto v0.37.0 // indirect
|
|
||||||
golang.org/x/net v0.38.0 // indirect
|
golang.org/x/net v0.38.0 // indirect
|
||||||
golang.org/x/sync v0.13.0 // indirect
|
golang.org/x/sync v0.13.0 // indirect
|
||||||
golang.org/x/sys v0.32.0 // indirect
|
golang.org/x/sys v0.32.0 // indirect
|
||||||
|
|||||||
@@ -52,6 +52,8 @@ github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
|||||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||||
github.com/golang-migrate/migrate/v4 v4.18.3 h1:EYGkoOsvgHHfm5U/naS1RP/6PL/Xv3S4B/swMiAmDLs=
|
github.com/golang-migrate/migrate/v4 v4.18.3 h1:EYGkoOsvgHHfm5U/naS1RP/6PL/Xv3S4B/swMiAmDLs=
|
||||||
github.com/golang-migrate/migrate/v4 v4.18.3/go.mod h1:99BKpIi6ruaaXRM1A77eqZ+FWPQ3cfRa+ZVy5bmWMaY=
|
github.com/golang-migrate/migrate/v4 v4.18.3/go.mod h1:99BKpIi6ruaaXRM1A77eqZ+FWPQ3cfRa+ZVy5bmWMaY=
|
||||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
|
|||||||
@@ -1,12 +1,23 @@
|
|||||||
package pages
|
package pages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"expenses/api"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NotFoundView(c *gin.Context, msg string) {
|
func NotFoundView(c *gin.Context, msg string) {
|
||||||
|
if strings.HasPrefix(c.Request.URL.Path, api.APIPath) {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{
|
||||||
|
"error": "not_found",
|
||||||
|
"message": "endpoint doesn't exist",
|
||||||
|
"path": c.Param("path"),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.HTML(404, "terminal", gin.H{
|
c.HTML(404, "terminal", gin.H{
|
||||||
"warning": true,
|
"warning": true,
|
||||||
"renderNavBar": false,
|
"renderNavBar": false,
|
||||||
|
|||||||
+11
-2
@@ -2,11 +2,13 @@ package pages
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
exp "expenses/expenses"
|
exp "expenses/expenses"
|
||||||
experr "expenses/expenses/errors"
|
experr "expenses/expenses/errors"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +17,14 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func categoriesGlobalPage(c *gin.Context) {
|
func categoriesGlobalPage(c *gin.Context) {
|
||||||
categories, err := exp.App.GetAllCategories()
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to fetch logged in user -", err.Error())
|
||||||
|
ServerErrorView(c, "The server too makes mistakes")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
categories, err := exp.App.GetAllCategories(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "The server too makes mistakes")
|
ServerErrorView(c, "The server too makes mistakes")
|
||||||
return
|
return
|
||||||
|
|||||||
+63
-23
@@ -6,10 +6,11 @@ import (
|
|||||||
experr "expenses/expenses/errors"
|
experr "expenses/expenses/errors"
|
||||||
mod "expenses/expenses/models"
|
mod "expenses/expenses/models"
|
||||||
"fmt"
|
"fmt"
|
||||||
fatqr "github.com/ESilva15/gofatqr"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
fatqr "github.com/ESilva15/gofatqr"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -17,7 +18,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func expensesPartial(c *gin.Context) {
|
func expensesPartial(c *gin.Context) {
|
||||||
expenses, err := exp.App.GetAllExpenses()
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "Could not get logged in user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
expenses, err := exp.App.GetAllExpenses(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Header("HX-Redirect", "/500")
|
c.Header("HX-Redirect", "/500")
|
||||||
return
|
return
|
||||||
@@ -29,7 +36,13 @@ func expensesPartial(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExpensesGlobalPage(c *gin.Context) {
|
func ExpensesGlobalPage(c *gin.Context) {
|
||||||
expenses, err := exp.App.GetAllExpenses()
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "Could not get logged in user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
expenses, err := exp.App.GetAllExpenses(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln("error:", err)
|
log.Panicln("error:", err)
|
||||||
c.Header("HX-Redirect", "/500")
|
c.Header("HX-Redirect", "/500")
|
||||||
@@ -45,6 +58,12 @@ func ExpensesGlobalPage(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func expensePage(c *gin.Context) {
|
func expensePage(c *gin.Context) {
|
||||||
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "Could not get logged in user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
expenseID, err := exp.ParseID(c.Param("id"))
|
expenseID, err := exp.ParseID(c.Param("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "failed to parse requested expenses id")
|
ServerErrorView(c, "failed to parse requested expenses id")
|
||||||
@@ -77,36 +96,30 @@ func expensePage(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
categories, err := exp.App.GetAllCategories()
|
categories, err := exp.App.GetAllCategories(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "failed to fetch categories")
|
ServerErrorView(c, "failed to fetch categories")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
stores, err := exp.App.GetAllStores()
|
stores, err := exp.App.GetAllStores(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "failed to fetch stores")
|
ServerErrorView(c, "failed to fetch stores")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
types, err := exp.App.GetAllTypes()
|
types, err := exp.App.GetAllTypes(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "failed to fetch types")
|
ServerErrorView(c, "failed to fetch types")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
users, err := exp.App.GetAllUsers()
|
users, err := exp.App.GetAllUsers(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "failed to fetch users")
|
ServerErrorView(c, "failed to fetch users")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// summary, err := expense.GetSummary()
|
|
||||||
// if err != nil {
|
|
||||||
// ServerErrorView(c, "failed to get summary")
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||||
"page": "expense",
|
"page": "expense",
|
||||||
"renderNavBar": false,
|
"renderNavBar": false,
|
||||||
@@ -122,25 +135,32 @@ func expensePage(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newExpensePage(c *gin.Context) {
|
func newExpensePage(c *gin.Context) {
|
||||||
categories, err := exp.App.GetAllCategories()
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to fetch logged in user -", err.Error())
|
||||||
|
ServerErrorView(c, "The server too makes mistakes")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
categories, err := exp.App.GetAllCategories(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "failed to fetch categories")
|
ServerErrorView(c, "failed to fetch categories")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
stores, err := exp.App.GetAllStores()
|
stores, err := exp.App.GetAllStores(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "failed to fetch stores")
|
ServerErrorView(c, "failed to fetch stores")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
types, err := exp.App.GetAllTypes()
|
types, err := exp.App.GetAllTypes(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "failed to fetch types")
|
ServerErrorView(c, "failed to fetch types")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
users, err := exp.App.GetAllUsers()
|
users, err := exp.App.GetAllUsers(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "failed to fetch users")
|
ServerErrorView(c, "failed to fetch users")
|
||||||
return
|
return
|
||||||
@@ -160,7 +180,14 @@ func newExpensePage(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createExpense(c *gin.Context) {
|
func createExpense(c *gin.Context) {
|
||||||
newExp, err := expenseFromForm(c)
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to fetch logged in user -", err.Error())
|
||||||
|
ServerErrorView(c, "The server too makes mistakes")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
newExp, err := expenseFromForm(c, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO
|
// TODO
|
||||||
// Change this to something the user can see
|
// Change this to something the user can see
|
||||||
@@ -168,7 +195,7 @@ func createExpense(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = exp.App.NewExpense(*newExp)
|
err = exp.App.NewExpense(ctx, *newExp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Header("HX-Trigger", fmt.Sprintf("{\"formState\":\"%s\"}", err.Error()))
|
c.Header("HX-Trigger", fmt.Sprintf("{\"formState\":\"%s\"}", err.Error()))
|
||||||
return
|
return
|
||||||
@@ -178,13 +205,20 @@ func createExpense(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateExpense(c *gin.Context) {
|
func updateExpense(c *gin.Context) {
|
||||||
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to fetch logged in user -", err.Error())
|
||||||
|
ServerErrorView(c, "The server too makes mistakes")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
expenseID, err := exp.ParseID(c.Param("id"))
|
expenseID, err := exp.ParseID(c.Param("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Header("HX-Redirect", "/404")
|
c.Header("HX-Redirect", "/404")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newExp, err := expenseFromForm(c)
|
newExp, err := expenseFromForm(c, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO
|
// TODO
|
||||||
// Change this to something the user can see
|
// Change this to something the user can see
|
||||||
@@ -235,14 +269,20 @@ func qrRequest(c *gin.Context) {
|
|||||||
var fat fatqr.FatQR
|
var fat fatqr.FatQR
|
||||||
err := fat.Scan(qr, 0)
|
err := fat.Scan(qr, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO
|
log.Println("error scanning QR code:", err)
|
||||||
// handle the error here for the user
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to find storeID
|
// try to find storeID
|
||||||
storeID, err := exp.App.GetStoreIDFromNIF(fat.TaxRegistrationNumber)
|
storeID, err := exp.App.GetStoreIDFromNIF(fat.TaxRegistrationNumber)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("error getting storeID:", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package pages
|
package pages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"expenses/expenses"
|
"expenses/expenses"
|
||||||
mod "expenses/expenses/models"
|
mod "expenses/expenses/models"
|
||||||
"time"
|
"time"
|
||||||
@@ -25,7 +26,7 @@ func processFormPayments(c *gin.Context) ([]mod.Payment, error) {
|
|||||||
return expenses.ParseFormPayments(userIDs, paymentIDs, values)
|
return expenses.ParseFormPayments(userIDs, paymentIDs, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
func expenseFromForm(c *gin.Context) (*mod.Expense, error) {
|
func expenseFromForm(c *gin.Context, ctx context.Context) (*mod.Expense, error) {
|
||||||
newDescription := c.PostForm("expense-desc")
|
newDescription := c.PostForm("expense-desc")
|
||||||
newDate := c.PostForm("expense-date")
|
newDate := c.PostForm("expense-date")
|
||||||
newQR := c.PostForm("expense-qr")
|
newQR := c.PostForm("expense-qr")
|
||||||
@@ -68,7 +69,8 @@ func expenseFromForm(c *gin.Context) (*mod.Expense, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
newExp := mod.Expense{
|
creator := *ctx.Value("user").(*mod.User)
|
||||||
|
return &mod.Expense{
|
||||||
Description: newDescription,
|
Description: newDescription,
|
||||||
Date: formattedDate,
|
Date: formattedDate,
|
||||||
Value: value,
|
Value: value,
|
||||||
@@ -81,15 +83,10 @@ func expenseFromForm(c *gin.Context) (*mod.Expense, error) {
|
|||||||
Store: mod.Store{
|
Store: mod.Store{
|
||||||
StoreID: storeID,
|
StoreID: storeID,
|
||||||
},
|
},
|
||||||
Owner: mod.User{
|
Owner: creator,
|
||||||
// TODO
|
|
||||||
// This needs to be dynamic - to be added once we have logins and whatnot
|
|
||||||
UserID: 1,
|
|
||||||
},
|
|
||||||
CreationDate: time.Now(),
|
CreationDate: time.Now(),
|
||||||
Shares: shares,
|
Shares: shares,
|
||||||
Payments: payments,
|
Payments: payments,
|
||||||
QRString: newQR,
|
QRString: newQR,
|
||||||
}
|
}, nil
|
||||||
return &newExp, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package pages
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getLoggedInUserCTX(c *gin.Context) (context.Context, error) {
|
||||||
|
if _, ok := c.Get("user"); !ok {
|
||||||
|
return nil, fmt.Errorf("unable to get user from gin context")
|
||||||
|
}
|
||||||
|
|
||||||
|
user, _ := c.Get("user")
|
||||||
|
ctx := context.WithValue(c.Request.Context(), "user", user)
|
||||||
|
|
||||||
|
return ctx, nil
|
||||||
|
}
|
||||||
+3
-11
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -25,16 +24,9 @@ func loginUser(c *gin.Context) {
|
|||||||
username := c.PostForm("login-user")
|
username := c.PostForm("login-user")
|
||||||
password := c.PostForm("login-pass")
|
password := c.PostForm("login-pass")
|
||||||
|
|
||||||
user, err := exp.App.GetUserByName(username)
|
user, err := exp.App.ValidateCredentials(username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO - go to an error page here
|
// TODO - go to an error page or login failed or something
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check password
|
|
||||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
|
||||||
if err != nil {
|
|
||||||
// TODO - wrong password
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,10 +34,16 @@ func overviewPage(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getResults(c *gin.Context) {
|
func getResults(c *gin.Context) {
|
||||||
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "Could not get logged in user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
startDate := c.PostForm("range-start") + " 00:00:00"
|
startDate := c.PostForm("range-start") + " 00:00:00"
|
||||||
endDate := c.PostForm("range-end") + " 23:59:59"
|
endDate := c.PostForm("range-end") + " 23:59:59"
|
||||||
|
|
||||||
queriedExpenses, err := exp.App.GetExpensesRanged(startDate, endDate)
|
queriedExpenses, err := exp.App.GetExpensesRanged(ctx, startDate, endDate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("getting expenses: %v", err)
|
log.Printf("getting expenses: %v", err)
|
||||||
return
|
return
|
||||||
|
|||||||
+8
-1
@@ -17,7 +17,14 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func storesGlobalPage(c *gin.Context) {
|
func storesGlobalPage(c *gin.Context) {
|
||||||
stores, err := exp.App.GetAllStores()
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to fetch logged in user -", err.Error())
|
||||||
|
ServerErrorView(c, "The server too makes mistakes")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stores, err := exp.App.GetAllStores(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "Failed to fetch stores content")
|
ServerErrorView(c, "Failed to fetch stores content")
|
||||||
return
|
return
|
||||||
|
|||||||
+8
-2
@@ -5,8 +5,8 @@ import (
|
|||||||
exp "expenses/expenses"
|
exp "expenses/expenses"
|
||||||
experr "expenses/expenses/errors"
|
experr "expenses/expenses/errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -14,7 +14,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func typesGlobalPage(c *gin.Context) {
|
func typesGlobalPage(c *gin.Context) {
|
||||||
types, err := exp.App.GetAllTypes()
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "could not get logged in user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
types, err := exp.App.GetAllTypes(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ServerErrorView(c, "the server failed to get the types")
|
ServerErrorView(c, "the server failed to get the types")
|
||||||
return
|
return
|
||||||
|
|||||||
+7
-2
@@ -1,3 +1,8 @@
|
|||||||
registry = dir.esilva.org
|
include .env
|
||||||
repo = esilva
|
|
||||||
|
REQUIRED_VARS := registry repo
|
||||||
|
|
||||||
|
$(foreach v,$(REQUIRED_VARS),\
|
||||||
|
$(if $(value $(v)),,$(error Variable '$(v)' is required but empty)))
|
||||||
|
|
||||||
tag = v0.0.83-alpha
|
tag = v0.0.83-alpha
|
||||||
|
|||||||
Reference in New Issue
Block a user