GetAllTrainingsForCategory.cs 1.0 KB

123456789101112131415161718192021222324252627282930
  1. using ApiMobileBelov.BaseConnection;
  2. using ApiMobileBelov.ModelBase;
  3. using Microsoft.AspNetCore.Mvc;
  4. namespace ApiMobileBelov.Controllers
  5. {
  6. [Route("api/[controller]/{id}")]
  7. [ApiController]
  8. [ApiExplorerSettings(GroupName = "Тренировки")]
  9. public class GetAllTrainingsForCategory : ControllerBase
  10. {
  11. ApplicationContext db = new ApplicationContext();
  12. [HttpGet]
  13. public IActionResult Index(int id)
  14. {
  15. var allItems = db.tableCategoryAndTraining.Where(x=>x.IdCategory == id).ToList();
  16. var allTrainings = db.tableTraining.ToList();
  17. List<Training>? allTrainingsCurrent = new List<Training>();
  18. foreach (var item in allItems)
  19. {
  20. var element = allTrainings.FirstOrDefault(x => x.IdTraining == item.IdTraining);
  21. if (element != null)
  22. {
  23. allTrainingsCurrent.Add(element);
  24. }
  25. }
  26. return Ok(allTrainingsCurrent);
  27. }
  28. }
  29. }