Ejemplo n.º 1
0
        public async Task <OneOf <NotFound, Success> > Execute(
            DocumentClient client,
            Configuration configuration,
            DeleteCourseRun request)
        {
            var documentUri = UriFactory.CreateDocumentUri(
                configuration.DatabaseId,
                configuration.CoursesCollectionName,
                request.CourseId.ToString());

            var partitionKey = new PartitionKey(request.ProviderUkprn);

            Course course;

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

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

            var courseRun = course.CourseRuns.SingleOrDefault(cr => cr.Id == request.CourseRunId);

            if (courseRun == null ||
                courseRun.RecordStatus == CourseStatus.Archived ||
                courseRun.RecordStatus == CourseStatus.Deleted)
            {
                return(new NotFound());
            }

            courseRun.RecordStatus = CourseStatus.Archived;
            courseRun.UpdatedBy    = request.UpdatedBy;
            courseRun.UpdatedDate  = request.UpdatedDate;

            course.CourseStatus = course.CourseRuns
                                  .Select(cr => cr.RecordStatus)
                                  .Aggregate((CourseStatus)0, (l, r) => l | r);

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

            return(new Success());
        }
        public async Task <OneOf <NotFound, Success> > Execute(SqlTransaction transaction, DeleteCourseRun query)
        {
            var sql = @$ "
DECLARE @Deleted INT

UPDATE Pttcd.CourseRuns SET
    CourseRunStatus = {(int)CourseStatus.Archived},
        public OneOf <NotFound, Success> Execute(InMemoryDocumentStore inMemoryDocumentStore, DeleteCourseRun request)
        {
            var course = inMemoryDocumentStore.Courses.All.SingleOrDefault(c => c.Id == request.CourseId);

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

            var courseRun = course.CourseRuns.SingleOrDefault(cr => cr.Id == request.CourseRunId);

            if (courseRun == null ||
                courseRun.RecordStatus == CourseStatus.Archived ||
                courseRun.RecordStatus == CourseStatus.Deleted)
            {
                return(new NotFound());
            }

            courseRun.RecordStatus = CourseStatus.Archived;

            course.CourseStatus = course.CourseRuns
                                  .Select(cr => cr.RecordStatus)
                                  .Aggregate((CourseStatus)0, (l, r) => l | r);

            inMemoryDocumentStore.Courses.Save(course);

            return(new Success());
        }