Inheritance: INotifyPropertyChanging, INotifyPropertyChanged
Exemplo n.º 1
0
        /// <summary>
        /// Creates a Group as a new entry in the DB.
        /// </summary>
        /// <param name="group">Group object to add to the DB.</param>
        /// <returns>ID of the created Group on success, 0 on failure.</returns>
        public static int CreateNewGroup(Group group)
        {
            try
            {
                using (SqlConnection connection = ConnectionManager.GetConnection())
                {
                    var data = new ActivEarthDataProvidersDataContext(connection);
                    var groupData = new GroupDataProvider
                    {
                        name = group.Name,
                        description = group.Description,
                        owner_id = group.Owner.UserID,
                        green_score = group.GreenScore,
                        badge_score = group.ActivityScore.BadgeScore,
                        challenge_score = group.ActivityScore.ChallengeScore,
                        contest_score = group.ActivityScore.ContestScore,
                    };
                    data.GroupDataProviders.InsertOnSubmit(groupData);
                    data.SubmitChanges();

                    foreach (User member in group.Members) {
                        GroupMemberDataProvider memberData = new GroupMemberDataProvider
                        {
                            user_id = member.UserID,
                            group_id = groupData.id
                        };
                        data.GroupMemberDataProviders.InsertOnSubmit(memberData);
                    }

                    foreach (string tag in group.HashTags)
                    {
                        GroupHashtagDataProvider hashtagData = new GroupHashtagDataProvider
                        {
                            hashtag = tag,
                            group_id = groupData.id
                        };
                        data.GroupHashtagDataProviders.InsertOnSubmit(hashtagData);
                    }

                    data.SubmitChanges();
                    data.Connection.Close();
                    return groupData.id;
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }
 partial void DeleteGroupHashtagDataProvider(GroupHashtagDataProvider instance);
 partial void UpdateGroupHashtagDataProvider(GroupHashtagDataProvider instance);
 partial void InsertGroupHashtagDataProvider(GroupHashtagDataProvider instance);
		private void detach_group_hashtags(GroupHashtagDataProvider entity)
		{
			this.SendPropertyChanging();
			entity.GroupDataProvider = null;
		}
Exemplo n.º 6
0
        /// <summary>
        /// Updates an existing Group in the DB.
        /// </summary>
        /// <param name="group">Group whose information needs to be updated.</param>
        /// <returns>True on success, false on failure.</returns>
        public static bool UpdateGroup(Group group)
        {
            try
            {
                using (SqlConnection connection = ConnectionManager.GetConnection())
                {
                    var data = new ActivEarthDataProvidersDataContext(connection);
                    int groupId = group.ID;
                    GroupDataProvider dbGroup = (from g in data.GroupDataProviders
                                                 where g.id == groupId
                                                 select g).FirstOrDefault();
                    if (dbGroup != null)
                    {
                        dbGroup.name = group.Name;
                        dbGroup.description = group.Description;
                        dbGroup.owner_id = group.Owner.UserID;
                        dbGroup.green_score = group.GreenScore;
                        dbGroup.badge_score = group.ActivityScore.BadgeScore;
                        dbGroup.challenge_score = group.ActivityScore.ChallengeScore;
                        dbGroup.contest_score = group.ActivityScore.ContestScore;

                        //Update group_hashtags table
                        List<GroupHashtagDataProvider> hashtags = (from h in data.GroupHashtagDataProviders
                                                 where h.group_id == groupId
                                                 select
                                                     h
                                         ).ToList();

                        foreach (GroupHashtagDataProvider hashtagData in hashtags)
                        {
                            if (!group.HashTags.Contains(hashtagData.hashtag)){
                                data.GroupHashtagDataProviders.DeleteOnSubmit(hashtagData);
                            }
                        }

                        foreach (string hashtag in group.HashTags)
                        {
                            bool found = false;
                            foreach (GroupHashtagDataProvider hashtagData in hashtags)
                            {
                                if (hashtagData.hashtag == hashtag)
                                {
                                    found = true;
                                }
                            }
                            if (!found)
                            {
                                GroupHashtagDataProvider hashtagData = new GroupHashtagDataProvider
                                {
                                    hashtag = hashtag,
                                    group_id = groupId
                                };
                                data.GroupHashtagDataProviders.InsertOnSubmit(hashtagData);
                            }
                        }

                        //Update group_members table
                        List<GroupMemberDataProvider> members = (from u in data.GroupMemberDataProviders
                                                where u.group_id == groupId
                                                select
                                                    u
                                              ).ToList();

                        foreach (GroupMemberDataProvider memberData in members)
                        {
                            bool found = false;
                            foreach (User member in group.Members)
                            {
                                if (memberData.user_id == member.UserID)
                                {
                                    found = true;
                                }
                            }
                            if (!found)
                            {
                                data.GroupMemberDataProviders.DeleteOnSubmit(memberData);
                            }
                        }

                        foreach (User member in group.Members)
                        {
                            bool found = false;
                            foreach (GroupMemberDataProvider memberData in members)
                            {
                                if (memberData.user_id == member.UserID)
                                {
                                    found = true;
                                }
                            }
                            if (!found)
                            {
                                GroupMemberDataProvider memberData = new GroupMemberDataProvider
                                {
                                    user_id = member.UserID,
                                    group_id = groupId
                                };
                                data.GroupMemberDataProviders.InsertOnSubmit(memberData);
                            }
                        }

                        //Update Teams table
                        List<TeamDataProvider> contests = (from c in data.TeamDataProviders
                                                 where c.group_id == groupId
                                                 select
                                                     c
                                                 ).ToList();

                        foreach (TeamDataProvider contestData in contests)
                        {
                            bool found = false;
                            foreach (Contest contest in group.Contests)
                            {
                                if (contestData.contest_id == contest.ID)
                                {
                                    found = true;
                                }
                            }
                            if (!found)
                            {
                                data.TeamDataProviders.DeleteOnSubmit(contestData);
                            }
                        }

                        RecentActivityDAO.UpdateGroupRecentActivity(group);

                        data.SubmitChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }