Create the following stored procedure in the database.
CREATE PROCEDURE dbo.mb_addfriend(@userid uniqueidentifier, @friendid uniqueidentifier)
AS
begin tran
begin try
insert into mb_friends values(@userid, @friendid)
update mb_profiles set friendscount = friendscount + 1
where userid = @userid;
update mb_profiles set followerscount = followerscount + 1
where userid = @friendid;
commit tran
end try
begin catch
rollback tran
raiserror('Could Not Add Friend',16,1);
end catch
CREATE PROCEDURE dbo.mb_addupdate(@userid uniqueidentifier, @text nvarchar(140))
as
begin tran
begin try
insert into mb_updates(userid,[text],[timestamp])
values (@userid,@text, getdate())
update mb_profiles set updatescount = updatescount + 1
where userid = @userid
commit tran
end try
begin catch
rollback tran
raiserror('Could not add an update!',16,1)
end catch
CREATE PROCEDURE dbo.mb_deleteupdate(@updateid int)
AS
declare @userid uniqueidentifier
begin tran
begin try
/* get userid for this update */
select @userid = userid from mb_updates
where updateid = @updateid
delete from mb_updates where updateid = @updateid
update mb_profiles set updatescount = updatescount -1
where userid = @userid;
commit tran
end try
begin catch
rollback tran
raiserror('Could not delete update',16,1);
end catch
CREATE PROCEDURE dbo.mb_findpeople(@userid uniqueidentifier, @pattern nvarchar(50))
AS
select au.userid, username, picturefile, onlinebio, followerscount, friendscount, updatescount
from mb_profiles p inner join aspnet_users au on (au.userid=p.userid)
where au.userid <> @userid and username like '%' + @pattern + '%' and
p.userid not in
( select friendid from mb_friends
where userid = @userid)
CREATE PROCEDURE dbo.mb_getfriends(@userid uniqueidentifier)
AS
select au.userid, username, picturefile, onlinebio, followerscount
from mb_profiles p inner join aspnet_users au on (au.userid=p.userid)
where p.userid in
( select friendid from mb_friends
where userid = @userid)
CREATE PROCEDURE dbo.mb_gettotalupdates(@userid uniqueidentifier)
AS
select picturefile,username, text, timestamp from mb_updates u inner join aspnet_users au on (u.userid = au.userid) inner join mb_profiles p on (u.userid = p.userid)
where u.userid = @userid
union
select picturefile, username, text, timestamp from mb_updates u inner join aspnet_users au on (u.userid = au.userid) inner join mb_profiles p on (u.userid = p.userid)
where u.userid in (select friendid from mb_friends where userid = @userid)
order by timestamp desc
CREATE PROCEDURE dbo.mb_getupdates(@userid uniqueidentifier)
AS
select updateid,picturefile, username, text, timestamp
from mb_updates u inner join aspnet_users au on (u.userid = au.userid)
inner join mb_profiles p on (u.userid = p.userid)
where u.userid = @userid
order by timestamp desc
CREATE PROCEDURE dbo.mb_getuserdetails(@userid uniqueidentifier)
AS
select onlinebio, picturefile, followerscount, friendscount, updatescount
from mb_profiles where userid = @userid
CREATE PROCEDURE dbo.mb_removefriend(@userid uniqueidentifier, @friendid uniqueidentifier)
AS
begin tran
begin try
delete from mb_friends where userid = @userid and friendid = @friendid
update mb_profiles set friendscount = friendscount - 1
where userid = @userid;
update mb_profiles set followerscount = followerscount - 1
where userid = @friendid;
commit tran
end try
begin catch
rollback tran
raiserror('Could Not Remove Friend',16,1);
end catch
CREATE PROCEDURE dbo.mb_updateuserprofile(@userid uniqueidentifier,@onlinebio nvarchar(200),@picturefile nvarchar(50))
AS
if @picturefile <> ''
update mb_profiles set picturefile = @picturefile, onlinebio = @onlinebio
where userid = @userid;
else
update mb_profiles set onlinebio = @onlinebio
where userid = @userid;