feat: accept description and is active fields

This commit is contained in:
2025-05-31 17:30:07 +02:00
parent e49c0bbe45
commit 5c43f6d72a
2 changed files with 31 additions and 4 deletions

View File

@ -29,6 +29,6 @@ func NewApiServiceDTO(service repository.ApiService) ApiServiceDTO {
GrantTypes: service.GrantTypes, GrantTypes: service.GrantTypes,
CreatedAt: service.CreatedAt, CreatedAt: service.CreatedAt,
UpdatedAt: service.UpdatedAt, UpdatedAt: service.UpdatedAt,
IsActive: false, IsActive: service.IsActive,
} }
} }

View File

@ -11,6 +11,7 @@ import (
"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/jackc/pgx/v5/pgtype"
) )
type AdminHandler struct { type AdminHandler struct {
@ -107,14 +108,24 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) {
return return
} }
service, err := h.repo.CreateApiService(r.Context(), repository.CreateApiServiceParams{ params := repository.CreateApiServiceParams{
ClientID: clientId, ClientID: clientId,
ClientSecret: hashSecret, ClientSecret: hashSecret,
Name: req.Name, Name: req.Name,
RedirectUris: req.RedirectUris, RedirectUris: req.RedirectUris,
Scopes: req.Scopes, Scopes: req.Scopes,
GrantTypes: req.GrantTypes, GrantTypes: req.GrantTypes,
}) IsActive: req.IsActive,
}
if req.Description != "" {
params.Description = pgtype.Text{
String: req.Description,
Valid: true,
}
}
service, err := h.repo.CreateApiService(r.Context(), params)
if err != nil { if err != nil {
web.Error(w, "failed to create new api service", http.StatusInternalServerError) web.Error(w, "failed to create new api service", http.StatusInternalServerError)
return return
@ -124,8 +135,24 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) {
service.ClientSecret = clientSecret service.ClientSecret = clientSecret
type ApiServiceCredentials struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
type Response struct {
Service ApiServiceDTO `json:"service"`
Credentials ApiServiceCredentials `json:"credentials"`
}
encoder := json.NewEncoder(w) encoder := json.NewEncoder(w)
if err := encoder.Encode(NewApiServiceDTO(service)); err != nil { if err := encoder.Encode(Response{
Service: NewApiServiceDTO(service),
Credentials: ApiServiceCredentials{
ClientId: service.ClientID,
ClientSecret: service.ClientSecret,
},
}); err != nil {
web.Error(w, "failed to encode response", http.StatusInternalServerError) web.Error(w, "failed to encode response", http.StatusInternalServerError)
} }
} }