From 4abf9414dc6cc19bd4d4ee8a3d55fecdbbf78eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Schr=C3=B6ter?= Date: Mon, 28 Oct 2024 17:19:52 +0100 Subject: [PATCH] fix save button --- app_http_handlers.go | 18 ++++++++++++++++++ main.go | 2 ++ paperless.go | 42 ++++++++++++++++++++++++++++++++++++++++++ types.go | 20 ++++++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/app_http_handlers.go b/app_http_handlers.go index 53db4d7..c7ab6af 100644 --- a/app_http_handlers.go +++ b/app_http_handlers.go @@ -225,3 +225,21 @@ func (app *App) getAllJobsHandler(c *gin.Context) { c.JSON(http.StatusOK, jobList) } + +// getDocumentHandler handles the retrieval of a document by its ID +func (app *App) getDocumentHandler() gin.HandlerFunc { + return func(c *gin.Context) { + id := c.Param("id") + parsedID, err := strconv.Atoi(id) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid document ID"}) + return + } + document, err := app.Client.GetDocument(c, parsedID) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, document) + } +} diff --git a/main.go b/main.go index bc1c419..234b5a5 100644 --- a/main.go +++ b/main.go @@ -131,6 +131,8 @@ func main() { api := router.Group("/api") { api.GET("/documents", app.documentsHandler) + // http://localhost:8080/api/documents/544 + api.GET("/documents/:id", app.getDocumentHandler()) api.POST("/generate-suggestions", app.generateSuggestionsHandler) api.PATCH("/update-documents", app.updateDocumentsHandler) api.GET("/filter-tag", func(c *gin.Context) { diff --git a/paperless.go b/paperless.go index 03cc92a..ec2ecb4 100644 --- a/paperless.go +++ b/paperless.go @@ -175,6 +175,48 @@ func (c *PaperlessClient) DownloadPDF(ctx context.Context, document Document) ([ return io.ReadAll(resp.Body) } +func (c *PaperlessClient) GetDocument(ctx context.Context, documentID int) (Document, error) { + path := fmt.Sprintf("api/documents/%d/", documentID) + resp, err := c.Do(ctx, "GET", path, nil) + if err != nil { + return Document{}, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + bodyBytes, _ := io.ReadAll(resp.Body) + return Document{}, fmt.Errorf("error fetching document %d: %d, %s", documentID, resp.StatusCode, string(bodyBytes)) + } + + var documentResponse GetDocumentApiResponse + err = json.NewDecoder(resp.Body).Decode(&documentResponse) + if err != nil { + return Document{}, err + } + + allTags, err := c.GetAllTags(ctx) + if err != nil { + return Document{}, err + } + + tagNames := make([]string, len(documentResponse.Tags)) + for i, resultTagID := range documentResponse.Tags { + for tagName, tagID := range allTags { + if resultTagID == tagID { + tagNames[i] = tagName + break + } + } + } + + return Document{ + ID: documentResponse.ID, + Title: documentResponse.Title, + Content: documentResponse.Content, + Tags: tagNames, + }, nil +} + // UpdateDocuments updates the specified documents with suggested changes func (c *PaperlessClient) UpdateDocuments(ctx context.Context, documents []DocumentSuggestion) error { // Fetch all available tags diff --git a/types.go b/types.go index d0bb637..72238c0 100644 --- a/types.go +++ b/types.go @@ -36,6 +36,26 @@ type GetDocumentsApiResponse struct { } `json:"results"` } +type GetDocumentApiResponse struct { + ID int `json:"id"` + Correspondent interface{} `json:"correspondent"` + DocumentType interface{} `json:"document_type"` + StoragePath interface{} `json:"storage_path"` + Title string `json:"title"` + Content string `json:"content"` + Tags []int `json:"tags"` + Created time.Time `json:"created"` + CreatedDate string `json:"created_date"` + Modified time.Time `json:"modified"` + Added time.Time `json:"added"` + ArchiveSerialNumber interface{} `json:"archive_serial_number"` + OriginalFileName string `json:"original_file_name"` + ArchivedFileName string `json:"archived_file_name"` + Owner int `json:"owner"` + UserCanChange bool `json:"user_can_change"` + Notes []interface{} `json:"notes"` +} + // Document is a stripped down version of the document object from paperless-ngx. // Response payload for /documents endpoint and part of request payload for /generate-suggestions endpoint type Document struct {