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