Compare commits

...

3 Commits

Author SHA1 Message Date
c81a0d3294 Fix failing tests 2025-05-13 19:34:19 +02:00
dd942f12f7 Do not catch norows error 2025-05-13 19:34:19 +02:00
f29eeae9d9 Remove get allowance endpoint (#35)
The GET /user/:userId endpoint already does this operation

Closes #10

Reviewed-on: #35
2025-05-13 15:24:15 +02:00
3 changed files with 16 additions and 31 deletions

View File

@ -49,11 +49,8 @@ func (db *Db) GetUsers() ([]User, error) {
func (db *Db) GetUser(id int) (*UserWithAllowance, error) {
user := &UserWithAllowance{}
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 = ?").
err := db.db.Query("select u.id, u.name, (select ifnull(sum(h.amount), 0) from history h where h.user_id = u.id) from users u where u.id = ?").
Bind(id).ScanSingle(&user.ID, &user.Name, &user.Allowance)
if errors.Is(err, mysqlite.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, err
}

View File

@ -62,15 +62,15 @@ func getUser(c *gin.Context) {
}
user, err := db.GetUser(userId)
if errors.Is(err, mysqlite.ErrNoRows) {
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
return
}
if err != nil {
log.Printf("Error getting user: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
if user == nil {
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
return
}
c.IndentedJSON(http.StatusOK, user)
}
@ -305,6 +305,17 @@ func postAllowance(c *gin.Context) {
return
}
exists, err := db.UserExists(userId)
if err != nil {
log.Printf(ErrCheckingUserExist, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
if !exists {
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
return
}
err = db.AddAllowance(userId, &allowanceRequest)
if err != nil {
log.Printf("Error updating allowance: %v", err)

View File

@ -60,29 +60,6 @@ paths:
description: The users could not be found.
/user/{userId}/allowance:
get:
summary: Gets the allowance breakdown of a user
parameters:
- in: path
name: userId
description: The user ID
required: true
schema:
type: integer
responses:
200:
description: Information about the allowance of the user
content:
application/json:
schema:
type: object
properties:
allowance:
type: integer
description: The total allowance value of the user, in cents.
goals:
type: array
$ref: "#/components/schemas/goal"
post:
summary: Updates the allowance of a user
parameters: