jokes-bapak2/api/app/handler/submit/submit_add.go

130 lines
2.8 KiB
Go
Raw Normal View History

2021-08-04 02:51:22 +00:00
package submit
import (
2021-09-27 10:10:19 +00:00
"jokes-bapak2-api/app/core"
2021-09-27 13:02:58 +00:00
"net/url"
2021-08-04 02:51:22 +00:00
"strings"
"time"
2021-09-27 13:02:58 +00:00
"github.com/Masterminds/squirrel"
2021-08-04 02:51:22 +00:00
"github.com/georgysavva/scany/pgxscan"
"github.com/gofiber/fiber/v2"
2021-09-27 13:02:58 +00:00
"github.com/jackc/pgx/v4"
2021-08-04 02:51:22 +00:00
)
func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error {
2021-09-27 13:02:58 +00:00
conn, err := d.DB.Acquire(*d.Context)
if err != nil {
return err
}
defer conn.Release()
var body Submission
2021-09-27 13:02:58 +00:00
err = c.BodyParser(&body)
2021-08-04 02:51:22 +00:00
if err != nil {
return err
}
// Image and/or Link should not be empty
if body.Image == "" && body.Link == "" {
return c.Status(fiber.StatusBadRequest).JSON(Error{
2021-09-27 13:02:58 +00:00
Error: "A link or an image should be supplied in a form of multipart/form-data",
2021-08-04 02:51:22 +00:00
})
}
// Author should be supplied
if body.Author == "" {
return c.Status(fiber.StatusBadRequest).JSON(Error{
2021-09-27 13:02:58 +00:00
Error: "An author key consisting on the format \"yourname <youremail@mail>\" must be supplied",
2021-08-04 02:51:22 +00:00
})
} else {
// Validate format
valid := core.ValidateAuthor(body.Author)
if !valid {
return c.Status(fiber.StatusBadRequest).JSON(Error{
2021-09-27 13:02:58 +00:00
Error: "Please stick to the format of \"yourname <youremail@mail>\" and within 200 characters",
2021-08-04 02:51:22 +00:00
})
}
}
2021-09-27 13:02:58 +00:00
var link string
2021-08-04 02:51:22 +00:00
// Check link validity if link was provided
if body.Link != "" {
valid, err := core.CheckImageValidity(d.HTTP, body.Link)
2021-08-04 02:51:22 +00:00
if err != nil {
return err
}
if !valid {
return c.Status(fiber.StatusBadRequest).JSON(Error{
2021-08-04 02:51:22 +00:00
Error: "URL provided is not a valid image",
})
}
2021-09-27 13:02:58 +00:00
link = body.Link
2021-08-04 02:51:22 +00:00
}
// If image was provided
if body.Image != "" {
image := strings.NewReader(body.Image)
2021-09-27 13:02:58 +00:00
link, err = core.UploadImage(d.HTTP, image)
2021-08-04 02:51:22 +00:00
if err != nil {
return err
}
}
2021-09-27 13:02:58 +00:00
// Validate if link already exists
sql, args, err := d.Query.
Select("link").
From("submission").
Where(squirrel.Eq{"link": link}).
ToSql()
if err != nil {
return err
}
v, err := conn.Query(*d.Context, sql, args...)
if err != nil && err != pgx.ErrNoRows {
return err
}
defer v.Close()
if err == nil {
return c.Status(fiber.StatusConflict).JSON(Error{
Error: "Given link is already on the submission queue.",
})
}
2021-08-04 02:51:22 +00:00
now := time.Now().UTC().Format(time.RFC3339)
2021-09-27 13:02:58 +00:00
sql, args, err = d.Query.
2021-08-04 02:51:22 +00:00
Insert("submission").
Columns("link", "created_at", "author").
2021-09-27 13:02:58 +00:00
Values(link, now, body.Author).
2021-08-04 02:51:22 +00:00
Suffix("RETURNING id,created_at,link,author,status").
ToSql()
if err != nil {
return err
}
var submission []Submission
2021-09-27 13:02:58 +00:00
result, err := conn.Query(*d.Context, sql, args...)
2021-08-04 02:51:22 +00:00
if err != nil {
return err
}
defer result.Close()
err = pgxscan.ScanAll(&submission, result)
if err != nil {
return err
}
return c.
2021-08-04 10:08:06 +00:00
Status(fiber.StatusCreated).
JSON(ResponseSubmission{
2021-09-27 13:02:58 +00:00
Message: "Joke submitted. Please wait for a few days for admin to approve your submission.",
Submission: submission[0],
AuthorPage: "/submit?author=" + url.QueryEscape(body.Author),
2021-08-04 02:51:22 +00:00
})
}