diff --git a/api/endpotins.go b/api/endpotins.go index 5fecd2e..619d5dd 100644 --- a/api/endpotins.go +++ b/api/endpotins.go @@ -5,6 +5,7 @@ import ( "time" "git.tek.govt.hu/dowerx/szoe-pontok/api/auth" + "git.tek.govt.hu/dowerx/szoe-pontok/api/task" "github.com/gin-gonic/gin" ) @@ -26,6 +27,11 @@ func Listen(address string, path string) { apiAuth.GET("login", auth.Login) } + apiAdmin := api.Group("admin").Use(auth.LoggedIn).Use(auth.IsAdmin) + { + apiAdmin.POST("task/add", task.Add) + } + apiTest := api.Group("test").Use(auth.LoggedIn).Use(auth.IsAdmin) { apiTest.GET("logged_in", func(c *gin.Context) { diff --git a/api/task/task.go b/api/task/task.go new file mode 100644 index 0000000..fe456d1 --- /dev/null +++ b/api/task/task.go @@ -0,0 +1,51 @@ +package task + +import ( + "net/http" + + "git.tek.govt.hu/dowerx/szoe-pontok/database/task" + "git.tek.govt.hu/dowerx/szoe-pontok/model" + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" + "github.com/go-playground/validator/v10" +) + +func Add(c *gin.Context) { + var tsk model.Task + if c.MustBindWith(&tsk, binding.Form) != nil { + return + } + + issuer, ok := c.Get("neptun") + if !ok { + c.JSON(http.StatusBadRequest, gin.H{ + "status": http.StatusBadRequest, + "error": "not logged in", + }) + return + } + + tsk.Issuer = issuer.(string) + + val := validator.New(validator.WithRequiredStructEnabled()) + if err := val.Struct(tsk); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "status": http.StatusBadRequest, + "error": err.Error(), + }) + return + } + + if err := task.Add(tsk); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "status": http.StatusBadRequest, + "error": err.Error(), + }) + return + } else { + c.JSON(http.StatusOK, gin.H{ + "status": http.StatusOK, + "message": "added", + }) + } +} diff --git a/database/auth/login.go b/database/auth/login.go index b9244aa..96dd351 100644 --- a/database/auth/login.go +++ b/database/auth/login.go @@ -23,11 +23,7 @@ func generateToken(length int) string { func Login(user model.User) (string, error) { db := database.GetDB() - rows, err := db.NamedQuery(`select "password" from "user" where "neptun" = :neptun and "email" = :email`, - map[string]interface{}{ - "neptun": user.Neptun, - "email": user.Email, - }) + rows, err := db.NamedQuery(`select "password" from "user" where "neptun" = :neptun and "email" = :email`, user) if err != nil { return "", err diff --git a/database/auth/register.go b/database/auth/register.go index 5ca2a2b..05a7e32 100644 --- a/database/auth/register.go +++ b/database/auth/register.go @@ -14,12 +14,9 @@ func Register(user model.User) error { return err } - _, err = db.NamedExec(`insert into "user" ("neptun", "email", "password") values (:neptun, :email ,:password)`, - map[string]interface{}{ - "neptun": user.Neptun, - "email": user.Email, - "password": string(bytes), - }) + user.Password = string(bytes) + + _, err = db.NamedExec(`insert into "user" ("neptun", "email", "password") values (:neptun, :email ,:password)`, user) return err } diff --git a/database/task/task.go b/database/task/task.go new file mode 100644 index 0000000..33a3dc8 --- /dev/null +++ b/database/task/task.go @@ -0,0 +1,19 @@ +package task + +import ( + "git.tek.govt.hu/dowerx/szoe-pontok/database" + "git.tek.govt.hu/dowerx/szoe-pontok/model" +) + +func Add(task model.Task) error { + db := database.GetDB() + + _, err := db.NamedExec( + `insert into "task" ("description", "points", "recipient", "issuer") values + (:description, :points, + (select "id" from "user" where "neptun" = :recipient), + (select "id" from "user" where "neptun" = :issuer) + )`, task) + + return err +} diff --git a/model/model.go b/model/model.go index 405cc6a..1c8f8c4 100644 --- a/model/model.go +++ b/model/model.go @@ -13,6 +13,6 @@ type Task struct { Description string `db:"description" form:"description" json:"description"` Points int `db:"points" form:"points" json:"points" validate:"required"` Recipient string `db:"recipient" form:"recipient" json:"recipient" validate:"required,len=6"` - Issuer string `db:"issuer" form:"issuer" json:"issuer" validate:"required,len=6"` + Issuer string `db:"issuer" form:"issuer" json:"issuer" validate:"len=6"` CreatedDate time.Time `db:"created_date" json:"created_date"` }