private void LoginButton_Click(object sender, EventArgs e)
        {
            loginButton.Enabled = false;
            // This line will yield control to the UI as the request
            // from the web service is happening.
            //
            // The UI thread is now free to perform other work.
            if (string.IsNullOrWhiteSpace(boxUser.Text) || string.IsNullOrWhiteSpace(boxPass.Text))
            {
                MessageBox.Show("Mohon isi user dan/atau password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                // refer to BackgroundTask.cs for more detail
                //UserData userData = new UserData();

                /*string result = new UserData().execute(userBox.Text, passBox.Text);
                 * // compare response
                 * if (result.Contains("PEL-"))
                 * {
                 *  // initialize MainForm
                 *  //mainForm = new MainForm();
                 *  this.Hide();
                 *  using (MainForm = new MainForm())
                 *  {
                 *      MainForm.FormClosing += MainForm_Closing;
                 *      //mainForm.ShowDialog();
                 *      if (MainForm.ShowDialog() == DialogResult.OK)
                 *      {
                 *          //Console.WriteLine("user " + result);
                 *          MainForm.UserLogged = result;
                 *      }
                 *  }
                 * }
                 * else
                 *  MessageBox.Show("Login gagal! Silakan periksa kembali data anda.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 */
                Datas datas = new Datas();
                datas.id_supplier = boxUser.Text;
                datas.password    = boxPass.Text;
                var ser = datas.ToJson();

                // Specify requirement to POST
                //var stringData = await _httpClient.GetStringAsync();
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://kitku.id/pelanggan/login");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";

                // write data
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    //string json = sr.ReadToEnd();
                    streamWriter.Write(ser);
                }

                // get response
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var res = Datas.FromJson(streamReader.ReadToEnd());
                    //Console.WriteLine(res.id_supplier);

                    // if data gotten
                    if (res.message != null)
                    {
                        // initialize MainForm
                        //mainForm = new MainForm();
                        this.Hide();
                        using (MainForm = new MainForm())
                        {
                            MainForm.FormClosing += MainForm_Closing;
                            //mainForm.ShowDialog();
                            if (MainForm.ShowDialog() == DialogResult.OK)
                            {
                                //Console.WriteLine("user " + result);
                                MainForm.UserLogged = res.message;
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Tidak ditemukan supplier dengan nama ini.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            loginButton.Enabled = true;
        }
Esempio n. 2
0
 public static string ToJson(this Datas self) => JsonConvert.SerializeObject(self, Converter.Settings);
        private void ButtonSimpan_Click(object sender, EventArgs e)
        {
            buttonSimpan.Enabled = false;
            if (string.IsNullOrEmpty(boxIDSupplier.Text))
            {
                MessageBox.Show("Silakan dapatkan kode mitra terlebih dahulu dengan mengecek nama mitra", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (
                !string.IsNullOrEmpty(boxKategori.Text) ||
                !string.IsNullOrEmpty(boxKodeBarang.Text) ||
                !string.IsNullOrEmpty(boxNamaBarang.Text) ||
                !string.IsNullOrEmpty(boxSatuanInt.Text) ||
                !string.IsNullOrEmpty(boxHarga.Text) ||
                !string.IsNullOrEmpty(boxDeskripsi.Text) ||
                fileDialog != null
                )
            {
                // convert to json
                Datas datas = new Datas();
                datas.kategori    = boxKategori.Text;
                datas.id_barang   = boxKodeBarang.Text;
                datas.id_supplier = id_supplier;
                datas.nama        = boxNamaBarang.Text;
                datas.deskripsi   = boxDeskripsi.Text;
                datas.satuan      = boxSatuanInt.Text;
                datas.harga       = boxHarga.Text;
                var ser = datas.ToJson();
                Console.WriteLine(ser);

                bool dbSuccess = false, imageSuccess = false;

                // Specify requirement to POST
                //var stringData = await _httpClient.GetStringAsync();
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://kitku.id/produk/add");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";

                // write data
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    //string json = sr.ReadToEnd();
                    streamWriter.Write(ser);
                }

                // get response
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var res = Datas.FromJson(streamReader.ReadToEnd());
                    //Console.WriteLine(res.id_supplier);

                    // if data gotten
                    if (res.message != null)
                    {
                        dbSuccess        = true;
                        boxIDBarang.Text = res.id_barang;
                    }
                }

                string imageUploadMessage = UploadImage.HttpUploadFile("https://kitku.id/produk/updatepic/" + datas.nama.Replace(" ", ""), fileDialog.FileName, null);
                if (imageUploadMessage.Contains("Success"))
                {
                    imageSuccess = true;
                }

                if (dbSuccess && imageSuccess)
                {
                    MessageBox.Show("Produk berhasil ditambahkan!", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    if (!dbSuccess)
                    {
                        MessageBox.Show("Produk gagal ditambahkan!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    if (!imageSuccess)
                    {
                        MessageBox.Show("Gambar gagal diupload!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Silakan lengkapi data dan gambar untuk menambah produk.", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            buttonSimpan.Enabled = true;
        }