/// <summary> /// Add or update friendship in database. /// </summary> /// <param name="userOneId">user one id.</param> /// <param name="userTwoId">user two id.</param> /// <param name="accepted">is friendship request accepted.</param> /// <returns>true if operations success; otherwise, false.</returns> public static bool SetFriendship(int userOneId, int userTwoId, bool accepted) { using (var DBcontext = new LinqToSqlDataContext()) { // get friendship records var data = from friendship in DBcontext.Friends where (friendship.friend_one.Equals(userOneId) & friendship.friend_two.Equals(userTwoId)) | (friendship.friend_two.Equals(userOneId) & friendship.friend_one.Equals(userTwoId)) select friendship; // already friends if (data.Any() && data.First().accepted) { return false; } else { // accept friendship request if (data.Any() && !data.First().accepted) { data.First().accepted = accepted; } // add friendship request else { Friendship f = new Friendship { friend_one = userOneId, friend_two = userTwoId, accepted = accepted }; DBcontext.Friends.InsertOnSubmit(f); } try { DBcontext.SubmitChanges(); return true; } catch (Exception e) { log.Error(e); return false; } } } }
partial void DeleteFriend(Friendship instance);
partial void UpdateFriend(Friendship instance);
private void detach_Friends1(Friendship entity) { this.SendPropertyChanging(); entity.User1 = null; }
private void attach_Friends1(Friendship entity) { this.SendPropertyChanging(); entity.User1 = this; }
partial void InsertFriend(Friendship instance);