|
| 1 | +using Microsoft.AspNetCore.Identity; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Text; |
| 6 | +using Taskify.Application.DTOs.UserTeamDTOs; |
| 7 | +using Taskify.Application.ResultPattern; |
| 8 | +using Taskify.Application.Services.AccountServices; |
| 9 | +using Taskify.Domain.Entities; |
| 10 | +using Taskify.Domain.Repositories; |
| 11 | + |
| 12 | +namespace Taskify.Application.Services.UserTeamServices |
| 13 | +{ |
| 14 | + public class UserTeamService : IUserTeamService |
| 15 | + { |
| 16 | + private readonly IUnitOfWork _unitOfWork; |
| 17 | + private readonly IAccountService _accountService; |
| 18 | + private readonly UserManager<ApplicationUser> _userManager; |
| 19 | + public UserTeamService(IUnitOfWork unitOfWork, IAccountService accountService, UserManager<ApplicationUser> userManager) |
| 20 | + { |
| 21 | + _unitOfWork = unitOfWork; |
| 22 | + _accountService = accountService; |
| 23 | + _userManager = userManager; |
| 24 | + } |
| 25 | + |
| 26 | + #region Add user to team |
| 27 | + public async Task<Result<UserTeamDTO>> AddUserToTeamAsync(UserTeamDTO userTeamDTO) |
| 28 | + { |
| 29 | + try |
| 30 | + { |
| 31 | + var user = await _userManager.Users.SingleOrDefaultAsync(u => u.Id == userTeamDTO.UserId); |
| 32 | + if (user == null) |
| 33 | + { |
| 34 | + return Result<UserTeamDTO>.Failure("User does not exist"); |
| 35 | + } |
| 36 | + var team = await _unitOfWork.TeamRepository.GetByIdAsync(userTeamDTO.TeamId); |
| 37 | + if (team == null) |
| 38 | + { |
| 39 | + return Result<UserTeamDTO>.Failure("Team does not exist"); |
| 40 | + } |
| 41 | + var userTeam = new UserTeam |
| 42 | + { |
| 43 | + UserId = userTeamDTO.UserId, |
| 44 | + TeamId = userTeamDTO.TeamId |
| 45 | + }; |
| 46 | + await _unitOfWork.UserTeamRepository.AddAsync(userTeam); |
| 47 | + await _unitOfWork.SaveChangesAsync(); |
| 48 | + return Result<UserTeamDTO>.Success(); |
| 49 | + } |
| 50 | + catch (Exception ex) |
| 51 | + { |
| 52 | + return Result<UserTeamDTO>.Failure($"an error occured, {ex}"); |
| 53 | + } |
| 54 | + } |
| 55 | + #endregion |
| 56 | + |
| 57 | + #region Remove user from team |
| 58 | + public async Task<Result<UserTeamDTO>> RemoveUserFromTeamAsync(UserTeamDTO userTeamDTO) |
| 59 | + { |
| 60 | + try |
| 61 | + { |
| 62 | + var userTeam = await _unitOfWork.UserTeamRepository |
| 63 | + .GetSingleAsync(u => u.UserId== userTeamDTO.UserId && u.TeamId == userTeamDTO.TeamId); |
| 64 | + if (userTeam == null) |
| 65 | + { |
| 66 | + return Result<UserTeamDTO>.Failure($"Invalid input"); |
| 67 | + } |
| 68 | + _unitOfWork.UserTeamRepository.Delete(userTeam); |
| 69 | + await _unitOfWork.SaveChangesAsync(); |
| 70 | + return Result<UserTeamDTO>.Success(); |
| 71 | + } |
| 72 | + catch (Exception ex) |
| 73 | + { |
| 74 | + return Result<UserTeamDTO>.Failure($"an error occured, {ex}"); |
| 75 | + } |
| 76 | + } |
| 77 | + #endregion |
| 78 | + |
| 79 | + #region Get User Teams |
| 80 | + public async Task<Result<List<UserTeamDTO>>> GetUserTeamsAsync(Guid? userId) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + userId ??= _accountService.GetCurrentUserId().Result.Data; |
| 85 | + var userTeams = await _unitOfWork.UserTeamRepository |
| 86 | + .GetAll(u => u.UserId == userId) |
| 87 | + .Select(ut => new UserTeamDTO {UserId = ut.UserId, TeamId = ut.TeamId }) |
| 88 | + .ToListAsync(); |
| 89 | + if(userTeams is null) |
| 90 | + return Result<List<UserTeamDTO>>.Failure($"Couldn't fetch the user Teams"); |
| 91 | + return Result<List<UserTeamDTO>>.Success(userTeams); |
| 92 | + } |
| 93 | + catch(Exception ex) { |
| 94 | + return Result<List<UserTeamDTO>>.Failure($"an error occured, {ex}"); |
| 95 | + } |
| 96 | + } |
| 97 | + #endregion |
| 98 | + |
| 99 | + } |
| 100 | +} |
0 commit comments