-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from Alper-Soy/develop
Prod - 08/26/2024
- Loading branch information
Showing
40 changed files
with
1,096 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Application.Features.Followers.Commands.FollowToggle; | ||
using Application.Features.Followers.Queries.Follow; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace API.Controllers; | ||
|
||
public class FollowController : BaseApiController | ||
{ | ||
[HttpPost("{username}")] | ||
public async Task<IActionResult> Follow(string username) | ||
{ | ||
return HandleResult(await Mediator.Send(new FollowToggleCommand | ||
{ TargetUsername = username })); | ||
} | ||
|
||
[HttpGet("{username}")] | ||
public async Task<IActionResult> GetFollowings(string username, string predicate) | ||
{ | ||
return HandleResult(await Mediator.Send(new GetFollowQuery { Username = username, Predicate = predicate })); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
Application/Features/Followers/Commands/FollowToggle/FollowToggleCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Application.Core; | ||
using MediatR; | ||
|
||
namespace Application.Features.Followers.Commands.FollowToggle; | ||
|
||
public class FollowToggleCommand : IRequest<Result<Unit>> | ||
{ | ||
public string TargetUsername { get; set; } | ||
} |
44 changes: 44 additions & 0 deletions
44
Application/Features/Followers/Commands/FollowToggle/FollowToggleHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Application.Core; | ||
using Application.Interfaces; | ||
using Domain.Entities; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Persistence; | ||
|
||
namespace Application.Features.Followers.Commands.FollowToggle; | ||
|
||
public class FollowToggleHandler(DataContext context, IUserAccessor userAccessor) | ||
: IRequestHandler<FollowToggleCommand, Result<Unit>> | ||
{ | ||
public async Task<Result<Unit>> Handle(FollowToggleCommand request, CancellationToken cancellationToken) | ||
{ | ||
var observer = await context.Users.FirstOrDefaultAsync(x => | ||
x.UserName == userAccessor.GetUsername()); | ||
|
||
var target = await context.Users.FirstOrDefaultAsync(x => | ||
x.UserName == request.TargetUsername); | ||
|
||
if (target == null) return null; | ||
|
||
var following = await context.UserFollowings.FindAsync(observer.Id, target.Id); | ||
|
||
if (following == null) | ||
{ | ||
following = new UserFollowing | ||
{ | ||
Observer = observer, | ||
Target = target | ||
}; | ||
|
||
context.UserFollowings.Add(following); | ||
} | ||
else | ||
{ | ||
context.UserFollowings.Remove(following); | ||
} | ||
|
||
var success = await context.SaveChangesAsync() > 0; | ||
|
||
return success ? Result<Unit>.Success(Unit.Value) : Result<Unit>.Failure("Failed to update following"); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Application/Features/Followers/Queries/Follow/GetFollowHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Application.Core; | ||
using Application.Features.Profiles.Contracts; | ||
using Application.Interfaces; | ||
using AutoMapper; | ||
using AutoMapper.QueryableExtensions; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Persistence; | ||
|
||
namespace Application.Features.Followers.Queries.Follow; | ||
|
||
public class GetFollowHandler(DataContext context, IMapper mapper, IUserAccessor userAccessor) | ||
: IRequestHandler<GetFollowQuery, Result<List<ProfileDto>>> | ||
{ | ||
public async Task<Result<List<ProfileDto>>> Handle(GetFollowQuery request, CancellationToken cancellationToken) | ||
{ | ||
var profiles = new List<ProfileDto>(); | ||
|
||
switch (request.Predicate) | ||
{ | ||
case "followers": | ||
profiles = await context.UserFollowings.Where(x => x.Target.UserName == request.Username) | ||
.Select(u => u.Observer) | ||
.ProjectTo<ProfileDto>(mapper.ConfigurationProvider, | ||
new { currentUsername = userAccessor.GetUsername() }) | ||
.ToListAsync(); | ||
break; | ||
case "following": | ||
profiles = await context.UserFollowings.Where(x => x.Observer.UserName == request.Username) | ||
.Select(u => u.Target) | ||
.ProjectTo<ProfileDto>(mapper.ConfigurationProvider, | ||
new { currentUsername = userAccessor.GetUsername() }) | ||
.ToListAsync(); | ||
break; | ||
} | ||
|
||
return Result<List<ProfileDto>>.Success(profiles); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Application/Features/Followers/Queries/Follow/GetFollowQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Application.Core; | ||
using Application.Features.Profiles.Contracts; | ||
using MediatR; | ||
|
||
namespace Application.Features.Followers.Queries.Follow; | ||
|
||
public class GetFollowQuery : IRequest<Result<List<ProfileDto>>> | ||
{ | ||
public string Predicate { get; set; } | ||
public string Username { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.