コード例 #1
0
        public async Task Basic_Post_And_Delete_Offering()
        {
            var departmentId = "0001";
            var courseId     = "001";
            var termId       = "000";

            SetupEntities(departmentId, courseId, termId);

            var offering = new Offering
            {
                SectionName = "A",
                TermId      = termId
            };

            var offeringDTO = new NewOfferingDTO
            {
                Offering = offering,
                CourseId = courseId
            };

            var postResult = await _controller.PostNewOffering(offeringDTO);

            Assert.IsType <CreatedAtActionResult>(postResult.Result);

            var deleteResult = await _controller.DeleteOffering(offering.Id);

            Assert.Equal(offering, deleteResult.Value);

            var getResult = await _controller.GetOffering(offering.Id);

            Assert.Equal(Status.Deleted, getResult.Value.Offering.IsDeletedStatus);
        }
コード例 #2
0
        public async Task Get_Offerings_By_Student()
        {
            var departmentId = "0001";
            var courseId     = "001";
            var termId       = "000";

            SetupEntities(departmentId, courseId, termId);

            var offerings = new List <Offering> {
                new Offering
                {
                    SectionName = "A",
                    TermId      = termId
                },
                new Offering
                {
                    SectionName = "B",
                    TermId      = termId
                },
                new Offering
                {
                    SectionName = "C",
                    TermId      = termId
                }
            };

            foreach (var offering in offerings)
            {
                var offeringDTO = new NewOfferingDTO
                {
                    Offering = offering,
                    CourseId = courseId
                };

                var postResult = await _controller.PostNewOffering(offeringDTO);

                Assert.IsType <CreatedAtActionResult>(postResult.Result);
            }

            var playlist = new Playlist {
                OfferingId = offerings[1].Id
            };

            _context.Playlists.Add(playlist);
            _context.Medias.Add(new Media {
                PlaylistId = playlist.Id
            });

            var getResult = await _controller.GetOfferingsByStudent();

            List <OfferingDTO> offeringsByStudent = getResult.Value.ToList();

            Assert.Single(offeringsByStudent);
            AssertOfferingDTO(offeringsByStudent[0], offerings[1], courseId, departmentId);
        }
コード例 #3
0
        public async Task Post_Invalid_and_Valid_Offerings()
        {
            var departmentId = "0001";
            var courseId     = "001";
            var termId       = "000";

            SetupEntities(departmentId, courseId, termId);

            var offering = new Offering
            {
                SectionName = "A",
                TermId      = termId
            };

            var offeringDTO = new NewOfferingDTO
            {
                Offering = offering
            };

            var postResult = await _controller.PostNewOffering(offeringDTO);

            Assert.IsType <BadRequestObjectResult>(postResult.Result);

            offeringDTO.DepartmentId = "invalid-ID";

            postResult = await _controller.PostNewOffering(offeringDTO);

            Assert.IsType <BadRequestObjectResult>(postResult.Result);

            offeringDTO.NewCourseNumber = "002";

            postResult = await _controller.PostNewOffering(offeringDTO);

            Assert.IsType <BadRequestObjectResult>(postResult.Result);

            offeringDTO.DepartmentId = departmentId;

            postResult = await _controller.PostNewOffering(offeringDTO);

            Assert.IsType <CreatedAtActionResult>(postResult.Result);
        }
コード例 #4
0
        public async Task Basic_Post_Put_Offering()
        {
            var departmentId = "0001";
            var courseId     = "001";
            var termId       = "000";

            SetupEntities(departmentId, courseId, termId);

            var offering = new Offering
            {
                Id          = "offering",
                SectionName = "A",
                TermId      = termId
            };

            var offeringDTO = new NewOfferingDTO
            {
                Offering = offering,
                CourseId = courseId
            };

            var postResult = await _controller.PostNewOffering(offeringDTO);

            Assert.IsType <CreatedAtActionResult>(postResult.Result);

            offering.SectionName = "B";

            var putResult = await _controller.PutOffering(offering.Id, offering);

            Assert.IsType <NoContentResult>(putResult);

            var getResult = await _controller.GetOffering(offering.Id);

            AssertOfferingDTO(getResult.Value, offering, courseId, departmentId);

            var newOffering = _context.Offerings.Find(offering.Id);

            Assert.Equal(PublishStatus.Published, newOffering.PublishStatus);
            Assert.Equal(Visibility.Visible, newOffering.Visibility);
        }
