Skip to content

Commit eb08e9b

Browse files
UserTeam Feature Added
1 parent 733e523 commit eb08e9b

5 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Taskify.Application.DTOs.UserTeamDTOs;
5+
using Taskify.Application.ResultPattern;
6+
using Taskify.Application.Services.UserTeamServices;
7+
using Taskify.Domain.Const;
8+
9+
namespace Taskify.Api.Controllers
10+
{
11+
[Route("api/[controller]")]
12+
[ApiController]
13+
public class UserTeamController : ControllerBase
14+
{
15+
private readonly IUserTeamService _userTeamService;
16+
public UserTeamController(IUserTeamService userTeamService)
17+
{
18+
_userTeamService = userTeamService;
19+
}
20+
[Authorize(Roles = $"{UserRole.Admin}, {UserRole.Moderator}")]
21+
[HttpPost]
22+
public async Task<Result<UserTeamDTO>> CreateAsync([FromBody] UserTeamDTO userTeamDTO)
23+
{
24+
return await _userTeamService.AddUserToTeamAsync(userTeamDTO);
25+
}
26+
27+
[Authorize(Roles = $"{UserRole.Admin}, {UserRole.Moderator}")]
28+
[HttpDelete]
29+
public async Task<Result<UserTeamDTO>> DeleteAsync([FromBody] UserTeamDTO userTeamDTO)
30+
{
31+
return await _userTeamService.RemoveUserFromTeamAsync(userTeamDTO);
32+
}
33+
34+
[Authorize]
35+
[HttpGet()]
36+
public async Task<Result<List<UserTeamDTO>>> GetUserTeamsAsync([FromQuery] Guid? userId)
37+
{
38+
return await _userTeamService.GetUserTeamsAsync(userId);
39+
}
40+
}
41+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Taskify.Application.DTOs.UserTeamDTOs
6+
{
7+
public class UserTeamDTO
8+
{
9+
public Guid UserId { get; set; }
10+
public int TeamId { get; set; }
11+
}
12+
}

Taskify.Application/DependencyInjection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Taskify.Application.Helpers;
1010
using Taskify.Application.Services.AccountServices;
1111
using Taskify.Application.Services.TeamServices;
12+
using Taskify.Application.Services.UserTeamServices;
1213
using Taskify.Domain.Entities;
1314
using Taskify.Infrastructure.Contexts;
1415

@@ -53,6 +54,7 @@ public static IServiceCollection AddApplication(this IServiceCollection services
5354

5455
services.AddScoped<IAccountService, AccountService>();
5556
services.AddScoped<ITeamService, TeamService>();
57+
services.AddScoped<IUserTeamService, UserTeamService>();
5658
return services;
5759
}
5860
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Taskify.Application.DTOs.UserTeamDTOs;
5+
using Taskify.Application.ResultPattern;
6+
using Taskify.Domain.Entities;
7+
8+
namespace Taskify.Application.Services.UserTeamServices
9+
{
10+
public interface IUserTeamService
11+
{
12+
Task<Result<UserTeamDTO>> AddUserToTeamAsync(UserTeamDTO userTeamDTO);
13+
Task<Result<UserTeamDTO>> RemoveUserFromTeamAsync(UserTeamDTO userTeamDTO);
14+
Task<Result<List<UserTeamDTO>>> GetUserTeamsAsync(Guid? userId);
15+
}
16+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)