public async Task <IActionResult> Create(ChallengesDetailViewModel model) { var site = await GetCurrentSiteAsync(); if (site.MaxPointsPerChallengeTask.HasValue && model.Challenge.TasksToComplete.HasValue && model.Challenge.TasksToComplete != 0) { double pointsPerChallenge = (double)model.Challenge.PointsAwarded / model.Challenge.TasksToComplete.Value; if (pointsPerChallenge > site.MaxPointsPerChallengeTask) { ModelState.AddModelError("Challenge.PointsAwarded", $"Too many points awarded."); } } if (model.BadgeUploadImage != null && (string.IsNullOrWhiteSpace(model.BadgeMakerImage) || !model.UseBadgeMaker)) { if (Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpg" && Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpeg" && Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".png") { ModelState.AddModelError("BadgeUploadImage", "Please use a .jpg or .png image"); } } if (ModelState.IsValid) { try { var challenge = model.Challenge; if (model.BadgeUploadImage != null || !string.IsNullOrWhiteSpace(model.BadgeMakerImage)) { byte[] badgeBytes; string filename; if (!string.IsNullOrWhiteSpace(model.BadgeMakerImage) && (model.BadgeUploadImage != null || model.UseBadgeMaker)) { var badgeString = model.BadgeMakerImage.Split(',').Last(); badgeBytes = Convert.FromBase64String(badgeString); filename = "badge.png"; } else { using (var fileStream = model.BadgeUploadImage.OpenReadStream()) { using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); badgeBytes = ms.ToArray(); } } filename = Path.GetFileName(model.BadgeUploadImage.FileName); } Badge newBadge = new Badge() { Filename = filename }; var badge = await _badgeService.AddBadgeAsync(newBadge, badgeBytes); challenge.BadgeId = badge.Id; } challenge = await _challengeService.AddChallengeAsync(challenge); AlertSuccess = $"Challenge '<strong>{challenge.Name}</strong>' was successfully created"; return(RedirectToAction("Edit", new { id = challenge.Id })); } catch (GraException gex) { ShowAlertWarning("Unable to add challenge: ", gex.Message); } } PageTitle = "Create Challenge"; model = await GetDetailLists(model); return(View(model)); }
public async Task <IActionResult> Create(ProgramDetailViewModel model) { if (!string.IsNullOrWhiteSpace(model.Program.JoinBadgeName) || !string.IsNullOrWhiteSpace(model.BadgeMakerImage) || model.BadgeUploadImage != null) { if (string.IsNullOrWhiteSpace(model.Program.JoinBadgeName)) { ModelState.AddModelError("Program.JoinBadgeName", "Please provide a name for the badge"); } if (string.IsNullOrWhiteSpace(model.BadgeMakerImage) && model.BadgeUploadImage == null) { ModelState.AddModelError("BadgePath", "Please provide an image for the badge."); } else if (model.BadgeUploadImage != null && (string.IsNullOrWhiteSpace(model.BadgeMakerImage) || !model.UseBadgeMaker) && (Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpg" && Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpeg" && Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".png")) { ModelState.AddModelError("BadgeUploadImage", "Please use a .jpg or .png image."); } } if (model.Program.AgeMaximum < model.Program.AgeMinimum) { ModelState.AddModelError("Program.AgeMaximum", "The maximum age cannot be lower than the minimum age."); } if (ModelState.IsValid) { try { if (model.BadgeUploadImage != null || !string.IsNullOrWhiteSpace(model.BadgeMakerImage)) { byte[] badgeBytes; string filename; if (!string.IsNullOrWhiteSpace(model.BadgeMakerImage) && (model.BadgeUploadImage == null || model.UseBadgeMaker)) { var badgeString = model.BadgeMakerImage.Split(',').Last(); badgeBytes = Convert.FromBase64String(badgeString); filename = "badge.png"; } else { using (var fileStream = model.BadgeUploadImage.OpenReadStream()) { using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); badgeBytes = ms.ToArray(); } } filename = Path.GetFileName(model.BadgeUploadImage.FileName); } var newBadge = new Badge { Filename = filename }; var badge = await _badgeService.AddBadgeAsync(newBadge, badgeBytes); model.Program.JoinBadgeId = badge.Id; model.Program.JoinBadgeName = model.Program.JoinBadgeName.Trim(); } model.Program.AskAge = model.AgeValues >= 1; model.Program.AgeRequired = model.AgeValues == 2; model.Program.AskSchool = model.SchoolValues >= 1; model.Program.SchoolRequired = model.SchoolValues == 2; model.Program.Name = model.Program.Name.Trim(); var program = await _siteService.AddProgramAsync(model.Program); ShowAlertSuccess($"Added Program \"{program.Name}\"!"); return(RedirectToAction(nameof(Edit), new { id = program.Id })); } catch (GraException gex) { ShowAlertDanger("Unable to add Program: ", gex); } } var dailyLiteracyTipList = await _dailyLiteracyTipService.GetListAsync(); var pointTranslationList = await _pointTranslationService.GetListAsync(); model.DailyLiteracyTipList = new SelectList(dailyLiteracyTipList, "Id", "Name"); model.PointTranslationList = new SelectList(pointTranslationList, "Id", "TranslationName"); PageTitle = "Create Program"; return(View("Detail", model)); }
public async Task <IActionResult> Create(EventsDetailViewModel model) { var requireSecretCode = await GetSiteSettingBoolAsync( SiteSettingKey.Events.RequireBadge); if (model.Event.AllDay) { if (model.Event.EndDate.HasValue && model.Event.StartDate > model.Event.EndDate) { ModelState.AddModelError("Event.EndDate", "The End date cannot be before the Start date"); } } else { if (model.Event.EndDate.HasValue && model.Event.StartDate.TimeOfDay > model.Event.EndDate.Value.TimeOfDay) { ModelState.AddModelError("Event.EndDate", "The End time cannot be before the Start time"); } } if (model.UseLocation && !model.Event.AtLocationId.HasValue) { ModelState.AddModelError("Event.AtLocationId", "The At Location field is required."); } if (!model.UseLocation && !model.Event.AtBranchId.HasValue) { ModelState.AddModelError("Event.AtBranchId", "The At Branch field is required."); } if (model.IncludeSecretCode || requireSecretCode) { if (string.IsNullOrWhiteSpace(model.BadgeMakerImage) && model.BadgeUploadImage == null) { ModelState.AddModelError("BadgemakerImage", "A badge is required."); } else if (model.BadgeUploadImage != null && (string.IsNullOrWhiteSpace(model.BadgeMakerImage) || !model.UseBadgeMaker) && (Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpg" && Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpeg" && Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".png")) { ModelState.AddModelError("BadgeUploadImage", "Please use a .jpg or .png image."); } } else { ModelState.Remove(nameof(model.SecretCode)); ModelState.Remove(nameof(model.AwardMessage)); ModelState.Remove(nameof(model.AwardPoints)); } if (!string.IsNullOrWhiteSpace(model.Event.ExternalLink)) { try { model.Event.ExternalLink = new UriBuilder( model.Event.ExternalLink).Uri.AbsoluteUri; } catch (Exception) { ModelState.AddModelError("Event.ExternalLink", "Invalid URL"); } } if (ModelState.IsValid) { try { if (model.Event.ChallengeId.HasValue) { model.Event.ChallengeGroupId = null; } if (model.Event.AllDay) { model.Event.StartDate = model.Event.StartDate.Date; if (model.Event.EndDate.HasValue) { if (model.Event.EndDate.Value.Date == model.Event.StartDate.Date) { model.Event.EndDate = null; } else { model.Event.EndDate = model.Event.EndDate.Value.Date; } } } else { if (model.Event.EndDate.HasValue) { if (model.Event.EndDate.Value.TimeOfDay == model.Event.StartDate.TimeOfDay) { model.Event.EndDate = null; } else { model.Event.EndDate = model.Event.StartDate.Date + model.Event.EndDate.Value.TimeOfDay; } } } int?triggerId = null; if (model.IncludeSecretCode) { byte[] badgeBytes; string filename; if (!string.IsNullOrWhiteSpace(model.BadgeMakerImage) && (model.BadgeUploadImage == null || model.UseBadgeMaker)) { var badgeString = model.BadgeMakerImage.Split(',').Last(); badgeBytes = Convert.FromBase64String(badgeString); filename = "badge.png"; } else { using (var fileStream = model.BadgeUploadImage.OpenReadStream()) { using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); badgeBytes = ms.ToArray(); } } filename = Path.GetFileName(model.BadgeUploadImage.FileName); } Badge newBadge = new Badge() { Filename = filename }; var badge = await _badgeService.AddBadgeAsync(newBadge, badgeBytes); Trigger trigger = new Trigger { Name = $"Event '{model.Event.Name}' code", SecretCode = model.SecretCode, AwardMessage = model.AwardMessage, AwardPoints = model.AwardPoints, AwardBadgeId = badge.Id, }; triggerId = (await _triggerService.AddAsync(trigger)).Id; } var graEvent = model.Event; if (model.UseLocation) { graEvent.AtBranchId = null; } else { graEvent.AtLocationId = null; } graEvent.IsActive = true; graEvent.IsValid = true; if (triggerId.HasValue) { graEvent.RelatedTriggerId = triggerId; } await _eventService.Add(graEvent); ShowAlertSuccess($"Event '{graEvent.Name}' created."); if (graEvent.IsCommunityExperience) { return(RedirectToAction("CommunityExperiences")); } return(RedirectToAction("Index")); } catch (GraException gex) { ShowAlertWarning("Could not create event: ", gex.Message); } } PageTitle = "Create Event"; var systemList = await _siteService.GetSystemList(true); var branchList = await _siteService.GetBranches(model.SystemId, true); var locationList = await _eventService.GetLocations(); var programList = await _siteService.GetProgramList(); model.SystemList = new SelectList(systemList, "Id", "Name"); model.BranchList = new SelectList(branchList, "Id", "Name"); model.LocationList = new SelectList(locationList, "Id", "Name"); model.ProgramList = new SelectList(programList, "Id", "Name"); model.RequireSecretCode = requireSecretCode; return(View(model)); }
public async Task <IActionResult> Create(TriggersDetailViewModel model) { byte[] badgeBytes = null; var badgeRequiredList = new List <int>(); var challengeRequiredList = new List <int>(); model.IgnorePointLimits = UserHasPermission(Permission.IgnorePointLimits); model.MaxPointLimit = await _triggerService .GetMaximumAllowedPointsAsync(GetCurrentSiteId()); if (!model.IgnorePointLimits && model.MaxPointLimit.HasValue && model.Trigger.AwardPoints > model.MaxPointLimit) { ModelState.AddModelError("Trigger.AwardPoints", $"You may award up to {model.MaxPointLimit} points."); } if (!string.IsNullOrWhiteSpace(model.BadgeRequiredList)) { badgeRequiredList = model.BadgeRequiredList .Split(',') .Where(_ => !string.IsNullOrWhiteSpace(_)) .Select(int.Parse) .ToList(); } if (!string.IsNullOrWhiteSpace(model.ChallengeRequiredList)) { challengeRequiredList = model.ChallengeRequiredList .Split(',') .Where(_ => !string.IsNullOrWhiteSpace(_)) .Select(int.Parse) .ToList(); } var requirementCount = badgeRequiredList.Count + challengeRequiredList.Count; if (string.IsNullOrWhiteSpace(model.BadgeAltText)) { ModelState.AddModelError("BadgeAltText", "The badge's alternative text is required."); } if (string.IsNullOrWhiteSpace(model.BadgeMakerImage) && model.BadgeUploadImage == null) { ModelState.AddModelError("BadgePath", "A badge is required."); } else if (model.BadgeUploadImage != null && (string.IsNullOrWhiteSpace(model.BadgeMakerImage) || !model.UseBadgeMaker)) { if (!ValidImageExtensions.Contains( Path.GetExtension(model.BadgeUploadImage.FileName).ToLowerInvariant())) { ModelState.AddModelError("BadgeUploadImage", $"Image must be one of the following types: {string.Join(", ", ValidImageExtensions)}"); } if (model.BadgeUploadImage != null) { try { using (var ms = new MemoryStream()) { await model.BadgeUploadImage.CopyToAsync(ms); badgeBytes = ms.ToArray(); } await _badgeService.ValidateBadgeImageAsync(badgeBytes); } catch (GraException gex) { ModelState.AddModelError("BadgeUploadImage", gex.Message); } } } if (!model.IsSecretCode) { if ((!model.Trigger.Points.HasValue || model.Trigger.Points < 1) && requirementCount < 1) { ModelState.AddModelError("TriggerRequirements", "Points or a Challenge/Trigger item is required."); } else if ((!model.Trigger.ItemsRequired.HasValue || model.Trigger.ItemsRequired < 1) && requirementCount >= 1) { ModelState.AddModelError("Trigger.ItemsRequired", "Please enter how many of the Challenge/Trigger item are required."); } else if (model.Trigger.ItemsRequired > requirementCount) { ModelState.AddModelError("Trigger.ItemsRequired", "Items Required can not be greater than the number of Challenge/Trigger items."); } } else if (string.IsNullOrWhiteSpace(model.Trigger.SecretCode)) { ModelState.AddModelError("Trigger.SecretCode", "The Secret Code field is required."); } else if (await _triggerService.CodeExistsAsync(model.Trigger.SecretCode)) { ModelState.AddModelError("Trigger.SecretCode", "That Secret Code already exists."); } if (model.AwardsPrize && string.IsNullOrWhiteSpace(model.Trigger.AwardPrizeName)) { ModelState.AddModelError("Trigger.AwardPrizeName", "The Prize Name field is required."); } if (model.AwardsMail) { if (string.IsNullOrWhiteSpace(model.Trigger.AwardMailSubject)) { ModelState.AddModelError("Trigger.AwardMailSubject", "The Mail Subject field is required."); } if (string.IsNullOrWhiteSpace(model.Trigger.AwardMail)) { ModelState.AddModelError("Trigger.AwardMail", "The Mail Message field is required."); } } if (ModelState.IsValid) { try { if (model.IsSecretCode) { model.Trigger.Points = null; model.Trigger.ItemsRequired = null; model.Trigger.LimitToSystemId = null; model.Trigger.LimitToBranchId = null; model.Trigger.LimitToProgramId = null; model.Trigger.SecretCode = model .Trigger.SecretCode .Trim() .ToLowerInvariant(); model.Trigger.BadgeIds = new List <int>(); model.Trigger.ChallengeIds = new List <int>(); } else { model.Trigger.SecretCode = null; model.Trigger.BadgeIds = badgeRequiredList; model.Trigger.ChallengeIds = challengeRequiredList; } if (!model.AwardsPrize) { model.Trigger.AwardPrizeName = ""; model.Trigger.AwardPrizeRedemptionInstructions = ""; } if (!model.AwardsMail) { model.Trigger.AwardMailSubject = ""; model.Trigger.AwardMail = ""; } if (model.BadgeUploadImage != null || !string.IsNullOrWhiteSpace(model.BadgeMakerImage)) { string filename; if (!string.IsNullOrWhiteSpace(model.BadgeMakerImage) && (model.BadgeUploadImage == null || model.UseBadgeMaker)) { var badgeString = model.BadgeMakerImage.Split(',').Last(); badgeBytes = Convert.FromBase64String(badgeString); filename = "badge.png"; } else { if (badgeBytes == null) { using (var ms = new MemoryStream()) { await model.BadgeUploadImage.CopyToAsync(ms); badgeBytes = ms.ToArray(); } } filename = Path.GetFileName(model.BadgeUploadImage.FileName); } var newBadge = new Badge { Filename = filename, AltText = model.BadgeAltText }; var badge = await _badgeService.AddBadgeAsync(newBadge, badgeBytes); model.Trigger.AwardBadgeId = badge.Id; } var trigger = await _triggerService.AddAsync(model.Trigger); ShowAlertSuccess($"Trigger '<strong>{trigger.Name}</strong>' was successfully created"); return(RedirectToAction("Index")); } catch (GraException gex) { ShowAlertWarning("Unable to add trigger: ", gex.Message); } } model.Action = "Create"; model.SystemList = new SelectList(await _siteService.GetSystemList(), "Id", "Name"); if (model.Trigger.LimitToSystemId.HasValue) { model.BranchList = new SelectList( await _siteService.GetBranches(model.Trigger.LimitToSystemId.Value), "Id", "Name"); } else { model.BranchList = new SelectList(await _siteService.GetAllBranches(), "Id", "Name"); } model.ProgramList = new SelectList(await _siteService.GetProgramList(), "Id", "Name"); model.TriggerRequirements = await _triggerService .GetRequirementsByIdsAsync(badgeRequiredList, challengeRequiredList); foreach (var requirement in model.TriggerRequirements) { requirement.BadgePath = _pathResolver.ResolveContentPath(requirement.BadgePath); } if (model.EditVendorCode) { model.VendorCodeTypeList = new SelectList( await _vendorCodeService.GetTypeAllAsync(), "Id", "Description"); } if (model.EditAvatarBundle) { model.UnlockableAvatarBundleList = new SelectList( await _avatarService.GetAllBundlesAsync(true), "Id", "Name"); } if (model.MaxPointLimit.HasValue) { model.MaxPointsMessage = $"(Up to {model.MaxPointLimit.Value} points)"; } PageTitle = "Create Trigger"; return(View("Detail", model)); }
public async Task <IActionResult> Create(TriggersDetailViewModel model) { List <int> badgeRequiredList = new List <int>(); List <int> challengeRequiredList = new List <int>(); if (!string.IsNullOrWhiteSpace(model.BadgeRequiredList)) { badgeRequiredList = model.BadgeRequiredList .Replace("<", "") .Split('>') .Where(_ => !string.IsNullOrWhiteSpace(_)) .Select(Int32.Parse) .ToList(); } if (!string.IsNullOrWhiteSpace(model.ChallengeRequiredList)) { challengeRequiredList = model.ChallengeRequiredList .Replace("<", "") .Split('>') .Where(_ => !string.IsNullOrWhiteSpace(_)) .Select(Int32.Parse) .ToList(); } var requirementCount = badgeRequiredList.Count + challengeRequiredList.Count; if (string.IsNullOrWhiteSpace(model.BadgeMakerImage) && model.BadgeUploadImage == null) { ModelState.AddModelError("BadgePath", "A badge is required."); } else if (model.BadgeUploadImage != null && (string.IsNullOrWhiteSpace(model.BadgeMakerImage) || !model.UseBadgeMaker) && (Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpg" && Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".jpeg" && Path.GetExtension(model.BadgeUploadImage.FileName).ToLower() != ".png")) { ModelState.AddModelError("BadgeUploadImage", "Please use a .jpg or .png image."); } if (!model.IsSecretCode) { if ((!model.Trigger.Points.HasValue || model.Trigger.Points < 1) && requirementCount < 1) { ModelState.AddModelError("TriggerRequirements", "Points or a Challenge/Trigger item is required."); } else if ((!model.Trigger.ItemsRequired.HasValue || model.Trigger.ItemsRequired < 1) && requirementCount >= 1) { ModelState.AddModelError("Trigger.ItemsRequired", "Please enter how many of the Challenge/Trigger item are required."); } else if (model.Trigger.ItemsRequired > requirementCount) { ModelState.AddModelError("Trigger.ItemsRequired", "Items Required can not be greater than the number of Challenge/Trigger items."); } } else if (string.IsNullOrWhiteSpace(model.Trigger.SecretCode)) { ModelState.AddModelError("Trigger.SecretCode", "The Secret Code field is required."); } else if (await _triggerService.CodeExistsAsync(model.Trigger.SecretCode)) { ModelState.AddModelError("Trigger.SecretCode", "That Secret Code already exists."); } if (ModelState.IsValid) { try { if (model.IsSecretCode) { model.Trigger.Points = null; model.Trigger.ItemsRequired = null; model.Trigger.LimitToSystemId = null; model.Trigger.LimitToBranchId = null; model.Trigger.LimitToProgramId = null; model.Trigger.SecretCode = model.Trigger.SecretCode.Trim().ToLower(); model.Trigger.BadgeIds = new List <int>(); model.Trigger.ChallengeIds = new List <int>(); } else { model.Trigger.SecretCode = null; model.Trigger.BadgeIds = badgeRequiredList; model.Trigger.ChallengeIds = challengeRequiredList; } if (model.BadgeUploadImage != null || !string.IsNullOrWhiteSpace(model.BadgeMakerImage)) { byte[] badgeBytes; string filename; if (!string.IsNullOrWhiteSpace(model.BadgeMakerImage) && (model.BadgeUploadImage != null || model.UseBadgeMaker)) { var badgeString = model.BadgeMakerImage.Split(',').Last(); badgeBytes = Convert.FromBase64String(badgeString); filename = "badge.png"; } else { using (var fileStream = model.BadgeUploadImage.OpenReadStream()) { using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); badgeBytes = ms.ToArray(); } } filename = Path.GetFileName(model.BadgeUploadImage.FileName); } Badge newBadge = new Badge() { Filename = filename }; var badge = await _badgeService.AddBadgeAsync(newBadge, badgeBytes); model.Trigger.AwardBadgeId = badge.Id; } var trigger = await _triggerService.AddAsync(model.Trigger); ShowAlertSuccess($"Trigger '<strong>{trigger.Name}</strong>' was successfully created"); return(RedirectToAction("Index")); } catch (GraException gex) { ShowAlertWarning("Unable to add trigger: ", gex.Message); } } model.Action = "Create"; model.SystemList = new SelectList((await _siteService.GetSystemList()), "Id", "Name"); if (model.Trigger.LimitToSystemId.HasValue) { model.BranchList = new SelectList( (await _siteService.GetBranches(model.Trigger.LimitToSystemId.Value)), "Id", "Name"); } else { model.BranchList = new SelectList((await _siteService.GetAllBranches()), "Id", "Name"); } model.ProgramList = new SelectList((await _siteService.GetProgramList()), "Id", "Name"); model.VendorCodeTypeList = new SelectList( (await _vendorCodeService.GetTypeAllAsync()), "Id", "Name"); model.TriggerRequirements = await _triggerService .GetRequirementsByIdsAsync(badgeRequiredList, challengeRequiredList); foreach (var requirement in model.TriggerRequirements) { requirement.BadgePath = _pathResolver.ResolveContentPath(requirement.BadgePath); } PageTitle = "Create Trigger"; return(View("Detail", model)); }
public async Task <IActionResult> Create(ChallengesDetailViewModel model) { byte[] badgeBytes = null; model.IgnorePointLimits = UserHasPermission(Permission.IgnorePointLimits); model.MaxPointLimit = await _challengeService .GetMaximumAllowedPointsAsync(GetCurrentSiteId()); if (!model.IgnorePointLimits && model.Challenge.TasksToComplete.HasValue && model.Challenge.TasksToComplete != 0) { double pointsPerChallenge = (double)model.Challenge.PointsAwarded / model.Challenge.TasksToComplete.Value; if (pointsPerChallenge > model.MaxPointLimit) { ModelState.AddModelError("Challenge.PointsAwarded", $"A challenge with {model.Challenge.TasksToComplete} tasks may award a maximum of {model.MaxPointLimit * model.Challenge.TasksToComplete} points."); } } if (model.BadgeUploadImage != null && (string.IsNullOrWhiteSpace(model.BadgeMakerImage) || !model.UseBadgeMaker)) { if (!ValidImageExtensions.Contains(Path .GetExtension(model.BadgeUploadImage.FileName) .ToLowerInvariant())) { ModelState.AddModelError("BadgeUploadImage", $"Image must be one of the following types: {string.Join(", ", ValidImageExtensions)}"); } try { using (var ms = new MemoryStream()) { await model.BadgeUploadImage.CopyToAsync(ms); badgeBytes = ms.ToArray(); } await _badgeService.ValidateBadgeImageAsync(badgeBytes); } catch (GraException gex) { ModelState.AddModelError("BadgeUploadImage", gex.Message); } } if (ModelState.IsValid) { try { var challenge = model.Challenge; if (model.BadgeUploadImage != null || !string.IsNullOrWhiteSpace(model.BadgeMakerImage)) { string filename; if (!string.IsNullOrWhiteSpace(model.BadgeMakerImage) && (model.BadgeUploadImage == null || model.UseBadgeMaker)) { var badgeString = model.BadgeMakerImage.Split(',').Last(); badgeBytes = Convert.FromBase64String(badgeString); filename = "badge.png"; } else { if (badgeBytes == null) { using (var ms = new MemoryStream()) { await model.BadgeUploadImage.CopyToAsync(ms); badgeBytes = ms.ToArray(); } } filename = Path.GetFileName(model.BadgeUploadImage.FileName); } var newBadge = new Badge { Filename = filename }; var badge = await _badgeService.AddBadgeAsync(newBadge, badgeBytes); challenge.BadgeId = badge.Id; } var serviceResult = await _challengeService.AddChallengeAsync(challenge); if (serviceResult.Status == ServiceResultStatus.Warning && !string.IsNullOrWhiteSpace(serviceResult.Message)) { ShowAlertWarning(serviceResult.Message); } AlertSuccess = $"Challenge '<strong>{serviceResult.Data.Name}</strong>' was successfully created"; return(RedirectToAction("Edit", new { id = serviceResult.Data.Id })); } catch (GraException gex) { ShowAlertWarning("Unable to add challenge: ", gex.Message); } } PageTitle = "Create Challenge"; model = await GetDetailLists(model); if (model.MaxPointLimit.HasValue) { model.MaxPointsMessage = $"(Up to {model.MaxPointLimit.Value} points per required task)"; } return(View(model)); }