/// <summary>
        /// Updates or creates a resource based on the resource identifier. The PUT operation is used to update or create a resource by identifier.  If the resource doesn't exist, the resource will be created using that identifier.  Additionally, natural key values cannot be changed using this operation, and will not be modified in the database.  If the resource &quot;id&quot; is provided in the JSON body, it will be ignored as well.
        /// </summary>
        /// <param name="id">A resource identifier specifying the resource to be updated.</param>
        /// <param name="IfMatch">The ETag header value used to prevent the PUT from updating a resource modified by another consumer.</param>
        /// <param name="body">The JSON representation of the &quot;studentProgramAssociation&quot; resource to be updated.</param>
        /// <returns>A RestSharp <see cref="IRestResponse"/> instance containing the API response details.</returns>
        public IRestResponse PutStudentProgramAssociation(string id, string IfMatch, StudentProgramAssociation body)
        {
            var request = new RestRequest("/studentProgramAssociations/{id}", Method.PUT);

            request.RequestFormat = DataFormat.Json;

            request.AddUrlSegment("id", id);
            // verify required params are set
            if (id == null || body == null)
            {
                throw new ArgumentException("API method call is missing required parameters");
            }
            request.AddHeader("If-Match", IfMatch);
            request.AddBody(body);
            request.Parameters.First(param => param.Type == ParameterType.RequestBody).Name = "application/json";
            var response = client.Execute(request);

            var location = response.Headers.FirstOrDefault(x => x.Name == "Location");

            if (location != null && !string.IsNullOrWhiteSpace(location.Value.ToString()))
            {
                body.id = location.Value.ToString().Split('/').Last();
            }
            return(response);
        }
Example #2
0
        /// <summary>
        /// Creates or updates resources based on the natural key values of the supplied resource. The POST operation can be used to create or update resources. In database terms, this is often referred to as an &quot;upsert&quot; operation (insert + update).  Clients should NOT include the resource &quot;id&quot; in the JSON body because it will result in an error (you must use a PUT operation to update a resource by &quot;id&quot;). The web service will identify whether the resource already exists based on the natural key values provided, and update or create the resource appropriately.
        /// </summary>
        /// <param name="body">The JSON representation of the &quot;studentProgramAssociation&quot; resource to be created or updated.</param>
        /// <returns>A RestSharp <see cref="IRestResponse"/> instance containing the API response details.</returns>
        public IRestResponse PostStudentProgramAssociations(StudentProgramAssociation body)
        {
            var request = new RestRequest("/studentProgramAssociations", Method.POST);

            request.RequestFormat = DataFormat.Json;

            // verify required params are set
            if (body == null)
            {
                throw new ArgumentException("API method call is missing required parameters");
            }
            request.AddBody(body);
            var response = client.Execute(request);

            var location = response.Headers.FirstOrDefault(x => x.Name == "Location");

            if (location != null && !string.IsNullOrWhiteSpace(location.Value.ToString()))
            {
                body.id = location.Value.ToString().Split('/').Last();
            }
            return(response);
        }
            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());
                }
            }