feat: added pagination to GET /user/invites

This commit is contained in:
2026-09-01 10:18:08 +02:00
parent 02931d16be
commit 1695a57dbd
5 changed files with 104 additions and 9 deletions
+62 -3
View File
@@ -3,6 +3,7 @@ package handler
import (
"log/slog"
"net/http"
"strconv"
"github.com/robindittmar/dttmr-api/internal/api/response"
"github.com/robindittmar/dttmr-api/internal/domain"
@@ -94,12 +95,59 @@ func (h *InviteHandler) DeleteInvite(w http.ResponseWriter, r *http.Request) {
// @Tags Invite
// @Accept json
// @Produce json
// @Success 200 {object} []domain.Invite
// @Param page query int false "page"
// @Param count query int false "count"
// @Success 200 {object} response.Paginated[domain.Invite]
// @Error 400 {object} response.ErrorResponse "failed to decode request body"
// @Error 400 {object} response.ErrorResponse "invalid value for page"
// @Error 400 {object} response.ErrorResponse "invalid value for count"
// @Error 500 {object} response.ErrorResponse "failed to get invites"
// @Router /user/invites [get]
func (h *InviteHandler) GetInvites(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pageStr := r.URL.Query().Get("page")
if pageStr == "" {
pageStr = "1"
}
countStr := r.URL.Query().Get("count")
if countStr == "" {
countStr = "10"
}
page, err := strconv.Atoi(pageStr)
if err != nil {
slog.ErrorContext(ctx,
"failed to read page from query",
slog.String("page", pageStr))
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request url")
return
}
count, err := strconv.Atoi(countStr)
if err != nil {
slog.ErrorContext(ctx,
"failed to read count from query",
slog.String("count", countStr))
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request url")
return
}
if page < 1 {
slog.ErrorContext(ctx,
"page parameter is invalid",
slog.Int("page", page))
response.Error(ctx, w, http.StatusBadRequest, "invalid value for page")
return
}
if count <= 0 {
slog.ErrorContext(ctx,
"count parameter is invalid",
slog.Int("count", count))
response.Error(ctx, w, http.StatusBadRequest, "invalid value for count")
return
}
authContext, err := domain.GetAuthContext(ctx)
if err != nil {
slog.ErrorContext(ctx, "failed to get auth context", slog.Any("error", err))
@@ -107,12 +155,23 @@ func (h *InviteHandler) GetInvites(w http.ResponseWriter, r *http.Request) {
return
}
invites, err := h.InviteService.GetInvites(ctx, authContext.UserID)
invites, err := h.InviteService.GetInvites(ctx, authContext.UserID, page, count)
if err != nil {
slog.ErrorContext(ctx, "failed to get invites", slog.Any("error", err))
response.Error(ctx, w, http.StatusInternalServerError, "failed to get invites")
return
}
response.JSON(ctx, w, http.StatusOK, invites)
total, err := h.InviteService.CountInvites(ctx, authContext.UserID)
if err != nil {
slog.ErrorContext(ctx, "failed to count invites", slog.Any("error", err))
response.Error(ctx, w, http.StatusInternalServerError, "failed to get invites")
return
}
response.JSON(ctx, w, http.StatusOK, response.Paginated[domain.Invite]{
Count: len(invites),
Total: total,
Data: invites,
})
}
+6
View File
@@ -0,0 +1,6 @@
package request
type GetInvitesPayload struct {
Page int
CountPerPage int
}
+7
View File
@@ -0,0 +1,7 @@
package response
type Paginated[T any] struct {
Count int `json:"count"`
Total int `json:"total"`
Data []T `json:"data"`
}