feat: sessions endpoints
This commit is contained in:
89
internal/admin/sessions.go
Normal file
89
internal/admin/sessions.go
Normal file
@ -0,0 +1,89 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"gitea.local/admin/hspguard/internal/repository"
|
||||
"gitea.local/admin/hspguard/internal/types"
|
||||
"gitea.local/admin/hspguard/internal/web"
|
||||
)
|
||||
|
||||
type GetSessionsParams struct {
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
// TODO: More filtering possibilities like onlyActive, expired, not-expired etc.
|
||||
}
|
||||
|
||||
func (h *AdminHandler) GetUserSessions(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
|
||||
params := GetSessionsParams{}
|
||||
|
||||
if limit, err := strconv.Atoi(q.Get("limit")); err == nil {
|
||||
params.Limit = int32(limit)
|
||||
}
|
||||
|
||||
if offset, err := strconv.Atoi(q.Get("offset")); err == nil {
|
||||
params.Offset = int32(offset)
|
||||
}
|
||||
|
||||
sessions, err := h.repo.GetUserSessions(r.Context(), repository.GetUserSessionsParams{
|
||||
Limit: params.Limit,
|
||||
Offset: params.Offset,
|
||||
})
|
||||
if err != nil {
|
||||
log.Println("ERR: Failed to read user sessions from db:", err)
|
||||
web.Error(w, "failed to retrieve sessions", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var mapped []*types.UserSessionDTO
|
||||
|
||||
for _, session := range sessions {
|
||||
mapped = append(mapped, types.NewUserSessionDTO(&session))
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(mapped); err != nil {
|
||||
log.Println("ERR: Failed to encode sessions in response:", err)
|
||||
web.Error(w, "failed to encode sessions", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AdminHandler) GetServiceSessions(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
|
||||
params := GetSessionsParams{}
|
||||
|
||||
if limit, err := strconv.Atoi(q.Get("limit")); err == nil {
|
||||
params.Limit = int32(limit)
|
||||
}
|
||||
|
||||
if offset, err := strconv.Atoi(q.Get("offset")); err == nil {
|
||||
params.Offset = int32(offset)
|
||||
}
|
||||
|
||||
sessions, err := h.repo.GetServiceSessions(r.Context(), repository.GetServiceSessionsParams{
|
||||
Limit: params.Limit,
|
||||
Offset: params.Offset,
|
||||
})
|
||||
if err != nil {
|
||||
log.Println("ERR: Failed to read api sessions from db:", err)
|
||||
web.Error(w, "failed to retrieve sessions", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var mapped []*types.ServiceSessionDTO
|
||||
|
||||
for _, session := range sessions {
|
||||
mapped = append(mapped, types.NewServiceSessionDTO(&session))
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(sessions); err != nil {
|
||||
log.Println("ERR: Failed to encode sessions in response:", err)
|
||||
web.Error(w, "failed to encode sessions", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
29
internal/types/session.go
Normal file
29
internal/types/session.go
Normal file
@ -0,0 +1,29 @@
|
||||
package types
|
||||
|
||||
import "gitea.local/admin/hspguard/internal/repository"
|
||||
|
||||
type ServiceSessionDTO struct {
|
||||
User UserDTO `json:"user"`
|
||||
ApiService ApiServiceDTO `json:"api_service"`
|
||||
repository.ServiceSession
|
||||
}
|
||||
|
||||
func NewServiceSessionDTO(row *repository.GetServiceSessionsRow) *ServiceSessionDTO {
|
||||
return &ServiceSessionDTO{
|
||||
User: NewUserDTO(&row.User),
|
||||
ApiService: NewApiServiceDTO(row.ApiService),
|
||||
ServiceSession: row.ServiceSession,
|
||||
}
|
||||
}
|
||||
|
||||
type UserSessionDTO struct {
|
||||
User UserDTO `json:"user"`
|
||||
repository.UserSession
|
||||
}
|
||||
|
||||
func NewUserSessionDTO(row *repository.GetUserSessionsRow) *UserSessionDTO {
|
||||
return &UserSessionDTO{
|
||||
User: NewUserDTO(&row.User),
|
||||
UserSession: row.UserSession,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user