2021-08-03 18:14:32 +00:00
|
|
|
package joke
|
|
|
|
|
|
|
|
import (
|
2021-09-27 10:10:19 +00:00
|
|
|
"jokes-bapak2-api/app/core"
|
2021-09-28 04:29:39 +00:00
|
|
|
"strconv"
|
2021-08-03 18:14:32 +00:00
|
|
|
|
|
|
|
"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-09-27 10:10:19 +00:00
|
|
|
conn, err := d.DB.Acquire(*d.Context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-28 04:29:39 +00:00
|
|
|
var jokeID int
|
2021-09-27 10:10:19 +00:00
|
|
|
err = conn.QueryRow(*d.Context, sql, args...).Scan(&jokeID)
|
2021-09-26 19:13:38 +00:00
|
|
|
if err != nil && err != pgx.ErrNoRows {
|
2021-08-03 18:14:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-28 04:29:39 +00:00
|
|
|
if strconv.Itoa(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-27 10:10:19 +00:00
|
|
|
r, err := conn.Query(*d.Context, 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-27 10:10:19 +00:00
|
|
|
err = core.SetAllJSONJoke(d.DB, d.Memory, d.Context)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-27 10:10:19 +00:00
|
|
|
err = core.SetTotalJoke(d.DB, d.Memory, d.Context)
|
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
|
|
|
}
|