Home > front end >  Unable to execute update from flutter to API
Unable to execute update from flutter to API

Time:08-02

I'm trying to update the API from flutter. So far i think flutter works fine in the flutter.dart it returns the value correctly, But im not sure if the API works correcly. Whenever i try to start the function in flutter the CMD returns this:

Update Service Method Invoked Cmd return

And this is one thing he needs to return in the Controller.cs

IRepository.cs

public interface IRepository<T>
{
    Task<List<T>> GetData();
    Task<T> GetDataById(int id);
    Task<T> InsertData(T service);
    Task<T> UpdateData(T service);
    Task<bool> DeleteData(T service);

}

KlantRepository.cs

public class KlantRepository: IRepository<KlantModel>
{
    private readonly DataContext _context;

    public async Task<KlantModel> UpdateData(KlantModel klant)
    {
        Console.WriteLine("Update method invoked");

        // _context.Entry(klant).State = EntityState.Modified;
        // await _context.SaveChangesAsync();

        _context.Update(klant).Property(x => x.KlantId).IsModified = false;
        _context.SaveChanges();

        await UpdateData(klant);
        return klant;
    }
}

KlantController

[ApiController]
[Route("api/Klant")]
[Produces("application/json")]
public class KlantController : ControllerBase
{

    private readonly IRepository<KlantModel> _repo;
    private readonly IMapper _mapper;
    public KlantController(IRepository<KlantModel> repo, IMapper mapper)
    {
        _repo = repo;
        _mapper = mapper;
    }

    [HttpPut("{id}")]
    public async Task<ActionResult<KlantModel>> UpdateService( int id, KlantModel klant)
    {
        Console.WriteLine("Update Service Method Invoked");
        try
        {
            if (id != klant.KlantId)
                return BadRequest("Employee ID mismatch");

            var employeeToUpdate = await _repo.GetDataById(id);

            if (employeeToUpdate == null)
                return NotFound($"Klant with Id = {id} not found");

            return await KlantRepository.UpdateData(klant);
        }
        catch (Exception)
        {
            return StatusCode(StatusCodes.Status500InternalServerError,
                "Error updating data");
        }
    }

As you can see he does call the api but the api somehow doesn't continu if anyone can help me with this issue that would make my day!!! thanks already

____________ !! UPDATE !! ___________

This is my Postman request Postman request

CodePudding user response:

The server responds with a 400 Bad Request. That could mean that your payload is incorrect. You are sending a property called "Mailaddres"; shouldn't that be "Mailaddress" or "Mailadres"? If it's correct check the other body properties with those from KlantModel.

Possible typo?

As a side note and maybe you already know that, but it's considered bad practice to directly send and receive model data. You should use DTOs and ViewModels instead. Cf. https://medium.com/@enocklubowa/why-you-need-to-use-dtos-in-your-rest-api-d9d6d7be5450#:~:text=The DTO is helping you,manipulating data in the database.

CodePudding user response:

According to your POSTMAN request, you want this as a body, so make sure you tag it as such:

UpdateService([FromRoute] int id, [FromBody] KlantModel klant)
  • Related