コード例 #5
0
        public async Task Put_Json_Metadata()
        {
            var departmentId = "0001";
            var courseId     = "001";
            var termId       = "000";

            SetupEntities(departmentId, courseId, termId);

            var offering = new Offering
            {
                SectionName = "A",
                TermId      = termId
            };

            var offeringDTO = new NewOfferingDTO
            {
                Offering = offering,
                CourseId = courseId
            };

            var postResult = await _controller.PostNewOffering(offeringDTO);

            Assert.IsType <CreatedAtActionResult>(postResult.Result);

            var metadata = new JObject
            {
                { "example", "Hello world!" }
            };

            var putResult = await _controller.PutOffering(offering.Id, metadata);

            Assert.IsType <NoContentResult>(putResult);

            var getResult = await _controller.GetOffering(offering.Id);

            Assert.Equal(metadata, getResult.Value.Offering.JsonMetadata);
            AssertOfferingDTO(getResult.Value, offering, courseId, departmentId);
        }
コード例 #6
0
        public async Task <ActionResult <Offering> > PostNewOffering(NewOfferingDTO newOfferingDTO)
        {
            if (newOfferingDTO == null)
            {
                return(BadRequest());
            }

            if (newOfferingDTO.CourseId == null)
            {
                if (newOfferingDTO.DepartmentId == null || newOfferingDTO.NewCourseNumber == null)
                {
                    return(BadRequest("Must specify departmentId and newCourseNumber"));
                }

                var isValidDept = await _context.Departments.AnyAsync(d => d.Id == newOfferingDTO.DepartmentId);

                if (!isValidDept)
                {
                    return(BadRequest("Invalid department ID"));
                }

                var course = await _context.Courses.Where(c => c.CourseNumber == newOfferingDTO.NewCourseNumber && c.DepartmentId == newOfferingDTO.DepartmentId).FirstOrDefaultAsync();

                if (course == null)
                {
                    course = new Course
                    {
                        DepartmentId = newOfferingDTO.DepartmentId,
                        CourseNumber = newOfferingDTO.NewCourseNumber
                    };

                    await _context.Courses.AddAsync(course);

                    await _context.SaveChangesAsync();
                }

                newOfferingDTO.CourseId = course.Id;
            }

            _context.Offerings.Add(newOfferingDTO.Offering);
            await _context.SaveChangesAsync();

            _context.CourseOfferings.Add(new CourseOffering
            {
                CourseId   = newOfferingDTO.CourseId,
                OfferingId = newOfferingDTO.Offering.Id
            });

            var user = await _userUtils.GetUser(User);

            if (user != null)
            {
                await _context.UserOfferings.AddAsync(new UserOffering
                {
                    ApplicationUserId = user.Id,
                    IdentityRole      = _context.Roles.Where(r => r.Name == Globals.ROLE_INSTRUCTOR).FirstOrDefault(),
                    OfferingId        = newOfferingDTO.Offering.Id
                });

                await _context.SaveChangesAsync();
            }

            return(CreatedAtAction("GetOffering", new { id = newOfferingDTO.Offering.Id }, newOfferingDTO.Offering));
        }
コード例 #7
0
        public async Task Get_Offerings_By_Instructor()
        {
            var departmentId = "0001";
            var courseId     = "001";
            var termId       = "000";

            SetupEntities(departmentId, courseId, termId);

            var offerings = new List <Offering> {
                new Offering
                {
                    SectionName = "A",
                    TermId      = termId
                },
                new Offering
                {
                    SectionName = "B",
                    TermId      = termId
                },
                new Offering
                {
                    SectionName = "C",
                    TermId      = termId
                }
            };

            foreach (var offering in offerings)
            {
                var offeringDTO = new NewOfferingDTO
                {
                    Offering = offering,
                    CourseId = courseId
                };

                var postResult = await _controller.PostNewOffering(offeringDTO);

                Assert.IsType <CreatedAtActionResult>(postResult.Result);
            }

            // This offering shouldn't be returned because it was made by a different user
            var ignoredOfferingId = "shouldIgnore";

            _context.Offerings.Add(new Offering
            {
                Id          = ignoredOfferingId,
                SectionName = "D",
                TermId      = termId
            });
            _context.CourseOfferings.Add(new CourseOffering
            {
                CourseId   = courseId,
                OfferingId = ignoredOfferingId
            });
            _context.UserOfferings.Add(new UserOffering
            {
                ApplicationUserId = "otherUser",
                IdentityRole      = _context.Roles.Where(r => r.Name == Globals.ROLE_INSTRUCTOR).FirstOrDefault(),
                OfferingId        = ignoredOfferingId
            });
            _context.SaveChanges();

            var getResult = await _controller.GetOfferingsByInstructor(TestGlobals.TEST_USER_ID);

            List <OfferingDTO> offeringsByInstructor = getResult.Value.ToList();

            Assert.Equal(3, offeringsByInstructor.Count());

            for (int i = 0; i < offerings.Count(); i++)
            {
                // The returned offerings should be in opposite order because they are
                // returned in descending order based on when they were created
                var reverseIdx = offerings.Count() - i - 1;
                AssertOfferingDTO(offeringsByInstructor[reverseIdx], offerings[i], courseId, departmentId);
            }
        }