Пример #1
0
        public bool UpdateTeam(Team team)
        {
            bool success = true;
            int  i       = 0; //index for loops

            //first determine the ID of the team that we are updating
            Data.Entities.Team DBTeam = _db.Team.Where(z => z.Teamname.Equals(team.teamname)).FirstOrDefault();

            //ensure that our DBteam is not null, we have an equal number of roles and users, and that we have more than 0 roles
            if (DBTeam != null && team.Roles.Count == team.Userlist.Count && team.Roles.Count > 0)
            {
                int DBTeamID = DBTeam.Id;
                //next, determine the elements in the USERTEAM table that have that teams ID
                List <Data.Entities.UserTeam> DBUserTeam = _db.UserTeam.Where(p => p.Teamid == DBTeamID).ToList();
                //now, remove those elements from the userteam table
                if (DBUserTeam.Count > 0)
                {
                    foreach (var item in DBUserTeam)
                    {
                        _db.UserTeam.Remove(item);
                    }
                }
                //now, I want to add the elements from this team into the userteam table

                List <User> DomainUser = team.Userlist;
                List <bool> DomainRole = team.Roles;

                for (i = 0; i < DomainUser.Count; i++)
                {
                    //determine the ID for each individual user in the team
                    Data.Entities.User DBUser = _db.User.Where(z => z.Username.Equals(DomainUser[i].username)).FirstOrDefault();
                    if (DBUser == null)
                    {
                        //if the user didnt exist, we want to stop the program and return false
                        success = false;
                        return(success);
                    }
                    else
                    {
                        //here we add each user into the userteam table
                        Data.Entities.UserTeam userteam = new Data.Entities.UserTeam();
                        userteam.Teamid = DBTeamID;
                        userteam.Leader = DomainRole[i];
                        userteam.Userid = DBUser.Id;
                        _db.UserTeam.Add(userteam);
                        _db.SaveChanges();
                    }
                }
            }
            else
            {
                success = false;
                return(success);
            }

            return(success);
        }
Пример #2
0
        public bool AddTeam(Team team)
        {
            //intialize boolean that indicates if method succeeded to false
            bool success = false;

            //search database to see if the team already exists in the database
            Data.Entities.Team x = _db.Team.Where(a => a.Teamname.Equals(team.teamname)).FirstOrDefault();
            //if the team does not exist, add the team to the database
            if (x == null)
            {
                _db.Team.Add(Mapper.Map(team));
                //set boolean to true to indicate that we successfully added a team
                _db.SaveChanges();
                success = true;
            }
            //now, we need to add its userteams
            //now, I want to add the elements from this team into the userteam table

            List <User> DomainUser = team.Userlist;
            List <bool> DomainRole = team.Roles;

            //reobtain the current team from the database to obtain its ID
            Data.Entities.Team DBteam = _db.Team.Where(a => a.Teamname.Equals(team.teamname)).FirstOrDefault();

            for (int i = 0; i < DomainUser.Count; i++)
            {
                //determine the ID for each individual user in the team
                Data.Entities.User DBUser = _db.User.Where(z => z.Username.Equals(DomainUser[i])).FirstOrDefault();
                if (DBUser == null)
                {
                    //if the user didnt exist, we do not add any userteams
                }
                else
                {
                    //here we add each user into the userteam table
                    Data.Entities.UserTeam userteam = new Data.Entities.UserTeam();
                    userteam.Teamid = DBteam.Id;
                    userteam.Leader = DomainRole[i];
                    userteam.Userid = DBUser.Id;
                    _db.UserTeam.Add(userteam);
                    _db.SaveChanges();
                }
            }

            return(success);
        }
        public static Data.Entities.Team Map(Domain.Team dmTeam)
        {
            Data.Entities.Team            deTeam     = new Entities.Team();
            List <Data.Entities.UserTeam> deUserTeam = new List <Entities.UserTeam>();

            if (dmTeam.Userlist.Count > 0 && dmTeam.Userlist.Count == dmTeam.Roles.Count)
            {
                for (int i = 0; i < dmTeam.Userlist.Count; i++)
                {
                    Data.Entities.UserTeam soloUserTeam = new Data.Entities.UserTeam();
                    soloUserTeam.Leader = dmTeam.Roles[i];
                    soloUserTeam.Userid = dmTeam.Userlist[i].id;
                    deUserTeam.Add(soloUserTeam);
                }
            }

            if (dmTeam.id != null)
            {
                deTeam.Id = (int)dmTeam.id;
            }
            deTeam.UserTeam = deUserTeam;
            deTeam.Teamname = dmTeam.teamname;
            return(deTeam);
        }