2021-08-04 02:51:22 +00:00
|
|
|
package submit
|
|
|
|
|
|
|
|
import (
|
2021-11-01 12:32:33 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error {
|
2021-10-18 08:31:17 +00:00
|
|
|
conn, err := d.DB.Acquire(c.Context())
|
2021-09-27 13:02:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
2021-11-01 12:32:33 +00:00
|
|
|
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 == "" {
|
2021-11-01 12:32:33 +00:00
|
|
|
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 == "" {
|
2021-11-01 12:32:33 +00:00
|
|
|
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
|
2021-10-18 08:31:17 +00:00
|
|
|
valid := validator.ValidateAuthor(body.Author)
|
2021-08-04 02:51:22 +00:00
|
|
|
if !valid {
|
2021-11-01 12:32:33 +00:00
|
|
|
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 != "" {
|
2021-10-18 08:31:17 +00:00
|
|
|
valid, err := validator.CheckImageValidity(d.HTTP, body.Link)
|
2021-08-04 02:51:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !valid {
|
2021-11-01 12:32:33 +00:00
|
|
|
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
|
2021-11-01 12:32:33 +00:00
|
|
|
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 {
|
2021-11-01 12:32:33 +00:00
|
|
|
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
|
|
|
|
2021-11-01 12:32:33 +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).
|
2021-11-01 12:32:33 +00:00
|
|
|
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.",
|
2021-11-01 12:32:33 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|