parent
44d85fc155
commit
62f43d7eb3
2
backend/.gitignore
vendored
Normal file
2
backend/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*.db3
|
||||||
|
*.db3-*
|
@ -385,3 +385,22 @@ func TestPutTaskInvalidTaskId(t *testing.T) {
|
|||||||
}
|
}
|
||||||
e.PUT("/task/999").WithJSON(requestBody).Expect().Status(404)
|
e.PUT("/task/999").WithJSON(requestBody).Expect().Status(404)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPostAllowance(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
|
||||||
|
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200)
|
||||||
|
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200)
|
||||||
|
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: -10}).Expect().Status(200)
|
||||||
|
|
||||||
|
response := e.GET("/user/1").Expect().Status(200).JSON().Object()
|
||||||
|
response.Value("allowance").Number().IsEqual(100 + 20 - 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostAllowanceInvalidUserId(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
|
||||||
|
e.POST("/user/999/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().
|
||||||
|
Status(404)
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.seeseepuff.be/seeseemelk/mysqlite"
|
"gitea.seeseepuff.be/seeseemelk/mysqlite"
|
||||||
)
|
)
|
||||||
@ -45,11 +46,11 @@ func (db *Db) GetUsers() ([]User, error) {
|
|||||||
return users, nil
|
return users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Db) GetUser(id int) (*User, error) {
|
func (db *Db) GetUser(id int) (*UserWithAllowance, error) {
|
||||||
user := &User{}
|
user := &UserWithAllowance{}
|
||||||
|
|
||||||
err := db.db.Query("select id, name from users where id = ?").
|
err := db.db.Query("select u.id, u.name, sum(h.amount) from users u join history h on h.user_id = u.id where u.id = ?").
|
||||||
Bind(id).ScanSingle(&user.ID, &user.Name)
|
Bind(id).ScanSingle(&user.ID, &user.Name, &user.Allowance)
|
||||||
if errors.Is(err, mysqlite.ErrNoRows) {
|
if errors.Is(err, mysqlite.ErrNoRows) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@ -237,3 +238,19 @@ func (db *Db) UpdateTask(id int, task *CreateTaskRequest) error {
|
|||||||
}
|
}
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *Db) AddAllowance(userId int, allowance *PostAllowance) error {
|
||||||
|
tx, err := db.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.MustRollback()
|
||||||
|
|
||||||
|
err = tx.Query("insert into history (user_id, date, amount) values (?, ?, ?)").
|
||||||
|
Bind(userId, time.Now().Unix(), allowance.Allowance).
|
||||||
|
Exec()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
@ -5,11 +5,21 @@ type User struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserWithAllowance struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Allowance int `json:"allowance"`
|
||||||
|
}
|
||||||
|
|
||||||
type Allowance struct {
|
type Allowance struct {
|
||||||
Allowance int `json:"allowance"`
|
Allowance int `json:"allowance"`
|
||||||
Goals []Goal `json:"goals"`
|
Goals []Goal `json:"goals"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PostAllowance struct {
|
||||||
|
Allowance int `json:"allowance"`
|
||||||
|
}
|
||||||
|
|
||||||
// Task represents a task in the system.
|
// Task represents a task in the system.
|
||||||
type Task struct {
|
type Task struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
|
@ -289,6 +289,31 @@ func putTask(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
|
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func postAllowance(c *gin.Context) {
|
||||||
|
userIdStr := c.Param("userId")
|
||||||
|
userId, err := strconv.Atoi(userIdStr)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Invalid user ID: %v", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var allowanceRequest PostAllowance
|
||||||
|
if err := c.ShouldBindJSON(&allowanceRequest); err != nil {
|
||||||
|
log.Printf("Error parsing request body: %v", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.AddAllowance(userId, &allowanceRequest)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error updating allowance: %v", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Allowance updated successfully"})
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
Initialises the database, and then starts the server.
|
Initialises the database, and then starts the server.
|
||||||
@ -308,6 +333,7 @@ func start(ctx context.Context, config *ServerConfig) {
|
|||||||
router.GET("/api/tasks", getTasks)
|
router.GET("/api/tasks", getTasks)
|
||||||
router.GET("/api/task/:taskId", getTask)
|
router.GET("/api/task/:taskId", getTask)
|
||||||
router.PUT("/api/task/:taskId", putTask)
|
router.PUT("/api/task/:taskId", putTask)
|
||||||
|
router.POST("/api/user/:userId/allowance", postAllowance)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: config.Addr,
|
Addr: config.Addr,
|
||||||
|
@ -53,6 +53,9 @@ paths:
|
|||||||
name:
|
name:
|
||||||
type: string
|
type: string
|
||||||
description: The user name
|
description: The user name
|
||||||
|
allowance:
|
||||||
|
type: integer
|
||||||
|
description: The total amount of allowance the user has
|
||||||
404:
|
404:
|
||||||
description: The users could not be found.
|
description: The users could not be found.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user