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

100 lines
2.3 KiB
Go
Raw Normal View History

2021-08-04 02:51:22 +00:00
package submit
import (
"jokes-bapak2-api/core/schema"
2021-10-30 03:24:53 +00:00
core "jokes-bapak2-api/core/submit"
"jokes-bapak2-api/core/validator"
2021-09-27 13:02:58 +00:00
"net/url"
2021-08-04 02:51:22 +00:00
"strings"
"github.com/gofiber/fiber/v2"
)
func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error {
conn, err := d.DB.Acquire(c.Context())
2021-09-27 13:02:58 +00:00
if err != nil {
return err
}
defer conn.Release()
var body schema.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(schema.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(schema.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 := validator.ValidateAuthor(body.Author)
2021-08-04 02:51:22 +00:00
if !valid {
return c.Status(fiber.StatusBadRequest).JSON(schema.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 := validator.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(schema.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-28 06:34:02 +00:00
2021-09-27 13:02:58 +00:00
// Validate if link already exists
validateLink, err := validator.SubmitLinkExists(d.DB, c.Context(), d.Query, link)
2021-09-27 13:02:58 +00:00
if err != nil {
return err
}
2021-09-28 06:34:02 +00:00
if validateLink {
return c.Status(fiber.StatusConflict).JSON(schema.Error{
2021-09-27 13:02:58 +00:00
Error: "Given link is already on the submission queue.",
})
}
2021-08-04 02:51:22 +00:00
submission, err := core.SubmitJoke(d.DB, c.Context(), body, link)
2021-08-04 02:51:22 +00:00
if err != nil {
return err
}
return c.
2021-08-04 10:08:06 +00:00
Status(fiber.StatusCreated).
JSON(schema.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,
2021-09-27 13:02:58 +00:00
AuthorPage: "/submit?author=" + url.QueryEscape(body.Author),
2021-08-04 02:51:22 +00:00
})
}