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()); }
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()); }