transfer-allowances (#143)
All checks were successful
Backend Build and Test / build (push) Successful in 10m9s
Backend Deploy / build (push) Successful in 11m20s

Reviewed-on: #143
This commit was merged in pull request #143.
This commit is contained in:
2025-10-08 20:21:09 +02:00
parent cdbac17215
commit c9a96f937a
7 changed files with 401 additions and 159 deletions

View File

@@ -4,15 +4,14 @@ import (
"context"
"embed"
"errors"
"fmt"
"gitea.seeseepuff.be/seeseemelk/mysqlite"
"github.com/adhocore/gronx"
"log"
"net"
"net/http"
"os"
"strconv"
"gitea.seeseepuff.be/seeseemelk/mysqlite"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
@@ -26,6 +25,8 @@ const (
ErrInvalidUserID = "Invalid user ID"
ErrUserNotFound = "User not found"
ErrCheckingUserExist = "Error checking user existence: %v"
ErrInsufficientFunds = "Insufficient funds in source allowance"
ErrDifferentUsers = "Allowances do not belong to the same user"
)
// ServerConfig holds configuration for the server.
@@ -439,11 +440,8 @@ func createTask(c *gin.Context) {
}
if taskRequest.Schedule != nil {
valid := gronx.IsValid(*taskRequest.Schedule)
if !valid {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid cron schedule: %s", *taskRequest.Schedule)})
return
}
c.JSON(http.StatusBadRequest, gin.H{"error": "Schedules are not yet supported"})
return
}
// If assigned is not nil, check if user exists
@@ -653,6 +651,32 @@ func getHistory(c *gin.Context) {
c.IndentedJSON(http.StatusOK, history)
}
func transfer(c *gin.Context) {
var transferRequest TransferRequest
if err := c.ShouldBindJSON(&transferRequest); err != nil {
log.Printf("Error parsing request body: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
err := db.TransferAllowance(transferRequest.From, transferRequest.To, transferRequest.Amount)
if err != nil {
if errors.Is(err, mysqlite.ErrNoRows) {
c.JSON(http.StatusNotFound, gin.H{"error": "Allowance not found"})
return
}
if err.Error() == ErrInsufficientFunds || err.Error() == ErrDifferentUsers {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
log.Printf("Error transferring allowance: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Transfer successful"})
}
/*
Initialises the database, and then starts the server.
If the context gets cancelled, the server is shutdown and the database is closed.
@@ -688,6 +712,7 @@ func start(ctx context.Context, config *ServerConfig) {
router.PUT("/api/task/:taskId", putTask)
router.DELETE("/api/task/:taskId", deleteTask)
router.POST("/api/task/:taskId/complete", completeTask)
router.POST("/api/transfer", transfer)
srv := &http.Server{
Addr: config.Addr,