Commit f78ff2de by Joona Romppanen

UserLoginControl cleanup & better exception handler

1 parent 9f01e3a2
...@@ -5,16 +5,20 @@ namespace MoyaAdminLib ...@@ -5,16 +5,20 @@ namespace MoyaAdminLib
{ {
public class MoyaApiException : WebException public class MoyaApiException : WebException
{ {
public HttpStatusCode StatusCode { get; set; }
public MoyaApiException() public MoyaApiException()
{ {
} }
public MoyaApiException(string message) : base(message) public MoyaApiException(string message, HttpStatusCode statusCode) : base(message)
{ {
StatusCode = statusCode;
} }
public MoyaApiException(string message, Exception innerException) : base(message, innerException) public MoyaApiException(string message, HttpStatusCode statusCode, Exception innerException) : base(message, innerException)
{ {
StatusCode = statusCode;
} }
} }
} }
...@@ -141,8 +141,7 @@ namespace MoyaAdminLib ...@@ -141,8 +141,7 @@ namespace MoyaAdminLib
if ((int)response.StatusCode < 200 && (int)response.StatusCode >= 300) if ((int)response.StatusCode < 200 && (int)response.StatusCode >= 300)
{ {
var message = string.Format("Request failed. Received HTTP {0}", response.StatusCode); throw new MoyaApiException("Request failed", response.StatusCode);
throw new ApplicationException(message);
} }
using (var responseStream = response.GetResponseStream()) using (var responseStream = response.GetResponseStream())
...@@ -171,7 +170,7 @@ namespace MoyaAdminLib ...@@ -171,7 +170,7 @@ namespace MoyaAdminLib
{ {
var responseValue = StreamToString(responseStream); var responseValue = StreamToString(responseStream);
Console.WriteLine("Response was " + responseValue); Console.WriteLine("Response was " + responseValue);
throw new MoyaApiException(responseValue, e); throw new WebException(responseValue, e);
} }
......
...@@ -60,13 +60,13 @@ ...@@ -60,13 +60,13 @@
this.txtEmail.Name = "txtEmail"; this.txtEmail.Name = "txtEmail";
this.txtEmail.Size = new System.Drawing.Size(395, 26); this.txtEmail.Size = new System.Drawing.Size(395, 26);
this.txtEmail.TabIndex = 67; this.txtEmail.TabIndex = 67;
this.txtEmail.TextChanged += new System.EventHandler(this.txtEmail_TextChanged); this.txtEmail.TextChanged += new System.EventHandler(this.TxtEmail_TextChanged);
this.txtEmail.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtEmail_KeyUp); this.txtEmail.KeyUp += new System.Windows.Forms.KeyEventHandler(this.TxtEmail_KeyUp);
// //
// timer1 // timer1
// //
this.timer1.Interval = 500; this.timer1.Interval = 500;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick); this.timer1.Tick += new System.EventHandler(this.Timer1_Tick);
// //
// infoLabel // infoLabel
// //
...@@ -89,7 +89,7 @@ ...@@ -89,7 +89,7 @@
this.passwordTextBox.Size = new System.Drawing.Size(125, 26); this.passwordTextBox.Size = new System.Drawing.Size(125, 26);
this.passwordTextBox.TabIndex = 93; this.passwordTextBox.TabIndex = 93;
this.passwordTextBox.Visible = false; this.passwordTextBox.Visible = false;
this.passwordTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.passwordTextBox_KeyDown); this.passwordTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.PasswordTextBox_KeyDown);
// //
// loginButton // loginButton
// //
...@@ -100,7 +100,7 @@ ...@@ -100,7 +100,7 @@
this.loginButton.Text = "Kirjaudu tapahtumaan"; this.loginButton.Text = "Kirjaudu tapahtumaan";
this.loginButton.UseVisualStyleBackColor = true; this.loginButton.UseVisualStyleBackColor = true;
this.loginButton.Visible = false; this.loginButton.Visible = false;
this.loginButton.Click += new System.EventHandler(this.loginButton_Click); this.loginButton.Click += new System.EventHandler(this.LoginButton_Click);
// //
// forgottenPasswordButton // forgottenPasswordButton
// //
...@@ -121,7 +121,7 @@ ...@@ -121,7 +121,7 @@
this.btnNewProfile.Text = "Lisää omat tiedot"; this.btnNewProfile.Text = "Lisää omat tiedot";
this.btnNewProfile.UseVisualStyleBackColor = true; this.btnNewProfile.UseVisualStyleBackColor = true;
this.btnNewProfile.Visible = false; this.btnNewProfile.Visible = false;
this.btnNewProfile.Click += new System.EventHandler(this.btnNewProfile_Click); this.btnNewProfile.Click += new System.EventHandler(this.BtnNewProfile_Click);
// //
// UserLoginControl // UserLoginControl
// //
......
using System; using System;
using System.Collections.Generic; using System.Diagnostics;
using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using MoyaAdminLib; using MoyaAdminLib;
using System.Web.Script.Serialization; using System.Web.Script.Serialization;
...@@ -17,25 +12,27 @@ namespace MoyaSignup ...@@ -17,25 +12,27 @@ namespace MoyaSignup
public partial class UserLoginControl : UserControl public partial class UserLoginControl : UserControl
{ {
public event EventHandler LoginOk; public event EventHandler LoginOk;
public delegate void ThermoPrinterEvent(ThermoPrinter.Models model); public delegate void ThermoPrinterEvent(ThermoPrinter.Models model);
public event ThermoPrinterEvent SetPrinter;
public event ThermoPrinterEvent SetPrinter;
public User CurrentUser;
public UserLoginControl() public UserLoginControl()
{ {
InitializeComponent(); InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent; BackColor = Color.Transparent;
infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi"; infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
} }
private void UserLoginControl_Load(object sender, EventArgs e) private void UserLoginControl_Load(object sender, EventArgs e)
{ {
} }
private void txtEmail_TextChanged(object sender, EventArgs e) private void TxtEmail_TextChanged(object sender, EventArgs e)
{ {
string txt = txtEmail.Text; var txt = txtEmail.Text;
if (IsValidEmail(txt)) if (IsValidEmail(txt))
{ {
timer1.Stop(); timer1.Stop();
...@@ -45,14 +42,12 @@ namespace MoyaSignup ...@@ -45,14 +42,12 @@ namespace MoyaSignup
{ {
if (txtEmail.Text == "thermo2") if (txtEmail.Text == "thermo2")
{ {
if(SetPrinter != null) SetPrinter?.Invoke(ThermoPrinter.Models.IDP3210);
SetPrinter(ThermoPrinter.Models.IDP3210);
txtEmail.Text = "OK"; txtEmail.Text = "OK";
} }
else if (txtEmail.Text == "thermo1") else if (txtEmail.Text == "thermo1")
{ {
if(SetPrinter != null) SetPrinter?.Invoke(ThermoPrinter.Models.TMT188II);
SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.TMT188II);
txtEmail.Text = "OK"; txtEmail.Text = "OK";
} }
...@@ -63,25 +58,35 @@ namespace MoyaSignup ...@@ -63,25 +58,35 @@ namespace MoyaSignup
} }
} }
bool IsValidEmail(string email) private static bool IsValidEmail(string email)
{ {
if (!email.Contains("@") || !email.Contains(".")) if (!email.Contains("@") || !email.Contains("."))
{
return false; return false;
string[] parts = email.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries); }
if (parts.Count<string>() == 2)
var parts = email.Split(new[] {"@"}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2)
{ {
if (!parts[1].Contains(".")) if (!parts[1].Contains("."))
{
return false; return false;
string[] domainparts = parts[1].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); }
if (domainparts.Count<string>() < 2) var domainparts = parts[1].Split(new[] {"."}, StringSplitOptions.RemoveEmptyEntries);
if (domainparts.Length < 2)
{
return false; return false;
string ltd = domainparts[domainparts.Count<string>() - 1]; }
if (ltd.Length < 2) var ltd = domainparts[domainparts.Length - 1];
if (ltd.Length < 2)
{
return false; return false;
}
}
else
{
return false;
} }
else return false;
try try
{ {
...@@ -93,102 +98,121 @@ namespace MoyaSignup ...@@ -93,102 +98,121 @@ namespace MoyaSignup
return false; return false;
} }
} }
public User CurrentUser;
private void timer1_Tick(object sender, EventArgs e) private void Timer1_Tick(object sender, EventArgs e)
{ {
RestClient client = new RestClient(); var client = new RestClient();
try try
{ {
string json = client.MakeRequest("user", "email=" + txtEmail.Text); var json = client.MakeRequest("user", "email=" + txtEmail.Text);
JavaScriptSerializer ser = new JavaScriptSerializer(); var ser = new JavaScriptSerializer();
Eventuser euser = ser.Deserialize<Eventuser>(json); var euser = ser.Deserialize<Eventuser>(json);
if (euser != null) if (euser != null)
{ {
btnNewProfile.Visible = false; btnNewProfile.Visible = false;
CurrentUser = new User(euser); CurrentUser = new User(euser);
infoLabel.Text = "Hei! " + CurrentUser + " Kirjoita salasanasi ja kirjaudu"; infoLabel.Text = "Hei! " + CurrentUser + " Kirjoita salasanasi ja kirjaudu";
showLogin(true); ShowLogin(true);
} }
} }
catch (MoyaApiException) catch (WebException ex)
{ {
CurrentUser = null; if (ex is MoyaApiException && ((MoyaApiException) ex).StatusCode == HttpStatusCode.NotFound)
infoLabel.Text = "Uusi kävijä?"; {
btnNewProfile.Visible = true; CurrentUser = null;
showLogin(false); infoLabel.Text = "Uusi kävijä?";
btnNewProfile.Visible = true;
ShowLogin(false);
}
else
{
MessageBox.Show(
"Yhteys taustajärjestelmään epäonnistui. Ota yhteys infoon.",
"Järjestelmävirhe",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
} }
catch (WebException) catch (Exception ex)
{ {
using (var eventLog = new EventLog("Application"))
{
eventLog.Source = "MoyaSignup";
eventLog.WriteEntry(string.Format("Odottamaton virhe:\n{0}", ex.StackTrace), EventLogEntryType.Error, 666);
}
MessageBox.Show( MessageBox.Show(
"Yhteys taustajärjestelmään epäonnistui. Ota yhteys infoon.", "Odottamaton virhe. Ota yhteys infoon.",
"Järjestelmävirhe", "Järjestelmävirhe",
MessageBoxButtons.OK, MessageBoxButtons.OK,
MessageBoxIcon.Error MessageBoxIcon.Error
); );
} }
timer1.Stop(); timer1.Stop();
} }
void showLogin(bool show) private void ShowLogin(bool show)
{ {
passwordTextBox.Visible = show; passwordTextBox.Visible = show;
loginButton.Visible = show; loginButton.Visible = show;
} }
private void login()
{
RestClient client;
client = new RestClient(RestClient.ApiURL, HttpVerb.POST); private void Login()
{
var client = new RestClient(RestClient.ApiURL, HttpVerb.POST);
//client.PostData = "password=" + WebUtility.UrlEncode(passwordTextBox.Text); //client.PostData = "password=" + WebUtility.UrlEncode(passwordTextBox.Text);
client.PostData = "password=" + passwordTextBox.Text; client.PostData = "password=" + passwordTextBox.Text;
client.ContentType = "application/x-www-form-urlencoded"; client.ContentType = "application/x-www-form-urlencoded";
try try
{ {
string json = client.MakeRequest("v2/user/" + CurrentUser.UserId + "/check-password"); var json = client.MakeRequest("v2/user/" + CurrentUser.UserId + "/check-password");
//string json2 = client.MakeRequest("user/"+ CurrentUser.UserId +"/check-password"); // string json2 = client.MakeRequest("user/"+ CurrentUser.UserId +"/check-password");
///MoyaWeb/rest/user/eventusers // MoyaWeb/rest/user/eventusers
JavaScriptSerializer ser = new JavaScriptSerializer(); var ser = new JavaScriptSerializer();
Eventuser euser = ser.Deserialize<Eventuser>(json); var euser = ser.Deserialize<Eventuser>(json);
if (euser != null) if (euser != null)
{ {
CurrentUser = new User(euser); CurrentUser = new User(euser)
CurrentUser.Email = txtEmail.Text; {
if (LoginOk != null) Email = txtEmail.Text
LoginOk(this, null); };
LoginOk?.Invoke(this, null);
} }
else else
wrongPass(); WrongPass();
} }
catch (Exception ex) catch (Exception)
{ {
wrongPass(); WrongPass();
} }
} }
private void loginButton_Click(object sender, EventArgs e)
private void LoginButton_Click(object sender, EventArgs e)
{ {
login(); Login();
} }
private void wrongPass()
private void WrongPass()
{ {
infoLabel.Text = "Väärä salasana. Syötä salasana uudelleen"; infoLabel.Text = "Väärä salasana. Syötä salasana uudelleen";
} }
private void passwordTextBox_KeyDown(object sender, KeyEventArgs e) private void PasswordTextBox_KeyDown(object sender, KeyEventArgs e)
{ {
if (e.KeyData == Keys.Enter) if (e.KeyData == Keys.Enter)
login(); Login();
} }
private void btnNewProfile_Click(object sender, EventArgs e) private void BtnNewProfile_Click(object sender, EventArgs e)
{ {
CurrentUser = new User(); CurrentUser = new User
CurrentUser.Email = txtEmail.Text; {
Email = txtEmail.Text
};
//Send info to parent control //Send info to parent control
if (LoginOk != null) LoginOk?.Invoke(this, null);
LoginOk(this, null);
} }
...@@ -199,7 +223,7 @@ namespace MoyaSignup ...@@ -199,7 +223,7 @@ namespace MoyaSignup
passwordTextBox.Visible = false; passwordTextBox.Visible = false;
passwordTextBox.Text = ""; passwordTextBox.Text = "";
SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent; BackColor = Color.Transparent;
infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi"; infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
loginButton.Visible = false; loginButton.Visible = false;
} }
...@@ -211,15 +235,15 @@ namespace MoyaSignup ...@@ -211,15 +235,15 @@ namespace MoyaSignup
Application.Exit(); Application.Exit();
return; return;
} }
else if (e.Alt && e.KeyCode == Keys.F5)
if (e.Alt && e.KeyCode == Keys.F5)
{ {
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210); //userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
//MessageBox.Show("Printteri: IDP3210"); //MessageBox.Show("Printteri: IDP3210");
} }
} }
private void txtEmail_KeyUp(object sender, KeyEventArgs e) private void TxtEmail_KeyUp(object sender, KeyEventArgs e)
{ {
if (e.Alt && e.Control && e.KeyCode == Keys.S) if (e.Alt && e.Control && e.KeyCode == Keys.S)
{ {
...@@ -227,12 +251,11 @@ namespace MoyaSignup ...@@ -227,12 +251,11 @@ namespace MoyaSignup
Application.Exit(); Application.Exit();
return; return;
} }
else if (e.Alt && e.KeyCode == Keys.F5) if (e.Alt && e.KeyCode == Keys.F5)
{ {
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210); //userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
//MessageBox.Show("Printteri: IDP3210"); //MessageBox.Show("Printteri: IDP3210");
} }
} }
} }
} }
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!