fix save button

This commit is contained in:
Dominik Schröter 2024-10-28 17:19:52 +01:00
parent cc1d66e1dc
commit 4abf9414dc
4 changed files with 82 additions and 0 deletions

View file

@ -225,3 +225,21 @@ func (app *App) getAllJobsHandler(c *gin.Context) {
c.JSON(http.StatusOK, jobList) 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)
}
}

View file

@ -131,6 +131,8 @@ func main() {
api := router.Group("/api") api := router.Group("/api")
{ {
api.GET("/documents", app.documentsHandler) api.GET("/documents", app.documentsHandler)
// http://localhost:8080/api/documents/544
api.GET("/documents/:id", app.getDocumentHandler())
api.POST("/generate-suggestions", app.generateSuggestionsHandler) api.POST("/generate-suggestions", app.generateSuggestionsHandler)
api.PATCH("/update-documents", app.updateDocumentsHandler) api.PATCH("/update-documents", app.updateDocumentsHandler)
api.GET("/filter-tag", func(c *gin.Context) { api.GET("/filter-tag", func(c *gin.Context) {

View file

@ -175,6 +175,48 @@ func (c *PaperlessClient) DownloadPDF(ctx context.Context, document Document) ([
return io.ReadAll(resp.Body) 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 // UpdateDocuments updates the specified documents with suggested changes
func (c *PaperlessClient) UpdateDocuments(ctx context.Context, documents []DocumentSuggestion) error { func (c *PaperlessClient) UpdateDocuments(ctx context.Context, documents []DocumentSuggestion) error {
// Fetch all available tags // Fetch all available tags

View file

@ -36,6 +36,26 @@ type GetDocumentsApiResponse struct {
} `json:"results"` } `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. // 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 // Response payload for /documents endpoint and part of request payload for /generate-suggestions endpoint
type Document struct { type Document struct {