Esempio n. 1
0
        public async Task <IActionResult> AddOneTimeSubscription(AddSubscriptionViewModel model)
        {
            IEnumerable <Course> teacherCourses = _courseService.GetTeacherCourses(
                HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier), model.SchoolId, true);

            if (teacherCourses == null || teacherCourses.Where(cour => cour.AllowOneTimePrice == true).Count() == 0)
            {
                TempData["ErrorMessage"] = "У вас немає активних курсів з разовими абонементми";
                return(RedirectToAction(nameof(Index), new { model.SchoolId }));
            }
            model.TeacherCourses = teacherCourses;
            Subscription subscription = new Subscription();

            if (model == null)
            {
                return(RedirectToAction(nameof(AddOneTimeSubscription)));
            }
            if (model.Student == null)
            {
                return(RedirectToAction(nameof(AddOneTimeSubscription)));
            }
            if (string.IsNullOrEmpty(model.Student.FullName) && string.IsNullOrEmpty(model.Student.Id))
            {
                TempData["ErrorMessage"] = "Потрібно вибрати учня ";
                return(View(model));
            }
            if (string.IsNullOrEmpty(model.Student.Id) && (model.Student.FullName.Length < 5))
            {
                TempData["ErrorMessage"] = "Ім'я повинно бути довше 5 символів";
                return(View(model));
            }
            if (string.IsNullOrEmpty(model.SelectedPaymentType))
            {
                TempData["ErrorMessage"] = "Виберіть тип оплати";
                return(View(model));
            }
            if (model.Month.Year < 2000)
            {
                return(RedirectToAction(nameof(AddOneTimeSubscription)));
            }
            if (model.SelectedCours == null || string.IsNullOrEmpty(model.SelectedCours.Id))
            {
                return(RedirectToAction(nameof(AddOneTimeSubscription)));
            }
            if (!string.IsNullOrEmpty(model.Student.Id))
            {
                var student = await _userService.GetUserAsync(model.Student.Id);

                subscription.AppUserId = model.Student.Id;
                subscription.FullName  = student.FullName;
                subscription.Phone     = student.PhoneNumber;
            }
            else if (!string.IsNullOrEmpty(model.Student.FullName))
            {
                subscription.FullName = model.Student.FullName;
                if (!string.IsNullOrEmpty(model.Student.PhoneNumber))
                {
                    subscription.Phone = model.Student.PhoneNumber;
                }
            }
            else
            {
                return(RedirectToAction(nameof(AddOneTimeSubscription)));
            }
            subscription.Id                = Guid.NewGuid().ToString();
            subscription.Period            = model.Month;
            subscription.CourseId          = model.SelectedCours.Id;
            subscription.MonthSubscription = false;
            subscription.CreatedDatetime   = DateTime.Now;
            subscription.Payments          = new List <Payment>
            {
                new Payment
                {
                    DateTime      = DateTime.Now,
                    PaymentTypeId = model.SelectedPaymentType,
                    Price         = model.SelectedCours.Price,
                    PayedToId     = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier),
                },
            };
            subscription.Price = teacherCourses.FirstOrDefault(cour => cour.Id == model.SelectedCours.Id).OneTimePrice;

            IdentityResult result = await _subscriptionsService.CreateSubscriptionAsync(subscription);

            if (result.Succeeded)
            {
                TempData["SuccessMessage"] = "Відвідування додано";
            }
            else if (result.Errors.Count() > 0)
            {
                string erorMessage = "";
                foreach (var error in result.Errors)
                {
                    erorMessage += error.Description;
                }
                TempData["ErrorMessage"] = erorMessage;
            }
            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> PostSubscriptionAsync()
        {
            await _subscriptionsService.CreateSubscriptionAsync();

            return(NoContent());
        }