private Command AddCourse(string textCommandLine)
        {
            Regex regex   = new Regex("\"(.*?)\"");
            var   matches = regex.Matches(textCommandLine);
            var   args    = new string[matches.Count];

            for (int i = 0; i < args.Length; i++)
            {
                args[i] = matches[i].Value.Replace("\"", "");
            }

            var course = new Course
            {
                Name           = args[0],
                Section        = args[1],
                Room           = args[2],
                PrimaryTeacher = new Teacher {
                    Email = args[3]
                }
            };

            return(new Command(() =>
            {
                var createCourseCommand = new CreateCourseCommand(_courseService, course);
                createCourseCommand.Execute();
                _course = createCourseCommand.Result;
                return _course;
            }, CommandType.AddCourse));
        }
        public bool UpdateCourse(int id, [FromBody] CreateCourseCommand request)
        {
            CourseContext       context = HttpContext.RequestServices.GetService(typeof(CourseContext)) as CourseContext;
            UpdateCourseHandler handler = new UpdateCourseHandler(context);

            return(handler.Handle(id, request));
        }
Пример #3
0
 public CreateCourseService(CreateCourseCommand courseCommand, ICourseRepository courseRepository)
     : base(courseCommand)
 {
     _courseCommand    = courseCommand;
     _courseRepository = courseRepository;
     Run();
 }
Пример #4
0
        public bool Handle(int courseId, CreateCourseCommand request)
        {
            var model = request.Adapt <Model.MedicalCourse>();

            using (MySqlConnection conn = _context.GetConnection())
            {
                conn.Open();

                string query = string.Format("update courses set course_name='{1}', qualification='{2}' where course_id={0}",
                                             courseId.ToString(),
                                             model.Name,
                                             model.Qualification);

                MySqlCommand cmd = new MySqlCommand(query, conn);

                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    string s = ex.Message;
                    return(false);
                }
                finally
                {
                    conn.CloseAsync();
                }
            }

            return(true);
        }
Пример #5
0
        public async Task <IActionResult> Create(CourseDto courseDto)
        {
            var createCourseCommand = new CreateCourseCommand(courseDto);
            var response            = await _mediator.Send(createCourseCommand);

            return(Ok(response));
        }
