package apis import ( "fmt" "net/http" "github.com/gin-gonic/gin" "planner/core" ) func getUserByName(c *gin.Context) { var q struct { Name string `fdb:"name"` } if c.ShouldBind(&q) == nil { user, err := core.UserGet(db, q.Name) if err != nil { c.String(http.StatusInternalServerError, "Unable to get user: "+err.Error()) return } c.JSON(http.StatusOK, user) } } func createUser(c *gin.Context) { var u core.User if c.ShouldBind(&u) == nil { err := u.Create(db) if err != nil { c.String(http.StatusInternalServerError, "Unable to create user: "+err.Error()) return } c.Status(http.StatusCreated) } else { fmt.Print("Could not bind model") c.String(http.StatusInternalServerError, "Unable to bind user") } } func login(c *gin.Context) { var q struct { Username string `json:"username" form:"username"` Password string `json:"password" form:"password"` } if c.ShouldBind(&q) == nil { if q.Username == "" { c.String(http.StatusBadRequest, "Login data is null") } else { user, err := core.UserGet(db, q.Username) if err != nil { c.String(http.StatusInternalServerError, "Unable to get user: "+err.Error()) return } if user.Password == q.Password { c.JSON(http.StatusOK, gin.H{"username": user.Username}) } else { c.Status(http.StatusForbidden) } } } else { c.String(http.StatusBadRequest, "Unable to bind data") } } func bindUserAPIs(r *gin.Engine) { r.POST("/login", login) r.GET("/user", getUserByName) r.POST("/user", createUser) }