github.com/altipla-consulting/ravendb-go-client@v0.1.3/create_database_operation.go (about) 1 package ravendb 2 3 import ( 4 "net/http" 5 "net/url" 6 "strconv" 7 ) 8 9 var _ IServerOperation = &CreateDatabaseOperation{} 10 11 // CreateDatabaseOperation represents "create database" operation 12 type CreateDatabaseOperation struct { 13 databaseRecord *DatabaseRecord 14 replicationFactor int 15 } 16 17 // NewCreateDatabaseOperation returns CreateDatabaseOperation 18 func NewCreateDatabaseOperation(databaseRecord *DatabaseRecord, replicationFactor int) *CreateDatabaseOperation { 19 if replicationFactor < 1 { 20 replicationFactor = 1 21 } 22 return &CreateDatabaseOperation{ 23 databaseRecord: databaseRecord, 24 replicationFactor: replicationFactor, 25 } 26 } 27 28 // GetCommand returns command for this operation 29 func (o *CreateDatabaseOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) { 30 return NewCreateDatabaseCommand(conventions, o.databaseRecord, o.replicationFactor) 31 } 32 33 var ( 34 _ RavenCommand = &CreateDatabaseCommand{} 35 ) 36 37 // CreateDatabaseCommand represents "create database" command 38 type CreateDatabaseCommand struct { 39 RavenCommandBase 40 41 conventions *DocumentConventions 42 databaseRecord *DatabaseRecord 43 replicationFactor int 44 databaseName string 45 46 Result *DatabasePutResult 47 } 48 49 // NewCreateDatabaseCommand returns new CreateDatabaseCommand 50 func NewCreateDatabaseCommand(conventions *DocumentConventions, databaseRecord *DatabaseRecord, replicationFactor int) (*CreateDatabaseCommand, error) { 51 if databaseRecord.DatabaseName == "" { 52 return nil, newIllegalArgumentError("databaseRecord.DatabaseName cannot be empty") 53 } 54 cmd := &CreateDatabaseCommand{ 55 RavenCommandBase: NewRavenCommandBase(), 56 57 conventions: conventions, 58 databaseRecord: databaseRecord, 59 replicationFactor: replicationFactor, 60 databaseName: databaseRecord.DatabaseName, 61 } 62 return cmd, nil 63 } 64 65 func (c *CreateDatabaseCommand) createRequest(node *ServerNode) (*http.Request, error) { 66 url := node.URL + "/admin/databases?name=" + url.QueryEscape(c.databaseName) 67 url += "&replicationFactor=" + strconv.Itoa(c.replicationFactor) 68 69 js, err := jsonMarshal(c.databaseRecord) 70 if err != nil { 71 return nil, err 72 } 73 return newHttpPut(url, js) 74 } 75 76 func (c *CreateDatabaseCommand) setResponse(response []byte, fromCache bool) error { 77 if len(response) == 0 { 78 return throwInvalidResponse() 79 } 80 return jsonUnmarshal(response, &c.Result) 81 }