-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: George J Padayatti <[email protected]>
- Loading branch information
1 parent
0d264cb
commit e144d64
Showing
5 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
Submodule config
updated
from 09f79d to ccedc3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package idp | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/bb-consent/api/src/common" | ||
"github.com/bb-consent/api/src/config" | ||
"github.com/bb-consent/api/src/v2/idp" | ||
"github.com/bb-consent/api/src/v2/paginate" | ||
) | ||
|
||
func returnHTTPResponse(resp interface{}, w http.ResponseWriter) { | ||
response, _ := json.Marshal(resp) | ||
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON) | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(response) | ||
} | ||
|
||
type listIdpsResp struct { | ||
Idps interface{} `json:"idps"` | ||
Pagination paginate.Pagination `json:"pagination"` | ||
} | ||
|
||
// ConfigListIdps | ||
func ConfigListIdps(w http.ResponseWriter, r *http.Request) { | ||
|
||
// Headers | ||
organisationId := r.Header.Get(config.OrganizationId) | ||
organisationId = common.Sanitize(organisationId) | ||
|
||
var resp listIdpsResp | ||
|
||
// Query params | ||
offset, limit := paginate.ParsePaginationQueryParams(r) | ||
|
||
// Repository | ||
idpRepo := idp.IdentityProviderRepository{} | ||
idpRepo.Init(organisationId) | ||
|
||
// Paginate idps | ||
var idps []idp.IdentityProvider | ||
query := paginate.PaginateDBObjectsQuery{ | ||
Filter: idpRepo.DefaultFilter, | ||
Collection: idp.Collection(), | ||
Context: context.Background(), | ||
Limit: limit, | ||
Offset: offset, | ||
} | ||
result, err := paginate.PaginateDBObjects(query, &idps) | ||
if err != nil { | ||
if errors.Is(err, paginate.EmptyDBError) { | ||
emptyIdps := make([]interface{}, 0) | ||
resp = listIdpsResp{ | ||
Idps: emptyIdps, | ||
Pagination: result.Pagination, | ||
} | ||
returnHTTPResponse(resp, w) | ||
return | ||
} | ||
m := "Failed to paginate idps" | ||
common.HandleErrorV2(w, http.StatusInternalServerError, m, err) | ||
return | ||
|
||
} | ||
resp = listIdpsResp{ | ||
Idps: result.Items, | ||
Pagination: result.Pagination, | ||
} | ||
|
||
returnHTTPResponse(resp, w) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters