|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2020 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Adam Janikowski |
| 21 | +// |
| 22 | + |
| 23 | +package arangodb |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "io" |
| 28 | + "net/http" |
| 29 | + |
| 30 | + "github.com/arangodb/go-driver/v2/arangodb/shared" |
| 31 | + "github.com/arangodb/go-driver/v2/connection" |
| 32 | + "github.com/arangodb/go-driver/v2/utils" |
| 33 | + "github.com/pkg/errors" |
| 34 | +) |
| 35 | + |
| 36 | +func newCollectionDocumentCreate(collection *collection) *collectionDocumentCreate { |
| 37 | + return &collectionDocumentCreate{ |
| 38 | + collection: collection, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +var _ CollectionDocumentCreate = &collectionDocumentCreate{} |
| 43 | + |
| 44 | +type collectionDocumentCreate struct { |
| 45 | + collection *collection |
| 46 | +} |
| 47 | + |
| 48 | +func (c collectionDocumentCreate) CreateDocumentsWithOptions(ctx context.Context, documents interface{}, opts *CollectionDocumentCreateOptions) (CollectionDocumentCreateResponseReader, error) { |
| 49 | + if !utils.IsListPtr(documents) && !utils.IsList(documents) { |
| 50 | + return nil, errors.Errorf("Input documents should be list") |
| 51 | + } |
| 52 | + |
| 53 | + url := c.collection.url("document") |
| 54 | + |
| 55 | + req, err := c.collection.connection().NewRequest(http.MethodPost, url) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + for _, modifier := range c.collection.withModifiers(opts.modifyRequest, connection.WithBody(documents), connection.WithFragment("multiple")) { |
| 61 | + if err = modifier(req); err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + var arr connection.Array |
| 67 | + |
| 68 | + resp, err := c.collection.connection().Do(ctx, req, &arr) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + switch code := resp.Code(); code { |
| 74 | + case http.StatusCreated: |
| 75 | + fallthrough |
| 76 | + case http.StatusAccepted: |
| 77 | + return newCollectionDocumentCreateResponseReader(arr, opts), nil |
| 78 | + default: |
| 79 | + return nil, shared.NewResponseStruct().AsArangoErrorWithCode(code) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func (c collectionDocumentCreate) CreateDocuments(ctx context.Context, documents interface{}) (CollectionDocumentCreateResponseReader, error) { |
| 84 | + return c.CreateDocumentsWithOptions(ctx, documents, nil) |
| 85 | +} |
| 86 | + |
| 87 | +func (c collectionDocumentCreate) CreateDocumentWithOptions(ctx context.Context, document interface{}, options *CollectionDocumentCreateOptions) (CollectionDocumentCreateResponse, error) { |
| 88 | + url := c.collection.url("document") |
| 89 | + |
| 90 | + var meta CollectionDocumentCreateResponse |
| 91 | + |
| 92 | + if options != nil { |
| 93 | + meta.Old = options.OldObject |
| 94 | + meta.New = options.NewObject |
| 95 | + } |
| 96 | + |
| 97 | + response := struct { |
| 98 | + *DocumentMeta `json:",inline"` |
| 99 | + *shared.ResponseStruct `json:",inline"` |
| 100 | + Old *UnmarshalInto `json:"old,omitempty"` |
| 101 | + New *UnmarshalInto `json:"new,omitempty"` |
| 102 | + }{ |
| 103 | + DocumentMeta: &meta.DocumentMeta, |
| 104 | + ResponseStruct: &meta.ResponseStruct, |
| 105 | + |
| 106 | + Old: newUnmarshalInto(meta.Old), |
| 107 | + New: newUnmarshalInto(meta.New), |
| 108 | + } |
| 109 | + |
| 110 | + resp, err := connection.CallPost(ctx, c.collection.connection(), url, &response, document, c.collection.withModifiers(options.modifyRequest)...) |
| 111 | + if err != nil { |
| 112 | + return CollectionDocumentCreateResponse{}, err |
| 113 | + } |
| 114 | + |
| 115 | + switch code := resp.Code(); code { |
| 116 | + case http.StatusCreated: |
| 117 | + fallthrough |
| 118 | + case http.StatusAccepted: |
| 119 | + return meta, nil |
| 120 | + default: |
| 121 | + return CollectionDocumentCreateResponse{}, response.AsArangoErrorWithCode(code) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func (c collectionDocumentCreate) CreateDocument(ctx context.Context, document interface{}) (CollectionDocumentCreateResponse, error) { |
| 126 | + return c.CreateDocumentWithOptions(ctx, document, nil) |
| 127 | +} |
| 128 | + |
| 129 | +func newCollectionDocumentCreateResponseReader(array connection.Array, options *CollectionDocumentCreateOptions) *collectionDocumentCreateResponseReader { |
| 130 | + c := &collectionDocumentCreateResponseReader{array: array, options: options} |
| 131 | + |
| 132 | + if c.options != nil { |
| 133 | + c.response.Old = newUnmarshalInto(c.options.OldObject) |
| 134 | + c.response.New = newUnmarshalInto(c.options.NewObject) |
| 135 | + } |
| 136 | + |
| 137 | + return c |
| 138 | +} |
| 139 | + |
| 140 | +var _ CollectionDocumentCreateResponseReader = &collectionDocumentCreateResponseReader{} |
| 141 | + |
| 142 | +type collectionDocumentCreateResponseReader struct { |
| 143 | + array connection.Array |
| 144 | + options *CollectionDocumentCreateOptions |
| 145 | + response struct { |
| 146 | + *DocumentMeta |
| 147 | + *shared.ResponseStruct `json:",inline"` |
| 148 | + Old *UnmarshalInto `json:"old,omitempty"` |
| 149 | + New *UnmarshalInto `json:"new,omitempty"` |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreateResponse, error) { |
| 154 | + if !c.array.More() { |
| 155 | + return CollectionDocumentCreateResponse{}, shared.NoMoreDocumentsError{} |
| 156 | + } |
| 157 | + |
| 158 | + var meta CollectionDocumentCreateResponse |
| 159 | + |
| 160 | + if c.options != nil { |
| 161 | + meta.Old = c.options.OldObject |
| 162 | + meta.New = c.options.NewObject |
| 163 | + } |
| 164 | + |
| 165 | + c.response.DocumentMeta = &meta.DocumentMeta |
| 166 | + c.response.ResponseStruct = &meta.ResponseStruct |
| 167 | + |
| 168 | + if err := c.array.Unmarshal(&c.response); err != nil { |
| 169 | + if err == io.EOF { |
| 170 | + return CollectionDocumentCreateResponse{}, shared.NoMoreDocumentsError{} |
| 171 | + } |
| 172 | + return CollectionDocumentCreateResponse{}, err |
| 173 | + } |
| 174 | + |
| 175 | + return meta, nil |
| 176 | +} |
0 commit comments