Skip to content

Commit

Permalink
Add delete endpoint for robot
Browse files Browse the repository at this point in the history
  • Loading branch information
GodVenn committed Apr 26, 2023
1 parent 603b1a0 commit 10f4585
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
20 changes: 19 additions & 1 deletion backend/api/Controllers/RobotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public async Task<ActionResult<Robot>> CreateRobot([FromBody] CreateRobotQuery r
/// </summary>
/// <remarks>
/// </remarks>
/// <response code="200"> The robot was succesfully updated </response>
/// <response code="200"> The robot was successfully updated </response>
/// <response code="400"> The robot data is invalid </response>
/// <response code="404"> There was no robot with the given ID in the database </response>
[HttpPut]
Expand Down Expand Up @@ -165,6 +165,24 @@ [FromBody] Robot robot
}
}

/// <summary>
/// Deletes the robot with the specified id from the database.
/// </summary>
[HttpDelete]
[Route("{id}")]
[ProducesResponseType(typeof(Mission), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<Robot>> DeleteRobot([FromRoute] string id)
{
var robot = await _robotService.Delete(id);
if (robot is null)
return NotFound($"Robot with id {id} not found");
return Ok(robot);
}

/// <summary>
/// Updates a robot's status in the database
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions backend/api/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IRobotService
public abstract Task<Robot?> ReadById(string id);
public abstract Task<Robot?> ReadByIsarId(string isarId);
public abstract Task<Robot> Update(Robot robot);
public abstract Task<Robot?> Delete(string id);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage(
Expand Down Expand Up @@ -87,5 +88,17 @@ public async Task<Robot> Update(Robot robot)
await _context.SaveChangesAsync();
return entry.Entity;
}

public async Task<Robot?> Delete(string id)
{
var robot = await GetRobotsWithSubModels().FirstOrDefaultAsync(ev => ev.Id.Equals(id));
if (robot is null)
return null;

_context.Robots.Remove(robot);
await _context.SaveChangesAsync();

return robot;
}
}
}

0 comments on commit 10f4585

Please sign in to comment.