jokes-bapak2/api/app/v1/handler/joke_add.go

32 lines
571 B
Go
Raw Normal View History

2021-07-09 06:11:11 +00:00
package handler
import (
"context"
2021-07-14 18:17:01 +00:00
"jokes-bapak2-api/app/v1/models"
2021-07-09 06:11:11 +00:00
"github.com/gofiber/fiber/v2"
)
func AddNewJoke(c *fiber.Ctx) error {
2021-07-09 12:13:19 +00:00
var body models.RequestJokePost
2021-07-09 06:11:11 +00:00
err := c.BodyParser(&body)
if err != nil {
return err
}
2021-07-14 18:17:01 +00:00
sql, args, err := psql.Insert("jokesbapak2").Columns("link", "creator").Values(body.Link, c.Locals("userID")).ToSql()
2021-07-09 06:11:11 +00:00
if err != nil {
return err
}
_, err = db.Query(context.Background(), sql, args...)
if err != nil {
return err
}
2021-07-09 12:13:19 +00:00
return c.Status(fiber.StatusCreated).JSON(models.ResponseJoke{
Link: body.Link,
2021-07-09 06:11:11 +00:00
})
}