Merge pull request 'Remplir dropdown admin upload documents avec types de document' (#99) from fix/template-dropdown-bucket-types into main

Reviewed-on: #99
This commit is contained in:
Victor Lacasse-Beaudoin 2023-08-15 15:21:29 -05:00
commit 395900ef02
2 changed files with 79 additions and 9 deletions

View file

@ -388,10 +388,56 @@ func handleAdmin(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 {
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{
KeyAuth: cfg.Server.Api.Auth,
Key: cfg.Server.Api.Key,
@ -400,29 +446,52 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error {
Password: cfg.Server.Admin.Password,
})
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")
document, err := c.FormFile("document")
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)
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
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)
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>
<select class="formSelect" name="bucket" id="bucket">
<option class="formOption" value="proces-verbaux">Procès verbaux</option>
<option class="formOption" value="politiques-et-reglements">Politiques et Règlements</option>
{{ range .Buckets }}
<option class="formOption" value="{{ .Name }}">{{ .DisplayName }}</option>
{{ end }}
</select>
<br>
Document: <input class="formDocUpload" type="file" name="document">