Skip to content

Commit

Permalink
fix(aai.backend.g1a):commit before rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
tans1 committed Sep 2, 2023
1 parent 9b50503 commit 56e423b
Show file tree
Hide file tree
Showing 44 changed files with 490 additions and 222 deletions.
22 changes: 22 additions & 0 deletions aait/backend/group-1A/Application/Contracts/IUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


using SocialSync.Application.Contracts;

namespace Application.Contracts
{
public interface IUnitOfWork : IDisposable
{
IAuthRepository AuthRepository{get;}
ICommentRepository CommentRepository{get;}
ICommentReactionRepository CommentReactionRepository{get;}
IFollowRepository FollowRepository{get;}
INotificationRepository NotificationRepository{get;}
ITagRepository TagRepository{get;}
IPostRepository PostRepository{get;}
IPostReactionRepository PostReactionRepository{get;}
IUserRepository UserRepository{get;}

IPostTagRepository PostTagRepository{get;}
Task<int> Save();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.Net;
using Application.Contracts;
using Application.Dtos.Authentication;
using Application.Features.Authentication.Requests;
using AutoMapper;
Expand All @@ -12,15 +13,17 @@ namespace SocialSync.Application.Features.Authentication.Handlers.Commands;
public class RegisterUserRequestHandler : IRequestHandler<RegisterUserRequest, RegisterResponseDto>
{

readonly IAuthRepository _authRepository;
// readonly IAuthRepository _authRepository;
readonly IMapper _mapper;
readonly IJwtGenerator _jwtGenerator;
private readonly IUnitOfWork _unitOfWork;

public RegisterUserRequestHandler( IAuthRepository authRepository, IMapper mapper, IJwtGenerator jwtGenerator)
public RegisterUserRequestHandler(IUnitOfWork unitOfWork,IMapper mapper, IJwtGenerator jwtGenerator)
{
_authRepository = authRepository;
// _authRepository = authRepository;
_mapper = mapper;
_jwtGenerator = jwtGenerator;
_unitOfWork = unitOfWork;

}
public async Task<RegisterResponseDto> Handle(RegisterUserRequest request, CancellationToken cancellationToken)
Expand All @@ -30,7 +33,7 @@ public async Task<RegisterResponseDto> Handle(RegisterUserRequest request, Cance
var validator = new RegisterUserDtoValidator();
var validationResult = await validator.ValidateAsync(request.RegisterUserDto, cancellationToken);
// Check if user exists
if (await _authRepository.UserExists(request.RegisterUserDto.Email))
if (await _unitOfWork.AuthRepository.UserExists(request.RegisterUserDto.Email))
{
throw new Exception("User already exists");
}
Expand All @@ -39,12 +42,12 @@ public async Task<RegisterResponseDto> Handle(RegisterUserRequest request, Cance
var user = _mapper.Map<User>(request.RegisterUserDto);

// Register user
var registeredUser = await _authRepository.RegisterUser(user);
var registeredUser = await _unitOfWork.AuthRepository.RegisterUser(user);


// Generate token
var token = await _jwtGenerator.CreateTokenAsync(registeredUser);

await _unitOfWork.Save();

return new RegisterResponseDto
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
using SocialSync.Application.Features.Authentication.Requests;
using SocialSync.Domain.Entities;
using SocialSync.Application.Dto.Authentication.validator;
using Application.Contracts;

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

public class LoginUserRequestHanlder : IRequestHandler<LoginUserRequest,LoginResponseDto>{

readonly IAuthRepository _authRepository;
// readonly IAuthRepository _authRepository;
private readonly IUnitOfWork _unitOfWork;
readonly IMapper _mapper;
readonly IJwtGenerator _jwtGenerator;

public LoginUserRequestHanlder(IAuthRepository authRepository, IMapper mapper, IJwtGenerator jwtGenerator)
public LoginUserRequestHanlder(IUnitOfWork unitOfWork, IMapper mapper, IJwtGenerator jwtGenerator)
{
_authRepository = authRepository;
// _authRepository = authRepository;
_unitOfWork = unitOfWork;
_mapper = mapper;
_jwtGenerator = jwtGenerator;
}
Expand All @@ -29,17 +32,18 @@ public async Task<LoginResponseDto> Handle(LoginUserRequest request, Cancellatio
throw new ValidationException();
}
// Check if user exists
if (!await _authRepository.UserExists(request.LoginUserDto.Email))
if (!await _unitOfWork.AuthRepository.UserExists(request.LoginUserDto.Email))
{
throw new Exception("User does not exist!");
}

// Login user
var user = await _authRepository.LoginUser(_mapper.Map<User>(request.LoginUserDto));
var user = await _unitOfWork.AuthRepository.LoginUser(_mapper.Map<User>(request.LoginUserDto));

// Generate token
var token = await _jwtGenerator.CreateTokenAsync(user);

await _unitOfWork.Save();

return new LoginResponseDto
{
UserName = user.Username,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Application.Common;
using Application.Contracts;
using Application.DTO.CommentDTO.DTO;
using Application.DTO.CommentDTO.Validations;
Expand All @@ -7,6 +8,7 @@
using Application.Features.NotificationFeaure.Requests.Commands;
using Application.Response;
using AutoMapper;
using Domain.Entites;
using Domain.Entities;
using MediatR;

Expand All @@ -16,15 +18,18 @@ namespace Application.Features.CommentFeatures.Handlers.Commands
public class CommentCreateHandler : IRequestHandler<CommentCreateCommand, BaseResponse<CommentResponseDTO>>
{
private readonly IMapper _mapper;
private readonly ICommentRepository _commentRepository;
// private readonly ICommentRepository _commentRepository;

private readonly IPostRepository _postRepository;
// private readonly IPostRepository _postRepository;

public CommentCreateHandler(IMapper mapper, ICommentRepository commentRepository, IPostRepository postRepository)
private readonly IUnitOfWork _unitOfWork;

public CommentCreateHandler(IUnitOfWork unitOfWork, IMapper mapper)
{
_unitOfWork = unitOfWork;
_mapper = mapper;
_commentRepository = commentRepository;
_postRepository = postRepository;
// _commentRepository = commentRepository;
// _postRepository = postRepository;
}

public async Task<BaseResponse<CommentResponseDTO>> Handle(CommentCreateCommand request, CancellationToken cancellationToken)
Expand All @@ -39,17 +44,28 @@ public async Task<BaseResponse<CommentResponseDTO>> Handle(CommentCreateCommand
throw new ValidationException(validationResult);
}

var postExist = await _postRepository.Exists(request.NewCommentData.PostId);
var postExist = await _unitOfWork.PostRepository.Exists(request.NewCommentData.PostId);
if (!postExist)
{
throw new NotFoundException("Post with the Provided Id doesn't exist");
}

var newComment = _mapper.Map<Comment>(request.NewCommentData);
newComment.UserId = request.userId;
var result = await _commentRepository.Add(newComment);
var result = await _unitOfWork.CommentRepository.Add(newComment);


// notification
// var post = await _unitOfWork.PostRepository.Get(result.PostId);
// var notification = new Notification(){
// Content = "A Comment has been given on your post",
// NotificationContentId = result.PostId,
// UserId = post.UserId,
// Comment = true
// };

// await _unitOfWork.NotificationRepository.Add(notification);
await _unitOfWork.Save();
return new BaseResponse<CommentResponseDTO> {
Success = true,
Message = "Comment is created successfully",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Application.Features.CommentFeatures.Requests.Commands;
using Application.Features.NotificationFeaure.Requests.Commands;
using Application.Response;
using Domain.Entites;
using MediatR;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,24 +14,34 @@ namespace Application.Features.CommentFeatures.Handlers.Commands
{
public class CommentDeleteHandler : IRequestHandler<CommentDeleteCommand, BaseResponse<int>>
{
private readonly ICommentRepository _commentRepository;
// private readonly ICommentRepository _commentRepository;
private readonly IUnitOfWork _unitOfWork;

public CommentDeleteHandler(ICommentRepository commentRepository)
public CommentDeleteHandler(IUnitOfWork unitOfWork)
{
_commentRepository = commentRepository;
// _commentRepository = commentRepository;
_unitOfWork = unitOfWork;
}

public async Task<BaseResponse<int>> Handle(CommentDeleteCommand request, CancellationToken cancellationToken)
{

var comment = await _commentRepository.Get(request.Id);
var comment = await _unitOfWork.CommentRepository.Get(request.Id);
if (comment == null)
{
throw new NotFoundException("Comment is not found");
}

var result = await _commentRepository.Delete(comment);

var result = await _unitOfWork.CommentRepository.Delete(comment);
await _unitOfWork.Save();
// notification
// var notification = new Notification()
// {
// Content = "Comment is deleted",
// NotificationContentId = request.Id,
// UserId = post.UserId,
// Comment = true
// };

return new BaseResponse<int> {
Success = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ namespace Application.Features.CommentFeatures.Handlers.Commands
{
public class UpdateCommentHandler : IRequestHandler<UpdateCommentCommand, BaseResponse<CommentResponseDTO>>
{
private readonly ICommentRepository _commentRepository;
// private readonly ICommentRepository _commentRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;

public UpdateCommentHandler(ICommentRepository commentRepository, IMapper mapper)
public UpdateCommentHandler(IUnitOfWork unitOfWork, IMapper mapper)
{
_commentRepository = commentRepository;
// _commentRepository = commentRepository;
_unitOfWork = unitOfWork;
_mapper = mapper;
}
public async Task<BaseResponse<CommentResponseDTO>> Handle(UpdateCommentCommand request, CancellationToken cancellationToken)
Expand All @@ -33,7 +35,7 @@ public async Task<BaseResponse<CommentResponseDTO>> Handle(UpdateCommentCommand
{
throw new ValidationException(validationResult);
}
var comment = await _commentRepository.Get(request.Id);
var comment = await _unitOfWork.CommentRepository.Get(request.Id);

if (comment == null)
{
Expand All @@ -43,9 +45,10 @@ public async Task<BaseResponse<CommentResponseDTO>> Handle(UpdateCommentCommand

_mapper.Map(request.CommentData, comment);

var updationResult = await _commentRepository.Update(comment);
var updationResult = await _unitOfWork.CommentRepository.Update(comment);
var result = _mapper.Map<CommentResponseDTO>(updationResult);

await _unitOfWork.Save();



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,28 @@ namespace Application.Features.CommentFeatures.Handlers.Queries
{
public class GetPostsComment : IRequestHandler<GetCommentsForPostQuery, BaseResponse<List<CommentResponseDTO>>>
{
private readonly ICommentRepository _commentRepository;
// private readonly ICommentRepository _commentRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
private readonly IPostRepository _postRepository;
// private readonly IPostRepository _postRepository;

public GetPostsComment(ICommentRepository commentRepository, IMapper mapper, IPostRepository postRepository)
public GetPostsComment(IUnitOfWork unitOfWork, IMapper mapper)
{
_commentRepository = commentRepository;
// _commentRepository = commentRepository;
_mapper = mapper;
_postRepository = postRepository;
// _postRepository = postRepository;
_unitOfWork = unitOfWork;
}

public async Task<BaseResponse<List<CommentResponseDTO>>> Handle(GetCommentsForPostQuery request, CancellationToken cancellationToken)
{
var postExist = await _postRepository.Exists(request.PostId);
var postExist = await _unitOfWork.PostRepository.Exists(request.PostId);
if (!postExist)
{
throw new NotFoundException("Post with the Provided Id is not found");
}

var result = await _commentRepository.GetByPostId(request.PostId);
var result = await _unitOfWork.CommentRepository.GetByPostId(request.PostId);
if (result == null)
{
throw new BadRequestException("Comments are not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ namespace Application.Features.CommentFeatures.Handlers.Queries
{
public class GetCommentQueryHandler : IRequestHandler<GetSingleCommentQuery, BaseResponse<CommentResponseDTO>>
{
private readonly ICommentRepository _commentRepository;
// private readonly ICommentRepository _commentRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;

public GetCommentQueryHandler(ICommentRepository commentRepository, IMapper mapper)
public GetCommentQueryHandler(IUnitOfWork unitOfWork,IMapper mapper)
{
_commentRepository = commentRepository;
// _commentRepository = commentRepository;
_unitOfWork = unitOfWork;
_mapper = mapper;
}

public async Task<BaseResponse<CommentResponseDTO>> Handle(GetSingleCommentQuery request, CancellationToken cancellationToken)
{
var result = await _commentRepository.Get(request.Id);
var result = await _unitOfWork.CommentRepository.Get(request.Id);
if (result == null)
{
throw new BadRequestException("Comment is not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ namespace Application.Features.CommentReactionFeature.Handlers.Commands
{
public class MakeCommentReactionHandler : IRequestHandler<MakeReactionOnComment, BaseResponse<int>>
{
private readonly ICommentReactionRepository _commentReactionRespository;
// private readonly ICommentReactionRepository _commentReactionRespository;
private readonly IMapper _mapper;
private readonly ICommentRepository _commentRepository;
// private readonly ICommentRepository _commentRepository;
private readonly IUnitOfWork _unitOfWork;

public MakeCommentReactionHandler(ICommentReactionRepository commentReactionRepository, IMapper mapper, ICommentRepository commentRepository)
public MakeCommentReactionHandler(IMapper mapper, IUnitOfWork unitOfWork)
{
_commentReactionRespository = commentReactionRepository;
// _commentReactionRespository = commentReactionRepository;
_mapper = mapper;
_commentRepository = commentRepository;
// _commentRepository = commentRepository;
_unitOfWork = unitOfWork;
}

public async Task<BaseResponse<int>> Handle(MakeReactionOnComment request, CancellationToken cancellationToken)
Expand All @@ -33,7 +35,7 @@ public async Task<BaseResponse<int>> Handle(MakeReactionOnComment request, Cance
throw new ValidationException(validationResult);
}

var exists = await _commentRepository.Exists(request.ReactionData.ReactedId);
var exists = await _unitOfWork.CommentRepository.Exists(request.ReactionData.ReactedId);
if (exists == false)
{
throw new NotFoundException("Comment is not found to make the Reactions");
Expand All @@ -57,7 +59,7 @@ public async Task<BaseResponse<int>> Handle(MakeReactionOnComment request, Cance



var result = await _commentReactionRespository.MakeReaction(request.UserId, commentReaction);
var result = await _unitOfWork.CommentReactionRepository.MakeReaction(request.UserId, commentReaction);
if (result == null)
{
throw new BadRequestException("Comment is not found");
Expand Down
Loading

0 comments on commit 56e423b

Please sign in to comment.