Skip to content

Commit

Permalink
Merge pull request #79 from LinThitHtwe/dev
Browse files Browse the repository at this point in the history
[Add] Admin Create Endpoint
  • Loading branch information
LinThitHtwe authored Sep 12, 2024
2 parents 1cc251f + 7da07c2 commit 4756362
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 19 deletions.
30 changes: 30 additions & 0 deletions REMS.BackendApi/Features/Admin/AdminController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Authorization;

namespace REMS.BackendApi.Features.Admin;

[Route("api/v1/admins")]
[ApiController]
[Authorize(Roles = "Admin")]
public class AdminController : ControllerBase
{
private readonly BL_Admin _blAdmin;

public AdminController(BL_Admin blAdmin)
{
_blAdmin = blAdmin;
}

[HttpPost]
public async Task<IActionResult> CreateAdmin(AdminRequestModel adminRequest)
{
try
{
var response = await _blAdmin.CreateAdmin(adminRequest);
return Ok(response);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
2 changes: 2 additions & 0 deletions REMS.BackendApi/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
global using REMS.Models.Authentication;
global using REMS.Models.Jwt;
global using REMS.Models.Transaction;
global using REMS.Models.Admin;
global using REMS.Modules.Features.Admin;
global using REMS.Modules.Features.Agent;
global using REMS.Modules.Features.Client;
global using REMS.Modules.Features.Property;
Expand Down
2 changes: 2 additions & 0 deletions REMS.BackendApi/ModularService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static WebApplicationBuilder AddDbService(this WebApplicationBuilder buil

public static WebApplicationBuilder AddDataAccessService(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<DA_Admin>();
builder.Services.AddScoped<DA_Agent>();
builder.Services.AddScoped<DA_Appointment>();
builder.Services.AddScoped<DA_Client>();
Expand All @@ -32,6 +33,7 @@ public static WebApplicationBuilder AddDataAccessService(this WebApplicationBuil

public static WebApplicationBuilder AddBusinessLogicService(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<BL_Admin>();
builder.Services.AddScoped<BL_Agent>();
builder.Services.AddScoped<BL_Appointment>();
builder.Services.AddScoped<BL_Client>();
Expand Down
20 changes: 10 additions & 10 deletions REMS.BackendApi/REMS.BackendApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<PropertyGroup Condition=" '$(RunConfiguration)' == 'https' "/>
<PropertyGroup Condition=" '$(RunConfiguration)' == 'http' "/>
<PropertyGroup Condition=" '$(RunConfiguration)' == 'https' " />
<PropertyGroup Condition=" '$(RunConfiguration)' == 'http' " />
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.20"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.20"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.20" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.20" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\REMS.Database\REMS.Database.csproj"/>
<ProjectReference Include="..\REMS.Mapper\REMS.Mapper.csproj"/>
<ProjectReference Include="..\REMS.Models\REMS.Models.csproj"/>
<ProjectReference Include="..\REMS.Modules\REMS.Modules.csproj"/>
<ProjectReference Include="..\REMS.Shared\REMS.Shared.csproj"/>
<ProjectReference Include="..\REMS.Database\REMS.Database.csproj" />
<ProjectReference Include="..\REMS.Mapper\REMS.Mapper.csproj" />
<ProjectReference Include="..\REMS.Models\REMS.Models.csproj" />
<ProjectReference Include="..\REMS.Modules\REMS.Modules.csproj" />
<ProjectReference Include="..\REMS.Shared\REMS.Shared.csproj" />
</ItemGroup>

</Project>
33 changes: 30 additions & 3 deletions REMS.Mapper/ChangeModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using REMS.Models.Transaction;

namespace REMS.Mapper;
namespace REMS.Mapper;

public static class ChangeModel
{
Expand Down Expand Up @@ -350,4 +348,33 @@ public static AppointmentModel Change(this Appointment appointment)
//}

#endregion

#region Admin

public static User Change(this AdminRequestModel adminRequest)
{
var user = new User()
{
Email = adminRequest.Email,
Name = adminRequest.Name,
Phone = adminRequest.Phone,
Password = adminRequest.Password
};

return user;
}

public static AdminResponseModel Change(this User user)
{
var adminResponse = new AdminResponseModel()
{
Email = user.Email,
Name = user.Name,
Phone = user.Phone
};

return adminResponse;
}

#endregion
}
4 changes: 3 additions & 1 deletion REMS.Mapper/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
global using REMS.Models.Client;
global using REMS.Models.Property;
global using REMS.Models.Review;
global using REMS.Models.Appointment;
global using REMS.Models.Appointment;
global using REMS.Models.Admin;
global using REMS.Models.Transaction;
13 changes: 13 additions & 0 deletions REMS.Models/Admin/AdminRequestModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace REMS.Models.Admin;

public class AdminRequestModel
{
public string Name { get; set; }

Check warning on line 5 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, ubuntu-latest)

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

Check warning on line 5 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, windows-latest)

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

Check warning on line 5 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x, windows-latest)

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string Email { get; set; }

Check warning on line 6 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, ubuntu-latest)

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 6 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, windows-latest)

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 6 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x, windows-latest)

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

public string Password { get; set; }

Check warning on line 8 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, ubuntu-latest)

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 8 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, windows-latest)

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 8 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x, windows-latest)

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

