Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(styles): implement ban functionality
- Loading branch information
Showing
5 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package style | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
|
||
"userstyles.world/handlers/jwt" | ||
"userstyles.world/models" | ||
"userstyles.world/modules/database" | ||
"userstyles.world/search" | ||
) | ||
|
||
func BanGet(c *fiber.Ctx) error { | ||
u, _ := jwt.User(c) | ||
|
||
// Check if logged-in user has permissions. | ||
if !u.IsModOrAdmin() { | ||
c.Status(fiber.StatusUnauthorized) | ||
return c.Render("err", fiber.Map{ | ||
"Title": "Can't do that", | ||
"User": u, | ||
}) | ||
} | ||
|
||
// Check if style exists. | ||
s, err := models.GetStyleByID(c.Params("id")) | ||
if err != nil { | ||
c.Status(fiber.StatusNotFound) | ||
return c.Render("err", fiber.Map{ | ||
"Title": "Style not found", | ||
"User": u, | ||
}) | ||
} | ||
|
||
return c.Render("style/ban", fiber.Map{ | ||
"Title": "Confirm ban", | ||
"User": u, | ||
"Style": s, | ||
}) | ||
} | ||
|
||
func BanPost(c *fiber.Ctx) error { | ||
u, _ := jwt.User(c) | ||
id := c.Params("id") | ||
|
||
// Check if logged-in user has permissions. | ||
if !u.IsModOrAdmin() { | ||
c.Status(fiber.StatusUnauthorized) | ||
return c.Render("err", fiber.Map{ | ||
"Title": "Can't do that", | ||
"User": u, | ||
}) | ||
} | ||
|
||
// Check if style exists. | ||
s, err := models.GetStyleByID(id) | ||
if err != nil { | ||
c.Status(fiber.StatusNotFound) | ||
return c.Render("err", fiber.Map{ | ||
"Title": "Style not found", | ||
"User": u, | ||
}) | ||
} | ||
|
||
// Delete from database. | ||
q := new(models.Style) | ||
if err = database.Conn.Delete(q, "styles.id = ?", id).Error; err != nil { | ||
log.Printf("Failed to delete style, err: %#+v\n", err) | ||
c.Status(fiber.StatusInternalServerError) | ||
return c.Render("err", fiber.Map{ | ||
"Title": "Internal server error", | ||
"User": u, | ||
}) | ||
} | ||
|
||
// Delete from search index. | ||
if err = search.DeleteStyle(s.ID); err != nil { | ||
log.Printf("Couldn't delete style %d failed, err: %s", s.ID, err.Error()) | ||
} | ||
|
||
return c.Redirect("/account", fiber.StatusSeeOther) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<svg xmlns="http://www.w3.org/2000/svg" width="{{ . }}" height="{{ . }}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> | ||
<circle cx="12" cy="12" r="10"></circle> | ||
<line x1="4.93" y1="4.93" x2="19.07" y2="19.07"></line> | ||
</svg> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="wrapper py:l"> | ||
<section class="ta:c"> | ||
<h1>{{ .Title }}</h1> | ||
<p>This action is irreversible.</p> | ||
</section> | ||
<section class="limit mt:l mx:a"> | ||
<form class="form-wrapper" method="post" action="/styles/ban/{{ .Style.ID }}"> | ||
<label class="mb:m">Are you sure you want to ban "{{ .Style.Name }}"?</label> | ||
<div> | ||
<button class="btn primary mr:s" type="submit">Confirm</button> | ||
<a class="fg:1" href="/style/{{ .Style.ID }}">Cancel</a> | ||
</div> | ||
</form> | ||
</section> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters