Remplir dropdown admin upload documents avec types de document #99

Merged
vlbeaudoin merged 1 commit from fix/template-dropdown-bucket-types into main 2023-08-15 16:21:30 -04:00
2 changed files with 79 additions and 9 deletions
Showing only changes of commit c74bfe153b - Show all commits

View file

@ -388,10 +388,56 @@ func handleAdmin(c echo.Context) error {
} }
func handleAdminDocumentsUpload(c echo.Context) error { func handleAdminDocumentsUpload(c echo.Context) error {
return c.Render(http.StatusOK, "admin-upload-html", nil) client, err := api.NewApiClientFromViper()
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
result, err := client.Call(http.MethodGet, "/v1/bucket")
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var buckets map[string]string
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
type Bucket struct {
Name string
DisplayName string
Documents []string
}
var data struct {
Buckets []Bucket
Message string
}
for bucketName, displayName := range buckets {
data.Buckets = append(data.Buckets, Bucket{
Name: bucketName,
DisplayName: displayName,
})
}
return c.Render(http.StatusOK, "admin-upload-html", data)
} }
func handleAdminDocumentsUploadPOST(c echo.Context) error { func handleAdminDocumentsUploadPOST(c echo.Context) error {
type Bucket struct {
Name string
DisplayName string
Documents []string
}
var data struct {
Buckets []Bucket
Message string
}
client, err := api.New(cfg.Server.Api.Protocol, cfg.Server.Api.Host, cfg.Server.Port, api.APIOptions{ client, err := api.New(cfg.Server.Api.Protocol, cfg.Server.Api.Host, cfg.Server.Port, api.APIOptions{
KeyAuth: cfg.Server.Api.Auth, KeyAuth: cfg.Server.Api.Auth,
Key: cfg.Server.Api.Key, Key: cfg.Server.Api.Key,
@ -400,29 +446,52 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error {
Password: cfg.Server.Admin.Password, Password: cfg.Server.Admin.Password,
}) })
if err != nil { if err != nil {
return c.Render(http.StatusInternalServerError, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#api.New: %s", err)}) data.Message = fmt.Sprintf("handleAdminDocumentsUploadPOST#api.New: %s", err)
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
}
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)
}
var buckets map[string]string
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
for bucketName, displayName := range buckets {
data.Buckets = append(data.Buckets, Bucket{
Name: bucketName,
DisplayName: displayName,
})
} }
bucket := c.FormValue("bucket") bucket := c.FormValue("bucket")
document, err := c.FormFile("document") document, err := c.FormFile("document")
if err != nil { if err != nil {
return c.Render(http.StatusBadRequest, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#c.FormFile: %s", err)}) data.Message = fmt.Sprintf("handleAdminDocumentsUploadPOST#c.FormFile: %s", err)
return c.Render(http.StatusBadRequest, "admin-upload-html", data)
} }
response, err := client.UploadDocument(bucket, document) response, err := client.UploadDocument(bucket, document)
if err != nil { if err != nil {
return c.Render(http.StatusInternalServerError, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#client.UploadDocument: %s", err)}) data.Message = fmt.Sprintf("handleAdminDocumentsUploadPOST#client.UploadDocument: %s", err)
return c.Render(http.StatusInternalServerError, "admin-upload-html", data)
} }
// Format response // Format response
var message, info, status string var info, status string
info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Info.Size, response.Info.Bucket, response.Info.Object) info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Info.Size, response.Info.Bucket, response.Info.Object)
status = response.Message status = response.Message
message = fmt.Sprintf("%s - %s", status, info) data.Message = fmt.Sprintf("%s - %s", status, info)
return c.Render(http.StatusOK, "admin-upload-html", struct{ Message string }{Message: message}) return c.Render(http.StatusOK, "admin-upload-html", data)
} }

View file

@ -13,8 +13,9 @@
<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">
<option class="formOption" value="proces-verbaux">Procès verbaux</option> {{ range .Buckets }}
<option class="formOption" value="politiques-et-reglements">Politiques et Règlements</option> <option class="formOption" value="{{ .Name }}">{{ .DisplayName }}</option>
{{ end }}
</select> </select>
<br> <br>
Document: <input class="formDocUpload" type="file" name="document"> Document: <input class="formDocUpload" type="file" name="document">