private void clickOnBtnConfirm(object sender, EventArgs e) { string token = ""; try { token = joinService.findToken(); } catch (Exception ex) { MessageBox.Show("Unexpected error in database has occured."); } if (("false").Equals(token)) { MessageBox.Show("First you must log in."); this.Close(); } else { if (("").Equals(tfSecondMember.Text)) { MessageBox.Show("Fill in username of your team colleague."); } else { TeamPairDTO teamPair = new TeamPairDTO(); teamPair.teamMemberName = tfSecondMember.Text; teamPair.token = token; joinService.isConnected(teamPair, this); } } }
/// <summary> /// method checks internet connection before uploading name of new team member /// </summary> /// <param name="teamPair"></param> /// <param name="joinWindow"></param> public void isConnected(TeamPairDTO teamPair, JoinWindow joinWindow) { try { using (WebClient webClient = new WebClient()) { using (var stream = webClient.OpenRead(Utils.serviceAddress)) { stream.Close(); this.uploadTeamMemberUsername(teamPair, joinWindow); } } } catch (Exception ex) { MessageBox.Show("Server is unavailable. Check your internet connection."); } }
/// <summary> /// method uploads name of new team member /// </summary> /// <param name="teamPair">instance of TeamPairDTO containing token and new team member name</param> /// <param name="joinWindow">window for joining colleague to team</param> public void uploadTeamMemberUsername(TeamPairDTO teamPair, JoinWindow joinWindow) { using (WebClient webClient = new WebClient()) { string result = ""; string data = ""; webClient.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8"; data = ChangeService.EncodeNonAsciiCharacters(teamPair.serialize()); try { result = webClient.UploadString(Utils.serviceAddress + "/auth/pair", data); joinWindow.closeWindow(); MessageBox.Show("Confirmation email has been sent to email address of your colleague."); } catch(WebException ex) { var response = ex.Response as HttpWebResponse; int code = (int)response.StatusCode; if (code == 401) { joinWindow.closeWindow(); MessageBox.Show("Please, log in once again."); } else if (code == 403) { MessageBox.Show("Self-addition to team is forbidden."); } else if (code == 405) { joinWindow.closeWindow(); MessageBox.Show("Your colleague has already invited you to team. You must click on link in the email in your email address."); } else if (code == 400) { MessageBox.Show("Your colleague must log in or you have filled incorrect username."); } else if (code == 409) { MessageBox.Show("Filled colleague is already in your team."); } else if (code == 406) { joinWindow.closeWindow(); MessageBox.Show("Team can have maximally 2 members."); } } catch (Exception ex2) { MessageBox.Show("Unexpected error has occured."); } } }