Skip to content

Commit

Permalink
feat(AAiT-backend-2B): Add Feeds Feature (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
petrosbeyene authored Sep 4, 2023
1 parent 224ae2a commit 80047cf
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 26 deletions.
2 changes: 1 addition & 1 deletion aait/backend/group-2B/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
<PackageReference Include="MoonSharp" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj"/>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace SocialSync.Application.Contracts.Persistence;
public interface IPostRepository : IGenericRepository<Post>
{
public Task<IReadOnlyList<Post>> GetPostsByUserIdAsync(int userId);
public Task<IReadOnlyList<Post>> GetFeedsByUserIdAsync(int userID);
public Task<IReadOnlyList<Post>> GetPostsByTagsAsync(List<string> tags);
public Task<bool> ExistsAsync(int id);
public Task LikePostAsync(int id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using AutoMapper;
using MediatR;
using SocialSync.Application.Common.Responses;
using SocialSync.Application.Contracts.Persistence;
using SocialSync.Application.DTOs.PostDtos;
using SocialSync.Application.Features.Feed.Requests.Queries;

namespace SocialSync.Application.Features.Feed.Handlers.Queries;

public class GetFeedsByUserIdRequestHandler : IRequestHandler<GetFeedsByUserIdRequest, CommonResponse<List<PostDto>>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
public GetFeedsByUserIdRequestHandler(IUnitOfWork unitOfWork, IMapper mapper){
_unitOfWork = unitOfWork;
_mapper = mapper;
}

public async Task<CommonResponse<List<PostDto>>> Handle(GetFeedsByUserIdRequest request, CancellationToken cancellationToken){
var feeds = await _unitOfWork.PostRepository.GetFeedsByUserIdAsync(request.UserID);
if(feeds == null){
CommonResponse<List<PostDto>>.Failure("Feeds Not Found!");
}

return CommonResponse<List<PostDto>>.Success(_mapper.Map<List<PostDto>>(feeds));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MediatR;
using SocialSync.Application.Common.Responses;
using SocialSync.Application.DTOs.PostDtos;

namespace SocialSync.Application.Features.Feed.Requests.Queries;

public class GetFeedsByUserIdRequest : IRequest<CommonResponse<List<PostDto>>>
{
public int UserID;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public async Task<IReadOnlyList<Post>> GetPostsByUserIdAsync(int userId)
return postsByUser;
}

public async Task<IReadOnlyList<Post>> GetFeedsByUserIdAsync(int userID)
{
var feedsForUser = await _dbContext.Posts
.Where(p => p.User.Followers.Any(f => f.Id == userID) || p.Interactions.Any(i => i.User.Followers.Any(f => f.Id == userID)))
.ToListAsync();

return feedsForUser;
}

public async Task<bool> ExistsAsync(int id)
{
var post = await _dbContext.Posts
Expand Down
28 changes: 28 additions & 0 deletions aait/backend/group-2B/WebApi/Controllers/FeedsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SocialSync.Application.Features.Feed.Requests.Queries;

namespace SocialSync.WebApi.Controllers;

[Route("api/[controller]")]
[ApiController]
public class FeedsController : ControllerBase
{
private readonly IMediator _mediator;
public FeedsController(IMediator mediator){
_mediator = mediator;
}

[HttpGet]
public async Task<IActionResult> GetFeeds(int userID){
var feedsRequest = new GetFeedsByUserIdRequest {UserID = userID};
var response = await _mediator.Send(feedsRequest);

if(response.IsSuccess){
return Ok(response);
}
else{
return BadRequest(response);
}
}
}
45 changes: 20 additions & 25 deletions aait/backend/group-2B/WebApi/WebApi.csproj
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="11.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../Persistence/Persistence.csproj" />
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
<ProjectReference Include="..\Application\Application.csproj" />
</ItemGroup>


<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MediatR" Version="11.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Persistence/Persistence.csproj" />
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
<ProjectReference Include="..\Application\Application.csproj" />
</ItemGroup>
</Project>

0 comments on commit 80047cf

Please sign in to comment.