public async Task <IActionResult> Create([FromBody] SkillCreateDTO skillDTO) { try { _logger.LogInfo($"Skill submission attempted."); if (skillDTO == null) { _logger.LogWarn($"Empty Request was submitted."); return(BadRequest(ModelState)); } if (!ModelState.IsValid) { _logger.LogWarn($"Skill Data was Invalid."); return(BadRequest(ModelState)); } var skill = _mapper.Map <Skill>(skillDTO); var isSuccess = await _skillRepository.Create(skill); if (!isSuccess) { return(InternalError($"Skill creation failed.")); } _logger.LogInfo("Skill created"); return(Created("Create", new { skill })); } catch (Exception e) { return(InternalError($"{e.Message} - {e.InnerException}")); } }
public async void CreateAsync_GivenTwoValidSkills_ReturnsTwoNewSkillIDs() { var skill1 = new SkillCreateDTO { Name = "Cooking" }; var skill2 = new SkillCreateDTO { Name = "Dancing" }; using (var repository = new SkillRepository(context)) { var id1 = await repository.CreateAsync(skill1); var id2 = await repository.CreateAsync(skill2); var foundSkill1 = await context.Skills.FirstAsync(); var foundSkill2 = (await context.Skills.ToArrayAsync())[1]; Assert.Equal(foundSkill1.Id, id1); Assert.Equal(foundSkill2.Id, id2); } }
public async Task <int> Create(SkillCreateDTO skill) { var response = await _client.PostAsync("api/v1/skills", skill.ToHttpContent()); var newSkillId = response.Content.To <int>().Result; return(response.IsSuccessStatusCode ? newSkillId : -1); }
public async void CreateAsync_GivenValidSkill_ReturnsNewSkillID() { var skill = new SkillCreateDTO { Name = "Cooking" }; using (var repository = new SkillRepository(context)) { var id = await repository.CreateAsync(skill); var foundSkill = await context.Skills.FirstAsync(); Assert.Equal(foundSkill.Id, id); } }
public async void CreateAsync_GivenNothingCreated_ReturnsERROR_CREATING() { var skillToCreate = new SkillCreateDTO { Name = "Skill" }; skillRepositoryMock.Setup(c => c.FindAsync(skillToCreate.Name)).ReturnsAsync(default(SkillDTO)); skillRepositoryMock.Setup(c => c.CreateAsync(skillToCreate)).ReturnsAsync(0); using (var logic = new SkillLogic(skillRepositoryMock.Object, userRepositoryMock.Object, projectRepositoryMock.Object)) { var response = await logic.CreateAsync(skillToCreate); Assert.Equal(ResponseLogic.ERROR_CREATING, response); skillRepositoryMock.Verify(c => c.FindAsync(skillToCreate.Name)); skillRepositoryMock.Verify(c => c.CreateAsync(skillToCreate)); } }
public async void CreateAsync_GivenValidSkill_ReturnsSUCCESS() { var skillToCreate = new SkillCreateDTO { Name = "Skill" }; skillRepositoryMock.Setup(c => c.FindAsync(skillToCreate.Name)).ReturnsAsync(default(SkillDTO)); skillRepositoryMock.Setup(c => c.CreateAsync(skillToCreate)).ReturnsAsync(1); using (var logic = new SkillLogic(skillRepositoryMock.Object, userRepositoryMock.Object, projectRepositoryMock.Object)) { var response = await logic.CreateAsync(skillToCreate); Assert.Equal(ResponseLogic.SUCCESS, response); skillRepositoryMock.Verify(c => c.FindAsync(skillToCreate.Name)); skillRepositoryMock.Verify(c => c.CreateAsync(skillToCreate)); } }
public async Task <IActionResult> Post([FromBody] SkillCreateDTO skill) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var success = await _skillLogic.CreateAsync(skill); if (success == ResponseLogic.SUCCESS) { var createdSkill = await _skillLogic.FindExactAsync(skill.Name); return(CreatedAtAction(nameof(Get), new { createdSkill.Id }, createdSkill.Id)); } else { return(StatusCode(500)); } }
public async Task <ResponseLogic> CreateAsync(SkillCreateDTO skill) { var currentSkill = await _repository.FindAsync(skill.Name); if (currentSkill != null) { return(ResponseLogic.SUCCESS); } var createdId = await _repository.CreateAsync(skill); if (createdId > 0) { return(ResponseLogic.SUCCESS); } else { return(ResponseLogic.ERROR_CREATING); } }
public async void CreateAsync_GivenSkillExists_ReturnsSUCCESS() { var skillToCreate = new SkillCreateDTO { Name = "Skill" }; var existingSkill = new SkillDTO { Id = 1, Name = "Skill" }; skillRepositoryMock.Setup(c => c.FindAsync(skillToCreate.Name)).ReturnsAsync(existingSkill); using (var logic = new SkillLogic(skillRepositoryMock.Object, userRepositoryMock.Object, projectRepositoryMock.Object)) { var response = await logic.CreateAsync(skillToCreate); Assert.Equal(ResponseLogic.SUCCESS, response); skillRepositoryMock.Verify(c => c.FindAsync(skillToCreate.Name)); skillRepositoryMock.Verify(c => c.CreateAsync(It.IsAny <SkillCreateDTO>()), Times.Never()); } }
public async Task <int> CreateAsync(SkillCreateDTO skill) { var skillToCreate = new Skill { Name = skill.Name }; var existingSkill = await _context.Skills.Where(s => s.Name == skill.Name).AsNoTracking().FirstOrDefaultAsync(); if (existingSkill != null) { throw new DbUpdateException("Skill already exists", (Exception)null); } _context.Skills.Add(skillToCreate); if (await saveContextChanges() > 0) { return(skillToCreate.Id); } else { throw new DbUpdateException("Error creating skill", (Exception)null); } }