public void SaveTeamViewModel(TeamEntityViewModel teamEntityViewModel, EditModes editMode)
 {
     switch (editMode)
     {
         case EditModes.Insert:
             SaveOnInsert(teamEntityViewModel);
             break;
         case EditModes.Update:
             SaveOnUpdate(teamEntityViewModel);
             break;
     }
     InitializeViewModel(teamEntityViewModel);
 }
 public ActionResult Edit(int? id, TeamEntityViewModel teamEntityViewModel)
 {
     try
     {
         _teamViewService.SaveTeamViewModel(teamEntityViewModel, EditModes.Update);
         return RedirectToAction("Index", "Administration");
     }
     catch(ValidationException)
     {
         _teamViewService.FillModelStateErrors(ModelState, teamEntityViewModel, EditModes.Update);
         return View(teamEntityViewModel);
     }
     catch(EntityException x)
     {
         Error = x.Message;
         return RedirectToAction("Index", "Administration");
     }
 }
 public TeamEntityViewModel MapToViewModel(Team model)
 {
     var viewModel = new TeamEntityViewModel();
     if (model != null)
     {
         viewModel.Id = model.TeamID;
         viewModel.Name = model.Name;
         if(model.Manager!=null)
         {
             viewModel.ManagerId = model.Manager.UserID;
             viewModel.ManagerName = model.Manager.Fullname;
         }
         else
         {
             viewModel.ManagerId = null;
             viewModel.ManagerName = string.Empty;
         }
     }
     return viewModel;
 }
 public void MapToModel(TeamEntityViewModel entityViewModel, Team model, bool mapManager)
 {
     model.Name = entityViewModel.Name;
     if(mapManager) model.Manager = _userRepository.GetByID((int)entityViewModel.ManagerId);
 }
 public void MapToModel(TeamEntityViewModel entityViewModel, Team model)
 {
     MapToModel(entityViewModel, model, true);
 }
 public ActionResult New(TeamEntityViewModel teamEntityViewModel, UserEntityViewModel userEntityViewModel)
 {
     try
     {
         teamEntityViewModel.ManagerEntityViewModel = userEntityViewModel;
         _teamViewService.SaveTeamViewModel(teamEntityViewModel, EditModes.Insert);
         return RedirectToAction("Index", "Administration");
     }
     catch(ValidationException)
     {
         _teamViewService.FillModelStateErrors(ModelState, teamEntityViewModel, EditModes.Insert);
         _teamViewService.InitializeViewModel(teamEntityViewModel);
         return View(teamEntityViewModel);
     }
     catch(EntityException x)
     {
         Error = x.Message;
         return RedirectToAction("Index", "Administration");
     }
 }
        private void SaveAndCreateManager(TeamEntityViewModel teamEntityViewModel)
        {
            _teamValidator.EntityViewModel = teamEntityViewModel;
            var managerViewModel = teamEntityViewModel.ManagerEntityViewModel;
            _userValidator.EntityViewModel = managerViewModel;
            bool isTeamValid = _teamValidator.IsInsertValid();
            bool isUserValid = _userValidator.IsInsertValid();
            bool isValid = isTeamValid && isUserValid;
            if(isValid)
            {
                var teamManager = new User();
                _userMapper.MapToModel(managerViewModel, teamManager);

                var team = new Team();
                _teamMapper.MapToModel(teamEntityViewModel, team, false);

                teamManager.UserLevel = (int) UserLevels.TeamManager;
                teamManager.ManagesTeam = team;
                team.Manager = teamManager;

                _teamRepository.Add(team);
                _teamRepository.Save();
            }
            else throw new ValidationException();
        }
 public void InitializeViewModel(TeamEntityViewModel teamEntityViewModel)
 {
     teamEntityViewModel.Managers = _userRepository.GetByUserLevel((int) UserLevels.Unassigned);
     teamEntityViewModel.ManagerEntityViewModel = new UserEntityViewModel();
 }
 public void FillModelStateErrors(ModelStateDictionary modelState, TeamEntityViewModel teamEntityViewModel, EditModes insert)
 {
     modelState.AddModelErrors(_teamValidator.GetInsertValidationErrors());
     modelState.AddModelErrors(_userValidator.GetInsertValidationErrors());
 }
        private void SaveOnUpdate(TeamEntityViewModel teamEntityViewModel)
        {
            _teamValidator.EntityViewModel = teamEntityViewModel;
            if(_teamValidator.IsUpdateValid())
            {
                var team = _teamRepository.GetByID(teamEntityViewModel.Id);
                _teamMapper.MapToModel(teamEntityViewModel, team, false);

                if(teamEntityViewModel.ChangeManager)
                {
                    var currentManager = team.Manager;
                    currentManager.UserLevel = (int) UserLevels.Unassigned;
                    currentManager.ManagesTeam = null;

                    var newManager = _userRepository.GetByID((int)teamEntityViewModel.ManagerId);
                    newManager.ManagesTeam = team;
                    newManager.UserLevel = (int) UserLevels.TeamManager;
                    team.Manager = newManager;

                    _userRepository.Save();
                    _userRepository.Save();
                    _teamRepository.Save();
                }
            }
            else throw new ValidationException();
        }
 private void SaveOnInsert(TeamEntityViewModel teamEntityViewModel)
 {
     if (teamEntityViewModel.CreateManager)
         SaveAndCreateManager(teamEntityViewModel);
     else
         SaveAndSelectManager(teamEntityViewModel);
 }
        private void SaveAndSelectManager(TeamEntityViewModel teamEntityViewModel)
        {
            _teamValidator.EntityViewModel = teamEntityViewModel;
            if(_teamValidator.IsInsertValid())
            {
                var team = new Team();
                _teamMapper.MapToModel(teamEntityViewModel, team);

                team.Manager.UserLevel = (int) UserLevels.TeamManager;
                team.Manager.ManagesTeam = team;

                _teamRepository.Add(team);
                _teamRepository.Save();
            }
            else throw new ValidationException();
        }