summaryrefslogtreecommitdiff
path: root/shrine/repositories/ipban.go
blob: 9c89dc7cc9a8d2051f432687c68ee8848a2bced8 (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
package repositories

import (
	"shrine/database"
	"shrine/models"
	"shrine/utils/meta"
)

func CreateIPBan(ip string, reason string) error {
	return database.DB.Create(&models.IPBan{
		IP:     ip,
		Reason: reason,
	}).Error
}

func DeleteIPBan(ip string) {
	database.DB.Where("ip = ?", ip).Delete(&models.IPBan{})
}

func IsIPBanned(ip string) bool {
	var count int64
	database.DB.Model(&models.IPBan{}).Where("ip = ?", ip).Count(&count)
	return count > 0
}

func ListIPBans(pagination meta.Pagination, sorting meta.Sorting) ([]models.IPBan, int64) {
	var ipBans []models.IPBan
	var total int64

	query := database.DB.Model(&models.IPBan{})
	query.Count(&total)
	pagination.Apply(sorting.Apply(query)).Find(&ipBans)

	return ipBans, total
}

func DeleteIPBanByID(id uint) {
	database.DB.Delete(&models.IPBan{}, id)
}