/// <summary> /// Gets the minimum and optional maximum badge value. /// </summary> /// <param name="BadgeName">Name of the badge to get the value for.</param> /// <returns></returns> public async virtual Task <string> MaxAwardValue(string BadgeName) { var allActivities = await ActivityCollection.GetAllActivitiesAsync(false); var activityIds = allActivities.Where(x => x.Name == BadgeName).Select(x => x.Id); var allBadges = await BadgeCollection.GetAllBadgesForActivitiesAsync(activityIds); var Badge = allBadges.Select(x => new { x.Id, x.BadgeAwardValue, x.BadgeAwardValueMax }).FirstOrDefault(); var valueObject = new { minval = Badge.BadgeAwardValue, maxval = Badge.BadgeAwardValueMax }; return(Newtonsoft.Json.JsonConvert.SerializeObject(valueObject));; }
public async virtual Task <ActionResult> SubmitActivityForm() { var submittedActivity = SubmitActivity.CreateActivitySubmission(AuthenticatedUser.EmployeeId); TryUpdateModel(submittedActivity, "SubmittedActivity"); var activities = await ActivityCollection.GetAllActivitiesAsync(false); var badges = await BadgeCollection.GetAllBadgesForActivitiesAsync(activities.Where(x => x.Id == submittedActivity.ActivityId).Select(x => x.Id)); var badge = badges.Select(x => new { x.Id, x.BadgeAwardValue, x.BadgeAwardValueMax }).FirstOrDefault(); if (submittedActivity.AwardValue > badge.BadgeAwardValueMax || submittedActivity.AwardValue < badge.BadgeAwardValue) { ModelState.AddModelError("SubmittedActivity.AwardValue", String.Format("Award Value not within acceptable range. ({0} - {1})", badge.BadgeAwardValue, badge.BadgeAwardValueMax)); } else if (string.IsNullOrWhiteSpace(submittedActivity.EmployeeIds)) { ModelState.AddModelError("SubmittedActivity.EmployeeIds", ApplicationResources.NoEmployeeIdsErrorMsg); } else { //Parse the list of employee ids the client form sent us. List <int> empIds = submittedActivity.EmployeeIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Convert(delegate(string item, out int result) { return(int.TryParse(item.Trim(), out result)); }); bool allSaved = true; foreach (int empId in empIds) { var singleActivity = SubmitActivity.CreateActivitySubmission(empId); singleActivity.ActivityId = submittedActivity.ActivityId; singleActivity.ActivitySubmissionDate = submittedActivity.ActivitySubmissionDate; singleActivity.Notes = submittedActivity.Notes; singleActivity.AwardValue = submittedActivity.AwardValue; var singActEdit = await ActivityEdit.GetActivityEditByIdAsync(singleActivity.ActivityId); singleActivity.EntryType = singActEdit.EntryType; if (!await SaveObjectAsync(singleActivity, true)) { allSaved = false; } } if (allSaved) { return(RedirectToAction(await Mvc.Activities.Actions.Index())); } } return(await Index()); }