Commit f78ff2de by Joona Romppanen

UserLoginControl cleanup & better exception handler

1 parent 9f01e3a2
......@@ -5,16 +5,20 @@ namespace MoyaAdminLib
{
public class MoyaApiException : WebException
{
public HttpStatusCode StatusCode { get; set; }
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
if ((int)response.StatusCode < 200 && (int)response.StatusCode >= 300)
{
var message = string.Format("Request failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
throw new MoyaApiException("Request failed", response.StatusCode);
}
using (var responseStream = response.GetResponseStream())
......@@ -171,7 +170,7 @@ namespace MoyaAdminLib
{
var responseValue = StreamToString(responseStream);
Console.WriteLine("Response was " + responseValue);
throw new MoyaApiException(responseValue, e);
throw new WebException(responseValue, e);
}
......
......@@ -60,13 +60,13 @@
this.txtEmail.Name = "txtEmail";
this.txtEmail.Size = new System.Drawing.Size(395, 26);
this.txtEmail.TabIndex = 67;
this.txtEmail.TextChanged += new System.EventHandler(this.txtEmail_TextChanged);
this.txtEmail.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtEmail_KeyUp);
this.txtEmail.TextChanged += new System.EventHandler(this.TxtEmail_TextChanged);
this.txtEmail.KeyUp += new System.Windows.Forms.KeyEventHandler(this.TxtEmail_KeyUp);
//
// timer1
//
this.timer1.Interval = 500;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.timer1.Tick += new System.EventHandler(this.Timer1_Tick);
//
// infoLabel
//
......@@ -89,7 +89,7 @@
this.passwordTextBox.Size = new System.Drawing.Size(125, 26);
this.passwordTextBox.TabIndex = 93;
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
//
......@@ -100,7 +100,7 @@
this.loginButton.Text = "Kirjaudu tapahtumaan";
this.loginButton.UseVisualStyleBackColor = true;
this.loginButton.Visible = false;
this.loginButton.Click += new System.EventHandler(this.loginButton_Click);
this.loginButton.Click += new System.EventHandler(this.LoginButton_Click);
//
// forgottenPasswordButton
//
......@@ -121,7 +121,7 @@
this.btnNewProfile.Text = "Lisää omat tiedot";
this.btnNewProfile.UseVisualStyleBackColor = true;
this.btnNewProfile.Visible = false;
this.btnNewProfile.Click += new System.EventHandler(this.btnNewProfile_Click);
this.btnNewProfile.Click += new System.EventHandler(this.BtnNewProfile_Click);
//
// UserLoginControl
//
......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MoyaAdminLib;
using System.Web.Script.Serialization;
......@@ -17,25 +12,27 @@ namespace MoyaSignup
public partial class UserLoginControl : UserControl
{
public event EventHandler LoginOk;
public delegate void ThermoPrinterEvent(ThermoPrinter.Models model);
public event ThermoPrinterEvent SetPrinter;
public event ThermoPrinterEvent SetPrinter;
public User CurrentUser;
public UserLoginControl()
{
InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
BackColor = Color.Transparent;
infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
}
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))
{
timer1.Stop();
......@@ -45,14 +42,12 @@ namespace MoyaSignup
{
if (txtEmail.Text == "thermo2")
{
if(SetPrinter != null)
SetPrinter(ThermoPrinter.Models.IDP3210);
SetPrinter?.Invoke(ThermoPrinter.Models.IDP3210);
txtEmail.Text = "OK";
}
else if (txtEmail.Text == "thermo1")
{
if(SetPrinter != null)
SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.TMT188II);
SetPrinter?.Invoke(ThermoPrinter.Models.TMT188II);
txtEmail.Text = "OK";
}
......@@ -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;
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("."))
{
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;
string ltd = domainparts[domainparts.Count<string>() - 1];
if (ltd.Length < 2)
}
var ltd = domainparts[domainparts.Length - 1];
if (ltd.Length < 2)
{
return false;
}
}
else
{
return false;
}
else return false;
try
{
......@@ -93,102 +98,121 @@ namespace MoyaSignup
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
{
string json = client.MakeRequest("user", "email=" + txtEmail.Text);
JavaScriptSerializer ser = new JavaScriptSerializer();
Eventuser euser = ser.Deserialize<Eventuser>(json);
var json = client.MakeRequest("user", "email=" + txtEmail.Text);
var ser = new JavaScriptSerializer();
var euser = ser.Deserialize<Eventuser>(json);
if (euser != null)
{
btnNewProfile.Visible = false;
CurrentUser = new User(euser);
infoLabel.Text = "Hei! " + CurrentUser + " Kirjoita salasanasi ja kirjaudu";
showLogin(true);
ShowLogin(true);
}
}
catch (MoyaApiException)
catch (WebException ex)
{
CurrentUser = null;
infoLabel.Text = "Uusi kävijä?";
btnNewProfile.Visible = true;
showLogin(false);
if (ex is MoyaApiException && ((MoyaApiException) ex).StatusCode == HttpStatusCode.NotFound)
{
CurrentUser = null;
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(
"Yhteys taustajärjestelmään epäonnistui. Ota yhteys infoon.",
"Järjestelmävirhe",
MessageBoxButtons.OK,
"Odottamaton virhe. Ota yhteys infoon.",
"Järjestelmävirhe",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
timer1.Stop();
}
void showLogin(bool show)
private void ShowLogin(bool show)
{
passwordTextBox.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=" + passwordTextBox.Text;
client.ContentType = "application/x-www-form-urlencoded";
try
{
string json = client.MakeRequest("v2/user/" + CurrentUser.UserId + "/check-password");
//string json2 = client.MakeRequest("user/"+ CurrentUser.UserId +"/check-password");
///MoyaWeb/rest/user/eventusers
JavaScriptSerializer ser = new JavaScriptSerializer();
Eventuser euser = ser.Deserialize<Eventuser>(json);
var json = client.MakeRequest("v2/user/" + CurrentUser.UserId + "/check-password");
// string json2 = client.MakeRequest("user/"+ CurrentUser.UserId +"/check-password");
// MoyaWeb/rest/user/eventusers
var ser = new JavaScriptSerializer();
var euser = ser.Deserialize<Eventuser>(json);
if (euser != null)
{
CurrentUser = new User(euser);
CurrentUser.Email = txtEmail.Text;
if (LoginOk != null)
LoginOk(this, null);
CurrentUser = new User(euser)
{
Email = txtEmail.Text
};
LoginOk?.Invoke(this, null);
}
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";
}
private void passwordTextBox_KeyDown(object sender, KeyEventArgs e)
private void PasswordTextBox_KeyDown(object sender, KeyEventArgs e)
{
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.Email = txtEmail.Text;
CurrentUser = new User
{
Email = txtEmail.Text
};
//Send info to parent control
if (LoginOk != null)
LoginOk(this, null);
LoginOk?.Invoke(this, null);
}
......@@ -199,7 +223,7 @@ namespace MoyaSignup
passwordTextBox.Visible = false;
passwordTextBox.Text = "";
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
BackColor = Color.Transparent;
infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
loginButton.Visible = false;
}
......@@ -211,15 +235,15 @@ namespace MoyaSignup
Application.Exit();
return;
}
else if (e.Alt && e.KeyCode == Keys.F5)
if (e.Alt && e.KeyCode == Keys.F5)
{
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.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)
{
......@@ -227,12 +251,11 @@ namespace MoyaSignup
Application.Exit();
return;
}
else if (e.Alt && e.KeyCode == Keys.F5)
if (e.Alt && e.KeyCode == Keys.F5)
{
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.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!