60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
|
package api
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
||
|
"github.com/pkg/errors"
|
||
|
"gitlab.com/wpetit/goweb/api"
|
||
|
"gitlab.com/wpetit/goweb/logger"
|
||
|
)
|
||
|
|
||
|
type UpdateTenantRequest struct {
|
||
|
Label *string `json:"label" validate:"omitempty"`
|
||
|
}
|
||
|
|
||
|
func (m *Mount) updateTenant(w http.ResponseWriter, r *http.Request) {
|
||
|
tenantID, ok := getTenantID(w, r)
|
||
|
if !ok {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
ctx := r.Context()
|
||
|
|
||
|
updateTenantReq := &UpdateTenantRequest{}
|
||
|
if ok := api.Bind(w, r, updateTenantReq); !ok {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
options := make([]datastore.TenantUpdateOptionFunc, 0)
|
||
|
|
||
|
if updateTenantReq.Label != nil {
|
||
|
options = append(options, datastore.WithTenantUpdateLabel(*updateTenantReq.Label))
|
||
|
}
|
||
|
|
||
|
tenant, err := m.tenantRepo.Update(
|
||
|
ctx,
|
||
|
tenantID,
|
||
|
options...,
|
||
|
)
|
||
|
if err != nil {
|
||
|
if errors.Is(err, datastore.ErrNotFound) {
|
||
|
api.ErrorResponse(w, http.StatusNotFound, ErrCodeNotFound, nil)
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = errors.WithStack(err)
|
||
|
logger.Error(ctx, "could not update tenant", logger.CapturedE(err))
|
||
|
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
api.DataResponse(w, http.StatusOK, struct {
|
||
|
Tenant *datastore.Tenant `json:"tenant"`
|
||
|
}{
|
||
|
Tenant: tenant,
|
||
|
})
|
||
|
}
|