Added points to the users table

This commit is contained in:
2023-07-21 22:36:41 +01:00
parent fa8e4486f3
commit 2aaa448331

25
main.go
View File

@@ -17,11 +17,12 @@ type loginInput struct {
Password string `json:"password"`
}
type loginOutput struct {
type userData struct {
UID int `json:"uid"`
Name string `json:"name"`
Password string `json:"password"`
Role string `json:"role"`
Points int `json:"points"`
}
type task struct {
@@ -34,14 +35,14 @@ type taskArray struct {
Tasks []task `json:"tasks"`
}
type historyReqData struct {
type newHistoryData struct {
UID int `json:"uid"`
TID int `json:"tid"`
Time string `json:"time"`
PointsGained int `json:"pointsGained"`
}
type historyResData struct {
type historyData struct {
User string `json:"user"`
Task string `json:"task"`
Time string `json:"time"`
@@ -49,32 +50,32 @@ type historyResData struct {
}
type historyArray struct {
History []historyResData `json:"history"`
History []historyData `json:"history"`
}
// log the user into their account
func login(c *gin.Context) {
// get the username and password from the request
var userData loginInput
if err := c.BindJSON(&userData); err != nil {
c.IndentedJSON(http.StatusBadRequest, userData)
var requestedUser loginInput
if err := c.BindJSON(&requestedUser); err != nil {
c.IndentedJSON(http.StatusBadRequest, requestedUser)
return
}
// check for user using given credentials
stmt, err := db.Prepare("SELECT * FROM users WHERE name=?")
checkErr(err)
defer stmt.Close()
var user loginOutput
err = stmt.QueryRow(userData.Name).Scan(&user.UID, &user.Name, &user.Password, &user.Role, &user.Points)
var user userData
err = stmt.QueryRow(requestedUser.Name).Scan(&user.UID, &user.Name, &user.Password, &user.Role, &user.Points)
if err != nil {
// search failed user not real
c.IndentedJSON(http.StatusNotFound, userData)
c.IndentedJSON(http.StatusNotFound, requestedUser)
panic(err)
return
}
if user.Password != userData.Password {
if user.Password != requestedUser.Password {
// user not real
c.IndentedJSON(http.StatusNotFound, userData)
c.IndentedJSON(http.StatusNotFound, requestedUser)
panic(err)
return
} else {