-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
[Add] Admin Create Endpoint
- Loading branch information
There are no files selected for viewing
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); | ||
} | ||
} | ||
} |
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 GitHub Actions / Build and Test (8.0.x, ubuntu-latest)
Check warning on line 5 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (8.0.x, windows-latest)
Check warning on line 5 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (6.0.x, windows-latest)
|
||
public string Email { get; set; } | ||
Check warning on line 6 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (8.0.x, ubuntu-latest)
Check warning on line 6 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (8.0.x, windows-latest)
Check warning on line 6 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (6.0.x, windows-latest)
|
||
|
||
public string Password { get; set; } | ||
Check warning on line 8 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (8.0.x, ubuntu-latest)
Check warning on line 8 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (8.0.x, windows-latest)
Check warning on line 8 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (6.0.x, windows-latest)
|
||
|
||
public string Phone { get; set; } | ||
Check warning on line 10 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (8.0.x, ubuntu-latest)
Check warning on line 10 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (8.0.x, windows-latest)
Check warning on line 10 in REMS.Models/Admin/AdminRequestModel.cs GitHub Actions / Build and Test (6.0.x, windows-latest)
|
||
} | ||
|
||
|
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 GitHub Actions / Build and Test (8.0.x, ubuntu-latest)
Check warning on line 5 in REMS.Models/Admin/AdminResponseModel.cs GitHub Actions / Build and Test (8.0.x, windows-latest)
Check warning on line 5 in REMS.Models/Admin/AdminResponseModel.cs GitHub Actions / Build and Test (6.0.x, windows-latest)
|
||
public string Email { get; set; } | ||
Check warning on line 6 in REMS.Models/Admin/AdminResponseModel.cs GitHub Actions / Build and Test (8.0.x, ubuntu-latest)
Check warning on line 6 in REMS.Models/Admin/AdminResponseModel.cs GitHub Actions / Build and Test (8.0.x, windows-latest)
Check warning on line 6 in REMS.Models/Admin/AdminResponseModel.cs GitHub Actions / Build and Test (6.0.x, windows-latest)
|
||
|
||
public string Phone { get; set; } | ||
Check warning on line 8 in REMS.Models/Admin/AdminResponseModel.cs GitHub Actions / Build and Test (8.0.x, ubuntu-latest)
Check warning on line 8 in REMS.Models/Admin/AdminResponseModel.cs GitHub Actions / Build and Test (8.0.x, windows-latest)
Check warning on line 8 in REMS.Models/Admin/AdminResponseModel.cs GitHub Actions / Build and Test (6.0.x, windows-latest)
|
||
} |
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; | ||
} | ||
} |
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."); | ||
} | ||
} | ||
|
||
|
||
} |