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