示例#1
0
        public ActionResult AddPreference(string name, int id, bool like)
        {
            try
            {
                LoginHelper.CheckAccess(Session);
            }
            catch (Exception)
            {
                return(RedirectToAction("Login", "Login"));
            }

            var handler = new PreferenceHandler();
            int prefId  = id;

            //new preference => add it to preferences table
            if (id == 0)
            {
                var entity = new PreferenceEntity
                {
                    Name = name.First().ToString().ToUpper() + name.Substring(1)
                };

                var prefResponse = handler.Add(entity);

                if (!prefResponse.CompletedRequest)
                {
                    return(RedirectToAction("Index", "Error", new { errorMessage = prefResponse.ErrorMessage.Replace(' ', '-') }));
                }

                prefId = prefResponse.Entity.Id;
            }

            //add to user's preferences
            int userProfileId = (int)Session["userProfileId"];
            var response      = handler.AddForUser(prefId, userProfileId, like);

            if (!response.CompletedRequest)
            {
                return(RedirectToAction("Index", "Error", new { errorMessage = response.ErrorMessage.Replace(' ', '-') }));
            }

            return(Content(prefId.ToString()));
        }