public async Task <OneOf <NotFound, Success> > Execute(DocumentClient client,
                                                               Configuration configuration, UpdateApprenticeship request)
        {
            var documentUri = UriFactory.CreateDocumentUri(
                configuration.DatabaseId,
                configuration.ApprenticeshipCollectionName,
                request.Id.ToString());

            var partitionKey = new PartitionKey(request.ProviderUkprn);

            Apprenticeship apprenticeship;

            try
            {
                var query = await client.ReadDocumentAsync <Apprenticeship>(
                    documentUri,
                    new RequestOptions()
                {
                    PartitionKey = partitionKey
                });

                apprenticeship = query.Document;
            }
            catch (DocumentClientException dex) when(dex.StatusCode == HttpStatusCode.NotFound)
            {
                return(new NotFound());
            }

            apprenticeship.ApprenticeshipTitle  = request.ApprenticeshipTitle;
            apprenticeship.ApprenticeshipType   = request.ApprenticeshipType;
            apprenticeship.MarketingInformation = request.MarketingInformation;
            apprenticeship.Url = request.Url;
            apprenticeship.ContactTelephone        = request.ContactTelephone;
            apprenticeship.ContactEmail            = request.ContactEmail;
            apprenticeship.ContactWebsite          = request.ContactWebsite;
            apprenticeship.ApprenticeshipLocations = request.ApprenticeshipLocations.Select(l => new ApprenticeshipLocation()
            {
                Address = l.Address,
                ApprenticeshipLocationType = l.ApprenticeshipLocationType,
                CreatedBy     = request.UpdatedBy.UserId,
                CreatedDate   = request.UpdatedDate,
                DeliveryModes = l.DeliveryModes.ToList(),
                Id            = l.Id ?? Guid.NewGuid(),
                LocationType  = l.LocationType,
                Name          = l.Name,
                National      = l.National,
                Phone         = l.Phone,
                ProviderUKPRN = apprenticeship.ProviderUKPRN,
                Radius        = l.Radius,
                RecordStatus  = 1,
                Regions       = l.Regions,
                UpdatedBy     = request.UpdatedBy.UserId,
                UpdatedDate   = request.UpdatedDate,
                VenueId       = l.VenueId
            }).ToList();
            apprenticeship.RecordStatus     = request.Status ?? apprenticeship.RecordStatus;
            apprenticeship.UpdatedDate      = request.UpdatedDate;
            apprenticeship.UpdatedBy        = request.UpdatedBy.UserId;
            apprenticeship.BulkUploadErrors = request.BulkUploadErrors?.ToList() ?? apprenticeship.BulkUploadErrors;

            request.StandardOrFramework.Switch(
                standard =>
            {
                apprenticeship.StandardId         = standard.CosmosId;
                apprenticeship.StandardCode       = standard.StandardCode;
                apprenticeship.Version            = standard.Version;
                apprenticeship.NotionalNVQLevelv2 = standard.NotionalNVQLevelv2;
            },
                framework =>
            {
                apprenticeship.FrameworkId   = framework.CosmosId;
                apprenticeship.FrameworkCode = framework.FrameworkCode;
                apprenticeship.ProgType      = framework.ProgType;
                apprenticeship.PathwayCode   = framework.PathwayCode;
            });

            await client.ReplaceDocumentAsync(
                documentUri,
                apprenticeship,
                new RequestOptions()
            {
                PartitionKey = partitionKey
            });

            return(new Success());
        }
        public async Task <OneOf <NotFound, Success> > Execute(SqlTransaction transaction, UpdateApprenticeship query)
        {
            var sql = $@"
UPDATE Pttcd.Apprenticeships SET
    UpdatedOn = @UpdatedOn,
    UpdatedBy = @UpdatedByUserId,
    ApprenticeshipTitle = s.StandardName,
    StandardCode = @StandardCode,
    StandardVersion = @StandardVersion,
    MarketingInformation = @MarketingInformation,
    ApprenticeshipWebsite = @ApprenticeshipWebsite,
    ContactTelephone = @ContactTelephone,
    ContactEmail = @ContactEmail,
    ContactWebsite = @ContactWebsite
FROM Pttcd.Apprenticeships a
CROSS JOIN Pttcd.Standards s
WHERE a.ApprenticeshipId = @ApprenticeshipId
AND a.ApprenticeshipStatus IN ({(int)ApprenticeshipStatus.Live}, {(int)ApprenticeshipStatus.Pending})
AND s.StandardCode = @StandardCode
AND s.Version = @StandardVersion

IF @@ROWCOUNT = 0
BEGIN
    SELECT 1 AS Result
    RETURN
END

MERGE Pttcd.ApprenticeshipLocations AS target
USING (
    SELECT v.VenueName Name, l.* FROM @ApprenticeshipLocations l
    LEFT JOIN Pttcd.Venues v ON l.VenueId = v.VenueId
) AS source
ON target.ApprenticeshipLocationId = source.ApprenticeshipLocationId
WHEN MATCHED THEN UPDATE SET
    UpdatedOn = @UpdatedOn,
    UpdatedBy = @UpdatedByUserId,
    Telephone = source.Telephone,
    VenueId = source.VenueId,
    [National] = source.[National],
    Radius = source.Radius,
    ApprenticeshipLocationType = source.ApprenticeshipLocationType,
    Name = source.Name,
    DeliveryModes = source.DeliveryModes
WHEN NOT MATCHED THEN
    INSERT (
        ApprenticeshipLocationId,
        ApprenticeshipId,
        ApprenticeshipLocationStatus,
        CreatedOn,
        CreatedBy,
        UpdatedOn,
        UpdatedBy,
        Telephone,
        VenueId,
        [National],
        Radius,
        ApprenticeshipLocationType,
        Name,
        DeliveryModes
    )
    VALUES (
        source.ApprenticeshipLocationId,
        @ApprenticeshipId,
        {(int)ApprenticeshipStatus.Live},
        @UpdatedOn,
        @UpdatedByUserId,
        @UpdatedOn,
        @UpdatedByUserId,
        source.Telephone,
        source.VenueId,
        source.[National],
        source.Radius,
        source.ApprenticeshipLocationType,
        source.Name,
        source.DeliveryModes
    )
WHEN NOT MATCHED BY SOURCE AND target.ApprenticeshipId = @ApprenticeshipId THEN
    UPDATE SET ApprenticeshipLocationStatus = {(int)ApprenticeshipStatus.Archived};

MERGE Pttcd.ApprenticeshipLocationRegions AS target
USING (SELECT ApprenticeshipLocationId, RegionId FROM @ApprenticeshipLocationSubRegions) AS source
ON target.ApprenticeshipLocationId = source.ApprenticeshipLocationId AND target.RegionId = source.RegionId
WHEN NOT MATCHED THEN INSERT (ApprenticeshipLocationId, RegionId) VALUES (source.ApprenticeshipLocationId, source.RegionId)
WHEN NOT MATCHED BY SOURCE AND target.ApprenticeshipLocationId IN (SELECT TOP 1 ApprenticeshipLocationId FROM @ApprenticeshipLocationSubRegions) THEN DELETE;

SELECT 0 AS Result";

            var paramz = new
            {
                query.ApprenticeshipId,
                query.UpdatedOn,
                UpdatedByUserId = query.UpdatedBy.UserId,
                query.Standard.StandardCode,
                StandardVersion = query.Standard.Version,
                query.MarketingInformation,
                query.ApprenticeshipWebsite,
                query.ContactTelephone,
                query.ContactEmail,
                query.ContactWebsite,
                ApprenticeshipLocations = TvpHelper.CreateApprenticeshipLocationsTable(
                    query.ApprenticeshipLocations.Select(l => (
                                                             l.ApprenticeshipLocationId,
                                                             l.Telephone,
                                                             l.VenueId,
                                                             l.National,
                                                             l.Radius,
                                                             l.ApprenticeshipLocationType,
                                                             l.DeliveryModes
                                                             ))),
                ApprenticeshipLocationSubRegions = TvpHelper.CreateApprenticeshipLocationSubRegionsTable(
                    from al in query.ApprenticeshipLocations
                    from subRegionId in al.SubRegionIds ?? Array.Empty <string>()
                    select(al.ApprenticeshipLocationId, subRegionId))
            };

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

            if (result == Result.Success)
            {
                return(new Success());
            }
            else
            {
                return(new NotFound());
            }
        }
