48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package httpapi
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"net/http"
|
||
|
|
"strconv"
|
||
|
|
|
||
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/feeds"
|
||
|
|
"github.com/go-chi/chi/v5"
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (s *Server) handleExportSelectedProducts(w http.ResponseWriter, r *http.Request) {
|
||
|
|
cid, _ := CompanyIDFromContext(r.Context())
|
||
|
|
feedID, err := uuid.Parse(chi.URLParam(r, "id"))
|
||
|
|
if err != nil {
|
||
|
|
Error(w, http.StatusBadRequest, "invalid id")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var body struct {
|
||
|
|
ProductIDs []string `json:"product_ids"`
|
||
|
|
}
|
||
|
|
if err := DecodeJSON(r, &body); err != nil {
|
||
|
|
Error(w, http.StatusBadRequest, "invalid json")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
ids := make([]uuid.UUID, 0, len(body.ProductIDs))
|
||
|
|
for _, raw := range body.ProductIDs {
|
||
|
|
id, err := uuid.Parse(raw)
|
||
|
|
if err != nil {
|
||
|
|
Error(w, http.StatusBadRequest, "invalid product_ids")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
ids = append(ids, id)
|
||
|
|
}
|
||
|
|
filename, mimeType, content, count, err := s.Feeds.ExportSelectedProducts(r.Context(), cid, feedID, ids)
|
||
|
|
if err != nil {
|
||
|
|
ClientOrLog(w, http.StatusBadRequest, "could not export selected products", err, feeds.ClientError)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
w.Header().Set("Content-Type", mimeType)
|
||
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
|
||
|
|
w.Header().Set("X-Products-Exported", strconv.Itoa(count))
|
||
|
|
w.Header().Set("Cache-Control", "no-store")
|
||
|
|
w.WriteHeader(http.StatusOK)
|
||
|
|
_, _ = w.Write(content)
|
||
|
|
}
|