public string Phone { get; set; }

Check warning on line 10 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, ubuntu-latest)

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

Check warning on line 10 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, windows-latest)

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

Check warning on line 10 in REMS.Models/Admin/AdminRequestModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x, windows-latest)

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


9 changes: 9 additions & 0 deletions REMS.Models/Admin/AdminResponseModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace REMS.Models.Admin;

public class AdminResponseModel
{
public string Name { get; set; }

Check warning on line 5 in REMS.Models/Admin/AdminResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, ubuntu-latest)

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

Check warning on line 5 in REMS.Models/Admin/AdminResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, windows-latest)

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

Check warning on line 5 in REMS.Models/Admin/AdminResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x, windows-latest)

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string Email { get; set; }

Check warning on line 6 in REMS.Models/Admin/AdminResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, ubuntu-latest)

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 6 in REMS.Models/Admin/AdminResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, windows-latest)

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 6 in REMS.Models/Admin/AdminResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x, windows-latest)

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

public string Phone { get; set; }

Check warning on line 8 in REMS.Models/Admin/AdminResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, ubuntu-latest)

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

Check warning on line 8 in REMS.Models/Admin/AdminResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x, windows-latest)

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

Check warning on line 8 in REMS.Models/Admin/AdminResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x, windows-latest)

Non-nullable property 'Phone' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
2 changes: 1 addition & 1 deletion REMS.Models/REMS.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\REMS.Database\REMS.Database.csproj"/>
<ProjectReference Include="..\REMS.Database\REMS.Database.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions REMS.Modules/Features/Admin/BL_Admin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace REMS.Modules.Features.Admin;

public class BL_Admin
{
private readonly DA_Admin _daAdmin;

public BL_Admin(DA_Admin daAdmin)
{
_daAdmin = daAdmin;
}

public async Task<Result<AdminResponseModel>> CreateAdmin(AdminRequestModel adminRequest)
{
var response = await _daAdmin.CreateAdmin(adminRequest);
return response;
}
}
49 changes: 49 additions & 0 deletions REMS.Modules/Features/Admin/DA_Admin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace REMS.Modules.Features.Admin;

public class DA_Admin
{
private readonly AppDbContext _db;

public DA_Admin(AppDbContext dbContext)
{
_db = dbContext;
}

public async Task<Result<AdminResponseModel>> CreateAdmin(AdminRequestModel adminRequest)
{
Result<AdminResponseModel> model = null;
try
{
await IsEmailAlreadyExist(adminRequest.Email);

var user = adminRequest.Change();

user.Role = "Admin";

await _db.Users.AddAsync(user);
await _db.SaveChangesAsync();

var adminResponse = user.Change();

model = Result<AdminResponseModel>.Success(adminResponse);
return model;
}
catch (Exception ex)
{
model = Result<AdminResponseModel>.Error(ex.Message);
return model;
}
}


private async Task IsEmailAlreadyExist(string email)
{
bool emailExists = await _db.Users.AnyAsync(user => user.Email == email);
if (emailExists)
{
throw new InvalidOperationException("An account with this email already exists.");
}
}


}
1 change: 1 addition & 0 deletions REMS.Modules/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
global using REMS.Database.AppDbContextModels;
global using REMS.Mapper;
global using REMS.Models;
global using REMS.Models.Admin;
global using REMS.Models.Agent;
global using REMS.Models.Client;
global using REMS.Models.Custom;
Expand Down
8 changes: 4 additions & 4 deletions REMS.Modules/REMS.Modules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\REMS.Database\REMS.Database.csproj"/>
<ProjectReference Include="..\REMS.Mapper\REMS.Mapper.csproj"/>
<ProjectReference Include="..\REMS.Models\REMS.Models.csproj"/>
<ProjectReference Include="..\REMS.Shared\REMS.Shared.csproj"/>
<ProjectReference Include="..\REMS.Database\REMS.Database.csproj" />
<ProjectReference Include="..\REMS.Mapper\REMS.Mapper.csproj" />
<ProjectReference Include="..\REMS.Models\REMS.Models.csproj" />
<ProjectReference Include="..\REMS.Shared\REMS.Shared.csproj" />
</ItemGroup>
</Project>

0 comments on commit 4756362

Please sign in to comment.