Пример #6
0
        public bool Handle(CreateCourseCommand request)
        {
            var model = request.Adapt <MedicalCourse>();

            using (MySqlConnection conn = _context.GetConnection())
            {
                conn.Open();
                string query = string.Format("insert into Courses(course_name, qualification) values('{0}', '{1}')",
                                             model.Name, model.Qualification);
                MySqlCommand cmd = new MySqlCommand(query, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch
                {
                    return(false);
                }
                finally
                {
                    conn.CloseAsync();
                }
            }

            return(true);
        }
Пример #7
0
        public void TitleIsValid_ShouldNotHaveError()
        {
            _command = new CreateCourseCommand {
                Title = "course name"
            };

            _sut.ShouldNotHaveValidationErrorFor(x => x.Title, _command);
        }
Пример #8
0
        public async Task WhenICreateACourseWithAnEmptyOrANull(string title)
        {
            _command = new CreateCourseCommand {
                Title = title
            };

            _response = await _client.PostAsync(BaseUrl, Utilities.GetRequestContent(_command));
        }
Пример #9
0
 public ActionResult <CourseDto> Create([FromBody] CreateCourseCommand command)
 {
     return(createCourseCommandHandler.Handle(command)
            .OnBoth((result) => responseHandler.GetCreatedResponse(result, "GetCourse", new
     {
         courseId = result.Value?.Id
     })));
 }
        public async Task <IActionResult> CreateCourse(
            [FromBody] CreateCourseCommand command,
            [FromServices] IMediator mediator)
        {
            var result = await mediator.Send(command);

            return(StatusCode(200, result));
        }
Пример #11
0
        public async Task WhenICreateACourseWithATitleLengthOverCharacters(int maximumTitleLength)
        {
            _command = new CreateCourseCommand {
                Title = new string('*', maximumTitleLength + 1)
            };

            _response = await _client.PostAsync(BaseUrl, Utilities.GetRequestContent(_command));
        }
Пример #12
0
        public async Task WhenICreateACourseWithAValidTitle()
        {
            _command = new CreateCourseCommand {
                Title = "new course"
            };

            _response = await _client.PostAsync(BaseUrl, Utilities.GetRequestContent(_command));
        }
Пример #13
0
        public void NullOrEmptyTitle_ShouldHaveError(string title)
        {
            _command = new CreateCourseCommand {
                Title = title
            };

            _sut.ShouldHaveValidationErrorFor(x => x.Title, _command);
        }
Пример #14
0
        public void TitleLengthIsOver60_ShouldHaveError()
        {
            _command = new CreateCourseCommand {
                Title = new string('*', 61)
            };

            _sut.ShouldHaveValidationErrorFor(x => x.Title, _command);
        }
Пример #15
0
        public async Task Should_update_grade()
        {
            // create course
            var createCourseCommand = new CreateCourseCommand()
            {
                Id          = Guid.NewGuid(),
                Code        = 1,
                Name        = "Course 1",
                Description = "Test"
            };

            var createCourseCommandHandler = new CreateCourseCommandHandler(this.autoMapper, this.context);

            var courseResult = await createCourseCommandHandler.Handle(createCourseCommand, CancellationToken.None);

            // Create student
            var createStudentCommand = new CreateStudentCommand()
            {
                Id          = Guid.NewGuid(),
                FirstName   = "Milos",
                LastName    = "Stojkovic",
                Address     = "Bata Noleta 31",
                City        = "Sokobanja",
                DateOfBirth = new DateTime(1991, 3, 18),
                State       = "Srbija",
                Gender      = 0
            };

            var createStudentCommandHandler = new CreateStudentCommandHandler(this.autoMapper, this.context);

            var studentResult = await createStudentCommandHandler.Handle(createStudentCommand, CancellationToken.None);

            var createOrUpdateEnrollmentCommand = new CreateOrUpdateEnrollmentCommand()
            {
                Id         = createStudentCommand.Id,
                CourseCode = createCourseCommand.Code,
                Grade      = (int)Domain.Enumerations.Grade.Seven
            };

            var createOrUpdateEnrollmentCommandHandler = new CreateOrUpdateEnrollmentCommandHandler(this.context);
            var result = await createOrUpdateEnrollmentCommandHandler.Handle(createOrUpdateEnrollmentCommand, CancellationToken.None);

            result.ShouldBe(true);

            var enrollment = await this.context.Enrollment
                             .FirstOrDefaultAsync(e => e.Course.Code == createOrUpdateEnrollmentCommand.CourseCode && e.Student.Id == createOrUpdateEnrollmentCommand.Id, CancellationToken.None);

            enrollment.Grade.ShouldBe(Domain.Enumerations.Grade.Seven);

            createOrUpdateEnrollmentCommand.Grade = (int)Domain.Enumerations.Grade.Eight;
            var updateResult = await createOrUpdateEnrollmentCommandHandler.Handle(createOrUpdateEnrollmentCommand, CancellationToken.None);

            updateResult.ShouldBe(true);
            var updatedEnrollment = await this.context.Enrollment
                                    .FirstOrDefaultAsync(e => e.Course.Code == createOrUpdateEnrollmentCommand.CourseCode && e.Student.Id == createOrUpdateEnrollmentCommand.Id, CancellationToken.None);

            updatedEnrollment.Grade.ShouldBe(Domain.Enumerations.Grade.Eight);
        }
        public void Create(CourseViewModel courseViewModel)
        {
            var createCourseCommand = new CreateCourseCommand(
                courseViewModel.Name,
                courseViewModel.Description,
                courseViewModel.ImageUrl);

            _bus.SendCommand(createCourseCommand);
        }
        public async Task <IActionResult> Index(CourseViewModel course)
        {
            var courseCommand = new CreateCourseCommand(course.Name);

            await _mediator.Publish(courseCommand);


            return(RedirectToAction("Index"));
        }
        public async Task Should_throw_delete_failure_exception()
        {
            // create course
            var createCourseCommand = new CreateCourseCommand()
            {
                Id          = Guid.NewGuid(),
                Code        = 1,
                Name        = "Course 1",
                Description = "Test"
            };

            var createCourseCommandHandler = new CreateCourseCommandHandler(this.autoMapper, this.context);
            var commandResult = await createCourseCommandHandler.Handle(createCourseCommand, CancellationToken.None);

            commandResult.ShouldBe(true);

            // Create student
            var createStudentCommand = new CreateStudentCommand()
            {
                Id          = Guid.NewGuid(),
                FirstName   = "Milos",
                LastName    = "Stojkovic",
                Address     = "Bata Noleta 31",
                City        = "Sokobanja",
                DateOfBirth = new DateTime(1991, 3, 18),
                State       = "Srbija",
                Gender      = 0
            };

            var createStudentCommandHandler = new CreateStudentCommandHandler(this.autoMapper, this.context);

            commandResult = await createStudentCommandHandler.Handle(createStudentCommand, CancellationToken.None);

            commandResult.ShouldBe(true);

            var createOrUpdateEnrollmentCommand = new CreateOrUpdateEnrollmentCommand()
            {
                Id         = createStudentCommand.Id,
                CourseCode = createCourseCommand.Code,
                Grade      = (int)Domain.Enumerations.Grade.Seven
            };

            var createOrUpdateEnrollmentCommandHandler = new CreateOrUpdateEnrollmentCommandHandler(this.context);

            commandResult = await createOrUpdateEnrollmentCommandHandler.Handle(createOrUpdateEnrollmentCommand, CancellationToken.None);

            commandResult.ShouldBe(true);

            var deleteStudentCommand = new DeleteStudentCommand()
            {
                Id = createStudentCommand.Id
            };

            var deleteStudentCommandHandler = new DeleteStudentCommandHandler(this.context);
            await Assert.ThrowsAsync <DeleteFailureException>(() => deleteStudentCommandHandler.Handle(deleteStudentCommand, CancellationToken.None));
        }
        public async Task <IActionResult> Create([FromBody] CreateCourseCommand command)
        {
            var result = await Mediator.Send(command);

            var baseUrl     = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}";
            var locationUri =
                $"{baseUrl}/{ApiRoutesV1.Courses.GetById.Replace("{courseId}", result.Data.Id.ToString())}";

            return(Created(locationUri, result));
        }
