Skip to content

Instantly share code, notes, and snippets.

@yaseralnajjar
Created December 31, 2017 15:10
Show Gist options
  • Save yaseralnajjar/b11117eb6271eec5f4081b71a3e5ca36 to your computer and use it in GitHub Desktop.
Save yaseralnajjar/b11117eb6271eec5f4081b71a3e5ca36 to your computer and use it in GitHub Desktop.
using System.Threading.Tasks;
using EntryAgents.Core.Enums;
using EntryAgents.Core.Extensions;
using EntryAgents.Core.Models;
using EntryAgents.Core.Services;
using EntryAgents.Web.Extensions;
using EntryAgents.Web.Mappers;
using EntryAgents.Web.Services;
using EntryAgents.Web.ViewModels.Agent;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace EntryAgents.Web.Controllers.Web
{
[Route("[controller]")]
[Authorize(Roles = nameof(Role.Admin) + "," + nameof(Role.Agent))]
public class AgentController : Controller
{
private readonly IEntryAgentRepository _repository;
private readonly IAgentStudentService _agentStudentService;
private readonly IAgentManager _agentManager;
private readonly UserManager<Agent> _userManager;
private readonly SignInManager<Agent> _signInManager;
private readonly IAuthorizationService _authorizationService;
private readonly IHostingEnvironment _environment;
public AgentController(IEntryAgentRepository repository,
IAgentStudentService agentStudentService,
IAgentManager agentManager,
UserManager<Agent> userManager,
SignInManager<Agent> signInManager,
IAuthorizationService authorizationService,
IHostingEnvironment environment)
{
_repository = repository;
_agentStudentService = agentStudentService;
_agentManager = agentManager;
_userManager = userManager;
_authorizationService = authorizationService;
_environment = environment;
_signInManager = signInManager;
}
// GET: Agents
public async Task<IActionResult> Index(string searchTerm = null, string applicationStatus = null, int studentPage = 1)
{
ViewBag.SearchTerm = searchTerm;
ViewBag.ApplicationStatus = applicationStatus;
var agent = await _userManager.GetUserAsync(User);
//var identity = (ClaimsIdentity)User.Identity;
//var claimsPrincipal = new ClaimsPrincipal(identity);
var agentStudents = await _agentStudentService.GetAgentStudents(agent);
var filteredStudents =
await agentStudents.FilterByApplicationStatus(applicationStatus)
.FilterByNameOrEmail(searchTerm)
.GetLastTen()
.ToListAsync();
var result = filteredStudents.ToStudentDetailsViewModels(_environment);
return View(result);
}
// Get: Agent/SetupAccount
[Route("SetupAccount")]
public async Task<IActionResult> SetupAccount()
{
var userName = HttpContext.User.Identity.Name;
var result = await _agentManager.GetSetupAccountViewModel(userName);
return View(result);
}
// Post: Agent/SetupAccount
[HttpPost]
[Route("SetupAccount")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SetupAccount(AgentSetupAccountViewModel agentSetupAccountViewModel)
{
if (ModelState.IsValid)
{
var userName = HttpContext.User.Identity.Name;
var agent = await _agentManager.Edit(userName, agentSetupAccountViewModel);
// TODO: update email after verification
// TODO: update username
// https://stackoverflow.com/questions/36367140/aspnet-core-generate-and-change-email-address
if (agent.Email != agentSetupAccountViewModel.Email)
{
agent.Email = agentSetupAccountViewModel.Email;
agent.UserName = agentSetupAccountViewModel.Email;
await _userManager.UpdateAsync(agent);
await _signInManager.RefreshSignInAsync(agent);
//await _signInManager.SignInAsync(agent, true);
//var token = await _userManager.GenerateChangeEmailTokenAsync(agent, agentSetupAccountViewModel.Email);
//await _userManager.ChangeEmailAsync(agent, agentSetupAccountViewModel.Email, token);
}
return RedirectToAction("RequestPayment", "Payment");
}
return View(agentSetupAccountViewModel);
}
// GET: /Agent/Referral/
[Route("/Agent/Referral/")]
public IActionResult Referral()
{
return View();
}
// Get: Admin/Agent/List
[Route("/Admin/Agent/List")]
[Authorize(Roles = nameof(Role.Admin))]
public async Task<IActionResult> List(string searchTerm = null, int agentPage = 1)
{
var pagedResult = await _repository.AgentData
.GetAll()
.FilterByNameOrEmail(searchTerm)
.TakePageAsync(agentPage, EntryAgentsConfig.DefaultPageSize);
var result = (await pagedResult.items.ToDetailsViewModel(_userManager))
.ToPagedListViewModel(agentPage, EntryAgentsConfig.DefaultPageSize, pagedResult.count);
return View(result);
}
// Get: /Admin/Agent/Edit/first@gmail.com
[Route("/Admin/Agent/Edit/{userName}")]
[Authorize(Roles = nameof(Role.Admin))]
public async Task<IActionResult> Edit(string userName)
{
var result = await _agentManager.GetSetupAccountViewModel(userName);
return View("SetupAccount", result);
}
// Post: /Admin/Agent/Edit/first@gmail.com
[HttpPost]
[Route("/Admin/Agent/Edit/{userName}")]
[ValidateAntiForgeryToken]
[Authorize(Roles = nameof(Role.Admin))]
public async Task<IActionResult> Edit(string userName, AgentSetupAccountViewModel agentSetupAccountViewModel)
{
IActionResult result = View("SetupAccount", agentSetupAccountViewModel);
if (ModelState.IsValid)
{
await _agentManager.Edit(userName, agentSetupAccountViewModel);
result = RedirectToAction("List", "Agent");
}
return result;
}
// GET: /Admin/Agent/Details/first@gmail.com
[Route("/Admin/Agent/Details/{username}")]
[Authorize(Roles = nameof(Role.Admin))]
public async Task<IActionResult> Details(string userName)
{
var agent = await _repository.AgentData.GetByUserName(userName);
agent.Applications = await _repository.ApplicationData.GetAgentApplications(agent).ToListAsync();
var result = await agent.ToDetailsViewModel(_userManager);
return View(result);
}
// GET: /Admin/Agent/ApproveDisapprove/first@gmail.com
[Route("/Admin/Agent/ApproveDisapprove/{username}")]
[Authorize(Roles = nameof(Role.Admin))]
public async Task<IActionResult> ApproveDisapprove(string userName)
{
var agent = await _repository.AgentData.GetByUserName(userName);
var isApproved = await _repository.AgentIdentity.ApproveDisapprove(agent);
ViewResult result;
if (isApproved)
{
result = View("ApproveSuccess", agent);
}
else
{
result = View("DisapproveSuccess", agent);
}
return result;
}
// GET: /Admin/Agent/BlockUnblock/first@gmail.com
[Route("/Admin/Agent/BlockUnblock/{username}")]
[Authorize(Roles = nameof(Role.Admin))]
public async Task<IActionResult> BlockUnblock(string userName)
{
var agent = await _repository.AgentData.GetByUserName(userName);
var isBlocked = await _repository.AgentIdentity.BlockUnblock(agent);
ViewResult result;
if (isBlocked)
{
result = View("BlockSuccess", agent);
}
else
{
result = View("UnblockSuccess", agent);
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment