Skip to content

Commit

Permalink
Możliwość wybierania tagów
Browse files Browse the repository at this point in the history
  • Loading branch information
protectedvoid21 committed Oct 13, 2024
1 parent 2275674 commit c5b1904
Show file tree
Hide file tree
Showing 28 changed files with 1,632 additions and 58 deletions.
53 changes: 26 additions & 27 deletions src/BHC24.Api/Controllers/SearchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BHC24.Api.Models;
using BHC24.Api.Models.Projects;
using BHC24.Api.Persistence;
using BHC24.Api.Persistence.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

Expand All @@ -19,15 +20,37 @@ public SearchController(BhcDbContext dbContext)
}

[HttpGet("projectByName")]
public async Task<Result<PaginationResponse<GetProjectResponse>>> SearchProjectNameAsync([FromQuery] string projectName, [FromQuery] PaginationRequest pagination, CancellationToken ct)
public async Task<Result<PaginationResponse<GetProjectResponse>>> SearchProjects(
[FromQuery] string projectName,
[FromQuery] string[]? tagNames,
[FromQuery] string? ownerName,
[FromQuery] PaginationRequest pagination,
CancellationToken ct)
{
var paginatedProjects = await _dbContext.Projects
.Where(x => x.Title.ToLower().Contains(projectName.ToLower()))
IQueryable<Project> projects = _dbContext.Projects;

if(!string.IsNullOrEmpty(projectName))
{
projects = projects.Where(p => p.Title.ToLower().Contains(projectName.ToLower()));
}

if(tagNames is { Length: > 0 })
{
projects = projects.Where(p => p.Tags.Any(t => tagNames.Contains(t.Name)));
}

if(!string.IsNullOrEmpty(ownerName))
{
projects = projects.Where(p => p.Owner.Name.ToLower().Contains(ownerName.ToLower()));
}

var paginatedProjects = await projects
.Select(p => new GetProjectResponse
{
Title = p.Title,
Description = p.Description,
Owner = p.Owner.Name,
GithubUrl = p.GithubRepositoryUrl,
Tags = p.Tags,
Collaborators = p.Collaborators
})
Expand All @@ -36,28 +59,4 @@ public async Task<Result<PaginationResponse<GetProjectResponse>>> SearchProjectN

return Result.Ok(paginatedProjects);
}

[HttpGet("searchProjectsByTags")]
public async Task<Result<PaginationResponse<GetProjectResponse>>> SearchProjectByTagsAsync([FromQuery] PaginationRequest request,
[FromQuery] string[] tagNames, CancellationToken ct)
{
var projects = await _dbContext.Projects.Where(p => p.Tags.Any(t => tagNames.Contains(t.Name)))
.Select(p => new GetProjectResponse
{
Title = p.Title,
Description = p.Description,
Owner = p.Owner.Name,
GithubUrl = p.GithubRepositoryUrl,
Tags = p.Tags,
Collaborators = p.Collaborators
})
.PaginateAsync(request, ct);

if(projects.TotalCount == 0)
{
return Result<PaginationResponse<GetProjectResponse>>.NotFound("No projects found");
}

return Result.Ok(projects);
}
}
35 changes: 35 additions & 0 deletions src/BHC24.Api/Controllers/TagController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using BHC24.Api.Models;
using BHC24.Api.Persistence;
using BHC24.Api.Persistence.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace BHC24.Api.Controllers;

[Route("api/[controller]")]
[ApiController, AllowAnonymous]
public class TagController : ControllerBase
{
private readonly BhcDbContext _dbContext;

public TagController(BhcDbContext dbContext)
{
_dbContext = dbContext;
}

[HttpGet]
public async Task<Result<List<TagResponse>>> GetTagsAsync(CancellationToken ct)
{
var tags = await _dbContext.Tags
.Select(t => new TagResponse
{
Id = t.Id,
Name = t.Name,
ImagePath = t.ImagePath
})
.ToListAsync(ct);

return Result.Ok(tags);
}
}
Loading

0 comments on commit c5b1904

Please sign in to comment.