feat: move api services dto into separate types file

This commit is contained in:
2025-06-15 18:08:34 +02:00
parent c7e88606e3
commit 213991126d
2 changed files with 48 additions and 40 deletions

View File

@ -4,45 +4,15 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
"time"
"gitea.local/admin/hspguard/internal/repository" "gitea.local/admin/hspguard/internal/repository"
"gitea.local/admin/hspguard/internal/types"
"gitea.local/admin/hspguard/internal/util" "gitea.local/admin/hspguard/internal/util"
"gitea.local/admin/hspguard/internal/web" "gitea.local/admin/hspguard/internal/web"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/google/uuid" "github.com/google/uuid"
) )
type ApiServiceDTO struct {
ID uuid.UUID `json:"id"`
ClientID string `json:"client_id"`
Name string `json:"name"`
Description *string `json:"description"`
IconUrl *string `json:"icon_url"`
RedirectUris []string `json:"redirect_uris"`
Scopes []string `json:"scopes"`
GrantTypes []string `json:"grant_types"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
IsActive bool `json:"is_active"`
}
func NewApiServiceDTO(service repository.ApiService) ApiServiceDTO {
return ApiServiceDTO{
ID: service.ID,
ClientID: service.ClientID,
Name: service.Name,
Description: service.Description,
IconUrl: service.IconUrl,
RedirectUris: service.RedirectUris,
Scopes: service.Scopes,
GrantTypes: service.GrantTypes,
CreatedAt: service.CreatedAt,
UpdatedAt: service.UpdatedAt,
IsActive: service.IsActive,
}
}
func (h *AdminHandler) GetApiServices(w http.ResponseWriter, r *http.Request) { func (h *AdminHandler) GetApiServices(w http.ResponseWriter, r *http.Request) {
services, err := h.repo.ListApiServices(r.Context()) services, err := h.repo.ListApiServices(r.Context())
if err != nil { if err != nil {
@ -51,15 +21,15 @@ func (h *AdminHandler) GetApiServices(w http.ResponseWriter, r *http.Request) {
return return
} }
apiServices := make([]ApiServiceDTO, 0) apiServices := make([]types.ApiServiceDTO, 0)
for _, service := range services { for _, service := range services {
apiServices = append(apiServices, NewApiServiceDTO(service)) apiServices = append(apiServices, types.NewApiServiceDTO(service))
} }
type Response struct { type Response struct {
Items []ApiServiceDTO `json:"items"` Items []types.ApiServiceDTO `json:"items"`
Count int `json:"count"` Count int `json:"count"`
} }
encoder := json.NewEncoder(w) encoder := json.NewEncoder(w)
@ -146,7 +116,7 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) {
service.ClientSecret = clientSecret service.ClientSecret = clientSecret
type Response struct { type Response struct {
Service ApiServiceDTO `json:"service"` Service types.ApiServiceDTO `json:"service"`
Credentials ApiServiceCredentials `json:"credentials"` Credentials ApiServiceCredentials `json:"credentials"`
} }
@ -155,7 +125,7 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := encoder.Encode(Response{ if err := encoder.Encode(Response{
Service: NewApiServiceDTO(service), Service: types.NewApiServiceDTO(service),
Credentials: ApiServiceCredentials{ Credentials: ApiServiceCredentials{
ClientId: service.ClientID, ClientId: service.ClientID,
ClientSecret: service.ClientSecret, ClientSecret: service.ClientSecret,
@ -183,7 +153,7 @@ func (h *AdminHandler) GetApiService(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := encoder.Encode(NewApiServiceDTO(service)); err != nil { if err := encoder.Encode(types.NewApiServiceDTO(service)); err != nil {
web.Error(w, "failed to encode response", http.StatusInternalServerError) web.Error(w, "failed to encode response", http.StatusInternalServerError)
} }
} }
@ -201,7 +171,7 @@ func (h *AdminHandler) GetApiServiceCID(w http.ResponseWriter, r *http.Request)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := encoder.Encode(NewApiServiceDTO(service)); err != nil { if err := encoder.Encode(types.NewApiServiceDTO(service)); err != nil {
web.Error(w, "failed to encode response", http.StatusInternalServerError) web.Error(w, "failed to encode response", http.StatusInternalServerError)
} }
} }
@ -303,7 +273,7 @@ func (h *AdminHandler) UpdateApiService(w http.ResponseWriter, r *http.Request)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := encoder.Encode(NewApiServiceDTO(updated)); err != nil { if err := encoder.Encode(types.NewApiServiceDTO(updated)); err != nil {
web.Error(w, "failed to send updated api service", http.StatusInternalServerError) web.Error(w, "failed to send updated api service", http.StatusInternalServerError)
} }
} }

View File

@ -0,0 +1,38 @@
package types
import (
"time"
"gitea.local/admin/hspguard/internal/repository"
"github.com/google/uuid"
)
type ApiServiceDTO struct {
ID uuid.UUID `json:"id"`
ClientID string `json:"client_id"`
Name string `json:"name"`
Description *string `json:"description"`
IconUrl *string `json:"icon_url"`
RedirectUris []string `json:"redirect_uris"`
Scopes []string `json:"scopes"`
GrantTypes []string `json:"grant_types"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
IsActive bool `json:"is_active"`
}
func NewApiServiceDTO(service repository.ApiService) ApiServiceDTO {
return ApiServiceDTO{
ID: service.ID,
ClientID: service.ClientID,
Name: service.Name,
Description: service.Description,
IconUrl: service.IconUrl,
RedirectUris: service.RedirectUris,
Scopes: service.Scopes,
GrantTypes: service.GrantTypes,
CreatedAt: service.CreatedAt,
UpdatedAt: service.UpdatedAt,
IsActive: service.IsActive,
}
}