Пример #1
0
        public async Task <IActionResult> Update(ProfileViewModel profileViewModel)
        {
            try
            {
                string response = await APICallerExtensions.APICallAsync("Profile/Save", profileViewModel, false, HttpContext.Session.GetObject(StorageType.Token).ToString());

                if (response.ToLower().Contains("exception:"))
                {
                    ModelState.AddModelError(string.Empty, response);
                    return(View(profileViewModel));
                }
                var content = JsonConvert.DeserializeObject <SingleResponse <ApplicationUser> >(response);
                if (!content.DidError)
                {
                    LocalStorageExtensions.Store(StorageType.Picture, content.Model.Picture);
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, content.Message);
                    return(View(profileViewModel));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return(View(profileViewModel));
            }
        }
Пример #2
0
        public async Task <IActionResult> Create([FromBody] SaleViewModel saleViewModel)
        {
            try
            {
                //SaleViewModel saleViewModel = new SaleViewModel();
                string userId = LocalStorageExtensions.Get(StorageType.UserId);
                saleViewModel.UserId = userId;
                string response = await APICallerExtensions.APICallAsync("Sale/Create", saleViewModel, false, HttpContext.Session.GetObject(StorageType.Token).ToString());

                if (response.ToLower().Contains("exception:"))
                {
                    ModelState.AddModelError(string.Empty, response);
                    return(Json(response));
                }
                var content = JsonConvert.DeserializeObject <SingleResponse <CreateSaleViewModel> >(response);
                if (!content.DidError)
                {
                    //Get Remaining Balance
                    RemainingBalanceViewModel remainingBalanceViewModel = new RemainingBalanceViewModel()
                    {
                        UserId = userId
                    };

                    string responseBalance = await APICallerExtensions.APICallAsync("RemainingBalance/GetByUserId", remainingBalanceViewModel, false, HttpContext.Session.GetObject(StorageType.Token).ToString());

                    if (responseBalance.ToLower().Contains("exception:"))
                    {
                        ModelState.AddModelError(string.Empty, responseBalance);
                    }
                    var contentBalance = JsonConvert.DeserializeObject <SingleResponse <RemainingBalance> >(responseBalance);
                    if (!contentBalance.DidError)
                    {
                        if (contentBalance.Model != null)
                        {
                            LocalStorageExtensions.Store(StorageType.Balance, contentBalance.Model.CurrentAmount.ToString());
                        }
                    }

                    return(Json(content.Model));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, content.Message);
                    return(Json(content.Message));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return(Json(ex.Message));
            }
        }
Пример #3
0
        public async Task <JsonResult> SaveSettings(UserSettingsViewModel userSettingsViewModel)
        {
            try
            {
                string response = await APICallerExtensions.APICallAsync("User/SaveSettings", userSettingsViewModel, false, HttpContext.Session.GetObject(StorageType.Token).ToString());

                if (response.ToLower().Contains("exception:"))
                {
                    ModelState.AddModelError(string.Empty, response);
                    return(Json(new
                    {
                        result = false,
                        error = response
                    }));
                }
                var content = JsonConvert.DeserializeObject <SingleResponse <ApplicationUser> >(response);
                if (!content.DidError)
                {
                    if (content.Model.IsCompanySelected)
                    {
                        LocalStorageExtensions.Store(StorageType.IsCompanySelected, "true");
                    }
                    if (content.Model.IsShopSelected)
                    {
                        LocalStorageExtensions.Store(StorageType.IsShopSelected, "true");
                    }

                    return(Json(new
                    {
                        content.Model,
                    }));
                }
                else
                {
                    return(Json(new
                    {
                        result = false,
                        error = content == null ? "Content is null or empty" : content.ErrorMessage
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    result = false,
                    error = ex.Message
                }));
            }
        }
Пример #4
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                string response = await APICallerExtensions.APICallAsync("Account/Login", model, false, HttpContext.Session.GetObject(StorageType.Token).ToString());

                if (string.IsNullOrEmpty(response) || response.ToLower().Contains("exception:"))
                {
                    ModelState.AddModelError(string.Empty, response);
                    return(View(model));
                }
                var content = JsonConvert.DeserializeObject <SingleResponse <UserModel> >(response);
                if (!content.DidError)
                {
                    if (content.Model.Role == "Shop")
                    {
                        RemainingBalanceViewModel remainingBalanceViewModel = new RemainingBalanceViewModel()
                        {
                            UserId = content.Model.Id
                        };

                        string responseBalance = await APICallerExtensions.APICallAsync("RemainingBalance/GetByUserId", remainingBalanceViewModel, false, HttpContext.Session.GetObject(StorageType.Token).ToString());

                        if (responseBalance.ToLower().Contains("exception:"))
                        {
                            ModelState.AddModelError(string.Empty, responseBalance);
                        }
                        var contentBalance = JsonConvert.DeserializeObject <SingleResponse <RemainingBalance> >(responseBalance);
                        if (!contentBalance.DidError)
                        {
                            if (contentBalance.Model != null)
                            {
                                LocalStorageExtensions.Store(StorageType.Balance, contentBalance.Model.CurrentAmount.ToString());
                            }
                            else
                            {
                                LocalStorageExtensions.Store(StorageType.Balance, "0.00");
                            }
                        }
                    }


                    LocalStorageExtensions.Store(StorageType.Token, content.Token);
                    LocalStorageExtensions.Store(StorageType.UserId, content.Model.Id);
                    LocalStorageExtensions.Store(StorageType.Username, content.Model.UserName);

                    //Sample get/set of http context sessions
                    //HttpContext.Session.SetObject(StorageType.Token, content.Token);
                    //var objComplex = HttpContext.Session.GetObject(StorageType.Token);

                    if (string.IsNullOrEmpty(content.Model.Picture))
                    {
                        if (_hostingEnvironment.IsDevelopment())
                        {
                            LocalStorageExtensions.Store(StorageType.Picture, "/app-assets/images/user.png");
                        }
                        else
                        {
                            LocalStorageExtensions.Store(StorageType.Picture, "../app-assets/images/user.png");
                        }
                    }
                    else
                    {
                        LocalStorageExtensions.Store(StorageType.Picture, content.Model.Picture);
                    }
                    LocalStorageExtensions.Store(StorageType.Role, content.Model.Role);
                    LocalStorageExtensions.Store(StorageType.Name, content.Model.FirstName + " " + content.Model.LastName);

                    LocalStorageExtensions.Store(StorageType.IsCompanySelected, content.Model.IsCompanySelected.ToString());
                    LocalStorageExtensions.Store(StorageType.IsShopSelected, content.Model.IsShopSelected.ToString());

                    if (content.Model.Role == "Admin" || content.Model.Role == "Company")
                    {
                        return(RedirectToAction("Index", "Dashboard"));
                    }
                    else if (content.Model.Role == "Shop")
                    {
                        return(RedirectToAction("Index", "Sale"));
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, content.Message);
                    return(View(model));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }