Advertisement
Guest User

LoginController

a guest
Mar 9th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. namespace Forum.App.Controllers
  2. {
  3.     using Forum.App.Controllers.Contracts;
  4.     using Forum.App.Services;
  5.     using Forum.App.UserInterface;
  6.     using Forum.App.UserInterface.Contracts;
  7.     using Forum.App.Views;
  8.     using System;
  9.  
  10.     public class LogInController : IController, IReadUserInfoController
  11.     {
  12.         private enum Command
  13.         {
  14.             ReadUsername,
  15.             ReadPassword,
  16.             LogIn,
  17.             Back
  18.         }
  19.  
  20.         public LogInController()
  21.         {
  22.             this.ResetLogin();
  23.         }
  24.  
  25.         public string Username { get; private set; }
  26.  
  27.         private string Password { get; set; }
  28.  
  29.         private bool Error { get; set; }
  30.  
  31.         public MenuState ExecuteCommand(int index)
  32.         {
  33.             switch ((Command)index)
  34.             {
  35.                 case Command.ReadUsername:
  36.                     this.ReadUsername();
  37.                     return MenuState.Login;
  38.                 case Command.ReadPassword:
  39.                     this.ReadPassword();
  40.                     return MenuState.Login;
  41.                 case Command.LogIn:
  42.                     bool loggedIn = UserService.TryLogInUser(this.Username, this.Password);
  43.                     if (loggedIn)
  44.                     {
  45.                         return MenuState.SuccessfulLogIn;
  46.                     }
  47.                     this.Error = true;
  48.                     return MenuState.Rerender;
  49.                 case Command.Back:
  50.                     this.ResetLogin();
  51.                     return MenuState.Back;
  52.             }
  53.  
  54.             throw new InvalidOperationException();
  55.         }
  56.  
  57.         public IView GetView(string userName)
  58.         {
  59.             return new LogInView(this.Error, this.Username, this.Password.Length);
  60.         }
  61.  
  62.         public void ReadPassword()
  63.         {
  64.             this.Password = ForumViewEngine.ReadRow();
  65.             ForumViewEngine.HideCursor();
  66.         }
  67.  
  68.         public void ReadUsername()
  69.         {
  70.             this.Username = ForumViewEngine.ReadRow();
  71.             ForumViewEngine.HideCursor();
  72.         }
  73.  
  74.         private void ResetLogin()
  75.         {
  76.             this.Error = false;
  77.             this.Username = String.Empty;
  78.             this.Password = String.Empty;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement