Exemplo n.º 1
0
        public async Task <IActionResult> Create(Guid?customerId, [FromBody] CustomerModel customer)
        {
            try
            {
                if (!customerId.HasValue)
                {
                    customerId = Guid.NewGuid();
                }

                var createdCustomer = customer.Clone();
                createdCustomer.CreationDate = DateTime.UtcNow;
                createdCustomer.LastUpdate   = null;
                createdCustomer.Id           = customerId.Value;

                Uri collectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionName);
                using (var client = CreateConnection())
                {
                    await client.CreateDocumentAsync(collectionUri, createdCustomer);
                }

                return(CreatedAtAction(nameof(Get), new { customerId = createdCustomer.Id }, createdCustomer));
            }
            catch (Exception ex)
            {
                // TODO: handle error
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                throw;
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Update(Guid customerId, [FromBody] CustomerModel customer)
        {
            try
            {
                Uri documentUri = UriFactory.CreateDocumentUri(databaseId, collectionName, customerId.ToString());
                using (var client = CreateConnection())
                {
                    var updatedCustomer = customer.Clone();
                    updatedCustomer.Id         = customerId;
                    updatedCustomer.LastUpdate = DateTime.UtcNow;
                    var response = await client.ReplaceDocumentAsync(documentUri, updatedCustomer, new RequestOptions()
                    {
                        PartitionKey = new PartitionKey(customerId.ToString())
                    });

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        CustomerModel returnCustomer = null;
                        using (var reader = new StreamReader(response.ResponseStream))
                        {
                            returnCustomer = JsonConvert.DeserializeObject <CustomerModel>(await reader.ReadToEndAsync());
                            return(AcceptedAtAction(nameof(Get), new { customerId = updatedCustomer.Id }, updatedCustomer));
                        }
                    }

                    return(new ContentResult()
                    {
                        StatusCode = (int)response.StatusCode
                    });
                }
            }
            catch (Exception ex)
            {
                // TODO: handle error
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                throw;
            }
        }