private string PutApi(KptRepository db, HttpListenerRequest req, Command cmd) { if (!req.HasEntityBody) { throw new ArgumentException("指定したAPIに必要なデータがありません。POST " + cmd.ToString()); } var body = new StreamReader(req.InputStream).ReadToEnd(); var keyValues = HttpUtility.ParseQueryString(body); if (cmd.IsUsers && cmd.UserId != 0 && cmd.IsKptBoards && cmd.KptBoardId != 0) { // PUT /v1/users/n/kptboards/n Console.WriteLine("ボード修正"); var kptBoard = db.GetKptBoardsById(cmd.UserId, cmd.KptBoardId); kptBoard.Time = DateTime.Now; kptBoard.Keep = keyValues.Get("keep"); kptBoard.Problem = keyValues.Get("problem"); kptBoard.Try = keyValues.Get("try"); db.UpdateKptBoard(kptBoard); return(""); } throw new ArgumentException("指定したAPIがありません。PUT " + cmd.ToString()); }
public void TestAddUser() { using (var db = new KptRepository()) { Assert.Equal(0, db.GetUsers().Count); db.AddUser(new User { Name = "Test01" }); Assert.Equal(1, db.GetUsers().Count); db.AddUser(new User { Name = "Test02", Age = 2 }); Assert.Equal(2, db.GetUsers().Count); } }
private static string PostApi(KptRepository db, HttpListenerRequest req, Command cmd) { if (!req.HasEntityBody) { throw new ArgumentException("指定したAPIに必要なデータがありません。POST " + cmd.ToString()); } var body = new StreamReader(req.InputStream).ReadToEnd(); var keyValues = HttpUtility.ParseQueryString(body); if (cmd.IsUsers && cmd.UserId == 0 && !cmd.IsKptBoards && cmd.KptBoardId == 0) { // POST /v1/users/ Console.WriteLine("人追加"); int?age = null; if (keyValues.Get("age") != null) { age = int.Parse(keyValues.Get("age")); } var user = new User { Name = keyValues.Get("name"), Age = age }; db.AddUser(user); return(""); } if (cmd.IsUsers && cmd.UserId != 0 && cmd.IsKptBoards && cmd.KptBoardId == 0) { // POST /v1/users/n/kptboards/ Console.WriteLine("ボード追加"); var kptBoard = new KptBoard { Time = DateTime.Now, Keep = keyValues.Get("keep"), Problem = keyValues.Get("problem"), Try = keyValues.Get("try"), UserId = cmd.UserId }; db.AddKptBoard(kptBoard); return(""); } throw new ArgumentException("指定したAPIがありません。POST " + cmd.ToString()); }
private string DeleteApi(KptRepository db, HttpListenerRequest req, Command cmd) { if (cmd.IsUsers && cmd.UserId != 0 && cmd.IsKptBoards && cmd.KptBoardId != 0) { // DELETE /v1/users/n/kptboards/n Console.WriteLine("ボード削除"); var kptBoard = new KptBoard { KptBoardId = cmd.KptBoardId }; db.DeleteKptBoard(kptBoard); return(""); } throw new ArgumentException("指定したAPIがありません。DELETE " + cmd.ToString()); }
public void TestAddUserWithKptBoard() { using (var db = new KptRepository()) { var user = new User { Name = "Test01", KptBoards = { new KptBoard { Time = DateTime.Now, Keep = "K011", Problem = "P011", Try = "T011" } } }; Assert.Equal(0, user.UserId); Assert.Equal(0, db.GetUsers().Count); db.AddUser(user); Assert.NotEqual(0, user.UserId); // AddUserした時点でUserIdが設定される Assert.Equal(1, db.GetUsers().Count); Assert.Equal(1, db.GetKptBoardsByUserId(user.UserId).Count); } }
internal void Listen() { var db = new KptRepository(); var listener = new HttpListener(); listener.Prefixes.Add("http://*****:*****@"{ ""system"":""kpt""}"; try { if (method == "GET") { responseString = GetApi(db, req, cmd); } else if (method == "POST") { responseString = PostApi(db, req, cmd); } else if (method == "PUT") { responseString = PutApi(db, req, cmd); } else if (method == "DELETE") { responseString = DeleteApi(db, req, cmd); } else { Console.WriteLine("不明"); } res.StatusCode = 200; res.ContentType = " application/json; charset=utf-8"; byte[] content = System.Text.Encoding.UTF8.GetBytes(responseString); res.ContentLength64 = content.Length; res.OutputStream.Write(content, 0, content.Length); } catch (Exception ex) { res.StatusCode = 500; // 404 でも良いのだがここは雑に 500 にまとめておく byte[] content = Encoding.UTF8.GetBytes(ex.Message); res.OutputStream.Write(content, 0, content.Length); content = Encoding.UTF8.GetBytes("\n"); res.OutputStream.Write(content, 0, content.Length); content = Encoding.UTF8.GetBytes(ex.StackTrace); res.OutputStream.Write(content, 0, content.Length); } res.Close(); } }
private static string GetApi(KptRepository db, HttpListenerRequest req, Command cmd) { string responseString; if (cmd.IsUsers && cmd.UserId == 0 && !cmd.IsKptBoards) { // GET /v1/users/ Console.WriteLine("人の一覧"); var sb = new StringBuilder("["); var users = db.GetUsers(); foreach (var user in users) { sb.Append("{"); sb.Append(string.Format($@"""userId"":{user.UserId},")); sb.Append(string.Format($@"""name"":""{user.Name}""")); sb.Append("},"); } if (sb.Length > 0) { sb.Remove(sb.Length - 1, 1); } sb.Append("]"); return(sb.ToString()); } if (cmd.IsUsers && cmd.UserId != 0 && !cmd.IsKptBoards) { // GET /v1/users/n Console.WriteLine("特定の人の詳細"); var user = db.UserFindById(cmd.UserId); var sb = new StringBuilder("{"); sb.Append(string.Format($@"""userId"":{user.UserId},")); sb.Append(string.Format($@"""name"":""{user.Name}"",")); if (user.Age == null) { sb.Append(string.Format($@"""age"":null")); } else { sb.Append(string.Format($@"""age"":{user.Age}")); } sb.Append("}"); return(sb.ToString()); } if (cmd.IsUsers && cmd.UserId != 0 && cmd.IsKptBoards && cmd.KptBoardId == 0) { // GET /v1/users/n/kptboards Console.WriteLine("特定の人のボードの一覧"); // User user = db.UserFindById(cmd.userId); var kptBoards = db.GetKptBoardsByUserId(cmd.UserId); var sb = new StringBuilder("["); foreach (var kptBoard in kptBoards) { sb.Append("{"); sb.Append(string.Format($@"""boardId"":{kptBoard.KptBoardId},")); sb.Append(string.Format($@"""time"":""{kptBoard.Time}"",")); sb.Append(string.Format($@"""keep"":""{kptBoard.Keep}"",")); sb.Append(string.Format($@"""problem"":""{kptBoard.Problem}"",")); sb.Append(string.Format($@"""try"":""{kptBoard.Try}""")); sb.Append("},"); } if (sb.Length > 0) { sb.Remove(sb.Length - 1, 1); } sb.Append("]"); return(sb.ToString()); } if (cmd.IsUsers && cmd.UserId != 0 && cmd.IsKptBoards && cmd.KptBoardId != 0) { // GET /v1/users/n/kptboards/n Console.WriteLine("特定の人の特定のボードの詳細"); var kptBoard = db.GetKptBoardsById(cmd.UserId, cmd.KptBoardId); return(responseString = string.Format(@"{{""boardId"":{0},""date"":""{1}"",""keep"":""{2}"",""problem"":""{3}"",""try"":""{4}""}}", kptBoard.KptBoardId, kptBoard.Time, kptBoard.Keep, kptBoard.Problem, kptBoard.Try)); } throw new ArgumentException("指定したAPIがありません。GET " + cmd.ToString()); }