Пример #3
0
        public OneOf <NotFound, Success> Execute(
            InMemoryDocumentStore inMemoryDocumentStore,
            UpdateApprenticeship request)
        {
            var apprenticeship = inMemoryDocumentStore.Apprenticeships.All.SingleOrDefault(p => p.Id == request.Id);

            if (apprenticeship == null)
            {
                return(new NotFound());
            }

            apprenticeship.ProviderUKPRN        = request.ProviderUkprn;
            apprenticeship.ApprenticeshipTitle  = request.ApprenticeshipTitle;
            apprenticeship.ApprenticeshipType   = request.ApprenticeshipType;
            apprenticeship.MarketingInformation = request.MarketingInformation;
            apprenticeship.Url = request.Url;
            apprenticeship.ContactTelephone        = request.ContactTelephone;
            apprenticeship.ContactEmail            = request.ContactEmail;
            apprenticeship.ContactWebsite          = request.ContactWebsite;
            apprenticeship.ApprenticeshipLocations = request.ApprenticeshipLocations.Select(l => new ApprenticeshipLocation()
            {
                Address = l.Address,
                ApprenticeshipLocationType = l.ApprenticeshipLocationType,
                CreatedBy     = request.UpdatedBy.Email,
                CreatedDate   = request.UpdatedDate,
                DeliveryModes = l.DeliveryModes.ToList(),
                Id            = Guid.NewGuid(),
                LocationType  = l.LocationType,
                Name          = l.Name,
                National      = l.National,
                Phone         = l.Phone,
                ProviderUKPRN = request.ProviderUkprn,
                Radius        = l.Radius,
                RecordStatus  = 1,
                Regions       = l.Regions,
                UpdatedBy     = request.UpdatedBy.Email,
                UpdatedDate   = request.UpdatedDate,
                VenueId       = l.VenueId
            }).ToList();
            apprenticeship.RecordStatus = 1;
            apprenticeship.UpdatedDate  = request.UpdatedDate;
            apprenticeship.UpdatedBy    = request.UpdatedBy.Email;

            request.StandardOrFramework.Switch(
                standard =>
            {
                apprenticeship.StandardId         = standard.CosmosId;
                apprenticeship.StandardCode       = standard.StandardCode;
                apprenticeship.Version            = standard.Version;
                apprenticeship.NotionalNVQLevelv2 = standard.NotionalNVQLevelv2;
            },
                framework =>
            {
                apprenticeship.FrameworkId   = framework.CosmosId;
                apprenticeship.FrameworkCode = framework.FrameworkCode;
                apprenticeship.ProgType      = framework.ProgType;
                apprenticeship.PathwayCode   = framework.PathwayCode;
            });

            inMemoryDocumentStore.Apprenticeships.Save(apprenticeship);

            return(new Success());
        }