public async Task <ActionResult> PostGroupInfo(AddGroupFromApp groupInfo) { Console.WriteLine("build a group"); string invitedCode = null; int userId = -1; int groupId = -1; using (SqlConnection sqlConnection = new SqlConnection(Global.connect_string)) { sqlConnection.Open(); //1.先確認Token並把UserId抓下來 string strCheckToken = "SELECT [UserId] FROM [UserInfo] WHERE [Token]=@Token AND [Email]=@ACCOUNT"; using (SqlCommand cmdReadUserId = new SqlCommand(strCheckToken, sqlConnection)) { cmdReadUserId.Parameters.AddWithValue("@Token", groupInfo.Token); string[] account = groupInfo.Token.Split("/"); cmdReadUserId.Parameters.AddWithValue("@ACCOUNT", account[0]); using (SqlDataReader readUserId = cmdReadUserId.ExecuteReader()) { if (readUserId.HasRows) { if (readUserId.Read()) { userId = (int)readUserId[0]; } else { return(CreatedAtAction("PostGroupInfo", new { errorcode = -402, msg = "can't find user" })); } } } } if (userId == -1) { return(CreatedAtAction("PostGroupInfo", new { errorcode = -402, msg = "can't find user" })); } //2.確認這個group有沒有人創了 string strCheckName = "SELECT [GroupName] FROM [GroupInfo] WHERE [GroupName]=@GroupName"; using (SqlCommand cmdCheckName = new SqlCommand(strCheckName, sqlConnection)) { cmdCheckName.Parameters.AddWithValue("@GroupName", groupInfo.GroupName); using (SqlDataReader readName = cmdCheckName.ExecuteReader()) { if (readName.HasRows) { if (readName.Read()) { readName.Close(); sqlConnection.Close(); return(CreatedAtAction("PostGroupInfo", new { errorcode = -400, msg = "group already exist" })); } } } } //3.可以創(你是管理員,會回傳給你邀請碼) string addIntoSql = " INSERT INTO [GroupInfo]([GroupName],[Age],[WinTime],[LoseTime],[InvitedCode],[GroupIntro]) VALUES(@name, @age, @win, @lose, @invite,@groupIntro) (SELECT [GroupId] FROM [GroupInfo] WHERE [GroupName]=@GroupName) "; using (SqlCommand cmdAddInfo = new SqlCommand(addIntoSql, sqlConnection)) { invitedCode = makeInvitedCode(groupInfo.GroupName); cmdAddInfo.Parameters.AddWithValue("@name", groupInfo.GroupName); cmdAddInfo.Parameters.AddWithValue("@age", GetGroupAge(groupInfo.Age)); cmdAddInfo.Parameters.AddWithValue("@win", 0); cmdAddInfo.Parameters.AddWithValue("@lose", 0); cmdAddInfo.Parameters.AddWithValue("@invite", invitedCode); if (groupInfo.GroupIntro == null) { groupInfo.GroupIntro = "no groupIntro"; } cmdAddInfo.Parameters.AddWithValue("@groupIntro", groupInfo.GroupIntro); cmdAddInfo.Parameters.AddWithValue("@GroupName", groupInfo.GroupName); //cmdAddInfo.ExecuteNonQuery();//要加才會執行命令阿-->cmdAddInfo.ExecuteReader()會做 using (SqlDataReader readGroupId = cmdAddInfo.ExecuteReader()) { if (readGroupId.HasRows) { if (readGroupId.Read()) { groupId = (int)readGroupId[0]; } } } } string addId = " INSERT INTO [Group_User]([GroupId],[UserId],[IsManager]) VALUES(@group, @user, @manager)"; using (SqlCommand cmdAddId = new SqlCommand(addId, sqlConnection)) { cmdAddId.Parameters.AddWithValue("@group", groupId); cmdAddId.Parameters.AddWithValue("@user", userId); cmdAddId.Parameters.AddWithValue("@manager", 1); cmdAddId.ExecuteNonQuery();//要加才會執行命令阿 } } await _context.SaveChangesAsync(); return(CreatedAtAction("PostGroupInfo", new { errorcode = -1, msg = "group build success", InvitedCode = invitedCode })); }
public ActionResult GetMyGroup(AddGroupFromApp groupInfo) { Console.WriteLine("show all of the group i have"); List <ShowGroup> showGroups = new List <ShowGroup>(); using (SqlConnection sqlConnection = new SqlConnection(Global.connect_string)) { sqlConnection.Open(); //1.先確認Token並把UserId抓下來 //int userId = CheckToken(sqlConnection, groupInfo.Token); int userId = -1; //1.先確認Token並把UserId抓下來 string strCheckToken = "SELECT [UserId] FROM [UserInfo] WHERE [Token]=@Token AND [Email]=@ACCOUNT"; using (SqlCommand cmdReadUserId = new SqlCommand(strCheckToken, sqlConnection)) { cmdReadUserId.Parameters.AddWithValue("@Token", groupInfo.Token); string[] account = groupInfo.Token.Split("/"); cmdReadUserId.Parameters.AddWithValue("@ACCOUNT", account[0]); using (SqlDataReader readUserId = cmdReadUserId.ExecuteReader()) { if (readUserId.HasRows) { if (readUserId.Read()) { userId = (int)readUserId[0]; } else { return(CreatedAtAction("GetMyGroup", new { errorcode = -402, msg = "can't find user" })); } } } } if (userId == -1) { return(CreatedAtAction("GetMyGroup", new { errorcode = -402, msg = "can't find user" })); } Console.WriteLine("UserId" + userId); //2.使用GroupId查詢List string getMyGroup = @" SELECT [GroupId],[GroupName],[GroupIntro],[InvitedCode],[GroupPhoto] FROM [GroupInfo] WHERE [GroupId] IN (SELECT GroupId FROM [Group_User] WHERE UserId =@id AND (IsManager=0 OR IsManager=1) AND (Accepted=1 OR Accepted=2))"; using (SqlCommand cmd = new SqlCommand(getMyGroup, sqlConnection)) { cmd.Parameters.AddWithValue("@id", userId); using (SqlDataReader readGroup = cmd.ExecuteReader()) { if (readGroup.HasRows) { while (readGroup.Read()) { ShowGroup newObj = new ShowGroup(); newObj.GroupId = readGroup.GetInt32(0); newObj.GroupName = readGroup.GetString(1); newObj.GroupIntro = readGroup.GetString(2); newObj.InvitedCode = readGroup.GetString(3); string photoRoute = null; if (!readGroup.IsDBNull(4)) { photoRoute = Global.group_photo_url + readGroup.GetString(4); } newObj.GroupPhoto = photoRoute; showGroups.Add(newObj); } //Console.WriteLine("3"); //readGroup.NextResult(); //Console.WriteLine("4"); } else { return(CreatedAtAction("GetMyGroup", new { errorcode = -402, msg = "you dont have any group" })); } } } } //Console.WriteLine(showGroups.ElementAt<ShowGroup>(0)); //Console.WriteLine(showGroups.ElementAt<ShowGroup>(1)); return(CreatedAtAction("GetMyGroup", new { errorcode = -1, msg = "success get group", MyGroups = showGroups })); }