Пример #20
0
 public async Task EditCourse(CourseFullInfoVM course)
 {
     var newCourse = new CreateCourseCommand
     {
         Title       = course.Title,
         ImgSrc      = course.ImgSrc,
         Description = course.Description,
         Difficulty  = course.Difficulty
     };
     await _httpClient.PostAsJsonAsync("https://localhost:7001/api/courses", newCourse);
 }
Пример #21
0
        public async Task MimoUserCannotCreateCourse()
        {
            var course = new CreateCourseCommand
            {
                Description = "test",
                Name        = "create"
            };
            var response = await httpClient.PostAsync("api/courses", BuildJsonContent(course));

            Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
        }
        public void ShouldReturnValidWhenCreateCommandValid()
        {
            var command = new CreateCourseCommand();

            command.Description = "Computer Science";
            command.IdCollege   = Guid.NewGuid();
            var handler = new CourseHandler(new CourseRepositoryMock());
            var result  = handler.Handle(command);

            Assert.AreEqual(true, result.Status);
        }
        public void ShouldReturnInvalidWhenCreateCommandNull()
        {
            var command = new CreateCourseCommand();

            command.Description = null;
            command.IdCollege   = Guid.NewGuid();
            var handler = new CourseHandler(new CourseRepositoryMock());
            var result  = handler.Handle(command);

            Assert.AreEqual(false, result.Status);
        }
