HobbiUsersController.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Microsoft.AspNetCore.Mvc;
  2. namespace API_development.Controllers
  3. {
  4. [Route("/api/[controller]")]
  5. [ApiController]
  6. [ApiExplorerSettings(GroupName = "Хобби пользователя")]
  7. public class HobbiUsersController : ControllerBase
  8. {
  9. ApplicationContext db = new ApplicationContext();
  10. [HttpGet]
  11. public IActionResult Index(string hobbie)
  12. {
  13. List<Users> users = new List<Users>();
  14. List<Hobbies> hobbies = db.tbHobbies.Where(tb => tb.name == hobbie).ToList();
  15. List<UsersHobbies> usersHobbies = db.tbUserHobbies.ToList();
  16. foreach (UsersHobbies elem in usersHobbies)
  17. {
  18. foreach (Hobbies hobbi in hobbies)
  19. {
  20. if (elem.idHobbi == hobbi.idHobbi)
  21. {
  22. Users user = db.tbUsers.Where(tb => tb.idUsers == elem.idUser).FirstOrDefault();
  23. if (user != null)
  24. {
  25. users.Add(user);
  26. }
  27. }
  28. }
  29. }
  30. return Ok(users);
  31. }
  32. }
  33. }