public HttpResponseMessage AddNewCourse([FromBody] AddNewCourseRequest request) { var response = _service.AddCourse(request); if (response) { return(Request.CreateResponse <bool>(HttpStatusCode.OK, response)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "No course added")); } }
public static CourseValidationData ConvertToCourseValidationData(this AddNewCourseRequest request) { return(new CourseValidationData { Id = request.Id, Title = request.Title, CategoryId = request.CategoryId, Description = request.Description, FrameworkId = request.FrameworkId, InstructorId = request.InstructorId, Price = request.Price, Rating = request.Rating }); }
public void AddNewCourse() { var request = new AddNewCourseRequest { Id = 444, Title = "Python and Machine Learning", Description = "A basic course with most famous machine learning libraries in Python", Rating = 4.1M, Price = 10, CategoryId = "ML", FrameworkId = "DJ", InstructorId = 3 }; var res = service.AddNewCourse(request); Xunit.Assert.True(res); }
public void AddNewCourseWithExistingId() { var request = new AddNewCourseRequest { Id = 444, Title = "Python and Machine Learning", Description = "A basic course with most famous machine learning libraries in Python", Rating = 4.1M, Price = 10, CategoryId = "ML", FrameworkId = "DJ", InstructorId = 3 }; var ex = Xunit.Assert.Throws <Exception>(() => service.AddNewCourse(request)); Xunit.Assert.Equal("The Course id is existing", ex.Message); }
private void ValidateNewCourse(AddNewCourseRequest request) { _validation.NotValidId(request.Id, $"{nameof(request.Id)}"); _validation.NotValidId(request.InstructorId, $"{nameof(request.Id)}"); _validation.NotValidField(request.FrameworkId, 5, $"{nameof(request.FrameworkId)}"); _validation.NotValidField(request.CategoryId, 3, $"{nameof(request.CategoryId)}"); _validation.NotValidField(request.Title, 100, $"{nameof(request.Title)}"); _validation.NotValidField(request.Description, 500, $"{nameof(request.Description)}"); _validation.NotValidRating(request.Rating, $"{nameof(request.Rating)}"); _validation.NotValidPrice(request.Price, $"{nameof(request.Price)}"); }
public bool AddNewCourse(AddNewCourseRequest request) { string sql = @"INSERT INTO Course (Id,Title,Description,Rating,Price,CategoryId,FrameworkId,InstructorId) VALUES (@Id,@Title,@Description,@Rating,@Price,@CategoryId,@FrameworkId,@InstructorId)"; int result; var parameters = new { request.Id, request.Title, request.Description, request.Rating, request.Price, request.CategoryId, request.FrameworkId, request.InstructorId }; using (var con = GetSqlConnection()) { using (var transaction = con.BeginTransaction()) { if (CheckIfCourseNotExists(request.Id)) { result = con.Execute(sql, parameters, transaction: transaction); } else { throw new Exception("The Course id is existing"); } transaction.Commit(); } } return(result == 1); }
public bool AddCourse(AddNewCourseRequest request) { ValidateNewCourse(request); return(_dbService.AddNewCourse(request)); }