Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
NancyFx module - FanClub demo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Nancy;
/// <summary>FanClub NancyFx Module</summary>
public class FanClubModule : NancyModule
{
private static readonly string HOME_URL = VirtualPathUtility.ToAbsolute("~/pogodnosti");
private static readonly string SENT_OK_URL = VirtualPathUtility.ToAbsolute("~/status/uspjeh");
private static readonly string SEND_ERR_URL = VirtualPathUtility.ToAbsolute("~/status/greska");
private static readonly string CANCEL_URL = HOME_URL;
public FanClubModule()
{
// == STATIC CONTENT
Get[@"/js/{name}"] = x => {
return Response.AsJs(string.Format("js/{0}", (string)x.name));
};
Get[@"/css/{name}"] = x => {
return Response.AsCss(string.Format("css/{0}", (string)x.name));
};
Get[@"/img/{name}"] = x => {
return Response.AsImage(string.Format("img/{0}", (string)x.name));
};
// == DYNAMIC CONTENT - routes list: /pogodnosti, /info, /partneri, /rezervacija/<title>, /status/<status>
// =============================================================================================================
Get["/{name}"] = x => {
string view = string.IsNullOrEmpty((string)x.name) ? "pogodnosti" : (string)x.name;
var articles = Repository.GetFanClubArticles();
var offers = Repository.GetActiveOffers();
var categorizedOffers = new Dictionary<dynamic, dynamic>();
offers.ToList().ForEach(o =>
{
if (!categorizedOffers.ContainsKey(o.categoryName.ToString()))
{
categorizedOffers.Add(o.categoryName, new List<dynamic>());
}
categorizedOffers[o.categoryName].Add(o);
});
dynamic model = new
{
Articles = articles,
CategorizedOffers = categorizedOffers,
};
return View[view, model];
};
Get["/rezervacija/{subject}"] = x => {
if (string.IsNullOrEmpty((string)x.subject))
return Response.AsRedirect(HOME_URL);
string view = "rezervacija";
string subject = (string)x.subject;
var articles = Repository.GetFanClubArticles();
dynamic model = new
{
Articles = articles,
Subject = subject,
Error = string.Empty,
};
return View[view, model];
};
Post["/rezervacija"] = x => {
if (Request.Form.cancel.Value != null)
return Response.AsRedirect(CANCEL_URL);
string senderName = Request.Form.name.Value.Trim();
string senderAddress = Request.Form.email.Value.Trim();
string fanclubno = Request.Form.fanclubno.Value.Trim();
string subject = Request.Form.subject.Value.Trim();
string body = Request.Form.body.Value.Trim();
try
{
FanClubMailer mailer = new FanClubMailer(senderName, senderAddress, fanclubno, subject, body);
try
{
mailer.ProcessReservation();
return Response.AsRedirect(SENT_OK_URL); // PRG redirect
}
catch
{
return Response.AsRedirect(SEND_ERR_URL); // PRG redirect
}
}
catch (Exception exc)
{
string view = "rezervacija";
var articles = Repository.GetFanClubArticles();
dynamic model = new
{
Articles = articles,
Subject = subject,
Error = exc.Message,
};
return View[view, model];
}
};
Get["/status/{status}"] = x => {
string view = "status";
string status = (string)x.status;
var articles = Repository.GetFanClubArticles();
dynamic model = new
{
Articles = articles,
Status = status,
};
return View[view, model];
};
Get["/"] = x => {
return Response.AsRedirect(HOME_URL);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment