public async Task <IActionResult> UpdateById(int id, [FromBody] HelpInstructionResource resource) { if (id <= 0) { return(BadRequest(string.Format(Constants.InvalidIdProvided, id))); } if (resource == null) { return(BadRequest(Constants.InvalidRequestPayload)); } var res = _context.HelpInstructions.Find(id); if (res != null) { res = _datamap.Map(resource, res); _context.Entry(res).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Ok(resource)); } else { return(NotFound(string.Format(Constants.IdNotFound, id))); } }
public async Task <IActionResult> Create([FromBody] HelpInstructionResource resource) { if (resource == null) { return(BadRequest(Constants.InvalidRequestPayload)); } var existsbyid = _context.HelpInstructions.Find(resource.InternalId) != null; var existsbykey = _context.HelpInstructions.Any(x => x.LookupKey == resource.LookupKey); if (!existsbyid && !existsbykey) { var result = _datamap.Map <HelpInstruction>(resource); _context.HelpInstructions.Add(result); await _context.SaveChangesAsync(); resource.InternalId = result.Id; return(Ok(resource)); } else if (existsbyid) { return(Conflict(string.Format(Constants.PrimaryKeyViolation, resource.InternalId))); } else { return(Conflict(string.Format(Constants.LookupKeyViolation, resource.LookupKey))); } }
public void UpdateById_WhenCalledWithNoId_ReturnsBadRequestResult() { Setup(); var hir = new HelpInstructionResource() { ExternalId = "test", HostKey = "testhost", LookupKey = "testkey", TooltipText = "test tooltip text" }; var r = _controller.UpdateById(0, hir); var objectResult = Assert.IsType <BadRequestObjectResult>(r.Result); var valresult = Assert.IsAssignableFrom <string>(objectResult.Value); Assert.Equal(string.Format(Constants.InvalidIdProvided, 0), valresult); }
public void UpdateById_WhenCalledWithNotExistingId_ReturnsNotFoundResult() { Setup(); var hir = new HelpInstructionResource() { InternalId = 1000, ExternalId = "updfailtest", HostKey = "updfailtest", LookupKey = "updfailtest", TooltipText = "updfailtest" }; var r = _controller.UpdateById(1000, hir); var objectResult = Assert.IsType <NotFoundObjectResult>(r.Result); var valresult = Assert.IsAssignableFrom <string>(objectResult.Value); Assert.Equal(string.Format(Constants.IdNotFound, 1000), valresult); }
public void Create_WhenCalledWithDupeLookupData_ReturnsConflictResult() { Setup(); var hir = new HelpInstructionResource() { InternalId = 101, ExternalId = "badcreatetest", HostKey = "badcreatehostkey", LookupKey = "testkey1", TooltipText = "create tooltip text which should never find the db" }; var r = _controller.Create(hir); var objectResult = Assert.IsType <ConflictObjectResult>(r.Result); var valresult = Assert.IsAssignableFrom <string>(objectResult.Value); Assert.Equal(string.Format(Constants.LookupKeyViolation, "testkey1"), valresult); }
public void UpdateById_WhenCalledWithValidData_ReturnsOkResult() { Setup(); var hir = new HelpInstructionResource() { InternalId = 1, ExternalId = "testupd", HostKey = "testhostupd", LookupKey = "testkeyupd", TooltipText = "test tooltip text updated" }; var r = _controller.UpdateById(1, hir); var objectResult = Assert.IsType <OkObjectResult>(r.Result); var valresult = Assert.IsAssignableFrom <HelpInstructionResource>(objectResult.Value); Assert.Equal(1, valresult.InternalId); Assert.Equal("testupd", valresult.ExternalId); Assert.Equal("testhostupd", valresult.HostKey); Assert.Equal("testkeyupd", valresult.LookupKey); Assert.Equal("test tooltip text updated", valresult.TooltipText); }
public void Create_WhenCalledWithValidData_ReturnsOkResult() { Setup(); var hir = new HelpInstructionResource() { InternalId = 100, ExternalId = "createtest", HostKey = "createhostkey", LookupKey = "createlookupkey", TooltipText = "create tooltip text" }; var r = _controller.Create(hir); var objectResult = Assert.IsType <OkObjectResult>(r.Result); var valresult = Assert.IsAssignableFrom <HelpInstructionResource>(objectResult.Value); Assert.Equal(100, valresult.InternalId); Assert.Equal("createtest", valresult.ExternalId); Assert.Equal("createhostkey", valresult.HostKey); Assert.Equal("createlookupkey", valresult.LookupKey); Assert.Equal("create tooltip text", valresult.TooltipText); }