Files
GiacPoints/main.go
2023-07-16 16:14:18 +01:00

99 lines
2.0 KiB
Go

package main
import (
"database/sql"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"net/http"
)
// define global db
var db, _ = sql.Open("sqlite3", "./database/data.db")
// types representing json queries
type loginInput struct {
Name string `json:"name"`
Password string `json:"password"`
}
type loginOutput struct {
UID string `json:"uid"`
Name string `json:"name"`
Password string `json:"password"`
Role string `json:"role"`
}
type task struct {
TID string `json:"tid"`
Name string `json:"name"`
Points int `json:"points"`
}
type historyData struct {
UID string `json:"uid"`
TID string `json:"tid"`
Time string `json:"time"`
PointsGained int `json:"pointsGained"`
}
// 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 {
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)
if err != nil {
// search failed user not real
c.IndentedJSON(http.StatusNotAcceptable, userData)
}
if user.Name != userData.Name || user.Password != userData.Password {
// user not real
c.IndentedJSON(http.StatusNotAcceptable, userData)
} else {
// user is in
c.IndentedJSON(http.StatusOK, user)
}
}
func getTasks(c *gin.Context) {
// return a list of all the tasks
}
func completeTask(c *gin.Context) {
// complete a task in the history log
}
func getHistory(c *gin.Context) {
// get the log of past points gained
}
func addTask(c *gin.Context) {
// add a task to the list of tasks
}
func main() {
router := gin.Default()
router.POST("/login", login)
router.GET("/getTasks", getTasks)
router.POST("/completeTask", completeTask)
router.POST("/addTask", addTask)
router.GET("/getHistory", getHistory)
router.Run("localhost:8080")
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}