private async void btnAddContact_Click(object sender, RoutedEventArgs e) { Contact newContact = new Contact(); Logic.HttpClientContact httpClientContact = new Logic.HttpClientContact(); if (txtemployee_id.Text != "") { int typeContact = Convert.ToInt32(((ComboBoxItem)cbTypeContact.SelectedItem).DataContext); newContact.contact_type = typeContact; newContact.content = txtContact.Text; newContact.employee_id = Convert.ToInt32(txtemployee_id.Text); ResultApiContact resultCreateContact = await httpClientContact.createNewContactAsyns(newContact); if (resultCreateContact.code == 0) { this.showContactsAsyns(newContact.employee_id); MessageBox.Show("Сохранено"); } else { MessageBox.Show(resultCreateContact.info); } } else { MessageBox.Show("Пользователь не сохранен"); } }
public async Task <List <Contact> > getListContactAsyns(int employee_id) { ResultApiContact resultApiContact = new ResultApiContact(); List <Contact> lstContacts = new List <Contact>(); HttpClient client = new HttpClient(); //string req = urlEmployeeApi + "?employee_id=" + employee_id.ToString(); string req = urlEmployeeApi + "/" + employee_id.ToString(); HttpResponseMessage response = await client.GetAsync(req); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); resultApiContact = JsonConvert.DeserializeObject <ResultApiContact>(responseBody); if (resultApiContact.code >= 0) { lstContacts = resultApiContact.lstContact; } return(lstContacts); }
public async Task <ResultApiContact> createNewContactAsyns(Contact contact) { HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(urlEmployeeApi); var contentJson = JsonConvert.SerializeObject(contact); var data = new StringContent(contentJson, Encoding.UTF8, "application/json"); var responce = await httpClient.PostAsync(httpClient.BaseAddress, data); string resultStr = responce.Content.ReadAsStringAsync().Result; ResultApiContact resultApi = (ResultApiContact)JsonConvert.DeserializeObject(resultStr, typeof(ResultApiContact)); return(resultApi); }
public ResultApiContact getContactsOfEmployee(int employee_id) { List <Contact> listContact = new List <Contact>(); var result = new ResultApiContact(); string get_contacts_of_employee = "get_contacts_of_employee"; using (SqlConnection connection = new SqlConnection(sqlConnectionString)) { SqlCommand command = new SqlCommand(get_contacts_of_employee, connection); command.CommandType = System.Data.CommandType.StoredProcedure; SqlParameter sp_employee_id = new SqlParameter("@employee_id", System.Data.SqlDbType.Int); sp_employee_id.Value = employee_id; sp_employee_id.Direction = System.Data.ParameterDirection.Input; command.Parameters.Add(sp_employee_id); command.Connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { Contact contact = new Contact(); contact.employee_id = contact.contact_id = Convert.ToInt32(reader.GetValue(0)); contact.employee_id = Convert.ToInt32(reader.GetValue(1)); contact.contact_type = Convert.ToInt32(reader.GetValue(2)); contact.content = (reader.GetValue(3)).ToString(); contact.verify = Convert.ToInt32(reader.GetValue(4)); listContact.Add(contact); } } command.Connection.Close(); result.code = 0; result.lstContact = listContact; return(result); } }
public ResultApiContact Post([FromBody] Contact newContact) { ResultApiContact result; try { result = db.addNewContact(newContact); } catch (Exception e) { result = new ResultApiContact(); result.code = -1; result.info = "Ошибка полкчения данных. " + e.Message.ToString(); } return(result); }
public ResultApiContact Get(int employee_id) { ResultApiContact resultApiContact = new ResultApiContact(); try { var lstContacts = db.getContactsOfEmployee(employee_id).lstContact; resultApiContact.lstContact = lstContacts; resultApiContact.code = 0; } catch (Exception e) { resultApiContact.code = -1; resultApiContact.info = "Ошибка получения списка контактов" + e.Message.ToString(); } return(resultApiContact); }
public ResultApiContact addNewContact(Contact newContact) { string info = string.Empty; int ret = 0; ResultApiContact resultApiContact = new ResultApiContact(); string iud_contact = "iud_contact"; using (SqlConnection connection = new SqlConnection(sqlConnectionString)) { SqlCommand command = new SqlCommand(iud_contact, connection); command.CommandType = System.Data.CommandType.StoredProcedure; SqlParameter sp_iud = new SqlParameter("@iud", System.Data.SqlDbType.Int); sp_iud.Value = 1; sp_iud.Direction = System.Data.ParameterDirection.Input; SqlParameter sp_contact_id = new SqlParameter("@contact_id", System.Data.SqlDbType.Int); sp_contact_id.Value = null; sp_contact_id.Direction = System.Data.ParameterDirection.Input; SqlParameter sp_employee_id = new SqlParameter("@employee_id", System.Data.SqlDbType.Int); sp_employee_id.Value = newContact.employee_id; sp_employee_id.Direction = System.Data.ParameterDirection.Input; SqlParameter sp_contact_type = new SqlParameter("@contact_type", System.Data.SqlDbType.Int); sp_contact_type.Value = newContact.contact_type; sp_contact_type.Direction = System.Data.ParameterDirection.Input; SqlParameter sp_content = new SqlParameter("@content", System.Data.SqlDbType.NVarChar); sp_content.Value = newContact.content; sp_content.Direction = System.Data.ParameterDirection.Input; SqlParameter sp_comment = new SqlParameter("@comment", System.Data.SqlDbType.NVarChar); sp_comment.Value = newContact.comment; sp_comment.Direction = System.Data.ParameterDirection.Input; SqlParameter sp_verify = new SqlParameter("@verify ", System.Data.SqlDbType.Int); sp_verify.Value = newContact.verify; sp_verify.Direction = System.Data.ParameterDirection.Input; command.Parameters.Add(sp_iud); command.Parameters.Add(sp_contact_id); command.Parameters.Add(sp_employee_id); command.Parameters.Add(sp_contact_type); command.Parameters.Add(sp_content); command.Parameters.Add(sp_comment); command.Parameters.Add(sp_verify); command.Connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { ret = Convert.ToInt32(reader.GetValue(0)); info = reader.GetValue(1).ToString(); } } command.Connection.Close(); if (ret > 0) { newContact.contact_id = ret; resultApiContact.code = 0; resultApiContact.info = info; resultApiContact.contact = newContact; } else { resultApiContact.code = -1; resultApiContact.info = info; resultApiContact.contact = newContact; } return(resultApiContact); } }