public async Task <AgeViewModel> GetAgeViewModel(string email)
        {
            AgeViewModel avm = new AgeViewModel();

            try
            {
                avm.UserProfile = await adb.UserAges.FirstOrDefaultAsync(x => x.Email == email.ToLower());

                List <UserAgeProfile> userAges = await adb.UserAges.OrderBy(x => x.Age).ToListAsync();

                if (userAges.Count > 0)
                {
                    avm.MinimumAge     = userAges.First().Age;
                    avm.MaximumAge     = userAges.Last().Age;
                    avm.AgeFrequencies = new int[(avm.MaximumAge - avm.MinimumAge) + 1];
                }
                foreach (UserAgeProfile user in userAges)
                {
                    avm.AgeFrequencies[user.Age - avm.MinimumAge]++;
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(avm);
        }
Beispiel #2
0
        public AgePage()
        {
            this.InitializeComponent();

            viewModel   = (Application.Current as App).BizLogic.GetViewModel <AgeViewModel>();
            DataContext = viewModel;
        }
Beispiel #3
0
        private void InitializeViews(int?age_id)
        {
            Age  age;
            User user = new UsersServices().GetByUserName(User.Identity.Name.ToString());

            if (age_id != null)
            {
                age = _ageService.GetById((int)age_id);
            }
            else
            {
                age = new Age();
            }
            _ageViewModel = new AgeViewModel(age);
        }
Beispiel #4
0
        public IActionResult Age()
        {
            AgeViewModel model = new AgeViewModel();

            return(View(model));
        }
Beispiel #5
0
        public ActionResult AgeCalculator(AgeViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (!model.BirthDate.All(char.IsDigit) || !(Convert.ToInt16(model.BirthDate) > 0) || !(Convert.ToInt16(model.BirthDate) <= 31) ||
                    !model.AsOfNowDate.All(char.IsDigit) || !(Convert.ToInt16(model.AsOfNowDate) > 0) || !(Convert.ToInt16(model.AsOfNowDate) <= 31))
                {
                    ModelState.AddModelError("", "Date should be between 1 to 31.");
                }
                if (!model.BirthMonth.All(char.IsDigit) || !(Convert.ToInt16(model.BirthMonth) > 0) || !(Convert.ToInt16(model.BirthMonth) <= 12) ||
                    !model.AsOfNowMonth.All(char.IsDigit) || !(Convert.ToInt16(model.AsOfNowMonth) > 0) || !(Convert.ToInt16(model.AsOfNowMonth) <= 12))
                {
                    ModelState.AddModelError("", "Month should be between 1 to 12.");
                }
                if (!model.BirthYear.All(char.IsDigit) || !(Convert.ToInt16(model.BirthYear) > 1950) || !(Convert.ToInt16(model.BirthYear) <= 2099) ||
                    !model.AsOfNowYear.All(char.IsDigit) || !(Convert.ToInt16(model.AsOfNowYear) > 1950) || !(Convert.ToInt16(model.AsOfNowYear) <= 2099))
                {
                    ModelState.AddModelError("", "Please enter valid year");
                }

                DateTime birthDateTime, asOfNowDateTime;
                if (!DateTime.TryParse(model.BirthYear + "-" + model.BirthMonth + "-" + model.BirthDate, out birthDateTime))
                {
                    ModelState.AddModelError("", "Please enter valid birth date");
                }
                if (!DateTime.TryParse(model.AsOfNowYear + "-" + model.AsOfNowMonth + "-" + model.AsOfNowDate, out asOfNowDateTime))
                {
                    ModelState.AddModelError("", "Please enter valid end date");
                }

                if (ModelState.IsValid)
                {
                    if (birthDateTime > asOfNowDateTime)
                    {
                        ModelState.AddModelError("", "Oops! Date of birth should not be greater than age at the date of");
                    }

                    if (ModelState.IsValid)
                    {
                        model.HasResult = true;
                        TimeSpan duratioDateTime = (asOfNowDateTime - birthDateTime);
                        model.StartDate     = birthDateTime;
                        model.EndDate       = asOfNowDateTime;
                        model.Days          = duratioDateTime.TotalDays;
                        model.Hours         = duratioDateTime.TotalHours;
                        model.Minutes       = duratioDateTime.TotalMinutes;
                        model.Seconds       = duratioDateTime.TotalSeconds;
                        model.Weeks         = string.Format("{0} Week(s) and {1} Day(s)", Math.Floor(model.Days / 7), (model.Days % 7));
                        model.LongDurations = DaysToMonthsYear(asOfNowDateTime, birthDateTime);
                        model.NextBirthday  = new DateTime(DateTime.Now.Year, birthDateTime.Month, birthDateTime.Day) > asOfNowDateTime
                            ? new DateTime(DateTime.Now.Year, birthDateTime.Month, birthDateTime.Day)
                            : new DateTime(DateTime.Now.Year, birthDateTime.Month, birthDateTime.Day).AddYears(1);
                        model.NextBirthdayDayLeft       = (int)(model.NextBirthday - asOfNowDateTime).TotalDays;
                        model.NextBirthdayLongDurations = DaysToMonthsYear(model.NextBirthday, asOfNowDateTime);
                        return(View(model));
                    }
                    return(View(new AgeViewModel()
                    {
                        HasResult = false
                    }));
                }
            }
            return(View(new AgeViewModel()
            {
                HasResult = false
            }));
        }
Beispiel #6
0
        public async Task <ActionResult> Index()
        {
            AgeViewModel avm = await access.GetAgeViewModel(User.Identity.Name);

            return(View(avm));
        }