public async Task <IActionResult> Post([FromBody] CreateAgentViewModel request) { try { //Name must be unique AgentModel namedAgent = repository.Find(null, d => d.Name.ToLower(null) == request.Name.ToLower(null))?.Items?.FirstOrDefault(); if (namedAgent != null) { ModelState.AddModelError("Agent", "Agent Name Already Exists"); return(BadRequest(ModelState)); } //create Agent app user var user = new ApplicationUser() { Name = request.Name, UserName = request.UserName }; var loginResult = await userManager.CreateAsync(user, request.Password).ConfigureAwait(false); if (!loginResult.Succeeded) { ModelState.AddModelError("Agent", "Failed to Create Agent User"); return(BadRequest(ModelState)); } else { Person newPerson = new Person() { Name = request.Name, IsAgent = true }; var person = personRepo.Add(newPerson); if (person == null) { ModelState.AddModelError("Agent", "Failed to Create Agent User"); return(BadRequest(ModelState)); } //Update the user var registeredUser = userManager.FindByNameAsync(user.UserName).Result; registeredUser.PersonId = (Guid)person.Id; await userManager.UpdateAsync(registeredUser).ConfigureAwait(false); //post Agent entity AgentModel newAgent = request.Map(request); return(await base.PostEntity(newAgent)); } } catch (Exception ex) { return(ex.GetActionResult()); } }
/// <summary> /// Creates necessary user tables for the provided request /// </summary> /// <param name="request"></param> public void CreateAgentUserAccount(CreateAgentViewModel request) { //name must be unique Agent namedAgent = _agentRepo.Find(null, d => d.Name.ToLower(null) == request.Name.ToLower(null))?.Items?.FirstOrDefault(); if (namedAgent != null) { throw new EntityAlreadyExistsException("Agent name already exists"); } Guid entityId = Guid.NewGuid(); if (request.Id == null || !request.Id.HasValue || request.Id.Equals(Guid.Empty)) { request.Id = entityId; } //create IsAgent person Person newPerson = new Person() { Name = request.Name, IsAgent = true }; if (newPerson == null) { throw new EntityOperationException("Failed to create agent user"); } //create agent app user var user = new ApplicationUser() { Name = request.Name, UserName = request.UserName, PersonId = newPerson.Id ?? Guid.Empty }; var loginResult = _userManager.CreateAsync(user, request.Password).Result; var errors = loginResult.Errors; if (errors.Any()) { foreach (var error in errors) { if (error.Code == "DuplicateUserName") { throw new EntityAlreadyExistsException("UserName already exists"); } } throw new EntityOperationException("Failed to create new agent credentials"); } else { _personRepo.Add(newPerson); } }
// GET: CreateUsers public ActionResult Create() { try { var createAgentViewModel = new CreateAgentViewModel() { ListofCategory = _category.GetAllActiveSelectListItemCategory() }; return(View(createAgentViewModel)); } catch (Exception) { throw; } }
private string UploadedFile(CreateAgentViewModel model) { string uniqueFileName = null; if (model.UploadPicture != null) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.UploadPicture.FileName); string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.UploadPicture.CopyTo(fileStream); } } return(uniqueFileName); }
public async Task <IActionResult> Post([FromBody] CreateAgentViewModel request) { try { _agentManager.CreateAgentUserAccount(request); //post agent entity Agent newAgent = request.Map(request); await _webhookPublisher.PublishAsync("Agents.NewAgentCreated", newAgent.Id.ToString(), newAgent.Name).ConfigureAwait(false); return(await base.PostEntity(newAgent)); } catch (Exception ex) { return(ex.GetActionResult()); } }
public static string CreateAgent(AuthAPIManager apiManager, ServerConnectionSettings serverSettings) { AgentsApi agentsApi = new AgentsApi(apiManager.Configuration); var agentModel = new CreateAgentViewModel(null, serverSettings.AgentName, serverSettings.DNSHost, serverSettings.MACAddress, serverSettings.IPAddress, true, null, null, null, null, true, false, null, serverSettings.AgentUsername, serverSettings.AgentPassword); try { return(agentsApi.ApiV1AgentsPostWithHttpInfo(agentModel).Data.Id.ToString()); } catch (Exception ex) { // In case of Unauthorized request if (ex.GetType().GetProperty("ErrorCode").GetValue(ex, null).ToString() == "401") { // Refresh Token and Call API agentsApi.Configuration.AccessToken = apiManager.GetToken(); return(agentsApi.ApiV1AgentsPostWithHttpInfo(agentModel).Data.Id.ToString()); } throw ex; } }
public async Task <IActionResult> CreateAgent(CreateAgentViewModel model) { if (ModelState.IsValid) { string uniqueFileName = UploadedFile(model); Agent myAgent = new Agent { FirstName = model.FirstName, LastName = model.LastName, BirthDate = model.BirthDate, HireDate = model.HireDate, AgentNumber = model.AgentNumber, ProfilePicture = uniqueFileName }; _context.Add(myAgent); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View()); }
public ActionResult Create(CreateAgentViewModel createAgentViewModel) { try { if (ModelState.IsValid) { var isUser = _iUserMaster.CheckUsernameExists(createAgentViewModel.UserName); if (createAgentViewModel.CategoryId == null) { ModelState.AddModelError("", "Select Category"); } else if (isUser) { ModelState.AddModelError("", "Username already exists"); } else { var usermaster = AutoMapper.Mapper.Map <Usermaster>(createAgentViewModel); usermaster.Status = true; usermaster.CreateDate = DateTime.Now; usermaster.UserId = 0; usermaster.CreatedBy = Convert.ToInt32(_sessionHandler.UserId); usermaster.IsFirstLogin = true; var salt = GenerateRandomNumbers.RandomNumbers(20); var saltedpassword = GenerateHashSha512.Sha512(createAgentViewModel.Password, salt); var savedAssignedRoles = new SavedAssignedRoles() { RoleId = Convert.ToInt16(StatusMain.Roles.Agent), AssignedRoleId = 0, Status = true, CreateDate = DateTime.Now }; var result = _iUserMaster.AddAgent(usermaster, saltedpassword, savedAssignedRoles, createAgentViewModel.CategoryId, salt); if (result > 0) { TempData["MessageCreateUsers"] = "Agent Created Successfully"; return(RedirectToAction("Create", "CreateAgent")); } else { return(View(createAgentViewModel)); } } createAgentViewModel.ListofCategory = _category.GetAllActiveSelectListItemCategory(); return(View("Create", createAgentViewModel)); } else { createAgentViewModel.ListofCategory = _category.GetAllActiveSelectListItemCategory(); return(View("Create", createAgentViewModel)); } } catch { throw; } }
public async Task <IActionResult> Post([FromBody] CreateAgentViewModel request) { try { //name must be unique Agent namedAgent = repository.Find(null, d => d.Name.ToLower(null) == request.Name.ToLower(null))?.Items?.FirstOrDefault(); if (namedAgent != null) { ModelState.AddModelError("Agent", "Agent Name Already Exists"); return(BadRequest(ModelState)); } Guid entityId = Guid.NewGuid(); if (request.Id == null || !request.Id.HasValue || request.Id.Equals(Guid.Empty)) { request.Id = entityId; } //create agent app user var user = new ApplicationUser() { Name = request.Name, UserName = request.UserName }; var loginResult = await userManager.CreateAsync(user, request.Password).ConfigureAwait(false); var errors = loginResult.Errors; if (errors.Any()) { foreach (var error in errors) { if (error.Code == "DuplicateUserName") { ModelState.AddModelError("Agent", "Username Already Exists"); return(BadRequest(ModelState)); } } ModelState.AddModelError("Agent", "Failed to Create Agent User"); return(BadRequest(ModelState)); } else { Person newPerson = new Person() { Name = request.Name, IsAgent = true }; var person = personRepo.Add(newPerson); if (person == null) { ModelState.AddModelError("Agent", "Failed to Create Agent User"); return(BadRequest(ModelState)); } //update the user var registeredUser = userManager.FindByNameAsync(user.UserName).Result; registeredUser.PersonId = (Guid)person.Id; await userManager.UpdateAsync(registeredUser).ConfigureAwait(false); //post agent entity Agent newAgent = request.Map(request); await webhookPublisher.PublishAsync("Agents.NewAgentCreated", newAgent.Id.ToString(), newAgent.Name).ConfigureAwait(false); return(await base.PostEntity(newAgent)); } } catch (Exception ex) { return(ex.GetActionResult()); } }