mirror of
https://github.com/icereed/paperless-gpt.git
synced 2025-03-14 05:38:01 -05:00
fix save button
This commit is contained in:
parent
cc1d66e1dc
commit
4abf9414dc
4 changed files with 82 additions and 0 deletions
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
2
main.go
2
main.go
|
@ -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) {
|
||||||
|
|
42
paperless.go
42
paperless.go
|
@ -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
|
||||||
|
|
20
types.go
20
types.go
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue