Search code examples
c#.netrestget

Custom get method with .net core


I want to create a custom get method such as: https://localhost:4200/api/get/Echeanciers?idengin=2 I want to get all "Echeancier" where id engin equals to 2. I don't know if its possible. I can explain clearly with this sql request:

select * from Echeancier where id_engin = 2

Here is my Echeancier models:

 public partial class Echeancier
 {
     public int IdEcheancier { get; set; }
     public string MoisEcheancier { get; set; }
     public string Montant { get; set; }
     public int IdEngin { get; set; }

     public virtual Engin IdEnginNavigation { get; set; }
 }

id engin is a property from an other class:

public partial class Engin
{
     public Engin()
     {
         Echeancier = new HashSet<Echeancier>();
     }

     public int IdEngin { get; set; }
     public int Matricule { get; set; }
     public string NumSerie { get; set; }
     public int? IdEcheancier { get; set; }
}

This is the Echeancier controller

 [Route("api/[controller]")]
 [ApiController]
 public class EcheanciersController : ControllerBase
 {
     private readonly dbSecurityParkContext _context;

     public EcheanciersController(dbSecurityParkContext context)
     {
         _context = context;
     }

     // GET: api/Echeanciers
     [HttpGet]
     public async Task<ActionResult<IEnumerable<Echeancier>>> GetEcheancier()
     {
         return await _context.Echeancier.ToListAsync();
     }

     // GET: api/Echeanciers/5
     [HttpGet("{id}")]
     public async Task<ActionResult<Echeancier>> GetEcheancier(int id)
     {
         var echeancier = await _context.Echeancier.FindAsync(id);

         if (echeancier == null)
         {
             return NotFound();
         }

         return echeancier;
     }

     // PUT: api/Echeanciers/5
     // To protect from overposting attacks, enable the specific properties you want to bind to, for
     // more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
     [HttpPut("{id}")]
     public async Task<IActionResult> PutEcheancier(int id, Echeancier echeancier)
     {
         if (id != echeancier.IdEcheancier)
         {
             return BadRequest();
         }

         _context.Entry(echeancier).State = EntityState.Modified;

         try
         {
             await _context.SaveChangesAsync();
         }
         catch (DbUpdateConcurrencyException)
         {
             if (!EcheancierExists(id))
             {
                 return NotFound();
             }
             else
             {
                 throw;
             }
         }

         return NoContent();
     }

     // POST: api/Echeanciers
     // To protect from overposting attacks, enable the specific properties you want to bind to, for
     // more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
     [HttpPost]
     public async Task<ActionResult<Echeancier>> PostEcheancier(Echeancier echeancier)
     {
         _context.Echeancier.Add(echeancier);
         try
         {
             await _context.SaveChangesAsync();
         }
         catch (DbUpdateException)
         {
             if (EcheancierExists(echeancier.IdEcheancier))
             {
                 return Conflict();
             }
             else
             {
                 throw;
             }
         }

         return CreatedAtAction("GetEcheancier", new { id = echeancier.IdEcheancier }, echeancier);
     }

     // DELETE: api/Echeanciers/5
     [HttpDelete("{id}")]
     public async Task<ActionResult<Echeancier>> DeleteEcheancier(int id)
     {
         var echeancier = await _context.Echeancier.FindAsync(id);
         if (echeancier == null)
         {
             return NotFound();
         }

         _context.Echeancier.Remove(echeancier);
         await _context.SaveChangesAsync();

         return echeancier;
     }

     private bool EcheancierExists(int id)
     {
         return _context.Echeancier.Any(e => e.IdEcheancier == id);
     }
 }

Solution

  • You can try:

    [HttpGet]
    public async Task<ActionResult<IEnumerable<Echeancier>>> GetEcheancier([FromQuery] int? idengin)
    {
        var query = _context.Echeancier.AsQueryable();
        if(idengin.HasValue)
        {
             query = query.Where(item=> item.IdEngin == idengin.Value);
        }
        return await query.ToListAsync();
    }