Skip to content

Commit

Permalink
feat(db-doc): add method to check if doc exists
Browse files Browse the repository at this point in the history
  • Loading branch information
rozanecm committed May 4, 2024
1 parent 2e1a309 commit d3a1596
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
)

Expand Down Expand Up @@ -341,3 +342,21 @@ func checkStructForJSONFields(resultVar interface{}) error {

return nil
}

func (db *Database) DocExists(ctx context.Context, docID string) (bool, error) {
code, responseBody, err := db.httpClient.Head(ctx, fmt.Sprintf("%s/%s", db.dbName, docID))
if err != nil {
return false, fmt.Errorf("error sending HEAD request: %w", err)
}

switch code {
case http.StatusOK:
return true, nil
case http.StatusNotModified:
return true, nil // Document exists but wasn't modified
case http.StatusNotFound:
return false, nil // Document doesn't exist
default:
return false, fmt.Errorf("unexpected response status code: %d. %s", code, string(responseBody))
}
}

0 comments on commit d3a1596

Please sign in to comment.