Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Application/Features/Auth/Interfaces/IUserService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Application.Features.Auth.DTOs;
using Domain.Enums;

namespace Application.Features.Auth.Interfaces;

public interface IUserService
{
Task<string> RegisterUserAsync(RegisterUserRequestDto registerUserRequestDto);
Task<LoginUserResponseDto> LoginUserAsync(LoginUserRequestDto loginUserRequestDto);
Task ChangeRoleTo(UserRole role);
Task<string> LogoutUserAsync();
Task<LoginUserResponseDto> RefreshTokenAsync(string refreshToken);
Task<ProfileResponseDto> ViewProfileAsync();
Expand Down
8 changes: 8 additions & 0 deletions Application/Features/Auth/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Application.Features.Auth.DTOs;
using Application.Features.Auth.Interfaces;
using Domain.Entities;
using Domain.Enums;

namespace Application.Features.Auth.Services;

Expand Down Expand Up @@ -68,6 +69,13 @@ public async Task<LoginUserResponseDto> LoginUserAsync(LoginUserRequestDto login
return new LoginUserResponseDto(token, refreshTokenValue);
}

public async Task ChangeRoleTo(UserRole role)
{
var userId =_userContext.UserId;
var user =await _userRepository.GetUserByIdAsync(userId);
user?.ChangeRoleTo(role);
}

public async Task<string> LogoutUserAsync()
{
var userId = _userContext.UserId;
Expand Down
26 changes: 24 additions & 2 deletions Application/Features/Tenant/Services/TenantService.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
using Application.Common.Interfaces;
using Application.Features.Auth.Interfaces;
using Application.Features.Auth.Services;
using Application.Features.Tenant.DTO_s;
using Application.Features.Tenant.Interfaces;
using Domain.Entities;
using Domain.Enums;
using Domain.Exceptions;
using Microsoft.Extensions.Configuration;

namespace Application.Features.Tenant.Services;

public class TenantService:TenantServiceContract
{
private readonly TenantRepositoryContract _tenantRepository;
private readonly IUSerContext _userContext;
private readonly IUserRepository _userRepository;

public TenantService(TenantRepositoryContract tenantRepository)
public TenantService(TenantRepositoryContract tenantRepository, IUSerContext userContext, IUserRepository userRepository)
{
_tenantRepository = tenantRepository;
_userContext = userContext;
_userRepository = userRepository;
}

public async Task<string> Register(RegisterTenantDTO dto)
{
//Todo: slug maker
// Todo : Date manager
// Todo : if for duplicate slug
var tenant = new Domain.Entities.Tenant(dto.Name,dto.Slug,dto.SubscriptionExpireDate);

var userId=_userContext.UserId;
var user = await _userRepository.GetUserByIdAsync(userId);

if(user==null)
throw new NotFoundException("User not found");

await _tenantRepository.RegisterTenant(tenant);
user.ChangeRoleTo(UserRole.Admin);
user.AssignTenantToUser(tenant.Id);
await _tenantRepository.SaveAsync();
return $"{tenant.Name} Registered";

return $"{tenant.Name} Registered belong to user {user.FullName} created";
}
}
8 changes: 8 additions & 0 deletions Domain/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public ICollection<RefreshToken> RefreshTokens { get; private set; } = new List<RefreshToken>();
public ICollection<Appointment> Appointments { get; private set; } = new List<Appointment>();

public User()

Check warning on line 21 in Domain/Entities/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Password' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 21 in Domain/Entities/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'PhoneNumber' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 21 in Domain/Entities/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Email' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 21 in Domain/Entities/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'FullName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
}

Expand All @@ -42,6 +42,14 @@
Password = password;
}

public void AssignTenantToUser(Guid tenantId)
{
TenantId = tenantId;
}
public void ChangeRoleTo(UserRole role)
{
Role = role;
}
public void UpdateProfile(string fullName, string phoneNumber)
{
FullName = fullName;
Expand Down
2 changes: 2 additions & 0 deletions api/Controllers/TenantController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Application.Features.Tenant.DTO_s;
using Application.Features.Tenant.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace api.Controllers;
Expand All @@ -15,6 +16,7 @@ public TenantController(TenantServiceContract tenantService)
}

[HttpPost]
[Authorize(Roles = "User,Admin")]
public async Task<IActionResult> RegisterTenant([FromBody] RegisterTenantDTO dto)
{
// Todo: expire date base on plan
Expand Down
Loading