github.com/ravendb/ravendb-go-client@v0.0.0-20240229102137-4474ee7aa0fa/put_indexes_operation.go (about) 1 package ravendb 2 3 import ( 4 "net/http" 5 ) 6 7 var ( 8 _ IMaintenanceOperation = &PutIndexesOperation{} 9 ) 10 11 // PutIndexesOperation represents put indexes operation 12 type PutIndexesOperation struct { 13 indexToAdd []*IndexDefinition 14 15 Command *PutIndexesCommand 16 } 17 18 // NewPutIndexesOperation returns new PutIndexesOperation 19 func NewPutIndexesOperation(indexToAdd ...*IndexDefinition) *PutIndexesOperation { 20 return &PutIndexesOperation{ 21 indexToAdd: indexToAdd, 22 } 23 } 24 25 // GetCommand returns a command for this operation 26 func (o *PutIndexesOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) { 27 var err error 28 o.Command, err = NewPutIndexesCommand(conventions, o.indexToAdd) 29 if err != nil { 30 return nil, err 31 } 32 return o.Command, nil 33 } 34 35 var _ RavenCommand = &PutIndexesCommand{} 36 37 // PutIndexesCommand represents put indexes command 38 type PutIndexesCommand struct { 39 RavenCommandBase 40 41 indexToAdd []map[string]interface{} 42 43 Result []*PutIndexResult 44 } 45 46 // NewPutIndexesCommand returns new PutIndexesCommand 47 func NewPutIndexesCommand(conventions *DocumentConventions, indexesToAdd []*IndexDefinition) (*PutIndexesCommand, error) { 48 if conventions == nil { 49 return nil, newIllegalArgumentError("conventions cannot be nil") 50 } 51 if indexesToAdd == nil { 52 return nil, newIllegalArgumentError("indexesToAdd cannot be nil") 53 } 54 55 cmd := &PutIndexesCommand{ 56 RavenCommandBase: NewRavenCommandBase(), 57 } 58 59 for _, indexToAdd := range indexesToAdd { 60 // Note: unlike java, Type is not calculated on demand. This is a decent 61 // place to ensure it. Assumes that indexToAdd will not be modified 62 // between now an CreateRequest() 63 indexToAdd.updateIndexTypeAndMaps() 64 65 panicIf(indexToAdd.Name == "", "Index name cannot be empty") 66 objectNode := convertEntityToJSON(indexToAdd, nil) 67 cmd.indexToAdd = append(cmd.indexToAdd, objectNode) 68 } 69 70 return cmd, nil 71 } 72 73 func (c *PutIndexesCommand) CreateRequest(node *ServerNode) (*http.Request, error) { 74 url := node.URL + "/databases/" + node.Database + "/admin/indexes" 75 76 m := map[string]interface{}{ 77 "Indexes": c.indexToAdd, 78 } 79 d, err := jsonMarshal(m) 80 if err != nil { 81 return nil, err 82 } 83 return newHttpPut(url, d) 84 } 85 86 func (c *PutIndexesCommand) SetResponse(response []byte, fromCache bool) error { 87 var res PutIndexesResponse 88 err := jsonUnmarshal(response, &res) 89 if err != nil { 90 return err 91 } 92 c.Result = res.Results 93 return nil 94 }