Пример #1
0
        public async Task <IActionResult> Publish()
        {
            if (!_session.GetInt32("UKPRN").HasValue)
            {
                return(RedirectToAction("Index", "Home", new { errmsg = "Please select a Provider." }));
            }

            var model = _session.GetObject <CopyCourseRunSaveViewModel>(CopyCourseRunSaveViewModelSessionKey);

            if (model == null)
            {
                return(NotFound());
            }

            if (!model.CourseId.HasValue)
            {
                return(NotFound());
            }

            var allRegions = await _regionCache.GetAllRegions();

            var allSubRegionIds = allRegions.SelectMany(r => r.SubRegions).Select(sr => sr.Id).ToHashSet();

            bool     flexibleStartDate  = model.StartDateType != StartDateType.SpecifiedStartDate;
            DateTime?specifiedStartDate = null;

            if (!flexibleStartDate)
            {
                specifiedStartDate = DateTime.ParseExact(
                    $"{int.Parse(model.Day):00}-{int.Parse(model.Month):00}-{model.Year}",
                    "dd-MM-yyyy",
                    System.Globalization.CultureInfo.InvariantCulture);
            }

            if (model.National == true)
            {
                model.SelectedRegions = null;
            }

            var validationResult = new CopyCourseRunSaveViewModelValidator(allRegions, _clock).Validate(model);

            if (!validationResult.IsValid)
            {
                return(BadRequest());
            }

            var createCommand = new CreateCourseRun()
            {
                CourseId          = model.CourseId.Value,
                CourseRunId       = Guid.NewGuid(),
                DurationUnit      = model.DurationUnit,
                DeliveryMode      = model.DeliveryMode,
                Cost              = !string.IsNullOrEmpty(model.Cost) ? Convert.ToDecimal(model.Cost) : (decimal?)null,
                CostDescription   = model.CostDescription ?? "",
                CourseName        = model.CourseName,
                CourseUrl         = model.Url,
                DurationValue     = Convert.ToInt32(model.DurationLength),
                ProviderCourseId  = model.CourseProviderReference ?? "",
                FlexibleStartDate = flexibleStartDate,
                StartDate         = specifiedStartDate,
                CreatedBy         = _currentUserProvider.GetCurrentUser(),
                CreatedOn         = _clock.UtcNow
            };

            switch (model.DeliveryMode)
            {
            case CourseDeliveryMode.ClassroomBased:
                createCommand.AttendancePattern = model.AttendanceMode;
                createCommand.StudyMode         = model.StudyMode;

                createCommand.SubRegionIds = null;
                createCommand.VenueId      = model.VenueId;
                break;

            case CourseDeliveryMode.WorkBased:
                createCommand.VenueId = null;

                var availableRegions = new SelectRegionModel();

                if (model.National == true)
                {
                    createCommand.National     = true;
                    createCommand.SubRegionIds = availableRegions.RegionItems.Select(x => x.Id).ToList();
                }
                else
                {
                    createCommand.National     = false;
                    createCommand.SubRegionIds = model.SelectedRegions.Where(id => allSubRegionIds.Contains(id));

                    createCommand.AttendancePattern = null;
                    createCommand.StudyMode         = null;
                }
                break;

            case CourseDeliveryMode.Online:

                createCommand.SubRegionIds      = null;
                createCommand.VenueId           = null;
                createCommand.AttendancePattern = null;
                createCommand.StudyMode         = null;
                break;
            }

            var createResult = await _sqlQueryDispatcher.ExecuteQuery(createCommand);

            if (!(createResult.Value is Success))
            {
                return(BadRequest());
            }

            _session.SetObject(CopyCourseRunPublishedCourseSessionKey, new PublishedCourseViewModel
            {
                CourseId    = createCommand.CourseId,
                CourseRunId = createCommand.CourseRunId,
                CourseName  = createCommand.CourseName
            });

            _session.Remove("NewAddedVenue");
            _session.Remove("Option");
            _session.Remove(CopyCourseRunSaveViewModelSessionKey);

            return(RedirectToAction("Published"));
        }
        public async Task <OneOf <NotFound, Success> > Execute(SqlTransaction transaction, CreateCourseRun query)
        {
            var sql = $@"
IF NOT EXISTS (SELECT 1 FROM Pttcd.Courses WHERE CourseId = @CourseId)
BEGIN
    SELECT 1 AS Result
    RETURN
END

INSERT INTO Pttcd.CourseRuns (
    CourseRunId,
    CourseId,
    CourseRunStatus,
    CreatedOn,
    CreatedBy,
    UpdatedOn,
    UpdatedBy,
    CourseName,
    VenueId,
    ProviderCourseId,
    DeliveryMode,
    FlexibleStartDate,
    StartDate,
    CourseWebsite,
    Cost,
    CostDescription,
    DurationUnit,
    DurationValue,
    StudyMode,
    AttendancePattern,
    [National]
)
VALUES (
    @CourseRunId,
    @CourseId,
    {(int)CourseStatus.Live},
    @CreatedOn,
    @CreatedByUserId,
    @CreatedOn,
    @CreatedByUserId,
    @CourseName,
    @VenueId,
    @ProviderCourseId,
    @DeliveryMode,
    @FlexibleStartDate,
    @StartDate,
    @CourseWebsite,
    @Cost,
    @CostDescription,
    @DurationUnit,
    @DurationValue,
    @StudyMode,
    @AttendancePattern,
    @National
)

INSERT INTO Pttcd.CourseRunSubRegions (CourseRunId, RegionId)
SELECT @CourseRunId, Value FROM @SubRegionIds

SELECT 0 AS Result

DECLARE @CourseRunIds Pttcd.GuidIdTable

INSERT INTO @CourseRunIds VALUES (@CourseRunId)

EXEC Pttcd.RefreshFindACourseIndex @CourseRunIds = @CourseRunIds, @Now = @CreatedOn";

            var paramz = new
            {
                query.CourseId,
                query.CourseRunId,
                query.CourseName,
                query.DeliveryMode,
                query.FlexibleStartDate,
                query.StartDate,
                CourseWebsite = query.CourseUrl,
                query.Cost,
                query.CostDescription,
                query.DurationUnit,
                query.DurationValue,
                query.ProviderCourseId,
                query.National,
                SubRegionIds = TvpHelper.CreateStringTable(query.SubRegionIds ?? Enumerable.Empty <string>()),
                query.VenueId,
                query.AttendancePattern,
                query.StudyMode,
                CreatedByUserId = query.CreatedBy.UserId,
                query.CreatedOn,
            };

            var result = await transaction.Connection.QuerySingleAsync <Result>(sql, paramz, transaction);

            if (result == Result.CourseNotFound)
            {
                return(new NotFound());
            }
            else
            {
                return(new Success());
            }
        }