Home > Enterprise >  ASP.NET Core - Trying to create an endpoint which deletes multiple records by ID
ASP.NET Core - Trying to create an endpoint which deletes multiple records by ID

Time:11-27

I'm trying to create an endpoint supposed to delete multiple ids. It should match deleteRoom(ids: number[]). You can see what I tried below but it doesn't match the request from the angular.

deleteRoom(ids: number[]) {
  return this.httpClient.delete(`${this.actionUrl}?id=${ids.toString()}`);
}
public class RoomsController : ApiControllerBase
{
    [HttpGet]
    public async Task<ActionResult<IList<RoomDto>>> GetRooms()
    {
        var result = await Mediator.Send(new GetRoomsQuery()).ConfigureAwait(false);

        return Ok(result);
    }

    [HttpGet("available")]
    public async Task<ActionResult<IList<RoomDto>>> GetAvailableRooms(
        [FromQuery] DateTime from,
        [FromQuery] DateTime to,
        [FromQuery] int? departmentId,
        [FromQuery] RoomType? roomType)
    {
        var query = new GetAvailableRoomsQuery
        {
            From = from,
            To = to,
            DepartmentId = departmentId,
            RoomType = roomType
        };

        var result = await Mediator.Send(query).ConfigureAwait(false);

        return Ok(result);
    }


    [HttpPost]
    public async Task<ActionResult<int>> Create(CreateRoomCommand command)
    {
        return await Mediator.Send(command).ConfigureAwait(false);
    }

    [HttpPut("{id:int}")]
    public async Task<ActionResult> Update(int id, UpdateRoomCommand command)
    {
        if (id != command.Id) return BadRequest();

        await Mediator.Send(command).ConfigureAwait(false);

        return NoContent();
    }

    [HttpDelete("{id:int}")]
    public async Task<ActionResult> Delete(int id)
    {
        await Mediator.Send(new DeleteRoomCommand {Id = id}).ConfigureAwait(false);

        return NoContent();
    }

    [HttpDelete("{ids}")]
    public async Task<ActionResult> Delete(int[] ids, DeleteRoomsCommand command)
    {
        await Mediator.Send(command).ConfigureAwait(false);

        return NoContent();
    }
}

CodePudding user response:

there two ways to use several ids in get request

  1. Route values

your url should be like this ( you can use something else instead of ",")

http:\\....\deleteRooms\1,2,3,4

the action should be like this

[HttpGet("DeleteRooms/{ids}")] //or httpdelete
public ActionResult DeleteRooms(string ids)
{
  string[] roomIds = ids.split(",");
  ...
}
  1. query string
http:\\....\deleteRooms?ids=1&ids=2&ids=3&ids=4

the action can be

[HttpGet("DeleteRooms")] //or httpdelete
public ActionResult DeleteRooms(int[] ids)
{
   ...
}

CodePudding user response:

If you notice, in your API Code, you are expecting a single integer but what you are passing is a comma separated numbers (which is a string)

[HttpDelete("{id:int}")]
public async Task<ActionResult> Delete(int id)
{

One option is to change int to string and do a string split within controller code.

public async Task<ActionResult> Delete(string ids)
{
  // split ids into array of int values. "1,2" into [1,2] using string.split
  • Related