< Summary

Information
Class: LGDXRobotCloud.API.Areas.Navigation.Controllers.RealmsController
Assembly: LGDXRobotCloud.API
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Areas/Navigation/Controllers/RealmsController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 115
Coverable lines: 115
Total lines: 225
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%2040%
GetRealms()0%620%
SearchRealms()100%210%
GetRealm()100%210%
GetDefaultRealm()100%210%
CreateRealm()100%210%
UpdateRealm()0%620%
TestDeleteRealm()100%210%
DeleteRealm()0%620%
SetGoalAsync()0%620%
AbortGoalAsync()0%620%
RefreshMapAsync()0%620%
SaveMapAsync()0%620%
EmergencyStopAsync()0%2040%
AbortSlamAsync()100%210%
CompleteSlamAsync()100%210%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Areas/Navigation/Controllers/RealmsController.cs

#LineLine coverage
 1using System.Text.Json;
 2using LGDXRobotCloud.API.Authorisation;
 3using LGDXRobotCloud.API.Services.Navigation;
 4using LGDXRobotCloud.Data.Models.DTOs.V1.Commands;
 5using LGDXRobotCloud.Data.Models.DTOs.V1.Responses;
 6using LGDXRobotCloud.Data.Models.Business.Navigation;
 7using Microsoft.AspNetCore.Authentication.JwtBearer;
 8using Microsoft.AspNetCore.Authorization;
 9using Microsoft.AspNetCore.Mvc;
 10using LGDXRobotCloud.Utilities.Constants;
 11using LGDXRobotCloud.Data.Models.DTOs.V1.Requests;
 12using LGDXRobotCloud.Protos;
 13using LGDXRobotCloud.API.Exceptions;
 14
 15namespace LGDXRobotCloud.API.Areas.Navigation.Controllers;
 16
 17[ApiController]
 18[Area("Navigation")]
 19[Route("[area]/[controller]")]
 20[Authorize(AuthenticationSchemes = LgdxRobotCloudAuthenticationSchemes.ApiKeyOrCertificateScheme)]
 21[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
 22[ValidateLgdxUserAccess]
 023public class RealmsController(
 024  IRealmService realmService,
 025  ISlamService slamService
 026) : ControllerBase
 27{
 028  private readonly IRealmService _realmService = realmService ?? throw new ArgumentNullException(nameof(realmService));
 029  private readonly ISlamService _slamService = slamService ?? throw new ArgumentNullException(nameof(slamService));
 30
 31  [HttpGet("")]
 32  [ProducesResponseType(typeof(IEnumerable<RealmListDto>), StatusCodes.Status200OK)]
 33  public async Task<ActionResult<IEnumerable<RealmListDto>>> GetRealms(string? name, int pageNumber = 1, int pageSize = 
 034  {
 035    pageSize = (pageSize > 100) ? 100 : pageSize;
 036    var (realms, PaginationHelper) = await _realmService.GetRealmsAsync(name, pageNumber, pageSize);
 037    Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(PaginationHelper));
 038    return Ok(realms.ToDto());
 039  }
 40
 41  [HttpGet("Search")]
 42  [ProducesResponseType(typeof(IEnumerable<RealmSearchDto>), StatusCodes.Status200OK)]
 43  public async Task<ActionResult<IEnumerable<RealmSearchDto>>> SearchRealms(string? name)
 044  {
 045    var realms = await _realmService.SearchRealmsAsync(name);
 046    return Ok(realms.ToDto());
 047  }
 48
 49  [HttpGet("{id}", Name = "GetRealm")]
 50  [ProducesResponseType(typeof(RealmDto), StatusCodes.Status200OK)]
 51  [ProducesResponseType(StatusCodes.Status404NotFound)]
 52  public async Task<ActionResult<RealmDto>> GetRealm(int id)
 053  {
 054    var realm = await _realmService.GetRealmAsync(id);
 055    return Ok(realm.ToDto());
 056  }
 57
 58  [HttpGet("Default")]
 59  [ProducesResponseType(typeof(RealmDto), StatusCodes.Status200OK)]
 60  [ProducesResponseType(StatusCodes.Status404NotFound)]
 61  public async Task<ActionResult<RealmDto>> GetDefaultRealm()
 062  {
 063    var realm = await _realmService.GetDefaultRealmAsync();
 064    return Ok(realm.ToDto());
 065  }
 66
 67  [HttpPost("")]
 68  [ProducesResponseType(typeof(RealmDto), StatusCodes.Status201Created)]
 69  public async Task<ActionResult> CreateRealm(RealmCreateDto realmCreateDto)
 070  {
 071    var realm = await _realmService.CreateRealmAsync(realmCreateDto.ToBusinessModel());
 072    return CreatedAtAction(nameof(GetRealm), new { id = realm.Id }, realm.ToDto());
 073  }
 74
 75  [HttpPut("{id}")]
 76  [ProducesResponseType(StatusCodes.Status204NoContent)]
 77  [ProducesResponseType(StatusCodes.Status404NotFound)]
 78  public async Task<ActionResult> UpdateRealm(int id, RealmUpdateDto realmUpdateDto)
 079  {
 080    if (!await _realmService.UpdateRealmAsync(id, realmUpdateDto.ToBusinessModel()))
 081    {
 082      return NotFound();
 83    }
 084    return NoContent();
 085  }
 86
 87  [HttpPost("{id}/TestDelete")]
 88  [ProducesResponseType(StatusCodes.Status200OK)]
 89  [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
 90  public async Task<ActionResult> TestDeleteRealm(int id)
 091  {
 092    await _realmService.TestDeleteRealmAsync(id);
 093    return Ok();
 094  }
 95
 96  [HttpDelete("{id}")]
 97  [ProducesResponseType(StatusCodes.Status204NoContent)]
 98  [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
 99  [ProducesResponseType(StatusCodes.Status404NotFound)]
 100  public async Task<ActionResult> DeleteRealm(int id)
 0101  {
 0102    await _realmService.TestDeleteRealmAsync(id);
 0103    if (!await _realmService.DeleteRealmAsync(id))
 0104    {
 0105      return NotFound();
 106    }
 0107    return NoContent();
 0108  }
 109
 110  /*
 111   * Slam
 112   */
 113
 114  [HttpPost("{id}/Slam/SetGoal")]
 115  [ProducesResponseType(StatusCodes.Status204NoContent)]
 116  [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
 117  public async Task<ActionResult> SetGoalAsync(int id, RobotDofDto goal)
 0118  {
 0119    if (await _slamService.AddSlamCommandAsync(id, new RobotClientsSlamCommands
 0120    {
 0121      SetGoal = new RobotClientsDof
 0122      {
 0123        X = goal.X,
 0124        Y = goal.Y,
 0125        Rotation = goal.Rotation
 0126      }
 0127    }))
 0128    {
 0129      return NoContent();
 130    }
 0131    throw new LgdxValidation400Expection(nameof(id), $"The realm has no robot running SLAM or the realm does not exist."
 0132  }
 133
 134  [HttpPost("{id}/Slam/AbortGoal")]
 135  [ProducesResponseType(StatusCodes.Status204NoContent)]
 136  [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
 137  public async Task<ActionResult> AbortGoalAsync(int id)
 0138  {
 0139    if (await _slamService.AddSlamCommandAsync(id, new RobotClientsSlamCommands
 0140    {
 0141      AbortGoal = true
 0142    }))
 0143    {
 0144      return NoContent();
 145    }
 0146    throw new LgdxValidation400Expection(nameof(id), $"The realm has no robot running SLAM or the realm does not exist."
 0147  }
 148
 149  [HttpPost("{id}/Slam/RefreshMap")]
 150  [ProducesResponseType(StatusCodes.Status204NoContent)]
 151  [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
 152  public async Task<ActionResult> RefreshMapAsync(int id)
 0153  {
 0154    if (await _slamService.AddSlamCommandAsync(id, new RobotClientsSlamCommands
 0155    {
 0156      RefreshMap = true
 0157    }))
 0158    {
 0159      return NoContent();
 160    }
 0161    throw new LgdxValidation400Expection(nameof(id), $"The realm has no robot running SLAM or the realm does not exist."
 0162  }
 163
 164  [HttpPost("{id}/Slam/SaveMap")]
 165  [ProducesResponseType(StatusCodes.Status204NoContent)]
 166  [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
 167  public async Task<ActionResult> SaveMapAsync(int id)
 0168  {
 0169    if (await _slamService.AddSlamCommandAsync(id, new RobotClientsSlamCommands
 0170    {
 0171      SaveMap = true
 0172    }))
 0173    {
 0174      return NoContent();
 175    }
 0176    throw new LgdxValidation400Expection(nameof(id), $"The realm has no robot running SLAM or the realm does not exist."
 0177  }
 178
 179  [HttpPost("{id}/Slam/EmergencyStop")]
 180  [ProducesResponseType(StatusCodes.Status204NoContent)]
 181  [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
 182  public async Task<ActionResult> EmergencyStopAsync(int id, EnableDto enableDto)
 0183  {
 0184    var command = new RobotClientsSlamCommands();
 0185    if (enableDto.Enable)
 0186    {
 0187      command.SoftwareEmergencyStopEnable = true;
 0188    }
 189    else
 0190    {
 0191      command.SoftwareEmergencyStopDisable = true;
 0192    }
 193
 0194    if (await _slamService.AddSlamCommandAsync(id, command))
 0195    {
 0196      return NoContent();
 197    }
 0198    throw new LgdxValidation400Expection(nameof(id), $"The realm has no robot running SLAM or the realm does not exist."
 0199  }
 200
 201  [HttpPost("{id}/Slam/Abort")]
 202  [ProducesResponseType(StatusCodes.Status204NoContent)]
 203  public async Task<ActionResult> AbortSlamAsync(int id)
 0204  {
 0205    await _slamService.AddSlamCommandAsync(id, new RobotClientsSlamCommands
 0206    {
 0207      AbortSlam = true
 0208    });
 0209    return NoContent();
 210    // Don't throw exception to allow the UI continue
 0211  }
 212
 213  [HttpPost("{id}/Slam/Complete")]
 214  [ProducesResponseType(StatusCodes.Status204NoContent)]
 215  public async Task<ActionResult> CompleteSlamAsync(int id, RealmMapUpdateDto realmMapUpdateDto)
 0216  {
 0217    await _realmService.UpdateRealmMapAsync(id, realmMapUpdateDto.ToBusinessModel());
 0218    await _slamService.AddSlamCommandAsync(id, new RobotClientsSlamCommands
 0219    {
 0220      CompleteSlam = true
 0221    });
 0222    return NoContent();
 223    // Don't throw exception to allow the UI continue
 0224  }
 225}