2021-08-03 18:14:32 +00:00
|
|
|
package joke
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"jokes-bapak2-api/app/v1/core"
|
|
|
|
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-09-26 19:13:38 +00:00
|
|
|
"github.com/jackc/pgx/v4"
|
2021-08-03 18:14:32 +00:00
|
|
|
)
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
func (d *Dependencies) UpdateJoke(c *fiber.Ctx) error {
|
2021-08-03 18:14:32 +00:00
|
|
|
id := c.Params("id")
|
|
|
|
// Check if the joke exists
|
2021-09-26 19:13:38 +00:00
|
|
|
sql, args, err := d.Query.
|
2021-08-04 10:14:33 +00:00
|
|
|
Select("id").
|
|
|
|
From("jokesbapak2").
|
|
|
|
Where(squirrel.Eq{"id": id}).
|
|
|
|
ToSql()
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var jokeID string
|
2021-09-26 19:13:38 +00:00
|
|
|
err = d.DB.QueryRow(context.Background(), sql, args...).Scan(&jokeID)
|
|
|
|
if err != nil && err != pgx.ErrNoRows {
|
2021-08-03 18:14:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if jokeID == id {
|
2021-09-26 19:13:38 +00:00
|
|
|
body := new(core.Joke)
|
2021-08-03 18:14:32 +00:00
|
|
|
err = c.BodyParser(&body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check link validity
|
2021-09-26 19:13:38 +00:00
|
|
|
valid, err := core.CheckImageValidity(d.HTTP, body.Link)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
2021-08-04 10:14:33 +00:00
|
|
|
return c.
|
|
|
|
Status(fiber.StatusBadRequest).
|
2021-09-26 19:13:38 +00:00
|
|
|
JSON(Error{
|
2021-08-04 10:14:33 +00:00
|
|
|
Error: "URL provided is not a valid image",
|
|
|
|
})
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
sql, args, err = d.Query.
|
2021-08-04 10:14:33 +00:00
|
|
|
Update("jokesbapak2").
|
|
|
|
Set("link", body.Link).
|
|
|
|
Set("creator", c.Locals("userID")).
|
|
|
|
ToSql()
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
r, err := d.DB.Query(context.Background(), sql, args...)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
defer r.Close()
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
err = core.SetAllJSONJoke(d.DB, d.Memory)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-26 19:13:38 +00:00
|
|
|
err = core.SetTotalJoke(d.DB, d.Memory)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-04 10:14:33 +00:00
|
|
|
return c.
|
|
|
|
Status(fiber.StatusOK).
|
2021-09-26 19:13:38 +00:00
|
|
|
JSON(ResponseJoke{
|
2021-08-04 10:14:33 +00:00
|
|
|
Message: "specified joke id has been updated",
|
|
|
|
Link: body.Link,
|
|
|
|
})
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 10:14:33 +00:00
|
|
|
return c.
|
|
|
|
Status(fiber.StatusNotAcceptable).
|
2021-09-26 19:13:38 +00:00
|
|
|
JSON(Error{
|
2021-08-04 10:14:33 +00:00
|
|
|
Error: "specified joke id does not exists",
|
|
|
|
})
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|