style: chained function line breaks

This commit is contained in:
Reinaldy Rafli 2021-08-04 17:14:33 +07:00
parent e38f36c579
commit b6788b33c6
7 changed files with 102 additions and 46 deletions

View File

@ -24,12 +24,18 @@ func AddNewJoke(c *fiber.Ctx) error {
}
if !valid {
return c.Status(fiber.StatusBadRequest).JSON(models.Error{
return c.
Status(fiber.StatusBadRequest).
JSON(models.Error{
Error: "URL provided is not a valid image",
})
}
sql, args, err := handler.Psql.Insert("jokesbapak2").Columns("link", "creator").Values(body.Link, c.Locals("userID")).ToSql()
sql, args, err := handler.Psql.
Insert("jokesbapak2").
Columns("link", "creator").
Values(body.Link, c.Locals("userID")).
ToSql()
if err != nil {
return err
}
@ -51,7 +57,9 @@ func AddNewJoke(c *fiber.Ctx) error {
return err
}
return c.Status(fiber.StatusCreated).JSON(models.ResponseJoke{
return c.
Status(fiber.StatusCreated).
JSON(models.ResponseJoke{
Link: body.Link,
})
}

View File

@ -19,7 +19,11 @@ func DeleteJoke(c *fiber.Ctx) error {
}
// Check if the joke exists
sql, args, err := handler.Psql.Select("id").From("jokesbapak2").Where(squirrel.Eq{"id": id}).ToSql()
sql, args, err := handler.Psql.
Select("id").
From("jokesbapak2").
Where(squirrel.Eq{"id": id}).
ToSql()
if err != nil {
return err
}
@ -31,7 +35,10 @@ func DeleteJoke(c *fiber.Ctx) error {
}
if jokeID == id {
sql, args, err = handler.Psql.Delete("jokesbapak2").Where(squirrel.Eq{"id": id}).ToSql()
sql, args, err = handler.Psql.
Delete("jokesbapak2").
Where(squirrel.Eq{"id": id}).
ToSql()
if err != nil {
return err
}
@ -52,11 +59,15 @@ func DeleteJoke(c *fiber.Ctx) error {
return err
}
return c.Status(fiber.StatusOK).JSON(models.ResponseJoke{
return c.
Status(fiber.StatusOK).
JSON(models.ResponseJoke{
Message: "specified joke id has been deleted",
})
}
return c.Status(fiber.StatusNotAcceptable).JSON(models.Error{
return c.
Status(fiber.StatusNotAcceptable).
JSON(models.Error{
Error: "specified joke id does not exists",
})
}

View File

@ -132,7 +132,9 @@ func JokeByID(c *fiber.Ctx) error {
}
if link == "" {
return c.Status(fiber.StatusNotFound).Send([]byte("Requested ID was not found."))
return c.
Status(fiber.StatusNotFound).
Send([]byte("Requested ID was not found."))
}
// Get image data

View File

@ -26,14 +26,18 @@ func TotalJokes(c *fiber.Ctx) error {
if err != nil {
if err.Error() == "Entry not found" {
return c.Status(fiber.StatusInternalServerError).JSON(models.Error{
return c.
Status(fiber.StatusInternalServerError).
JSON(models.Error{
Error: "no data found",
})
}
return err
}
return c.Status(fiber.StatusOK).JSON(models.ResponseJoke{
return c.
Status(fiber.StatusOK).
JSON(models.ResponseJoke{
Message: strconv.Itoa(int(total[0])),
})
}

View File

@ -14,7 +14,11 @@ import (
func UpdateJoke(c *fiber.Ctx) error {
id := c.Params("id")
// Check if the joke exists
sql, args, err := handler.Psql.Select("id").From("jokesbapak2").Where(squirrel.Eq{"id": id}).ToSql()
sql, args, err := handler.Psql.
Select("id").
From("jokesbapak2").
Where(squirrel.Eq{"id": id}).
ToSql()
if err != nil {
return err
}
@ -39,12 +43,18 @@ func UpdateJoke(c *fiber.Ctx) error {
}
if !valid {
return c.Status(fiber.StatusBadRequest).JSON(models.Error{
return c.
Status(fiber.StatusBadRequest).
JSON(models.Error{
Error: "URL provided is not a valid image",
})
}
sql, args, err = handler.Psql.Update("jokesbapak2").Set("link", body.Link).Set("creator", c.Locals("userID")).ToSql()
sql, args, err = handler.Psql.
Update("jokesbapak2").
Set("link", body.Link).
Set("creator", c.Locals("userID")).
ToSql()
if err != nil {
return err
}
@ -65,13 +75,17 @@ func UpdateJoke(c *fiber.Ctx) error {
return err
}
return c.Status(fiber.StatusOK).JSON(models.ResponseJoke{
return c.
Status(fiber.StatusOK).
JSON(models.ResponseJoke{
Message: "specified joke id has been updated",
Link: body.Link,
})
}
return c.Status(fiber.StatusNotAcceptable).JSON(models.Error{
return c.
Status(fiber.StatusNotAcceptable).
JSON(models.Error{
Error: "specified joke id does not exists",
})
}

View File

@ -24,7 +24,11 @@ func RequireAuth() fiber.Handler {
}
// Check if key exists
sql, args, err := psql.Select("token").From("administrators").Where(squirrel.Eq{"key": auth.Key}).ToSql()
sql, args, err := psql.
Select("token").
From("administrators").
Where(squirrel.Eq{"key": auth.Key}).
ToSql()
if err != nil {
return err
}
@ -33,7 +37,9 @@ func RequireAuth() fiber.Handler {
err = db.QueryRow(context.Background(), sql, args...).Scan(&token)
if err != nil {
if err.Error() == "no rows in result set" {
return c.Status(fiber.StatusForbidden).JSON(models.Error{
return c.
Status(fiber.StatusForbidden).
JSON(models.Error{
Error: "Invalid key",
})
}
@ -51,7 +57,10 @@ func RequireAuth() fiber.Handler {
}
if verify {
sql, args, err = psql.Update("administrators").Set("last_used", time.Now().UTC().Format(time.RFC3339)).ToSql()
sql, args, err = psql.
Update("administrators").
Set("last_used", time.Now().UTC().Format(time.RFC3339)).
ToSql()
if err != nil {
return err
}
@ -61,7 +70,11 @@ func RequireAuth() fiber.Handler {
return err
}
sql, args, err = psql.Select("id").From("administrators").Where(squirrel.Eq{"key": auth.Key}).ToSql()
sql, args, err = psql.
Select("id").
From("administrators").
Where(squirrel.Eq{"key": auth.Key}).
ToSql()
if err != nil {
return err
}
@ -75,7 +88,9 @@ func RequireAuth() fiber.Handler {
return c.Next()
}
return c.Status(fiber.StatusForbidden).JSON(models.Error{
return c.
Status(fiber.StatusForbidden).
JSON(models.Error{
Error: "Invalid key",
})
}

View File

@ -19,7 +19,9 @@ func OnlyIntegerAsID() fiber.Handler {
return c.Next()
}
return c.Status(fiber.StatusBadRequest).JSON(models.Error{
return c.
Status(fiber.StatusBadRequest).
JSON(models.Error{
Error: "only numbers are allowed as ID",
})
}