public IActionResult Post([FromBody] RequestModelIn requestIn) { if (ModelState.IsValid) { try { var request = mapper.Map <RequestModelIn, RequestEntity>(requestIn); var id = requestLogic.Add(request); var addedRequest = requestLogic.GetById(id); var addedRequestOut = mapper.Map <RequestEntity, RequestModelOut>(addedRequest); return(Created("Posted succesfully", addedRequestOut)); } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } catch (Exception ex) { return(BadRequest(ex.Message)); } } else { return(BadRequest(ModelState)); } }
public void TestInitialize() { webApiMapper = new WebApiMapper(); request = new RequestEntity { Detail = "Un contenedor roto en la esquina de Av. Italia y Bolivia", ApplicantName = "Federico Jacobo", Mail = "*****@*****.**", Phone = "098555666", RequestTypeEntityId = 1, Status = RequestStatus.CREADA.ToString() }; requestModelOut = new RequestModelOut() { Detail = "Un contenedor roto en la esquina de Av. Italia y Bolivia", ApplicantName = "Federico Jacobo", Mail = "*****@*****.**", Phone = "098555666", Status = "CREADA" }; requestModelIn = new RequestModelIn() { Detail = "Un contenedor roto en la esquina de Av. Italia y Bolivia", ApplicantName = "Federico Jacobo", Mail = "*****@*****.**", Phone = "098555666" }; requestLogicMock = new Mock <IRequestLogic>(); requestController = new RequestController(requestLogicMock.Object, webApiMapper); }
public IActionResult Put(int id, [FromBody] RequestModelIn requestIn) { if (ModelState.IsValid) { try { requestIn.Id = id; var request = mapper.Map <RequestModelIn, RequestEntity>(requestIn); requestLogic.Update(request); var updatedRequest = requestLogic.GetById(id); var updatedRequestModelOut = mapper.Map <RequestEntity, RequestModelOut>(updatedRequest); return(Created("Put succesfully", updatedRequestModelOut)); } catch (ArgumentException e) { return(BadRequest(e.Message)); } catch (Exception ex) { return(BadRequest(ex.Message)); } } else { return(BadRequest(ModelState)); } }
public void Put_Expects_BadRequest() { var modelIn = new RequestModelIn(); requestController.ModelState.AddModelError("", "Error"); var result = requestController.Put(0, modelIn); var createdResult = result as BadRequestObjectResult; Assert.IsNotNull(createdResult); Assert.AreEqual(400, createdResult.StatusCode); }