invite: add /count/used route
counts the number of active invites which have been used.
This commit is contained in:
@@ -275,6 +275,30 @@ func (app *appContext) GetInviteCount(gc *gin.Context) {
|
|||||||
gc.JSON(200, resp)
|
gc.JSON(200, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @Summary Get the number of invites stored in the database that have been used (but are still valid).
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} PageCountDTO
|
||||||
|
// @Router /invites/count [get]
|
||||||
|
// @Security Bearer
|
||||||
|
// @tags Invites
|
||||||
|
func (app *appContext) GetInviteUsedCount(gc *gin.Context) {
|
||||||
|
resp := PageCountDTO{}
|
||||||
|
var err error
|
||||||
|
resp.Count, err = app.storage.db.Count(&Invite{}, badgerhold.Where("usedBy").MatchFunc(func(ra *badgerhold.RecordAccess) (bool, error) {
|
||||||
|
field := ra.Field()
|
||||||
|
switch usedBy := field.(type) {
|
||||||
|
case [][]string:
|
||||||
|
return len(usedBy) > 0, nil
|
||||||
|
default:
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
if err != nil {
|
||||||
|
resp.Count = 0
|
||||||
|
}
|
||||||
|
gc.JSON(200, resp)
|
||||||
|
}
|
||||||
|
|
||||||
// @Summary Get invites.
|
// @Summary Get invites.
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Success 200 {object} getInvitesDTO
|
// @Success 200 {object} getInvitesDTO
|
||||||
|
|||||||
@@ -201,6 +201,7 @@ func (app *appContext) loadRoutes(router *gin.Engine) {
|
|||||||
api.POST(p+"/invites", app.GenerateInvite)
|
api.POST(p+"/invites", app.GenerateInvite)
|
||||||
api.GET(p+"/invites", app.GetInvites)
|
api.GET(p+"/invites", app.GetInvites)
|
||||||
api.GET(p+"/invites/count", app.GetInviteCount)
|
api.GET(p+"/invites/count", app.GetInviteCount)
|
||||||
|
api.GET(p+"/invites/count/used", app.GetInviteUsedCount)
|
||||||
api.DELETE(p+"/invites", app.DeleteInvite)
|
api.DELETE(p+"/invites", app.DeleteInvite)
|
||||||
api.POST(p+"/invites/profile", app.SetProfile)
|
api.POST(p+"/invites/profile", app.SetProfile)
|
||||||
api.GET(p+"/profiles", app.GetProfiles)
|
api.GET(p+"/profiles", app.GetProfiles)
|
||||||
|
|||||||
Reference in New Issue
Block a user