Home > Software engineering >  C# API query for Alternate key
C# API query for Alternate key

Time:08-15

I'm trying to do this query: SELECT * FROM Machine WHERE KlantId = [Id from url] in c# but its returing only 1 row when it should be 2 rows. I manualy change the input of KlantId in the url. The url looks like this: https://00.00.00.00:0000/api/Machine/1

Any help would be much appreciated.

Api output: enter image description here

Api Model:

[
  {
    "SerienummerId": 1,
    "Serienummer": "10K13321",
    "Bouwjaar": "2020",
    "Urenstand": "10",
    "Locatie": "-23134123, 123123",
    "KlantId": "1"
  },
  {
    "SerienummerId": 2,
    "Serienummer": "25K84314",
    "Bouwjaar": "1998",
    "Urenstand": "5010",
    "Locatie": "-23134123, 123123",
    "KlantId": "1"
  }
]

C# MachineRepository

public class MachineRepository: IRepository<MachineModel>
    {
        private readonly DataContext _context;
        public MachineRepository(DataContext context)
        {
            _context = context;
        }
        
        // Get Service by  id
        public async Task<MachineModel> GetDataById(int id)
        {
            return await _context.Machine.FirstOrDefaultAsync(x => x.KlantId == id);
        }
    }

MachineController:

[ApiController]
[Route("api/Machine")]
[Produces("application/json")]
public class MachineController : ControllerBase
{
    private readonly IRepository<MachineModel> _repo;
    private readonly IMapper _mapper;
    public MachineController(IRepository<MachineModel> repo, IMapper mapper)
    {
        _repo = repo;
        _mapper = mapper;
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetServiceById(int id)
    {
        Console.WriteLine("This is the comming id ");
        Console.WriteLine(id);

        var model = await _repo.GetDataById(id);
        return Ok(_mapper.Map<MachineDto>(model));
    }
}

CodePudding user response:

FirstOrDefaultAsync will return the top 1 record.

Change 1 - In C# MachineRepository, Instead of FirstOrDefaultAsync, replace this with Where and change the return type to List

Change 2 - In MachineController, Map it to List DTO

Note: I assume Machine is having array of records

 public class MachineRepository: IRepository<MachineModel>
 {
     private readonly DataContext _context;
     public MachineRepository(DataContext context)
     {
         _context = context;
     }
        
     // Get Service by  id
     public async Task<List<MachineModel>> GetDataById(int id) //change the return type to LIST
     {
         return await _context.Machine.Where(x => x.KlantId == id).ToListAsync();
      }
 }


[ApiController]
[Route("api/Machine")]
[Produces("application/json")]
public class MachineController : ControllerBase
{
    private readonly IRepository<MachineModel> _repo;
    private readonly IMapper _mapper;
    public MachineController(IRepository<MachineModel> repo, IMapper mapper)
    {
        _repo = repo;
        _mapper = mapper;
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetServiceById(int id)
    {
        Console.WriteLine("This is the comming id ");
        Console.WriteLine(id);

        var model = await _repo.GetDataById(id);
        return Ok(_mapper.Map<List<MachineDto>>(model)); //Map to List
    }
}
  • Related