wrapperr/modules/users.go
2023-10-29 18:03:55 +01:00

60 lines
1.3 KiB
Go

package modules
import (
"aunefyren/wrapperr/files"
"aunefyren/wrapperr/models"
"errors"
"log"
)
func UsersSaveUserEntry(newUser models.WrapperrUser) (err error) {
err = nil
users, err := files.GetUsers()
if err != nil {
log.Println("Failed to get users. Error: " + err.Error())
return errors.New("Failed to get users.")
}
var userFound = false
var userIndex = 0
for index, user := range users {
if user.UserID == newUser.UserID || user.User == newUser.User {
userFound = true
userIndex = index
}
}
if userFound {
users[userIndex].Wrappings = append(users[userIndex].Wrappings, newUser.Wrappings...)
} else {
users = append(users, newUser)
}
err = files.SaveUsers(users)
if err != nil {
log.Println("Failed to save new users. Error: " + err.Error())
return errors.New("Failed to save new users.")
}
return
}
func UsersGetUser(userID int) (user models.WrapperrUser, err error) {
err = nil
user = models.WrapperrUser{}
users, err := files.GetUsers()
if err != nil {
log.Println("Failed to get users. Error: " + err.Error())
return user, errors.New("Failed to get users.")
}
for _, foundUser := range users {
if foundUser.UserID == userID {
return foundUser, nil
}
}
return user, errors.New("User not found.")
}