/// <summary>
        /// This method is called by Users/Index view (loginPage) and takes a user and sends it to
        /// the API for authentication. If user is authenticated it's redirected to profilepage. Else
        /// an errormessage is displayed.
        /// </summary>
        /// <param name="user">A user (baseclass)</param>
        /// <returns></returns>
        public async Task <IActionResult> Login(User user)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // Get user
                    string uri      = "/User/Authenticate";
                    var    response = await _APIhelper.PostUserAsync(uri, user);

                    if (response.GetType() == typeof(Dictionary <string, string>))
                    {
                        Dictionary <string, string> dict = response as Dictionary <string, string>;
                        ModelState.AddModelError("User.UserName", dict["Content"]);
                        return(View("Index"));
                    }
                    var newUser = response as User;
                    _SessionHelper.SetSessionUser(HttpContext, newUser);
                    return(RedirectToAction("ProfilePage", new { id = newUser.UserId }));
                }
                catch (Exception ex)
                {
                    ViewBag.msg = "Woops what happend?" + " " + ex.Message;
                    return(View("Index"));
                }
            }
            else
            {
                return(View("Index", new BaseViewModel()
                {
                    User = user, Advert = await _Adhelper.ShowAd()
                }));
            }
        }
        /// <summary>
        /// Profile page for an Advertiser
        /// Shows all active ads and current balance
        /// </summary>
        /// <returns>Returns different views depending on the situation</returns>
        public async Task <IActionResult> Index()
        {
            var           user = _SessionHelper.GetSessionUser(HttpContext);
            List <Advert> newList;
            float         balance;

            if (user == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            try
            {
                //Hämtar all reklam som tillhör kunden
                string uri      = "/Advert/AdFrontPage/";
                var    response = await _APIhelper.PostAdvertAsync(uri, user);

                if (response.GetType() == typeof(Dictionary <string, string>))
                {
                    return(RedirectToAction("Index", "Home"));
                }
                newList = response as List <Advert>;

                //Hämtar kundens saldo
                uri      = "/Advert/GetAdvertiserBalance/";
                response = await _APIhelper.PostUserAsync(uri, user);

                if (response.GetType() == typeof(Dictionary <string, string>))
                {
                    return(RedirectToAction("Index", "Home"));
                }
                balance = (response as Advertiser).Balance;
            }
            catch (Exception)
            {
                return(RedirectToAction("Index", "Home"));
            }
            ViewData["User"]      = user;
            ViewData["ActiveAds"] = newList;
            ViewData["Balance"]   = balance + " SEK";
            return(View(new BaseViewModel()
            {
                Advert = await _Adhelper.ShowAd()
            }));
        }
Esempio n. 3
0
        public async Task <IActionResult> CreateLeague(League league)
        {
            User user = _SessionHelper.GetSessionUser(HttpContext);

            if (ModelState.IsValid)
            {
                league.LeagueOwnerId = user.UserId;
                string uri      = "/League/CreateLeague";
                var    response = await _APIhelper.PostUserAsync(uri, league);

                if (response.GetType() == typeof(Dictionary <string, string>))
                {
                    // This does not work atm because of partial view
                    var dict = response as Dictionary <string, string>;
                    ModelState.AddModelError("LeagueName", dict["Content"]);
                }
                var updatedUser = response as User;
                _SessionHelper.SetSessionUser(HttpContext, updatedUser);
            }
            return(RedirectToAction("Index", "Users", new { area = "" }));
        }