using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using MvcBootstrap.Models;
using System.Web.Hosting;
namespace MvcBootstrap.Controllers
{
public class HomeController : Controller
{
//AlbacoreComicsDB db = new AlbacoreComicsDB();
MvcBootstrapDB db = new MvcBootstrapDB();
public ActionResult Index()
{
ViewBag.lblErrMsg = "Welcome to MvcBoostrap. Thanks for stopping by.";
return View();
}
public ActionResult About()
{
return View();
}
//------------------------------------------------------------------
// derive AbstractComicClass
class AbstractComicClassDerived : AbstractComicClass
{
//implement abstract method GetAllComicsAbstract
public override List GetAllComicsAbstract()
{
//AlbacoreComicsDB db = new AlbacoreComicsDB();
MvcBootstrapDB db = new MvcBootstrapDB();
List comicsList = new List();
comicsList = db.Comics.OrderBy(c => c.Title).ToList();
return comicsList;
}
}
public ActionResult AbstractClassIndex()
{
//I can instantiate two instances of AbstractComicClassDerived class
//instantiation 1
AbstractComicClassDerived child1 = new AbstractComicClassDerived();
//use nonAbstract method of child1 derived class
List comicsList1 = child1.GetAllComicsNonAbstract();
//implement Abstract method of child1 derived class
List comicsList2 = child1.GetAllComicsAbstract();
//pass implementations to ViewBag
ViewBag.ComicsList1 = comicsList1;
ViewBag.ComicsList2 = comicsList2;
//-----------------------------------------------------------------
//instantiation 2
AbstractComicClassDerived child2 = new AbstractComicClassDerived();
//use nonAbstract method of child2 derived class
List comicsList3 = child2.GetAllComicsNonAbstract();
//implement Abstract method of child2 derived class
List
comicsList4 = child2.GetAllComicsAbstract();
ViewBag.ComicsList3 = comicsList3;
ViewBag.ComicsList4 = comicsList4;
return View(comicsList1);
}
public ActionResult Angular1()
{
return View();
}
public ActionResult Angular2()
{
return View();
}
public ActionResult CambridgeTest()
{
string[] emails = {
"test.email+amir@example.com",
"test.e.mail+amir.betty@example.com",
"testemail+lisa@exam.ple.com",
"test.e.m.a.i.l@example.com",
"testemaile@xample.com"
};
List<CambridgeModel> cmList = new List<CambridgeModel>();
cmList = CountUniqueEmails(emails);
return View(cmList);
}
public List<CambridgeModel> CountUniqueEmails(string[] emails)
{
// TODO: Implement a solution here
//Probably better to use Regular Expressions
int uniqueEmailCount = 0;
ArrayList uniqueEmails = new ArrayList();
List<CambridgeModel> cmList = new List<CambridgeModel>();
foreach (string s in emails)
{
string localName = string.Empty;
string domainName = string.Empty;
string newEmail = string.Empty;
//get position of the @ sign
int atSign = s.IndexOf("@");
//seperate local and domain names
localName = s.Substring(0, atSign);
domainName = s.Substring(atSign + 1);
//get position of the first + sign in the localName
int plusSign = localName.IndexOf("+");
if (plusSign > 0)
{
//get rid of anything after first '+' character
localName = localName.Substring(0, plusSign);
}
//replace any dots
localName = localName.Replace(".", "");
//reassemble email
newEmail = localName + "@" + domainName;
CambridgeModel cm = new CambridgeModel();
cm.OriginalEmail = s;
cm.EditedEmail = newEmail;
cmList.Add(cm);
//if first time thru, add to hold
if (uniqueEmails.Count == 0)
{
uniqueEmailCount = 1;
uniqueEmails.Add(newEmail);
}
else
{
//check if email was held
bool foundEmail = false;
foreach (string heldEmail in uniqueEmails)
{
if (heldEmail == newEmail)
{
foundEmail = true;
break;
}
}
if (foundEmail == false)
{
uniqueEmailCount++;
uniqueEmails.Add(newEmail);
}
}
}
ViewBag.lblEmailCount = uniqueEmailCount;
return cmList;
}
public ActionResult Carousel()
{
return View();
}
public ActionResult Columnizer()
{
return View();
}
public ActionResult ComicsAPI()
{
//get the default URI for ComicsAPI
string comicsAPIUrl = Url.HttpRouteUrl("DefaultApi", new { controller = "ComicsAPI", id = "" });
string Uri = new Uri(Request.Url, comicsAPIUrl).AbsoluteUri.ToString();
ViewBag.comicsAPIUrl = Uri;
return View();
}
public ActionResult ComicsByTitle(string inString)
{
return RedirectToAction("ComicsByTitle", "Home",
new { Area = "AlbacoreComics", id = inString });
}
public ActionResult ContactUs()
{
return View();
}
//declare a delegate
public delegate Comics DelegateComic(string inSearchBy, string inSearchString);
public Comics GetComicByDDL(string inSearchBy, string inSearchString)
{
Comics comic = new Comics();
comic = db.Comics.Where(c => c.Title == inSearchString).FirstOrDefault();
return comic;
}
public Comics GetComicBySearch(string inSearchBy, string inSearchString)
{
Comics comic = new Comics();
comic = db.Comics.Where(c => c.Title.Contains(inSearchString)).FirstOrDefault();
//comic = db.Comics.Where(c => c.Title == inSearchString).FirstOrDefault();
return comic;
}
//Controller method to call View
public ActionResult DelegateIndex(string inSearchBy, string inSearchString)
{
LoadTitlesDDL();
Comics comic = new Comics();
//instantiate new DelegateComic reference
DelegateComic getComicByDDL = new DelegateComic(GetComicByDDL);
DelegateComic getComicBySearch = new DelegateComic(GetComicBySearch);
//use the Delegate method to retrieve a comic
if (inSearchBy == "ByDDL")
{
comic = getComicByDDL(inSearchBy, inSearchString);
}
else if (inSearchBy == "BySearch")
{
comic = getComicBySearch(inSearchBy, inSearchString);
}
return View(comic);
}
public ActionResult ForgotLogIn()
{
return View();
}
class iComicsList : ComicsInterface
{
AlbacoreComicsDB db = new AlbacoreComicsDB();
public List
iComicsListAsc()
{
//AlbacoreComicsDB db = new AlbacoreComicsDB();
List
comicsList = db.Comics.OrderBy(c => c.Title).ToList();
return comicsList;
}
public List
iComicsListDesc()
{
//AlbacoreComicsDB db = new AlbacoreComicsDB();
List
comicsList = db.Comics.OrderByDescending(c => c.Title).ToList();
return comicsList;
}
}
public ActionResult InterfaceIndex()
{
iComicsList i = new iComicsList();
ViewBag.iComicsListAsc = i.iComicsListAsc();
ViewBag.iComicsListDesc = i.iComicsListDesc();
return View();
}
public void LoadTitlesDDL()
{
List
titlesList = db.Titles.ToList();
List
selTitles = new List
();
foreach (Titles title in titlesList)
{
selTitles.Add(new SelectListItem { Text = title.Title, Value = title.TitleID.ToString() });
}
ViewBag.Titles = selTitles;
}
public Boolean LogInn(string inString)
{
LogInModel lim = new LogInModel();
bool stat = lim.LogInn(inString);
return stat;
}
public ActionResult NestedDropdown()
{
return View();
}
public ActionResult RecursionIndex()
{
String d = HostingEnvironment.MapPath(@"/Areas/AlbacoreComics/Images");
List
fileList = new List
();
findFiles(d, fileList);
return View();
}
private void findFiles(string path, IList
fileList)
{
foreach(string fileName in Directory.GetFiles(path))
{
int loc = 0;
loc = fileName.IndexOf("Images");
string f = fileName.Substring(loc);
fileList.Add(f);
}
foreach(string directory in Directory.GetDirectories(path))
{
findFiles(directory, fileList);
}
ViewBag.ImageFiles = fileList;
}
public ActionResult Register()
{
ViewBag.lblErrMsg = "Enter your information below";
return View();
}
public ActionResult TableAPI()
{
//get the default URI for ComicsAPI
string comicsAPIUrl = Url.HttpRouteUrl("DefaultApi", new { controller = "ComicsAPI", id = "" });
string Uri = new Uri(Request.Url, comicsAPIUrl).AbsoluteUri.ToString();
ViewBag.comicsAPIUrl = Uri;
return View();
}
}
}