Last active
May 12, 2016 15:16
-
-
Save jaehue/111b887a82f96621ba2c25df6dc8612b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | |
| } | |
| }; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.