Ajouter et implémenter certaines responses #118

Merged
vlbeaudoin merged 4 commits from fix/bucket_list_response into main 2023-08-20 18:04:54 -04:00
2 changed files with 84 additions and 58 deletions
Showing only changes of commit 3ce7ac53ca - Show all commits

View file

@ -12,7 +12,7 @@
<form class="form adminUploadForm" action="/admin/documents/upload" method="post" enctype="multipart/form-data">
<label class="formLabel" for="bucket">Type de document:</label>
<select class="formSelect" name="bucket" id="bucket">
{{ range .Buckets }}
{{ range .Data.Buckets }}
<option class="formOption" value="{{ .Name }}">{{ .DisplayName }}</option>
{{ end }}
</select>

View file

@ -44,26 +44,27 @@ func HandleVieEtudianteOrganisme(c echo.Context) error {
}
func HandleDocumentation(c echo.Context) error {
var response models.HandleDocumentationResponse
client, err := api.NewApiClientFromViper()
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
result, err := client.Call(http.MethodGet, "/v1/bucket")
v1BucketListResponse, err := client.ListBuckets()
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
response.StatusCode = v1BucketListResponse.StatusCode
response.Message = v1BucketListResponse.Message
response.Error = err.Error()
return c.Render(response.StatusCode, "documentation-html", response)
}
var buckets map[string]string
//TODO check v1BucketListRespone StatusCode and Error
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var data []models.Bucket
for bucket, displayName := range buckets {
for bucket, displayName := range v1BucketListResponse.Data.Buckets {
// TODO move call to dedicated API client method
// TODO add Response type
content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
@ -94,16 +95,19 @@ func HandleDocumentation(c echo.Context) error {
documents_processed := documents
*/
data = append(data, models.Bucket{
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
Name: bucket,
DisplayName: displayName,
Documents: documents,
})
}
sort.SliceStable(data, func(i, j int) bool { return data[i].Name < data[j].Name })
sort.SliceStable(response.Data.Buckets, func(i, j int) bool { return response.Data.Buckets[i].Name < response.Data.Buckets[j].Name })
return c.Render(http.StatusOK, "documentation-html", data)
response.StatusCode = http.StatusOK
// TODO render .Message
return c.Render(response.StatusCode, "documentation-html", response.Data.Buckets)
}
func HandleFormulaires(c echo.Context) error {
@ -140,65 +144,80 @@ func HandleAdmin(c echo.Context) error {
}
func HandleAdminDocumentsUpload(c echo.Context) error {
var response models.HandleAdminDocumentsUploadResponse
client, err := api.NewApiClientFromViper()
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
response.StatusCode = http.StatusInternalServerError
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", nil)
}
result, err := client.Call(http.MethodGet, "/v1/bucket")
v1BucketListResponse, err := client.ListBuckets()
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
response.StatusCode = v1BucketListResponse.StatusCode
response.Error = err.Error()
response.Message = v1BucketListResponse.Message
return c.Render(response.StatusCode, "admin-upload-html", nil)
}
var buckets map[string]string
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var data struct {
Buckets []models.Bucket
Message string
}
for bucketName, displayName := range buckets {
data.Buckets = append(data.Buckets, models.Bucket{
for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
Name: bucketName,
DisplayName: displayName,
})
}
response.StatusCode = http.StatusOK
return c.Render(http.StatusOK, "admin-upload-html", data)
return c.Render(response.StatusCode, "admin-upload-html", response)
}
func HandleAdminDocumentsUploadPOST(c echo.Context) error {
/*
var data struct {
Buckets []models.Bucket
Message string
}
*/
var response models.HandleAdminDocumentsUploadResponse
client, err := api.NewApiClientFromViper()
if err != nil {
data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#api.New: %s", err)
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during api.newApiClientFromViper()"
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", response)
}
v1BucketListResponse, err := client.ListBuckets()
if err != nil {
response.StatusCode = v1BucketListResponse.StatusCode
response.Message = v1BucketListResponse.Message
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", response)
}
/*
result, err := client.Call(http.MethodGet, "/v1/bucket")
if err != nil {
data.Message = "Error during GET /v1/bucket"
return c.Render(http.StatusInternalServerError, "documentation-html", data)
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
}
var buckets map[string]string
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
return c.Render(http.StatusInternalServerError, "admin-upload-html", nil)
}
*/
for bucketName, displayName := range buckets {
data.Buckets = append(data.Buckets, models.Bucket{
for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
Name: bucketName,
DisplayName: displayName,
})
@ -208,24 +227,31 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error {
document, err := c.FormFile("document")
if err != nil {
data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#c.FormFile: %s", err)
return c.Render(http.StatusBadRequest, "admin-upload-html", data)
response.StatusCode = http.StatusBadRequest
response.Message = "Formulaire invalide"
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", response)
}
response, err := client.UploadDocument(bucket, document)
uploadDocumentResponse, err := client.UploadDocument(bucket, document)
if err != nil {
data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#client.UploadDocument: %s", err)
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
response.StatusCode = uploadDocumentResponse.StatusCode
response.Message = uploadDocumentResponse.Message
response.Error = err.Error()
return c.Render(response.StatusCode, "admin-upload-html", response)
}
// Format response
var info, status string
info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Data.Size, response.Data.Bucket, response.Data.Object)
info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Object)
status = response.Message
status = uploadDocumentResponse.Message
data.Message = fmt.Sprintf("%s - %s", status, info)
response.StatusCode = http.StatusOK
response.Message = fmt.Sprintf("%s - %s", status, info)
return c.Render(http.StatusOK, "admin-upload-html", data)
return c.Render(response.StatusCode, "admin-upload-html", response)
}