public static Friend MakeFriend(string userId, string friendCode) { Friend friend = null; UserRider rider = UserRider.Load(userId); if (rider != null) { Friend[] existingFriends = Load(userId); SQLiteDatabase db = new SQLiteDatabase(); // Some logic here - we only allow a new friend if // (a) The friendship does not already exist // (b) The friendCode is correct // (c) We haven't exeeded out friend limit string sqlFriend = "select UserId, UserName from cycli_riders where FriendCode = @c"; DataTable dt = db.GetDataTable(sqlFriend, "@c", friendCode); if (dt.Rows.Count > 0) { string friendId = dt.Rows[0].Field<string>("UserId"); string friendName = dt.Rows[0].Field<string>("UserName"); if (existingFriends.Count() < rider.MaximumFriends && existingFriends.Count(p => p.UserId == friendId) == 0) { string sql = "insert into cycli_friends (UserId, FriendId, Status) values (@u, @f, 'Accepted')"; if (db.ExecuteNonQuery(sql, "@u", userId, "@f", friendId) == 1) { friend = new Friend { UserName = friendName, UserId = friendId, Status = "Accepted" }; } } } // This query checks that the user has not already been invited db.Close(); } return friend; }
public static Friend[] Load(string userId) { List<Friend> friends = new List<Friend>(); SQLiteDatabase db = new SQLiteDatabase(); string sql = @"select * from (select r.UserId as UserId, r.UserName as UserName, " + "r.Turbo, t.Power_Model_C1, t.Power_Model_C2, t.Power_Model_C3, " + "f.Status as Status " + "from cycli_riders r, cycli_friends f, cycli_turbos t " + "where f.UserId = '" + userId + "' and r.UserId=f.FriendId " + "and r.Turbo = t.Type " + "union " + "select r.UserId as UserId, r.UserName as UserName, "+ "r.Turbo, t.Power_Model_C1, t.Power_Model_C2, t.Power_Model_C3, " + "f.Status as Status " + "from cycli_riders r, cycli_friends f, cycli_turbos t " + "where f.FriendId= '" + userId + "' and r.UserId=f.UserId "+ "and r.Turbo = t.Type " + ") order by UserName"; DataTable dtFriends = db.GetDataTable(sql); foreach (DataRow dr in dtFriends.Rows) { Friend f = new Friend() { UserId = dr.Field<string>("UserId"), UserName = dr.Field<string>("UserName"), Turbo = dr.Field<string>("Turbo"), Coefficients = new double[]{dr.Field<double>("Power_Model_C1"),dr.Field<double>("Power_Model_C2"),dr.Field<double>("Power_Model_C3")}, Status = (string)dr["Status"] }; friends.Add(f); } return friends.ToArray(); }