summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/shared/quota.go
blob: b892df4b2f60957c3b3ef20c71ebd65ae7501c58 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package shared

import (
	"net/http"

	quota_model "code.gitea.io/gitea/models/quota"
	"code.gitea.io/gitea/routers/api/v1/utils"
	"code.gitea.io/gitea/services/context"
	"code.gitea.io/gitea/services/convert"
)

func GetQuota(ctx *context.APIContext, userID int64) {
	used, err := quota_model.GetUsedForUser(ctx, userID)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "quota_model.GetUsedForUser", err)
		return
	}

	groups, err := quota_model.GetGroupsForUser(ctx, userID)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "quota_model.GetGroupsForUser", err)
		return
	}

	result := convert.ToQuotaInfo(used, groups, false)
	ctx.JSON(http.StatusOK, &result)
}

func CheckQuota(ctx *context.APIContext, userID int64) {
	subjectQuery := ctx.FormTrim("subject")

	subject, err := quota_model.ParseLimitSubject(subjectQuery)
	if err != nil {
		ctx.Error(http.StatusUnprocessableEntity, "quota_model.ParseLimitSubject", err)
		return
	}

	ok, err := quota_model.EvaluateForUser(ctx, userID, subject)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "quota_model.EvaluateForUser", err)
		return
	}

	ctx.JSON(http.StatusOK, &ok)
}

func ListQuotaAttachments(ctx *context.APIContext, userID int64) {
	opts := utils.GetListOptions(ctx)
	count, attachments, err := quota_model.GetQuotaAttachmentsForUser(ctx, userID, opts)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "GetQuotaAttachmentsForUser", err)
		return
	}

	result, err := convert.ToQuotaUsedAttachmentList(ctx, *attachments)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "convert.ToQuotaUsedAttachmentList", err)
	}

	ctx.SetLinkHeader(int(count), opts.PageSize)
	ctx.SetTotalCountHeader(count)
	ctx.JSON(http.StatusOK, result)
}

func ListQuotaPackages(ctx *context.APIContext, userID int64) {
	opts := utils.GetListOptions(ctx)
	count, packages, err := quota_model.GetQuotaPackagesForUser(ctx, userID, opts)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "GetQuotaPackagesForUser", err)
		return
	}

	result, err := convert.ToQuotaUsedPackageList(ctx, *packages)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "convert.ToQuotaUsedPackageList", err)
	}

	ctx.SetLinkHeader(int(count), opts.PageSize)
	ctx.SetTotalCountHeader(count)
	ctx.JSON(http.StatusOK, result)
}

func ListQuotaArtifacts(ctx *context.APIContext, userID int64) {
	opts := utils.GetListOptions(ctx)
	count, artifacts, err := quota_model.GetQuotaArtifactsForUser(ctx, userID, opts)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "GetQuotaArtifactsForUser", err)
		return
	}

	result, err := convert.ToQuotaUsedArtifactList(ctx, *artifacts)
	if err != nil {
		ctx.Error(http.StatusInternalServerError, "convert.ToQuotaUsedArtifactList", err)
	}

	ctx.SetLinkHeader(int(count), opts.PageSize)
	ctx.SetTotalCountHeader(count)
	ctx.JSON(http.StatusOK, result)
}