///Add a test DBNotification to see that they are working public async Task<DBNotification> TestDBNotification() { DBNotification DBNotification = new DBNotification(User.Id,"Medal","Test DBNotification"); await dbNotificationTable.InsertAsync(DBNotification); return DBNotification; }
/// join the club and delete the club request; returns true if success, false if failure public async Task<bool> AcceptClubRequest(string clubRequestId) { ClubRequest clubRequest = await clubRequestTable.LookupAsync(clubRequestId); //author joins the club await JoinClubByInvite(clubRequest.AccountId, clubRequest.ClubId); //make DBNotification Club club = await clubTable.LookupAsync(clubRequest.ClubId); DBNotification DBNotification = new DBNotification(clubRequest.AccountId,"request",club.Title+" has accepted your request!"); await dbNotificationTable.InsertAsync(DBNotification); //delete the club request try { await clubRequestTable.DeleteAsync(clubRequest); return true; } catch (Exception e) { return false; } }
/// Join a club after being invited; ignores if exclusive and returns the status of trying to join the club as an int: /// 1 - success, 2 - club full, 4 - already in club public async Task<int> JoinClubByInvite(string accountId, string clubId) { //assumes the club exists as a precondition Club club = await clubTable.LookupAsync(clubId); if (club.NumMembers >= CLUB_SIZE) { return 2; } var list = await memberJuncTable.Where(item => item.AccountId == accountId && item.ClubId == club.Id).ToListAsync(); if (list.Count > 0) { return 4; } //assuming all precondition do not prevent user from joining club MemberJunction memberJunc = new MemberJunction(accountId, club.Id); await memberJuncTable.InsertAsync(memberJunc); //update club club.NumMembers++; await clubTable.UpdateAsync(club); //update user Account account = await accountTable.LookupAsync(accountId); account.NumClubsIn++; await accountTable.UpdateAsync(account); //add DBNotifications DBNotification DBNotification = new DBNotification(account.Id,"join","You joined the club "+club.Title+"!"); await dbNotificationTable.InsertAsync(DBNotification); return 1; }
/// Send an invite to join a club to an account; returns 0 if success,1 if account is not a member of cloud club is in, 2 if invite already exists /// NOTE: you can currently send an invite to someone already in the club public async Task<int> CreateInvite(string clubId, string recipientId) { var list = await inviteTable.Where(item => item.ClubId == clubId && item.RecipientId == recipientId).ToListAsync(); //if the user doesn not already have in invite, make one if (list.Count > 0) { return 2; } else { Club club = await clubTable.LookupAsync(clubId); //check if recipient is a member of the cloud of the club var cloudMemList = await cloudJuncTable.Where(item=>item.CloudId==club.CloudId && item.AccountId==recipientId).ToListAsync(); if(cloudMemList.Count==0){ return 1; } //make invite Invite invite = new Invite(User.Id, recipientId, clubId); await inviteTable.InsertAsync(invite); //make a DBNotification DBNotification DBNotification = new DBNotification(recipientId,"invite",User.Username+" has invited you to join "+club.Title+"."); await dbNotificationTable.InsertAsync(DBNotification); return 0; } }
/// Makes a friend object in the friends table and deletes the friend request; returns true if success, false if not public async Task<bool> AcceptFriendRequest(string friendRequestId) { FriendRequest friendRequest = await friendRequestTable.LookupAsync(friendRequestId); //add to friends table Friends friends = new Friends(friendRequest.AuthorId, friendRequest.RecipientId); await friendsTable.InsertAsync(friends); //update accounts Account author = await accountTable.LookupAsync(friends.AuthorId); author.NumFriends++; await accountTable.UpdateAsync(author); Account recipient = await accountTable.LookupAsync(friends.RecipientId); recipient.NumFriends++; await accountTable.UpdateAsync(recipient); //add DBNotifications DBNotification DBNotificationAuthor = new DBNotification(author.Id,"friend","You and "+recipient.Username+" became friends!"); await dbNotificationTable.InsertAsync(DBNotificationAuthor); DBNotification DBNotificationRecipient = new DBNotification(recipient.Id, "friend", "You and "+author.Username+" became friends!"); await dbNotificationTable.InsertAsync(DBNotificationRecipient); //delete friendrequest try { await friendRequestTable.DeleteAsync(friendRequest); return true; } catch (Exception e) { return false; } }
/// Create a club; returns true if success, false if fails; fails if club name already in use or user is banned public async Task<bool> CreateClub(string title, string color, bool exclusive, List<string> tagList) { //check if banned (must get current instance of user account incase ban happend since login) User = await accountTable.LookupAsync(User.Id); if (DateTime.Compare(User.Banned, DateTime.Now) > 0) { return false; } Club club = new Club(User.CurrentCloudId, title, color, exclusive, User.Id); //check if the club name is in use var list = await clubTable.Where(item => item.Title.ToLower() == club.Title.ToLower() && item.CloudId==User.CurrentCloudId).ToListAsync(); if (list.Count > 0) { return false; } else { await clubTable.InsertAsync(club); //add tags to table based on title string[] titleTagList = title.Split(' '); foreach (string key in titleTagList) { Tag tag = new Tag(key.ToLower(), club.Id, club.CloudId); await tagTable.InsertAsync(tag); } //add tags to table based on tagList foreach (string key in tagList) { Tag tag = new Tag(key.ToLower(), club.Id,club.CloudId); await tagTable.InsertAsync(tag); } //add club founder to memberJunction //club size is initialized to one automatically MemberJunction memberJunction = new MemberJunction(User.Id, club.Id); await memberJuncTable.InsertAsync(memberJunction); //update user account User.NumClubsCreated++; User.NumClubsIn++; User.Points++; await accountTable.UpdateAsync(User); //make DBNotification DBNotification DBNotification = new DBNotification(User.Id, "join", "You created the club " + club.Title + "!"); await dbNotificationTable.InsertAsync(DBNotification); return true; } }