Пример #24
0
        public async Task Create(CourseDto courses)
        {
            var createCourseCommand = new CreateCourseCommand
            {
                Name        = courses.Name,
                Description = courses.Description,
                ImageUrl    = courses.ImageUrl
            };

            await _bus.Send(createCourseCommand);
        }
        public void CreateCourseCommand_WhenAParametersAreCorrect()
        {
            //Arrange
            var factoryMock  = new Mock <IAcademyFactory>();
            var databaseMock = new Mock <IDatabase>();

            //Act
            var createCourseCommmand = new CreateCourseCommand(factoryMock.Object, databaseMock.Object);

            //Assert
            Assert.IsNotNull(createCourseCommmand);
        }
Пример #26
0
        public void ReturnInstance_WhenArgumentsAreValid()
        {
            // Arrange
            var databaseMock = new Mock <IDatabase>();
            var factoryMock  = new Mock <IAcademyFactory>();

            // Act
            var createCourseCommmand = new CreateCourseCommand(factoryMock.Object, databaseMock.Object);

            // Assert
            Assert.IsNotNull(createCourseCommmand);
        }
        public void UpdateCourseTest()
        {
            // arrange
            List <MedicalCourse> list = new List <MedicalCourse>();
            int id = 2;

            MedicalCourse expected = new MedicalCourse
            {
                Id            = id,
                Name          = "Ёндокринологи¤",
                Qualification = 2
            };

            CreateCourseCommand command = new CreateCourseCommand
            {
                Name          = "Ёндокринологи¤",
                Qualification = 2
            };

            //act
            Course.Data.CourseContext courseContext       = new Course.Data.CourseContext(connString);
            UpdateCourseHandler       createCourseHandler = new UpdateCourseHandler(courseContext);

            createCourseHandler.Handle(2, command);


            using (conn = new MySqlConnection(connString))
            {
                conn.Open();
                string query = string.Format("select * from Courses where course_name='Ёндокринологи¤' and qualification=2");

                MySqlCommand cmd = new MySqlCommand(query, conn);

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(new MedicalCourse()
                        {
                            Id            = Convert.ToInt32(reader["course_id"]),
                            Name          = reader["course_name"].ToString(),
                            Qualification = Convert.ToInt32(reader["qualification"])
                        });
                    }
                }
            }

            if (list[0].Name == expected.Name && list[0].Qualification == expected.Qualification && list[0].Id == id)
            {
                Assert.IsTrue(true);
            }
        }
Пример #28
0
        public async Task Handle_WhenCourseAlreadyExists_ReturnCourseAlreadyExists()
        {
            var command = new CreateCourseCommand()
            {
                Name = "Physics"
            };

            CourseRepository.Setup(ur => ur.CourseExistsAsync(command.Name)).Returns(Task.FromResult(true));

            var result = await CreateCourseCommandHandler.Handle(command, CancellationToken.None);

            result.Status.Should().Be(CommandResultStatus.DuplicatedEntity);
        }
Пример #29
0
 public void Create(CreateCourseCommand command)
 {
     _db.Connection().Execute(
         "spCreateCourse",
         new
     {
         id          = command.Id,
         description = command.Description,
         idCollege   = command.IdCollege
     },
         commandType: CommandType.StoredProcedure
         );
 }
        public void SetUp()
        {
            _service    = new Mock <ICreateCourseService>();
            _unitOfWork = new Mock <IUnitOfWork>();
            _command    = new CreateCourseCommand {
                Title = "course title"
            };

            _sut = new CreateCourseCommandHandler(_service.Object, _unitOfWork.Object);

            _createdCourse = new Course(_command.Title, "creatorId", DateTime.Now);
            _service.Setup(c => c.CreateCourse(_command.Title)).Returns(_createdCourse);
        }