protected void InitializeTestData()
            {
                CreateTestServiceDescriptor();

                _studentRepo.UpsertAsync(_student, false, CancellationToken.None).WaitSafely();

                _programRepo.UpsertAsync(_program1, false, CancellationToken.None).WaitSafely();

                _programRepo.UpsertAsync(_program2, false, CancellationToken.None).WaitSafely();
            }
Пример #2
0
        public async Task ExecuteAsync(TContext context, TResult result, CancellationToken cancellationToken)
        {
            try
            {
                var updatedEntityResult = await _upsertEntity.UpsertAsync(
                    context.PersistentModel,
                    context.EnforceOptimisticLock, cancellationToken);

                context.PersistentModel = updatedEntityResult.Entity;

                // Set the resulting resource's identifier
                if (result.ResourceId == null)
                {
                    result.ResourceId = updatedEntityResult.Entity.Id;
                }

                result.ResourceWasCreated   = updatedEntityResult.IsCreated;
                result.ResourceWasUpdated   = updatedEntityResult.IsModified;
                result.ResourceWasPersisted = true;

                // Set the etag value
                result.ETag = _etagProvider.GetETag(updatedEntityResult.Entity);
            }

            catch (Exception ex)
            {
                result.Exception = ex;
            }
        }
 private void CreateTestServiceDescriptor()
 {
     _upsertService.UpsertAsync(_serviceDescriptor, false, CancellationToken.None)
     .WaitSafely();
 }
            protected override void Act()
            {
                // ----------------------------------------------------------------------
                // Create a new StudentProgramAssociation with a service
                // ----------------------------------------------------------------------
                var studentProgramAssociation = new StudentProgramAssociation
                {
                    StudentUniqueId                = _student.StudentUniqueId,
                    ProgramTypeDescriptor          = _program1.ProgramTypeDescriptor,
                    ProgramName                    = _program1.ProgramName,
                    ProgramEducationOrganizationId = _educationOrganization1,
                    BeginDate = DateTime.Today.AddDays(-60),
                    EducationOrganizationId = _educationOrganization1
                };

                var service = new StudentProgramAssociationService
                {
                    ServiceDescriptor = EdFiDescriptorReferenceSpecification.GetFullyQualifiedDescriptorReference(
                        _serviceDescriptor.Namespace,
                        _serviceDescriptor.CodeValue),
                    PrimaryIndicator          = true,
                    StudentProgramAssociation = studentProgramAssociation
                };

                studentProgramAssociation.StudentProgramAssociationServices.Add(service);

                var studentProgramAssociationUpsertResult = _studentProgramAssociationRepo.UpsertAsync(
                    studentProgramAssociation, false, CancellationToken.None)
                                                            .GetResultSafely();

                _actualStudentProgramAssociationUpsert1Results = new UpsertResults
                {
                    IsCreated  = studentProgramAssociationUpsertResult.IsCreated,
                    IsModified = studentProgramAssociationUpsertResult.IsModified
                };

                // ----------------------------------------------------------------------
                // Update the StudentProgramAssociation, removing the service
                // ----------------------------------------------------------------------
                var studentProgramAssociation2 = new StudentProgramAssociation
                {
                    StudentUniqueId                = _student.StudentUniqueId,
                    ProgramTypeDescriptor          = _program1.ProgramTypeDescriptor,
                    ProgramName                    = _program1.ProgramName,
                    ProgramEducationOrganizationId = _educationOrganization1,
                    BeginDate = DateTime.Today.AddDays(-60),
                    ReasonExitedDescriptor  = KnownDescriptors.ReasonExited.MovedOutOfState,
                    EducationOrganizationId = _educationOrganization1
                };

                studentProgramAssociationUpsertResult = _studentProgramAssociationRepo.UpsertAsync(
                    studentProgramAssociation2, false, CancellationToken.None)
                                                        .GetResultSafely();

                _actualStudentProgramAssociationUpsert2Results = new UpsertResults
                {
                    IsCreated  = studentProgramAssociationUpsertResult.IsCreated,
                    IsModified = studentProgramAssociationUpsertResult.IsModified
                };

                // Verify the service got removed
                using (var conn = GetSqlConnectionForOds())
                {
                    conn.Open();

                    var cmd = new SqlCommand(
                        $"SELECT COUNT(*) FROM edfi.StudentProgramAssociationService WHERE ProgramName = '{_program1.ProgramName}'",
                        conn);

                    _actualServiceDeletedWithSecondUpsert = 0 == Convert.ToInt32(cmd.ExecuteScalar());
                }

                // -------------------------------------------------------------------------------------------------------------
                // Create a new (derived class) StudentTitleIPartAProgramAssociation with a service hanging off the base class
                // -------------------------------------------------------------------------------------------------------------
                var studentTitleIPartA = new StudentTitleIPartAProgramAssociation
                {
                    // PK
                    StudentUniqueId                = _student.StudentUniqueId,
                    ProgramTypeDescriptor          = _program2.ProgramTypeDescriptor,
                    ProgramName                    = _program2.ProgramName,
                    ProgramEducationOrganizationId = _educationOrganization1,
                    BeginDate = DateTime.Today.AddDays(-60),
                    EducationOrganizationId = _educationOrganization2,

                    // Base class property
                    ReasonExitedDescriptor = KnownDescriptors.ReasonExited.MovedOutOfState,

                    // Derived class property
                    TitleIPartAParticipantDescriptor = KnownDescriptors.TitleIPartAParticipant.LocalNeglectedProgram
                };

                // Add a service to the base class
                var titleIService = new StudentTitleIPartAProgramAssociationService
                {
                    ServiceDescriptor = EdFiDescriptorReferenceSpecification.GetFullyQualifiedDescriptorReference(
                        _serviceDescriptor.Namespace,
                        _serviceDescriptor.CodeValue),
                    PrimaryIndicator = true,
                    StudentTitleIPartAProgramAssociation = studentTitleIPartA
                };

                studentTitleIPartA.StudentTitleIPartAProgramAssociationServices.Add(titleIService);

                var studentTitleIPartAProgramAssociationUpsertResult = _studentTitleIPartAProgramAssociationRepo.UpsertAsync(
                    studentTitleIPartA, false, CancellationToken.None)
                                                                       .GetResultSafely();

                _actualStudentTitleIPartAUpsert1Results = new UpsertResults
                {
                    IsCreated  = studentTitleIPartAProgramAssociationUpsertResult.IsCreated,
                    IsModified = studentTitleIPartAProgramAssociationUpsertResult.IsModified
                };

                // -------------------------------------------------------------------------------------------------------------
                // Create a new (derived class) StudentTitleIPartAProgramAssociation with a service hanging off the base class
                // -------------------------------------------------------------------------------------------------------------
                var studentTitleIPartA2 = new StudentTitleIPartAProgramAssociation
                {
                    // PK
                    StudentUniqueId                = _student.StudentUniqueId,
                    ProgramTypeDescriptor          = _program2.ProgramTypeDescriptor,
                    ProgramName                    = _program2.ProgramName,
                    ProgramEducationOrganizationId = _educationOrganization1,
                    BeginDate = DateTime.Today.AddDays(-60),
                    EducationOrganizationId = _educationOrganization2,

                    // Base class property
                    ReasonExitedDescriptor = KnownDescriptors.ReasonExited.GraduatedWithAHighSchoolDiploma,

                    // Derived class property
                    TitleIPartAParticipantDescriptor = KnownDescriptors.TitleIPartAParticipant.PublicSchoolwideProgram
                };

                // Updating derived record, removing the service
                studentTitleIPartAProgramAssociationUpsertResult = _studentTitleIPartAProgramAssociationRepo.UpsertAsync(
                    studentTitleIPartA2, false, CancellationToken.None)
                                                                   .GetResultSafely();

                _actualStudentTitleIPartAUpsert2Results = new UpsertResults
                {
                    IsCreated  = studentTitleIPartAProgramAssociationUpsertResult.IsCreated,
                    IsModified = studentTitleIPartAProgramAssociationUpsertResult.IsModified
                };

                // Verify the service got removed
                using (var conn = GetSqlConnectionForOds())
                {
                    conn.Open();

                    var cmd = new SqlCommand(
                        $"SELECT COUNT(*) FROM edfi.StudentProgramAssociationService WHERE ProgramName = '{_program2.ProgramName}'",
                        conn);

                    _actualServiceDeletedWithSecondDerivedClassUpsert = 0 == Convert.ToInt32(cmd.ExecuteScalar());
                }

                // Clean up the data now
                // Delete the concrete base class (StudentProgramAssociation)
                try
                {
                    var deleteStudentProgramAssociationByKey =
                        _container.Resolve <IDeleteEntityByKey <StudentProgramAssociation> >();

                    deleteStudentProgramAssociationByKey.DeleteByKeyAsync(studentProgramAssociation, null, CancellationToken.None)
                    .WaitSafely();
                }
                catch (Exception ex)
                {
                    _actualDeleteStudentProgramAssociationException = ex;
                }

                // Verify the program association got removed
                using (var conn = GetSqlConnectionForOds())
                {
                    conn.Open();

                    var cmd = new SqlCommand(
                        $"SELECT COUNT(*) FROM edfi.StudentProgramAssociationService WHERE ProgramName = '{_program1.ProgramName}'",
                        conn);

                    _actualProgramAssociationDeleted = 0 == Convert.ToInt32(cmd.ExecuteScalar());
                }

                // Delete the derived class (StudentProgramAssociation)
                try
                {
                    var deletestudentTitleIPartAAssociationById =
                        _container.Resolve <IDeleteEntityById <StudentTitleIPartAProgramAssociation> >();

                    deletestudentTitleIPartAAssociationById.DeleteByIdAsync(
                        studentTitleIPartA.Id, null, CancellationToken.None)
                    .WaitSafely();
                }
                catch (Exception ex)
                {
                    _actualDeleteStudentTitleIPartAAssociationException = ex;
                }

                // Verify the TitleI program association got removed
                using (var conn = GetSqlConnectionForOds())
                {
                    conn.Open();

                    var cmd = new SqlCommand(
                        $"SELECT COUNT(*) FROM edfi.StudentProgramAssociationService WHERE ProgramName = '{_program2.ProgramName}'",
                        conn);

                    _actualTitleIProgramAssociationDeleted = 0 == Convert.ToInt32(cmd.ExecuteScalar());
                }
            }
 /// <summary>
 /// Authorizes a call to update an entity.
 /// </summary>
 /// <returns>The specified entity if found; otherwise null.</returns>
 public async Task <UpsertEntityResult <T> > UpsertAsync(T entity, bool enforceOptimisticLock, CancellationToken cancellationToken)
 {
     // We do not need to perform authorization because the UpsertEntity will call other
     // methods (Create or Update) which will trigger the authorization.
     return(await _next.UpsertAsync(entity, enforceOptimisticLock, cancellationToken));
 }
Пример #6
0
 public async Task <UpsertEntityResult <TEntity> > UpsertAsync(TEntity entity, bool enforceOptimisticLock,
                                                               CancellationToken cancellationToken)
 => await _upsertEntity.UpsertAsync(entity, enforceOptimisticLock, cancellationToken);