Пример #1
0
        /// <summary>
        ///     Updates a mission track from values in the submitted model, provided that game rules allow it.
        /// </summary>
        /// <param name="model">The model containing new values.</param>
        /// <returns>Task.</returns>
        /// <exception cref="InvalidOperationException">Thrown if the track was not updated for any reason.</exception>
        public async Task UpdateTrackAsync(MissionTrackViewModel model)
        {
            Log.Info($"Updating track {model}");
            var maybeTrack = unitOfWork.MissionTracks.GetMaybe(model.Id);

            if (maybeTrack.None)
            {
                Log.Error($"The target track id={model.Id} was not found in the database");
                throw new InvalidOperationException("The track was not found in the database");
            }
            var targetLevel = model.MissionLevelId;
            var maybeLevel  = unitOfWork.MissionLevels.GetMaybe(targetLevel);

            if (maybeLevel.None)
            {
                Log.Error($"Update blocked because the target level id={targetLevel} was not found");
                throw new InvalidOperationException("Can't move the track to a non-existent level");
            }
            var dbLevel = maybeLevel.Single();

            if (dbLevel.Tracks.Any(p => p.Number == model.Number && p.Id != model.Id))
            {
                Log.Warn($"Update blocked because destination level id={targetLevel} already has a track number={model.Number}");
                throw new InvalidOperationException("The destination track already has that level number");
            }
            var dbTrack = maybeTrack.Single();

            mapper.Map(model, dbTrack);
            await unitOfWork.CommitAsync();
        }
        // GET: Admin/MissionTracks/Create
        public ActionResult Create()
        {
            var model = new MissionTrackViewModel();

            PopulatePickLists(model);
            return(View(model));
        }
        private void PopulatePickLists(MissionTrackViewModel model)
        {
            var badgeSelector = uow.Badges.PickList.ToSelectList();
            var levelSelector = uow.MissionLevels.PickList.ToSelectList();

            model.BadgePicker = badgeSelector;
            model.LevelPicker = levelSelector;
        }
        public async Task <ActionResult> Edit(
            [Bind(Include = "Id,Name,Number,AwardTitle,BadgeId,MissionLevelId")] MissionTrackViewModel missionTrack)
        {
            if (!ModelState.IsValid)
            {
                PopulatePickLists(missionTrack);
                return(View(missionTrack));
            }
            try
            {
                await gameEngine.UpdateTrackAsync(missionTrack);

                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
                PopulatePickLists(missionTrack);
                return(View(missionTrack));
            }
        }