public virtual async Task <ActionResult> AddBadgePost(BadgeEditViewModel badgeEditViewModel, HttpPostedFileBase badgeImage)
        {
            ClearModelErrors();
            var badgeEdit = BadgeEdit.CreateBadge();

            badgeEditViewModel.Badge = badgeEdit as BadgeEdit;
            if (badgeImage != null)
            {
                var bytes = badgeImage.InputStream.GetBytes();
                badgeEditViewModel.Badge.SetBadgeImage(bytes);
            }

            SetActivitiesToAdd(badgeEditViewModel);
            if (await SaveObjectAsync(badgeEditViewModel.Badge, be =>
            {
                UpdateModel(be, "Badge");
                CheckForValidImage(be);

                if (be.Priority == 0)
                {
                    be.Priority = Int32.MaxValue;
                }
            }, true))
            {
                return(RedirectToAction(Mvc.BadgeManager.Index().Result));
            }

            return(await AddBadge());
        }
        public async virtual Task <ActionResult> AddBadge()
        {
            var allActivities = await ActivityCollection.GetAllActivitiesAsync(false);

            var badgeEdit          = BadgeEdit.CreateBadge();
            var badgeEditViewModel = new BadgeEditViewModel(allActivities);

            badgeEditViewModel.Badge          = badgeEdit as BadgeEdit;
            badgeEditViewModel.Badge.Priority = 0;

            return(View(Mvc.BadgeManager.Views.AddBadge, badgeEditViewModel));
        }
        private static void SetActivitiesToRemove(BadgeEditViewModel badgeEditViewModel)
        {
            var activityIdsToRemove = badgeEditViewModel.Badge.BadgeActivities
                                      .Where(bae => bae.ActivityId != badgeEditViewModel.SelectedActivityId)
                                      .Select(bae => bae.ActivityId)
                                      .ToList();

            foreach (var activityId in activityIdsToRemove)
            {
                var badgeActivityEdit = badgeEditViewModel.Badge.BadgeActivities
                                        .Where(bae => bae.ActivityId == activityId)
                                        .Single();

                badgeEditViewModel.Badge.BadgeActivities.Remove(badgeActivityEdit);
            }
        }
        public virtual async Task <ActionResult> EditBadge(int id)
        {
            var allActivities = await ActivityCollection.GetAllActivitiesAsync(false);

            var badgeEdit = await BadgeEdit.GetBadgeEditByIdAsync(id);

            if (BusinessRules.HasPermission(AuthorizationActions.EditObject, badgeEdit))
            {
                var badgeEditViewModel = new BadgeEditViewModel(allActivities, badgeEdit.BadgeActivities);
                badgeEditViewModel.Badge = badgeEdit as BadgeEdit;
                if (badgeEditViewModel.Badge.Priority == Int32.MaxValue)
                {
                    badgeEditViewModel.Badge.Priority = 0;
                }

                return(View(Mvc.BadgeManager.Views.EditBadge, badgeEditViewModel));
            }

            return(RedirectToAction(Mvc.Error.AccessDenied()));
        }
Esempio n. 5
0
        public ActionResult Edit(BadgeEditViewModel badge)
        {
            if (badge.Image != null)
            {
                var fileName = Path.GetFileName(badge.Image.FileName);
                badge.ImageUrl = fileName;
                var path      = GetImagePath(badge.Id);
                var imagePath = path + "/" + fileName;
                Directory.CreateDirectory(path);
                badge.Image.SaveAs(imagePath);
            }
            else
            {
                badge.ImageUrl = _badgeRepository.Get(badge.Id).ImageUrl;
            }

            var editedBadge = Mapper.Map <BadgeEditViewModel, Badge>(badge);

            _badgeRepository.Update(editedBadge);
            return(RedirectToAction("Index"));
        }
        public virtual async Task <ActionResult> EditBadgePost(int id, BadgeEditViewModel badgeEditViewModel, HttpPostedFileBase badgeImage)
        {
            badgeEditViewModel.Badge = await BadgeEdit.GetBadgeEditByIdAsync(id) as BadgeEdit;

            if (badgeImage != null)
            {
                var bytes = badgeImage.InputStream.GetBytes();
                badgeEditViewModel.Badge.SetBadgeImage(bytes);
            }

            SetActivitiesToAdd(badgeEditViewModel);
            SetActivitiesToRemove(badgeEditViewModel);
            TryUpdateModel(badgeEditViewModel.Badge, "Badge");
            CheckForValidImage(badgeEditViewModel.Badge);

            if (await SaveObjectAsync(badgeEditViewModel.Badge, true))
            {
                return(RedirectToAction(Mvc.BadgeManager.Index().Result));
            }

            return(await EditBadge(id));
        }
        private static void SetActivitiesToAdd(BadgeEditViewModel badgeEditViewModel)
        {
            var activityIdsToAdd = new List <int>();

            if (badgeEditViewModel.SelectedActivityId.HasValue)
            {
                activityIdsToAdd = new List <int>()
                {
                    badgeEditViewModel.SelectedActivityId.Value
                };
            }

            foreach (var activityId in activityIdsToAdd)
            {
                if (!badgeEditViewModel.Badge.BadgeActivities.Where(bae => bae.ActivityId == activityId).Any())
                {
                    var badgeActivityEdit = BadgeActivityEdit.CreateBadgeActivity();
                    badgeActivityEdit.ActivityId = activityId;

                    badgeEditViewModel.Badge.BadgeActivities.Add(badgeActivityEdit);
                }
            }
        }