コード例 #1
0
        private async void btnKiralamaIstek_Click(object sender, EventArgs e)
        {
            Arac seciliArac = new Arac();

            if (seciliAracId == 0 || aracDurum == false)
            {
                MessageBox.Show("Araç Kiralanamaz");
            }
            else
            {
                //Kiralama tablosuna insertle
                try
                {
                    bool success = false;
                    // Create a HttpClient
                    using (var client = new HttpClient())
                    {
                        // Setup basics
                        client.BaseAddress = new Uri("http://localhost:25667/");
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(
                            new MediaTypeWithQualityHeaderValue("application/json"));

                        // Create post body object
                        seciliArac.Id = seciliAracId;


                        Kiralama kiralama = new Kiralama()
                        {
                            Arac           = seciliArac,
                            Calisan        = null,
                            kiralamaTarihi = DateTime.Now,
                            Musteri        = gkullanici,
                            odenecekTutar  = 0
                        };


                        // Serialize C# object to Json Object
                        var serializedProduct = JsonConvert.SerializeObject(kiralama);
                        // Json object to System.Net.Http content type
                        var content = new StringContent(serializedProduct, Encoding.UTF8, "application/json");
                        // Post Request to the URI
                        var result = await client.PostAsync("api/Kiralama", content);

                        // Check for result
                        if (result.IsSuccessStatusCode)
                        {
                            success = true;
                        }
                    }
                    // Set message depend on success info
                    var message = success ? "İstek Yollandı" : "failed";
                    // Inform user
                    MessageBox.Show("" + message);
                }
                catch (Exception ex)
                {
                    // Inform user
                    MessageBox.Show("Error happened: " + ex.Message);
                }


                //Arac Durumunu güncelle
                try
                {
                    bool success = false;
                    // Create a HttpClient
                    using (var client = new HttpClient())
                    {
                        // Setup basics
                        client.BaseAddress = new Uri("http://localhost:25667/");
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(
                            new MediaTypeWithQualityHeaderValue("application/json"));

                        seciliArac.aracDurum = false;


                        // Serialize C# object to Json Object
                        var serializedProduct = JsonConvert.SerializeObject(seciliArac);
                        // Json object to System.Net.Http content type
                        var content = new StringContent(serializedProduct, Encoding.UTF8, "application/json");
                        // Post Request to the URI
                        var result = await client.PutAsync("api/Arac/" + seciliAracId, content);

                        // Check for result
                        if (result.IsSuccessStatusCode)
                        {
                            success = true;
                        }
                    }
                    // Set message depend on success info
                    var message = success ? "Araç Durumu Güncellendi" : "failed";
                    // Inform user
                    MessageBox.Show("" + message);
                }
                catch (Exception ex)
                {
                    // Inform user
                    MessageBox.Show("Error happened: " + ex.Message);
                }
                //Araları çek
                try
                {
                    // Create a HttpClient
                    using (var client = new HttpClient())
                    {
                        // Setup basics
                        client.BaseAddress = new Uri("http://localhost:25667/");
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                        // Get Request from the URI
                        using (var result = await client.GetAsync("api/Arac"))
                        {
                            // Check the Result
                            if (result.IsSuccessStatusCode)
                            {
                                // Take the Result as a json string
                                var value = result.Content.ReadAsStringAsync().Result;

                                // Deserialize the string with a Json Converter to ResponseContent object and fill the datagrid
                                dataGrid_Arac.DataSource =
                                    JsonConvert.DeserializeObject <ResponseContent <Arac> >(value).Data.ToList();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Inform user if an error occurs
                    MessageBox.Show("Error happened: " + ex.Message);
                }
            }
        }
コード例 #2
0
        private async void btnKabulEt_Click(object sender, EventArgs e)
        {
            if (kiralamaId > 0)
            {
                // İsteği kabul et
                try
                {
                    bool success = false;
                    // Create a HttpClient
                    using (var client = new HttpClient())
                    {
                        // Setup basics
                        client.BaseAddress = new Uri("http://localhost:25667/");
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(
                            new MediaTypeWithQualityHeaderValue("application/json"));

                        Kiralama kullanici = new Kiralama()
                        {
                            Calisan        = gkullanici,
                            kiralamaTarihi = DateTime.Now
                        };


                        // Serialize C# object to Json Object
                        var serializedProduct = JsonConvert.SerializeObject(kullanici);
                        // Json object to System.Net.Http content type
                        var content = new StringContent(serializedProduct, Encoding.UTF8, "application/json");
                        // Post Request to the URI
                        var result = await client.PutAsync("api/Kiralama/" + kiralamaId, content);

                        // Check for result
                        if (result.IsSuccessStatusCode)
                        {
                            success = true;
                        }
                    }
                    // Set message depend on success info
                    var message = success ? "Araç Kiralandı" : "failed";
                    // Inform user
                    MessageBox.Show("" + message);
                }
                catch (Exception ex)
                {
                    // Inform user
                    MessageBox.Show("Error happened: " + ex.Message);
                }
                //İstekleri çek
                try
                {
                    // Create a HttpClient
                    using (var client = new HttpClient())
                    {
                        // Setup basics
                        client.BaseAddress = new Uri("http://localhost:25667/");
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                        // Get Request from the URI
                        using (var result = await client.GetAsync("api/Kiralama"))
                        {
                            // Check the Result
                            if (result.IsSuccessStatusCode)
                            {
                                // Take the Result as a json string
                                var value = result.Content.ReadAsStringAsync().Result;

                                // Deserialize the string with a Json Converter to ResponseContent object and fill the datagrid
                                istekler =
                                    JsonConvert.DeserializeObject <ResponseContent <Kiralama> >(value).Data.ToList();
                                dataGridView_KiralamaIstekler.DataSource = istekler;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Inform user if an error occurs
                    MessageBox.Show("Error happened: " + ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Kiralama verisi seçiniz");
            }
        }