Ajouter et implémenter certaines responses #118
2 changed files with 84 additions and 58 deletions
|
@ -12,7 +12,7 @@
|
||||||
<form class="form adminUploadForm" action="/admin/documents/upload" method="post" enctype="multipart/form-data">
|
<form class="form adminUploadForm" action="/admin/documents/upload" method="post" enctype="multipart/form-data">
|
||||||
<label class="formLabel" for="bucket">Type de document:</label>
|
<label class="formLabel" for="bucket">Type de document:</label>
|
||||||
<select class="formSelect" name="bucket" id="bucket">
|
<select class="formSelect" name="bucket" id="bucket">
|
||||||
{{ range .Buckets }}
|
{{ range .Data.Buckets }}
|
||||||
<option class="formOption" value="{{ .Name }}">{{ .DisplayName }}</option>
|
<option class="formOption" value="{{ .Name }}">{{ .DisplayName }}</option>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</select>
|
</select>
|
||||||
|
|
|
@ -44,26 +44,27 @@ func HandleVieEtudianteOrganisme(c echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleDocumentation(c echo.Context) error {
|
func HandleDocumentation(c echo.Context) error {
|
||||||
|
var response models.HandleDocumentationResponse
|
||||||
|
|
||||||
client, err := api.NewApiClientFromViper()
|
client, err := api.NewApiClientFromViper()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
|
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := client.Call(http.MethodGet, "/v1/bucket")
|
v1BucketListResponse, err := client.ListBuckets()
|
||||||
if err != nil {
|
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)
|
for bucket, displayName := range v1BucketListResponse.Data.Buckets {
|
||||||
if err != nil {
|
// TODO move call to dedicated API client method
|
||||||
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
|
// TODO add Response type
|
||||||
}
|
|
||||||
|
|
||||||
var data []models.Bucket
|
|
||||||
|
|
||||||
for bucket, displayName := range buckets {
|
|
||||||
content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
|
content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
|
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
|
||||||
|
@ -94,16 +95,19 @@ func HandleDocumentation(c echo.Context) error {
|
||||||
documents_processed := documents
|
documents_processed := documents
|
||||||
*/
|
*/
|
||||||
|
|
||||||
data = append(data, models.Bucket{
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
||||||
Name: bucket,
|
Name: bucket,
|
||||||
DisplayName: displayName,
|
DisplayName: displayName,
|
||||||
Documents: documents,
|
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 {
|
func HandleFormulaires(c echo.Context) error {
|
||||||
|
@ -140,65 +144,80 @@ func HandleAdmin(c echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleAdminDocumentsUpload(c echo.Context) error {
|
func HandleAdminDocumentsUpload(c echo.Context) error {
|
||||||
|
var response models.HandleAdminDocumentsUploadResponse
|
||||||
client, err := api.NewApiClientFromViper()
|
client, err := api.NewApiClientFromViper()
|
||||||
if err != nil {
|
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 {
|
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
|
for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
|
||||||
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
||||||
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{
|
|
||||||
Name: bucketName,
|
Name: bucketName,
|
||||||
DisplayName: displayName,
|
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 {
|
func HandleAdminDocumentsUploadPOST(c echo.Context) error {
|
||||||
var data struct {
|
/*
|
||||||
Buckets []models.Bucket
|
var data struct {
|
||||||
Message string
|
Buckets []models.Bucket
|
||||||
}
|
Message string
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
var response models.HandleAdminDocumentsUploadResponse
|
||||||
|
|
||||||
client, err := api.NewApiClientFromViper()
|
client, err := api.NewApiClientFromViper()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#api.New: %s", err)
|
response.StatusCode = http.StatusInternalServerError
|
||||||
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
|
response.Message = "Error during api.newApiClientFromViper()"
|
||||||
|
response.Error = err.Error()
|
||||||
|
|
||||||
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := client.Call(http.MethodGet, "/v1/bucket")
|
v1BucketListResponse, err := client.ListBuckets()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data.Message = "Error during GET /v1/bucket"
|
response.StatusCode = v1BucketListResponse.StatusCode
|
||||||
return c.Render(http.StatusInternalServerError, "documentation-html", data)
|
response.Message = v1BucketListResponse.Message
|
||||||
|
response.Error = err.Error()
|
||||||
|
|
||||||
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
||||||
}
|
}
|
||||||
|
|
||||||
var buckets map[string]string
|
/*
|
||||||
|
result, err := client.Call(http.MethodGet, "/v1/bucket")
|
||||||
|
if err != nil {
|
||||||
|
data.Message = "Error during GET /v1/bucket"
|
||||||
|
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
|
||||||
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(result, &buckets)
|
var buckets map[string]string
|
||||||
if err != nil {
|
|
||||||
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
for bucketName, displayName := range buckets {
|
err = json.Unmarshal(result, &buckets)
|
||||||
data.Buckets = append(data.Buckets, models.Bucket{
|
if err != nil {
|
||||||
|
return c.Render(http.StatusInternalServerError, "admin-upload-html", nil)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
for bucketName, displayName := range v1BucketListResponse.Data.Buckets {
|
||||||
|
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
|
||||||
Name: bucketName,
|
Name: bucketName,
|
||||||
DisplayName: displayName,
|
DisplayName: displayName,
|
||||||
})
|
})
|
||||||
|
@ -208,24 +227,31 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error {
|
||||||
|
|
||||||
document, err := c.FormFile("document")
|
document, err := c.FormFile("document")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#c.FormFile: %s", err)
|
response.StatusCode = http.StatusBadRequest
|
||||||
return c.Render(http.StatusBadRequest, "admin-upload-html", data)
|
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 {
|
if err != nil {
|
||||||
data.Message = fmt.Sprintf("HandleAdminDocumentsUploadPOST#client.UploadDocument: %s", err)
|
response.StatusCode = uploadDocumentResponse.StatusCode
|
||||||
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
|
response.Message = uploadDocumentResponse.Message
|
||||||
|
response.Error = err.Error()
|
||||||
|
|
||||||
|
return c.Render(response.StatusCode, "admin-upload-html", response)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format response
|
// Format response
|
||||||
var info, status string
|
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue