Skip to content

Instantly share code, notes, and snippets.

@jaehue
Last active May 12, 2016 15:16
Show Gist options
  • Select an option

  • Save jaehue/111b887a82f96621ba2c25df6dc8612b to your computer and use it in GitHub Desktop.

Select an option

Save jaehue/111b887a82f96621ba2c25df6dc8612b to your computer and use it in GitHub Desktop.
using Dapper;
using Nancy;
using Nancy.IO;
using Nancy.ModelBinding;
using Nancy.Json;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
namespace Sample
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Module : NancyModule
{
private string connString = "Server=192.168.99.100;Database=sample;Uid=root;Pwd=password;";
public Module()
{
Get["/users"] = _ =>
{
using (var connection = new MySqlConnection(connString))
{
var users = connection.Query<User>("select * from Users");
return Response.AsJson(users);
}
};
Get["/users/{id}"] = parameters =>
{
using (var connection = new MySqlConnection(connString))
{
var users = connection.Query<User>("select * from Users where Id = @Id", new { Id = (int)parameters.id });
return Response.AsJson(users.First());
}
};
Post["/users"] = _ =>
{
var user = this.Bind<User>();
using (var connection = new MySqlConnection(connString))
{
connection.Execute(@"insert Users(Id, Name) values (@Id, @Name)", user);
return "true";
}
};
}
}
}
@jaehue

jaehue commented May 12, 2016

Copy link
Copy Markdown
Author
  • "Dapper": "1.42.0",
  • "MySql.Data": "6.9.8",
  • "Nancy": "1.4.3",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment