Skip to content

Instantly share code, notes, and snippets.

@dalegaspi
Created May 31, 2014 18:28
Show Gist options
  • Save dalegaspi/894be71b5b52653e7553 to your computer and use it in GitHub Desktop.
Save dalegaspi/894be71b5b52653e7553 to your computer and use it in GitHub Desktop.
A simple Nancy Module that uses DataStax drivers to read data from Cassandra
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using Nancy;
using Nancy.ModelBinding;
using Cassandra.Data.Linq;
using Cassandra.Data;
using Cassandra;
namespace AP.ECRNextGen
{
[Table("latest_documents")]
public class APPLDocVersion
{
[PartitionKey(0)]
[Column("item_id")]
public Guid ItemId { get; set; }
[ClusteringKey(0, "DESC")]
[Column("record_seqno")]
public int RecordSequenceNumber { get; set; }
}
[Table("document_versions")]
public class LatestAPPLDocVersion
{
[PartitionKey(0)]
[Column("item_id")]
public Guid ItemId { get; set; }
[ClusteringKey(0)]
[Column("record_seqno")]
public int RecordSequenceNumber { get; set; }
}
[Table("documents")]
public class APPLDoc
{
[PartitionKey(0)]
[Column("item_id_rsn")]
public string ItemIdRSN { get; set; }
[ClusteringKey(0)]
[Column("record_seqno")]
public int RecordSequenceNumber { get; set; }
[Column("status")]
public string Status { get; set; }
[Column("appl")]
public string APPL { get; set; }
[Column("update_time")]
public DateTime UpdateTime { get; set; }
[Column("arrival_time")]
public DateTime ArrivalTime { get; set; }
}
public class DocumentModule : NancyModule
{
public DocumentModule()
{
var cluster = Cluster.Builder()
.AddContactPoint("127.0.0.1")
.Build();
var s = cluster.Connect("ci_appl");
Table<APPLDoc> docs_table = s.GetTable<APPLDoc>();
Table<APPLDocVersion> docs_versions_table = s.GetTable<APPLDocVersion>();
Get["/documents/{itemid}"] = _ =>
{
string item_id = _.itemid;
var latest_item = (from d in docs_versions_table
where d.ItemId == Guid.Parse(item_id)
orderby d.RecordSequenceNumber descending
select d).FirstOrDefault().Execute();
var iid_rsn = latest_item.ItemId.ToString("n") + "_" + latest_item.RecordSequenceNumber;
var appl_doc = (from d in docs_table
where d.ItemIdRSN == iid_rsn
select d.APPL).FirstOrDefault().Execute();
var response = (Response)appl_doc;
response.ContentType = "application/xml";
return response;
};
Get["/documents/{itemid}/versions/{rsn}"] = _ =>
{
string item_id = _.itemid;
string rsn = _.rsn;
var iid_rsn = item_id + "_" + rsn;
var appl_doc = (from d in docs_table
where d.ItemIdRSN == iid_rsn
select d.APPL).FirstOrDefault().Execute();
var response = (Response)appl_doc;
response.ContentType = "application/xml";
return response;
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment