HobbieUserController.cs 1.2 KB

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