Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back end #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Binary file modified BACK_END/.vs/MY_CALS_BACKEND/v16/.suo
Binary file not shown.
57 changes: 48 additions & 9 deletions BACK_END/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using MY_CALS_BACKEND.Dto;
using MY_CALS_BACKEND.Dto.Meals;
using MY_CALS_BACKEND.Dto.UserAccount;
using MY_CALS_BACKEND.Models;
Expand All @@ -24,15 +25,17 @@ public class AdminController : ControllerBase
private readonly IRepoMeals _repoMeals;
private readonly IMapper _mapper;
private readonly UserManager<UserAccount> _userManager;
private readonly RoleManager<Role> _roleManager;
private int userId;


public AdminController(IRepoUserAccount repoUserAccount, IMapper mapper, UserManager<UserAccount> userManager, IHttpContextAccessor httpContextAccessor, IRepoMeals repoMeals)
public AdminController(IRepoUserAccount repoUserAccount, IMapper mapper, UserManager<UserAccount> userManager, RoleManager<Role> roleManager, IHttpContextAccessor httpContextAccessor, IRepoMeals repoMeals)
{
_repoUserAccount = repoUserAccount;
_repoMeals = repoMeals;
_mapper = mapper;
_userManager = userManager;
_roleManager = roleManager;
userId = Int32.Parse(httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);
}

Expand All @@ -56,6 +59,7 @@ public async Task<IActionResult> GetUsers()
return Ok(usersForDisplayDTO);
}


[HttpGet("users/{id}")]
public async Task<IActionResult> GetUserInfo(int id)
{
Expand All @@ -74,6 +78,48 @@ public async Task<IActionResult> GetUserInfo(int id)
return NotFound("This user dosen't existe 💢 ");
}

[HttpPost("addmanager")]
public async Task<IActionResult> AddManger(UserRegisterDTO userRegisterDTO)
{
var checkedEmail = await _userManager.FindByEmailAsync(userRegisterDTO.Email);

var user = _mapper.Map<UserAccount>(userRegisterDTO);

if (checkedEmail != null)
{
return BadRequest("This email already exist try again");
}
else
{
user.UserName = userRegisterDTO.Email.Split("@")[0] + userRegisterDTO.Email.Split("@")[1];
}

if (await _roleManager.RoleExistsAsync("Admin") == false && await _roleManager.RoleExistsAsync("Manager") == false && await _roleManager.RoleExistsAsync("User") == false)
{
var roles = new List<Role>{
new Role{Name="Admin"},
new Role{Name="Manager"},
new Role{Name="User"},

};

foreach (var role in roles)
{
await _roleManager.CreateAsync(role);
}
}

var Result = await _userManager.CreateAsync(user, userRegisterDTO.Password);

if (Result.Succeeded)
{
//var confirmaEmailToken = await _userManager.GenerateEmailConfirmationTokenAsync(user);
await _userManager.AddToRoleAsync(user, userRegisterDTO.Role);
return Ok("User Created Successfully");
}
return BadRequest(Result.Errors);
}


[HttpGet("users/{id}/meals")]
public async Task<IActionResult> GetUserMeals(int id)
Expand All @@ -98,7 +144,6 @@ public async Task<IActionResult> GetUserMeals(int id)
}

[HttpGet("users/{id}/meals/{id_meal}")]

public async Task<IActionResult> GetUserMeal(int id, int id_meal)
{
var user = await _repoUserAccount.GetUserById(id);
Expand All @@ -120,7 +165,6 @@ public async Task<IActionResult> GetUserMeal(int id, int id_meal)
}

[HttpDelete("users/{id}/meals/{id_meal}/delete")]

public async Task<IActionResult> DeleteUserMeal(int id, int id_meal)
{
var user = await _repoUserAccount.GetUserById(id);
Expand All @@ -142,7 +186,6 @@ public async Task<IActionResult> DeleteUserMeal(int id, int id_meal)
}

[HttpPut("users/{id}/meals/{id_meal}/edit")]

public async Task<IActionResult> EditUserMeal(int id, int id_meal,MealForEditDTO mealForEditDTO)
{
var user = await _repoUserAccount.GetUserById(id);
Expand All @@ -152,11 +195,7 @@ public async Task<IActionResult> EditUserMeal(int id, int id_meal,MealForEditDTO
{
await _repoMeals.EditMeal(id_meal,mealForEditDTO) ;
var result = await _repoMeals.SaveMeal();
if (result)
{
return Ok("This Meal has been updated successfully 👍 !!!");
}
return NotFound("This meal dosen't existe 💢!!!");
return result ? Ok("This Meal has been updated successfully 👍 !!!") : (IActionResult)NotFound("This meal dosen't existe 💢!!!");
}
return BadRequest("Bad Action 😑 !!!");
}
Expand Down
6 changes: 3 additions & 3 deletions BACK_END/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"SqlConnection": "Server=DESKTOP-6DGS6FK;Initial Catalog=MyCalcsDB; Integrated Security=true;"
},
"ConnectionStrings": {
"SqlConnection": "Server=DESKTOP-3C9CT52;Initial Catalog=MyCalcsDB; Integrated Security=true;"
},

"AllowedHosts": "*",
"AppSettings": {
Expand Down
Binary file modified BACK_END/bin/Debug/netcoreapp3.1/MY_CALS_BACKEND.dll
Binary file not shown.
Binary file modified BACK_END/bin/Debug/netcoreapp3.1/MY_CALS_BACKEND.exe
Binary file not shown.
Binary file modified BACK_END/bin/Debug/netcoreapp3.1/MY_CALS_BACKEND.pdb
Binary file not shown.
6 changes: 3 additions & 3 deletions BACK_END/bin/Debug/netcoreapp3.1/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"SqlConnection": "Server=DESKTOP-6DGS6FK;Initial Catalog=MyCalcsDB; Integrated Security=true;"
},
"ConnectionStrings": {
"SqlConnection": "Server=DESKTOP-3C9CT52;Initial Catalog=MyCalcsDB; Integrated Security=true;"
},

"AllowedHosts": "*",
"AppSettings": {
Expand Down
Binary file modified BACK_END/obj/Debug/netcoreapp3.1/MY_CALS_BACKEND.dll
Binary file not shown.
Binary file modified BACK_END/obj/Debug/netcoreapp3.1/MY_CALS_BACKEND.pdb
Binary file not shown.
Binary file modified BACK_END/obj/Debug/netcoreapp3.1/apphost.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion BACK_END/obj/MY_CALS_BACKEND.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.301\\RuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.302\\RuntimeIdentifierGraph.json"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion BACK_END/obj/project.assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -8298,7 +8298,7 @@
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.301\\RuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\5.0.302\\RuntimeIdentifierGraph.json"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion BACK_END/obj/project.nuget.cache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 2,
"dgSpecHash": "p2yO5o88DbyD1rcPceRN4QN4HkG6vfwvtBs/JX60N4UromIzpoPcoZr2yd/y4wHS22umijnDlYtNh1rY1gJVIA==",
"dgSpecHash": "NShQTHv1wkbakI/OgyLobEcYfF+8ptlpARym0cYOeF9YDtDDaz3YJLoR/yqZq/XhZbZm24G/rbMNZj/qabkQcQ==",
"success": true,
"projectFilePath": "E:\\User_ID\\Data\\Dev\\Builds .NET\\.NET Core\\MyCals\\BACK_END\\MY_CALS_BACKEND.csproj",
"expectedPackageFiles": [
Expand Down