From 213991126d14fab89def38e726553ae737b4baaa Mon Sep 17 00:00:00 2001 From: LandaMm Date: Sun, 15 Jun 2025 18:08:34 +0200 Subject: [PATCH] feat: move api services dto into separate types file --- internal/admin/apiservices.go | 50 +++++++---------------------------- internal/types/apiservices.go | 38 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 internal/types/apiservices.go diff --git a/internal/admin/apiservices.go b/internal/admin/apiservices.go index 4529d6b..5b38391 100644 --- a/internal/admin/apiservices.go +++ b/internal/admin/apiservices.go @@ -4,45 +4,15 @@ import ( "encoding/json" "log" "net/http" - "time" "gitea.local/admin/hspguard/internal/repository" + "gitea.local/admin/hspguard/internal/types" "gitea.local/admin/hspguard/internal/util" "gitea.local/admin/hspguard/internal/web" "github.com/go-chi/chi/v5" "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) { services, err := h.repo.ListApiServices(r.Context()) if err != nil { @@ -51,15 +21,15 @@ func (h *AdminHandler) GetApiServices(w http.ResponseWriter, r *http.Request) { return } - apiServices := make([]ApiServiceDTO, 0) + apiServices := make([]types.ApiServiceDTO, 0) for _, service := range services { - apiServices = append(apiServices, NewApiServiceDTO(service)) + apiServices = append(apiServices, types.NewApiServiceDTO(service)) } type Response struct { - Items []ApiServiceDTO `json:"items"` - Count int `json:"count"` + Items []types.ApiServiceDTO `json:"items"` + Count int `json:"count"` } encoder := json.NewEncoder(w) @@ -146,7 +116,7 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) { service.ClientSecret = clientSecret type Response struct { - Service ApiServiceDTO `json:"service"` + Service types.ApiServiceDTO `json:"service"` 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") if err := encoder.Encode(Response{ - Service: NewApiServiceDTO(service), + Service: types.NewApiServiceDTO(service), Credentials: ApiServiceCredentials{ ClientId: service.ClientID, ClientSecret: service.ClientSecret, @@ -183,7 +153,7 @@ func (h *AdminHandler) GetApiService(w http.ResponseWriter, r *http.Request) { 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) } } @@ -201,7 +171,7 @@ func (h *AdminHandler) GetApiServiceCID(w http.ResponseWriter, r *http.Request) 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) } } @@ -303,7 +273,7 @@ func (h *AdminHandler) UpdateApiService(w http.ResponseWriter, r *http.Request) 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) } } diff --git a/internal/types/apiservices.go b/internal/types/apiservices.go new file mode 100644 index 0000000..01c9e54 --- /dev/null +++ b/internal/types/apiservices.go @@ -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, + } +}