Home > Blockchain >  Is there a way to edit data sent after posted to c# restapi?
Is there a way to edit data sent after posted to c# restapi?

Time:03-12

I get data posted to my C# REST API from a device which will not allow me to edit before sent. What I would like to do is edit at the controller. Currently the data being sent is this:

  {
    "id": 479,
    "imo": "9455052",
    "positionReceived": "2022-03-11 19:52 UTC26 minutes ago",
    "vesselsLocalTime": "2022-03-11 20:52 LT  (UTC  1)",
    "area": "WMED - West Mediterranean",
    "currentPort": "FOS SUR MER ANCH",
    "latitudeLongitude": "43.39003° / 4.959233°",
    "status": "At Anchor",
    "speedCourse": "0.1 kn / 331 °",
    "aisSource": "1706",
    "nearByVessels": null,
    "weatherWind": "Wind: 31 knots",
    "weatherWindDirection": "SE (129°)",
    "weatherAirTemperature": "14°C",
    "voyageStart": "ATD: 2022-03-06 17:12 LT  (UTC  1)",
    "voyageStartCountry": "DZ",
    "voyageStartPort": "ALG",
    "voyageEnd": null,
    "voyageEndCountry": "FR",
    "voyageEndPort": "FOS SUR MER ANCH",
    "voyageGeneralLocation": null,
    "voyageStartReportedActualTimeDeparture": "ATD: 2022-03-06 17:12 LT  (UTC  1)",
    "voyageEndReportedActualTimeArrival": "ATA: 2022-03-08 04:28 LT  (UTC  1)"
  }

What I need to understand is the process to edit the data for example positionReceived: 2022-03-11 19:52 UTC26 minutes ago",

I would like to store 2022-03-11 19:52 UTC

namespace Statistics.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DatapointsController : ControllerBase
{
    private IDatapointRepository datapoints;

    public DatapointsController(IDatapointRepository _datapoints)
    {
        this.datapoints = _datapoints;
    }

    [HttpGet]
    public ActionResult<IEnumerable<Datapoint>> GetAllDatapoints()
    {
        return datapoints.GetAllDatapoints();
    }

    [HttpGet("{id}")]
    public ActionResult<Datapoint> GetDatapoint(int id)
    {
        var datapoint = datapoints.GetDatapoint(id);
        if (datapoint == null)
        {
            return NotFound();
        }
        return datapoint;
    }

    [EnableCors("CorsPolicy")]
    [HttpPost]
    public ActionResult<Datapoint> PostDatapoint(Datapoint datapoint)
    {
        if (datapoints.AddNewDatapoint(datapoint))
        {
            return datapoint;
        }
        return BadRequest();
    }


}

CodePudding user response:

What is the problem with this flow?

[EnableCors("CorsPolicy")]
[HttpPost]
public ActionResult<Datapoint> PostDatapoint(Datapoint datapoint)
{
    datapoint.positionReceived = ... ; // here you can set whatever you want before saving
    if (datapoints.AddNewDatapoint(datapoint))
    {
        return datapoint;
    }
    return BadRequest();
}

There are many ways how transform the 2022-03-11 19:52 UTC26 minutes ago" to 2022-03-11 19:52 UTC. You need to analyze all possible values before and to choose what is the best work for you.

Example 1

datapoint.positionReceived = datapoint.positionReceived.Substring(0, 20);

Example 2

datapoint.positionReceived = datapoint.positionReceived.Split("UTC")[0]   "UTC";

Example 3

datapoint.positionReceived = datapoint.positionReceived.AsSpan().Slice(0,20).ToString();
  • Related