Skip to content

Commit

Permalink
feat: delete style
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusted committed May 25, 2021
1 parent 621836c commit c3ff6d7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/endpoints.MD
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ Example response
```JSON
Status: 200
{
"info": "Succesful removed the style!"
"data": "Succesful removed the style!"
}
```
or
```JSON
Status: 403
{
"err": "This style doesn't belong to you! ╰༼⇀︿⇀༽つ-]═──"
"data": "This style doesn't belong to you! ╰༼⇀︿⇀༽つ-]═──"
}
```

Expand Down
46 changes: 43 additions & 3 deletions handlers/api/styles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
"fmt"

"github.com/gofiber/fiber/v2"
"github.com/ohler55/ojg/oj"
"userstyles.world/database"
Expand Down Expand Up @@ -32,7 +34,7 @@ func StylesGet(c *fiber.Ctx) error {

}

var p = &oj.Parser{Reuse: true}
var JsonParser = &oj.Parser{Reuse: true}

func StylePost(c *fiber.Ctx) error {
u, _ := APIUser(c)
Expand All @@ -44,6 +46,7 @@ func StylePost(c *fiber.Ctx) error {
"data": "You need the \"style\" scope to do this.",
})
}

style, err := models.GetStyleByID(database.DB, styleID)
if err != nil {
return c.Status(500).
Expand All @@ -54,11 +57,11 @@ func StylePost(c *fiber.Ctx) error {
if style.UserID != u.ID {
return c.Status(403).
JSON(fiber.Map{
"data": "This style doesn't belong to this user.",
"data": "This style doesn't belong to you! ╰༼⇀︿⇀༽つ-]═──",
})
}
var postStyle models.Style
err = p.Unmarshal(c.Body(), &postStyle)
err = JsonParser.Unmarshal(c.Body(), &postStyle)
if err != nil {
return c.Status(500).
JSON(fiber.Map{
Expand All @@ -84,3 +87,40 @@ func StylePost(c *fiber.Ctx) error {
})

}

func DeleteStyle(c *fiber.Ctx) error {
u, _ := APIUser(c)
styleID := c.Params("id")

style, err := models.GetStyleByID(database.DB, styleID)
if err != nil {
return c.Status(500).
JSON(fiber.Map{
"data": "Error: Couldn't find style with ID.",
})
}
if style.UserID != u.ID {
return c.Status(403).
JSON(fiber.Map{
"data": "This style doesn't belong to you! ╰༼⇀︿⇀༽つ-]═──",
})
}

styleModel := new(models.Style)
err = database.DB.
Debug().
Delete(styleModel, "styles.id = ?", styleID).
Error

if err != nil {
fmt.Printf("Failed to delete style, err: %#+v\n", err)
return c.Status(500).
JSON(fiber.Map{
"data": "Error: Couldn't delete style",
})
}

return c.JSON(fiber.Map{
"data": "Succesful removed the style!",
})
}
1 change: 1 addition & 0 deletions handlers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func Initialize() {
v1.Get("/user/:identifier", api.SpecificUserGet)
v1.Get("/styles", api.ProtectedAPI, api.StylesGet)
v1.Post("/style/:id", api.ProtectedAPI, api.StylePost)
v1.Delete("/style/:id", api.ProtectedAPI, api.DeleteStyle)

oauthV1 := app.Group("/oauth")
oauthV1.Get("/authorize", oauth_provider.AuthorizeGet)
Expand Down

0 comments on commit c3ff6d7

Please sign in to comment.