Ejemplo n.º 1
0
        // add a stock listing
        static async Task AddAsync(string cnumber, int rlevel)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://x00077517.cloudapp.net/");                             // base URL for API Controller i.e. RESTFul service

                    // add an Accept header for JSON - preference for response 
                    client.DefaultRequestHeaders.
                        Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    // POST /api/CreditCard with a listing serialised in request body
                    // create a new credit card
                    CreditCardItem newCard = new CreditCardItem { Number = cnumber, RiskLevel = rlevel };
                    HttpResponseMessage response = await client.PostAsJsonAsync("api/CreditCard", newCard);   // or PostAsXmlAsync
                    if (response.IsSuccessStatusCode)                                                       // 200 .. 299
                    {
                        Uri newCardUri = response.Headers.Location;
                        var card = await response.Content.ReadAsAsync<CreditCardItem>();
                    }
                    else
                    {
                        Console.WriteLine(response.StatusCode + " " + response.ReasonPhrase);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        public CreditCardItem AddCard(CreditCardItem item)
        {
            item.Id = ObjectId.GenerateNewId().ToString();
            _creditCards.Insert(item);

            return item;
        }
 // POST api/CreditCard/{id}
 public void Update(string id, CreditCardItem value)
 {
     // check if given RiskValue is in range
     if (value.RiskLevel < 1 || value.RiskLevel > 3)
     {
         throw new HttpResponseException(HttpStatusCode.BadRequest);
     }
     // check if card number was given
     if (string.IsNullOrEmpty(value.Number))
     {
         throw new HttpResponseException(HttpStatusCode.BadRequest);
     }
     if (!_cards.UpdateCard(id, value))
     {
         throw new HttpResponseException(HttpStatusCode.NotFound);
     }
 }
        // POST api/CreditCard
        public CreditCardItem AddCard(CreditCardItem value)
        {
            // check if given RiskValue is in range
            if (value.RiskLevel < 1 || value.RiskLevel > 3)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            // check if card number was given
            if (string.IsNullOrEmpty(value.Number))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            CreditCardItem card = _cards.AddCard(value);

            return card;
        }
Ejemplo n.º 5
0
        // update a stock listing
        static async Task UpdateAsync(string id, string cnumber_new, int rlevel_new)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://x00077517.cloudapp.net/");

                    CreditCardItem card = new CreditCardItem() { Number = cnumber_new, RiskLevel = rlevel_new };                                            

                    // update by Put to /api/CreditCard/{id} a listing serialised in request body
                    HttpResponseMessage response = await client.PostAsJsonAsync("api/CreditCard/" + id, card);
                    if (!response.IsSuccessStatusCode)
                    {
                        Console.WriteLine(response.StatusCode + " " + response.ReasonPhrase);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        public bool UpdateCard(string id, CreditCardItem item)
        {
            IMongoQuery query = Query.EQ("_id", id);
            IMongoUpdate update = Update
                .Set("Number", item.Number)
                .Set("RiskLevel", item.RiskLevel);
            SafeModeResult result = _creditCards.Update(query, update);

            return result.UpdatedExisting;
        }