-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,49 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using ReasnAPI.Services; | ||
using System.Net.Http; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
Check warning on line 8 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs GitHub Actions / dotnet-tests (ubuntu-latest)
|
||
using ReasnAPI.Models.Database; | ||
|
||
|
||
namespace ReasnAPI.Controllers; | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
public class UsersController : ControllerBase | ||
{ | ||
private readonly RecomendationService _recomendationService; | ||
private readonly UserService _userService; | ||
private readonly HttpClient _httpClient; | ||
Check warning on line 20 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs GitHub Actions / dotnet-tests (ubuntu-latest)
Check warning on line 20 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs GitHub Actions / dotnet-tests (macos-latest)
|
||
|
||
public UsersController(RecomendationService recomendationService, UserService userService, HttpClient httpClient) | ||
{ | ||
_recomendationService = recomendationService; | ||
_userService = userService; | ||
_httpClient = httpClient; | ||
} | ||
|
||
[HttpGet] | ||
[Route("{username}")] | ||
public IActionResult GetUserByUsername(string username) | ||
Check warning on line 31 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs GitHub Actions / dotnet-tests (ubuntu-latest)
Check warning on line 31 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs GitHub Actions / dotnet-tests (macos-latest)
|
||
{ | ||
throw new NotImplementedException(); | ||
var user =_userService.GetUserByUsername(username); | ||
return Ok(user); | ||
} | ||
|
||
[HttpGet] | ||
[Route("{username}/recomendetevents")] | ||
public async Task<IActionResult> GetRecomendetEvents(string username) | ||
{ | ||
var currentUser = _userService.GetUserByUsername(username); | ||
var interests = currentUser.Interests; | ||
|
||
// Poprawka: Dodanie await i poprawienie b³êdu w nazwie metody | ||
var events = await _recomendationService.GetEventsByInterest(interests); | ||
Check warning on line 45 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs GitHub Actions / dotnet-tests (ubuntu-latest)
Check warning on line 45 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs GitHub Actions / dotnet-tests (macos-latest)
|
||
|
||
return Ok(events); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using ReasnAPI.Models.Database; | ||
using ReasnAPI.Models.DTOs; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using ReasnAPI.Mappers; | ||
|
||
namespace ReasnAPI.Services | ||
{ | ||
public class RecomendationService | ||
{ | ||
private readonly HttpClient httpClient; | ||
private readonly ReasnContext context; | ||
private readonly string flaskApiUrl; | ||
|
||
public RecomendationService(HttpClient httpClient, ReasnContext context, IConfiguration configuration) | ||
{ | ||
this.httpClient = httpClient; | ||
this.context = context; | ||
this.flaskApiUrl = $"{configuration.GetValue<string>("FlaskApi:BaseUrl")}/similar-tags"; | ||
} | ||
|
||
public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> interestsDto) | ||
{ | ||
var interests = interestsDto.Select(i => i.Interest.Name).ToList(); | ||
|
||
try | ||
{ | ||
var response = await httpClient.PostAsJsonAsync(flaskApiUrl, interests); | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
throw new HttpRequestException( | ||
$"Error fetching tags from Flask API. Status code: {response.StatusCode}"); | ||
} | ||
|
||
var tagNames = await response.Content.ReadFromJsonAsync<List<string>>(); | ||
|
||
if (tagNames == null || tagNames.Count == 0) | ||
{ | ||
return new List<EventDto>(); | ||
} | ||
|
||
var events = await context.Events | ||
.Include(e => e.Tags).Include(e => e.Parameters) | ||
.Where(e => e.Tags.Any(t => tagNames.Contains(t.Name))) | ||
.ToListAsync(); | ||
|
||
return events.ToDtoList(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Exception occurred while fetching events: {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
} | ||
} |