parent
f29eeae9d9
commit
94a20af04d
@ -49,11 +49,8 @@ func (db *Db) GetUsers() ([]User, error) {
|
|||||||
func (db *Db) GetUser(id int) (*UserWithAllowance, error) {
|
func (db *Db) GetUser(id int) (*UserWithAllowance, error) {
|
||||||
user := &UserWithAllowance{}
|
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)
|
Bind(id).ScanSingle(&user.ID, &user.Name, &user.Allowance)
|
||||||
if errors.Is(err, mysqlite.ErrNoRows) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -62,15 +62,15 @@ func getUser(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
user, err := db.GetUser(userId)
|
user, err := db.GetUser(userId)
|
||||||
|
if errors.Is(err, mysqlite.ErrNoRows) {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
|
||||||
|
return
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error getting user: %v", err)
|
log.Printf("Error getting user: %v", err)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if user == nil {
|
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.IndentedJSON(http.StatusOK, user)
|
c.IndentedJSON(http.StatusOK, user)
|
||||||
}
|
}
|
||||||
@ -305,6 +305,17 @@ func postAllowance(c *gin.Context) {
|
|||||||
return
|
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)
|
err = db.AddAllowance(userId, &allowanceRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error updating allowance: %v", err)
|
log.Printf("Error updating allowance: %v", err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user