namespace WebApplication1.Controllers { [ApiController] public class ExerciseController : ControllerBase { private readonly ILogger<ExerciseController> _logger; private readonly Models.APIContext context = new Models.APIContext(); public ExerciseController(ILogger<ExerciseController> logger) { _logger = logger; } // GET: api/v1/<ExerciseController> [HttpGet] [Route("api/v1/[controller]")] public async Task<IEnumerable<WebAPI.APIExercise>> Get() { List<WebAPI.APIExercise> exercises = new List<WebAPI.APIExercise>(); List<Models.ModelExercise> modelExercises = await context.ModelExercises.ToListAsync(); foreach (Models.ModelExercise modelExercise in modelExercises) { WebAPI.APIExercise exercise = new WebAPI.APIExercise() { Id = modelExercise.Id.ToString(), Name = modelExercise.Name, Filename = modelExercise.FileName, ServerIpAddress = modelExercise.ServerIpAddress }; exercises.Add(exercise); } return exercises; } // GET api/v1/<ExerciseController>/5 [HttpGet] [Route("api/v1/[controller]/{id}")] public async Task<WebAPI.APIExercise> Get(string id) { bool result = Int32.TryParse(id, out int intId); if (result) { Models.ModelExercise modelExercise = await context.ModelExercises.FirstOrDefaultAsync(ex => ex.Id == intId); if (modelExercise != null) { return new WebAPI.APIExercise() { Id = modelExercise.Id.ToString(), Name = modelExercise.Name, Filename = modelExercise.FileName, ServerIpAddress = modelExercise.ServerIpAddress }; } } return null; } } }
namespace WebApplication1.Hubs { public class ExerciseHub : Hub<IAPIClient> { private readonly Controllers.ExerciseController _exerciseController; public ExerciseHub(Controllers.ExerciseController exerciseController) { _exerciseController = exerciseController; } public async Task<IEnumerable<WebAPI.APIExercise>> Get() { return await _exerciseController.Get(); } } }
public async Task<IEnumerable<WebAPI.APIExercise>> Get() { return await context.ModelExercises .ProjectTo<WebAPI.APIExercise>(Mapper.ConfigurationProvider) .ToListAsync(); } public async Task<WebAPI.APIExercise> Get(string id) { bool result = Int32.TryParse(id, out int intId); if (!result) return null; return await context.ModelExercises .ProjectTo<WebAPI.APIExercise>(Mapper.ConfigurationProvider) .FirstOrDefaultAsync(ex => ex.Id == intId); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)