0% found this document useful (0 votes)
9 views

user create controller

The document outlines an asynchronous method for creating or updating a user in a wallet application. It checks if the user is logged in, retrieves user data, and handles the addition or removal of environment settings based on user input. Upon successful validation and API response, it updates the session and provides feedback on the operation's success or failure.

Uploaded by

Zak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

user create controller

The document outlines an asynchronous method for creating or updating a user in a wallet application. It checks if the user is logged in, retrieves user data, and handles the addition or removal of environment settings based on user input. Upon successful validation and API response, it updates the session and provides feedback on the operation's success or failure.

Uploaded by

Zak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

public async Task<ActionResult> Create()

{
if (WalletAppWorkSpace == null)
{
return RedirectToAction("Login", "Login");
}

ViewData["WalletAppWorkSpace"] = WalletAppWorkSpace;
ViewBag.UserType = new List<SelectListItem>()
{
new SelectListItem {Text = "Admin", Value = "ADMIN" },
new SelectListItem {Text = "Wallet", Value = "Wallet_User" },
new SelectListItem {Text = "Profile Builder", Value =
"Profile_Builder" }
};

if (_sessionUser != "") // Update


{
User user = JsonConvert.DeserializeObject<User>(_sessionUser);
if(user.UserType == Data.Platform.User.Type.Wallet_User)
{
ViewBag.IsWalletUser = "true";
ViewBag.IsUpdate = "true";
}
return View(user);
}

return View();
}

[HttpPost]
public async Task<ActionResult> Create(User user)
{

if (user.IsDDX)
{
user.Environments.Add("DDX");
}
else
{
user.Environments.Remove("DDX");
}
if (user.IsInBuiltCard)
{
user.Environments.Add("InBuiltCard");
}
else
{
user.Environments.Remove("InBuiltCard");
}
if (user.IsTestHarness)
{
user.Environments.Add("TestHarness");
}
else
{
user.Environments.Remove("TestHarness");
}
var loggedUser = WalletAppWorkSpace.UserObj;
ViewData["WalletAppWorkSpace"] = WalletAppWorkSpace;
ViewBag.UserType = new List<SelectListItem>()
{
new SelectListItem {Text = "Admin", Value = "ADMIN" },
new SelectListItem {Text = "Wallet User", Value = "Wallet_User"
},
new SelectListItem {Text = "Profile Builder", Value =
"Profile_Builder" }
};

var response = await WalletClient.Get();


userdata =
JsonConvert.DeserializeObject<List<User>>(response.ResponseResult);

if (ValidateUser(user))
{
user.CreatedBy = loggedUser.Id;

string apiRoute = "api/User/update";

if (_sessionUser == "")
{
apiRoute = "api/User/create";
}
else // For editing user, updating entered values in the UI form
{
User sessionUser =
JsonConvert.DeserializeObject<User>(_sessionUser);
sessionUser.Email = user.Email;
sessionUser.Name = user.Name;
sessionUser.Environments = user.Environments;
sessionUser.IsDDX = user.IsDDX;
sessionUser.IsInBuiltCard = user.IsInBuiltCard;
sessionUser.IsTestHarness = user.IsTestHarness;
sessionUser.AccessToWalletServer = user.AccessToWalletServer;
user = sessionUser; // Need to change this implementation
}

PhAPIClient client = new PhAPIClient(apiClient, apiRoute);


var userCreateResponse = await
client.Post(JsonConvert.SerializeObject(user));

if (userCreateResponse.StatusIsSuccessful == true)
{
User loggeduser = WalletAppWorkSpace.UserObj;
if (loggeduser.Email == user.Email)
{
WalletAppWorkSpace.UserObj = user;
string serializedWorksapce =
JsonConvert.SerializeObject(WalletAppWorkSpace);
HttpContext.Session.SetString(WalletAppConstants.WORKSPACE,
serializedWorksapce);
ViewData["WalletAppWorkSpace"] = WalletAppWorkSpace;
}

TempData["SuccessMessage"] = (_sessionUser == "") ?


"User created successfully, Credentials sent to the created
email" : "User updated successfully";
return RedirectToAction("UserDetails");
}
}
TempData["ErrorMessage"] = "please kindly fill the details correctly.";
return View(user);
}

You might also like