add task
This commit is contained in:
parent
84ead30400
commit
95f6de950f
@ -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) {
|
||||
|
51
api/task/task.go
Normal file
51
api/task/task.go
Normal file
@ -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",
|
||||
})
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
19
database/task/task.go
Normal file
19
database/task/task.go
Normal file
@ -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
|
||||
}
|
@ -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"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user