예제 #1
0
        public static string Save(DTO.Component component)
        {
            string message;

            using (client = new HttpClient {
                BaseAddress = new Uri(Properties.Settings.Default.ServerUrl)
            })
            {
                HttpResponseMessage response;

                if (component.Id > 0)
                {
                    response = client.PutAsJsonAsync("component", component).Result;
                }
                else
                {
                    var serializedComponent = JsonConvert.SerializeObject(component);
                    var content             = new StringContent(serializedComponent, Encoding.UTF8, "application/json");
                    response = client.PostAsync("component", content).Result;
                }

                message = response.IsSuccessStatusCode ? "Operação realizada com sucesso!" : "Falha ao realizar a operação: " + response.StatusCode;
            }

            return(message);
        }
예제 #2
0
        public FormComponentForm(FormComponent formComponent, int Id = 0)
        {
            InitializeComponent();

            this.formComponent = formComponent;

            this.tbId.Enabled = false;

            try
            {
                if (Id > 0)
                {
                    DTO.Component component = ComponentCtrl.GetById(Id);

                    this.tbId.Text     = Id.ToString();
                    tbName.Text        = component.Name;
                    tbDescription.Text = component.Description;
                    tbAmount.Text      = component.Amount.ToString();
                    tbPrice.Text       = component.Price.ToString("0.00");
                    this.tbId.ReadOnly = true;
                }

                this.tbName.Select();
            }
            catch (AggregateException ex)
            {
                MessageBox.Show("Aplicação servidora não responde: " + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problema na solicitação: " + ex.Message);
            }
        }
예제 #3
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            DTO.Component component = new DTO.Component();
            if (tbId.Text != "")
            {
                component.Id = Convert.ToInt32(tbId.Text);
            }

            string validationMessage = "";

            if (tbName.Text.Trim() == "")
            {
                validationMessage += "Nome do componente é obrigatório. ";
            }

            int amount;

            if (!int.TryParse(tbAmount.Text, out amount))
            {
                validationMessage += "Quantidade deve ser inteiro. ";
            }

            double price;

            if (!double.TryParse(tbPrice.Text, out price))
            {
                validationMessage += "Preço inválido!";
            }

            if (validationMessage == "")
            {
                component.Name        = tbName.Text;
                component.Description = tbDescription.Text;
                component.Amount      = amount;
                component.Price       = price;

                try
                {
                    string message = ComponentCtrl.Save(component);
                    this.formComponent.UpdateDgvComponents();
                    MessageBox.Show(message);
                    this.Close();
                }
                catch (AggregateException ex)
                {
                    MessageBox.Show("Operação não realizada porque a aplicação servidora não responde: " + ex.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Operação não realizada. Problema na solicitação: " + ex.Message);
                }
            }
            else
            {
                MessageBox.Show(validationMessage);
            }
        }
예제 #4
0
        public static DTO.Component GetById(int Id)
        {
            DTO.Component component = null;

            using (client = new HttpClient {
                BaseAddress = new Uri(Properties.Settings.Default.ServerUrl)
            })
            {
                HttpResponseMessage response = client.GetAsync("component/" + Id).Result;

                if (response.IsSuccessStatusCode)
                {
                    var jsonString = response.Content.ReadAsStringAsync().Result;
                    component = JsonConvert.DeserializeObject <DTO.Component>(jsonString);
                }

                return(component);
            }
        }