public async Task <IActionResult> CreateASkill([FromBody] DisciplineSkillResource req, int disciplineID) { if (req == null) { var error = new BadRequestException("The given request body is null / Request Body cannot be read"); return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException())); } if (disciplineID == 0 || disciplineID != req.DisciplineId) { var error = new BadRequestException("The given discipline ID is invalid"); return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException())); } if (String.IsNullOrEmpty(req.Name)) { var error = new BadRequestException("Skill Name cannot be null or empty"); return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException())); } try { var createdSkillID = await skillsRepository.CreateASkill(req); var response = new CreatedResponse <int>(createdSkillID, $"Successfully created skill '{createdSkillID}' associated with discipline '{disciplineID}'"); return(StatusCode(StatusCodes.Status201Created, response)); } catch (Exception err) { var errMessage = $"Source: {err.Source}\n Message: {err.Message}\n StackTrace: {err.StackTrace}\n"; if (err is SqlException) { var error = new InternalServerException(errMessage); return(StatusCode(StatusCodes.Status500InternalServerError, new CustomException <InternalServerException>(error).GetException())); } else { var error = new BadRequestException(errMessage); return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException())); } } }
public async Task <int> CreateASkill(DisciplineSkillResource skill) { var sql = @" insert into Skills (DisciplineId, Name) values (@DisciplineId, @Name); select cast(scope_identity() as int); ;"; using var connection = new SqlConnection(connectionString); connection.Open(); skill.SkillId = await connection.QuerySingleAsync <int>(sql, new { DisciplineId = skill.DisciplineId, Name = skill.Name }); return(skill.SkillId); }
public async void CreateASkill_CatchBlock_ReturnSqlException() { string errMessage = "Internal Server Error"; var sqlException = new SqlExceptionBuilder().WithErrorNumber(50000).WithErrorMessage(errMessage).Build(); Setup_SkillsRepo_CreateASkill_ThrowsException(sqlException); var skill = new DisciplineSkillResource { DisciplineId = 2, SkillId = 0, Name = "A Skill Name" }; var result = (await _controller.CreateASkill(skill, 2)) as ObjectResult; Assert.Equal(StatusCodes.Status500InternalServerError, result.StatusCode); var response = result.Value as InternalServerException; Assert.Equal(errMessage, response.status); }
private async void CreateASkill_TryBlock_ReturnValidId() { var skill = new DisciplineSkillResource { DisciplineId = 1, SkillId = 0, Name = "A Skill" }; var returnVal = 1; Setup_SkillsRepo_CreateASkill_Default(returnVal); var result = (await _controller.CreateASkill(skill, 1)) as ObjectResult; Verify_SkillsRepo_CreateASkill(Times.Once); Assert.Equal(StatusCodes.Status201Created, result.StatusCode); Assert.IsType <CreatedResponse <int> >(result.Value); var response = result.Value as CreatedResponse <int>; Assert.IsType <int>(response.payload); }
public async void CreateASkill_CatchBlock_ReturnBadException() { var errMessage = "Bad Request"; var badRequestException = new CustomException <BadRequestException>(new BadRequestException(errMessage)); Setup_SkillsRepo_CreateASkill_ThrowsException(badRequestException); var skill = new DisciplineSkillResource { DisciplineId = 2, SkillId = 0, Name = "" }; var result = (await _controller.CreateASkill(skill, 2)) as ObjectResult; Assert.Equal(StatusCodes.Status400BadRequest, result.StatusCode); Assert.IsType <BadRequestException>(result.Value); var response = result.Value as BadRequestException; Assert.Equal(errMessage, response.status); }