github.com/altipla-consulting/ravendb-go-client@v0.1.3/put_document_command.go (about)

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  var (
     8  	_ RavenCommand = &PutDocumentCommand{}
     9  )
    10  
    11  type PutDocumentCommand struct {
    12  	RavenCommandBase
    13  
    14  	_id           string
    15  	_changeVector *string
    16  	_document     map[string]interface{}
    17  
    18  	Result *PutResult
    19  }
    20  
    21  func NewPutDocumentCommand(id string, changeVector *string, document map[string]interface{}) *PutDocumentCommand {
    22  	panicIf(id == "", "Id cannot be null")
    23  	panicIf(document == nil, "document cannot be nil")
    24  
    25  	cmd := &PutDocumentCommand{
    26  		RavenCommandBase: NewRavenCommandBase(),
    27  
    28  		_id:           id,
    29  		_changeVector: changeVector,
    30  		_document:     document,
    31  	}
    32  	return cmd
    33  }
    34  
    35  func (c *PutDocumentCommand) createRequest(node *ServerNode) (*http.Request, error) {
    36  	url := node.URL + "/databases/" + node.Database + "/docs?id=" + urlEncode(c._id)
    37  
    38  	d, err := jsonMarshal(c._document)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	request, err := newHttpPut(url, d)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	addChangeVectorIfNotNull(c._changeVector, request)
    47  	return request, nil
    48  }
    49  
    50  func (c *PutDocumentCommand) setResponse(response []byte, fromCache bool) error {
    51  	return jsonUnmarshal(response, &c.Result)
    52  }