public ActionResult Create(UserTypeRating usertyperating)
 {
     if (ModelState.IsValid) {
         usertyperatingRepository.InsertOrUpdate(usertyperating);
         usertyperatingRepository.Save();
         return RedirectToAction("Index");
     } else {
         ViewBag.PossibleUser = userRepository.All;
         ViewBag.PossibleTypeRating = typeratingRepository.All;
         return View();
     }
 }
Exemplo n.º 2
0
        public ActionResult UserEdit(UserEditModel viewModel)
        {
            User user = userRepository.Find(UserContext.GetCurrent().ID);
            user.InjectFrom<NotNullValueInjection>(viewModel);
            viewModel.Rank = user.Rank;

            if (!ValidateNumberOfTyperatingsInternal(viewModel.TypeRatingIDs.Count(), user)) {

                // TODO loc
                ModelState.AddModelError("TypeRatingIDs", "No way");
            }

            HashSet<int> notReleaseableRatingIDs = new HashSet<int>(userTypeRatingRepository.All.Where(t => t.UserID == user.ID && t.ValidTo == null).ToList().Where(t => !t.CanBeReleased()).Select(t => t.TypeRatingID));
            HashSet<int> currentRatingIDsSelected = new HashSet<int>(viewModel.TypeRatingIDs.Select(t => int.Parse(t)));

            if (!notReleaseableRatingIDs.IsSubsetOf(currentRatingIDsSelected)) {
                throw new NotSupportedException("You cannot release type ratings that you don't hold for at least 50 days.");
            }

            if (ModelState.IsValid) {

                userRepository.InsertOrUpdate(user);
                userRepository.Save();

                var currentUserTypeRatings = userTypeRatingRepository.All.Where(t => t.UserID == user.ID && t.ValidTo == null).ToList();

                foreach (var rating in currentUserTypeRatings) {
                    if (!viewModel.TypeRatingIDs.Contains(rating.TypeRatingID.ToString())) {
                        UserTypeRating toChange = userTypeRatingRepository.Find(rating.ID);
                        toChange.ValidTo = DateTime.UtcNow;
                        userTypeRatingRepository.InsertOrUpdate(toChange);
                    }
                }

                foreach (string rating in viewModel.TypeRatingIDs) {
                    if (!currentUserTypeRatings.Any(t => t.TypeRatingID.ToString() == rating)) {
                        UserTypeRating newRating = new UserTypeRating();
                        newRating.TypeRatingID = int.Parse(rating);
                        newRating.UserID = user.ID;
                        newRating.ValidFrom = DateTime.UtcNow;
                        userTypeRatingRepository.InsertOrUpdate(newRating);
                    }
                }

                userTypeRatingRepository.Save();

                return RedirectToAction("UserEdit", new { id = user.ID });
            } else {

                SetupViewModel(viewModel);
                return View(viewModel);
            }
        }
 public void InsertOrUpdate(UserTypeRating usertyperating)
 {
     if (usertyperating.ID == default(int)) {
         // New entity
         context.UserTypeRating.Add(usertyperating);
     } else {
         // Existing entity
         context.UserTypeRating.Attach(usertyperating);
         context.Entry(usertyperating).State = EntityState.Modified;
     }
 }