summaryrefslogtreecommitdiff
path: root/shrine/controllers/warning.go
blob: 90ed526bb265f7862b06d0d47b43b4b66b63d7fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package controllers

import (
	"shrine/services"
	"shrine/types/warning"
	"shrine/utils/auth"
	"shrine/utils/meta"
	"shrine/utils/shortcuts"

	"github.com/gofiber/fiber/v2"
)

func WarnUserController(context *fiber.Ctx) error {
	admin := auth.GetUser(context)
	target, serviceErr := services.ResolveUser(meta.Request(context).MustHave().Param("username"))
	if serviceErr != nil {
		return shortcuts.HandleError(context, serviceErr)
	}

	body, err := meta.Body[warning.WarnRequest](context)
	if err != nil {
		return shortcuts.BadRequest(context, err)
	}

	result, serviceErr := services.WarnUser(admin, target, body)
	if serviceErr != nil {
		return shortcuts.HandleError(context, serviceErr)
	}

	return shortcuts.Created(context, result)
}

func DeactivateWarningController(context *fiber.Ctx) error {
	admin := auth.GetUser(context)
	ref := context.Params("ref")

	result, serviceErr := services.DeactivateWarning(admin, ref)
	if serviceErr != nil {
		return shortcuts.HandleError(context, serviceErr)
	}

	return shortcuts.Success(context, result)
}

func ListWarningsController(context *fiber.Ctx) error {
	username := meta.Request(context).MustHave().Param("username")
	pagination := meta.Paginate(context)

	items, total, serviceErr := services.ListWarnings(username, pagination)
	if serviceErr != nil {
		return shortcuts.HandleError(context, serviceErr)
	}

	return shortcuts.Success(context, pagination.Response(items, total))
}