I have created a get get by id and a delete api controller that are working just fine. What I am having problems with is creating an edit all the data by id. My examples of my api controllers look as:
[Authorize]
[Route("api/complaints")]
//get all
[HttpGet("")]
public JsonResult Get()
{
var complaints = _repository.GetAll();
var results = Mapper.Map<IEnumerable<CoViewModel>>(comps);
return Json(results);
}
//get by id
[HttpGet("{id}", Name = "GetByIdComp")]
public IActionResult GetById(int id)
{
var complaints = _repository.GetById(id);
if (complaints == null)
{
return HttpNotFound();
}
return new ObjectResult(comps);
}
//delete by id
[HttpDelete("{id}")]
public void Delete(int id)
{
_repository.Remove(id);
_repository.SaveAll();
}
//update/edit api not working
[HttpPut("{id}")]
public JsonResult Post([FromBody] CoViewModel vm, int id)
{
try
{
if (ModelState.IsValid)
{
var obj = Mapper.Map<COMP>(vm);
//Mapping from VM to Entities
_logger.LogInformation("made a change");
COMPLAINT updated = _repository.UpdateComp(obj, id);
Response.StatusCode = (int)HttpStatusCode.Accepted;
return Json(Mapper.Map<CoViewModel>(updated)); //Mapping from Ents to VM for the client
}
}
catch (Exception ex)
{
_logger.LogError("N'error", ex);
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { Message = ex.Message });
}
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { Message = "Failed", ModelState = ModelState });
}
My repository for my api controller is this:
public COMP UpdateComp(COMP newObj, int idOriginal)
{
var original = _context.Comps.FirstOrDefault(m => m.ID == idOriginal);
//Other related tables to update
//original.CHECKLISTs = newObj.CHECKLISTs;
//original.Division = newObj.Division;
_context.SaveChanges();
return original;
}
When I try to use postman to change by id and put in the http and seend in a put or post, I get the error:
{message": "Failed",
"modelState": {
"": {
"rawValue": null,
"attemptedValue": null,
"errors": [
{
"exception": {
"ClassName": "Newtonsoft.Json.JsonReaderException",
"Message": "Unexpected character encountered while parsing number: W. Path '', line 1, position 6.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at Newtonsoft.Json.JsonTextReader.ReadNumberIntoBuffer()\r\n at Newtonsoft.Json.JsonTextReader.ParseNumber()\r\n at Newtonsoft.Json.JsonTextReader.ParseValue()\r\n at Newtonsoft.Json.JsonTextReader.ReadInternal()\r\n at Newtonsoft.Json.JsonTextReader.Read()\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": "8\nReadNumberIntoBuffer\nNewtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed\nNewtonsoft.Json.JsonTextReader\nVoid ReadNumberIntoBuffer()",
"HResult": -2146233088,
"Source": "Newtonsoft.Json",
"WatsonBuckets": null
},
"errorMessage": ""
}
],
"validationState": 1
},
"id": {
"rawValue": "909",
"attemptedValue": "909",
"errors": [],
"validationState": 2
}
}
}