コード例 #1
0
        public ActionResult StoryPost(PostsViewModel viewModelPosts)
        {
            //Don't do any redirects, the slug is null when form is posted.


            //This is how you get the current user id.
            string userId         = "";
            var    claimsIdentity = User.Identity as ClaimsIdentity;

            if (claimsIdentity != null)
            {
                // the principal identity is a claims identity.
                // now we need to find the NameIdentifier claim
                var userIdClaim = claimsIdentity.Claims
                                  .FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);

                if (userIdClaim != null)
                {
                    userId = userIdClaim.Value;
                }
            }

            Posts posts = new Posts();

            posts.StoryId    = Int64.Parse(RouteData.Values["Id"].ToString()); //what if user changes id parameter in url then presses post??
            posts.UserId     = userId;
            posts.Username   = User.Identity.Name;
            posts.ReplyCount = 0;
            posts.LikeCount  = 0;
            posts.PostDate   = DateTime.Now.ToString("MM/dd/yyyy h:mm tt");
            posts.Post       = viewModelPosts.Post;
            posts.IsActive   = 1;

            if (ModelState.IsValid)
            {
                //Save to Database
                var db = new CareersDataContext();
                db.Posts.Add(posts);
                db.SaveChanges();

                //increment post count for current story
                var story = db.Stories.Find(Int64.Parse(RouteData.Values["Id"].ToString()));
                story.PostCount      += 1;
                db.Entry(story).State = EntityState.Modified;
                db.SaveChanges();


                //use StringBuilder here for optimization
                return(Redirect(@"~\" + "careers/" + RouteData.Values["careerName"].ToString() + "/" + RouteData.Values["Id"].ToString() + "/" + getSlug(posts.StoryId)));
            }

            return(Story()); //return the getStory action so that the new reply or post will be shown.
        }
コード例 #2
0
        public ActionResult Create(StoriesCreateViewModel viewModelStories) //Look into ViewModels, more work but may be worth it.
        {
            //sanitize url
            string inputUrlParam;
            string cleanUrlParam;

            if (RouteData.Values["careerName"] == null)
            {
                return(Redirect("/careers/administrative-assistant"));
            }
            else
            {
                inputUrlParam = RouteData.Values["careerName"].ToString();
                cleanUrlParam = URLFriendly(inputUrlParam);

                if (!inputUrlParam.Equals(cleanUrlParam))
                {
                    return(Redirect(cleanUrlParam));
                }
            }

            bool careerNameUrlFound = false;

            string careerNameUrl = "";

            if (RouteData.Values["careerName"] != null)
            {
                careerNameUrl = RouteData.Values["careerName"].ToString();
                careerNameUrl = careerNameUrl.Replace("-", " ");
            }

            var db   = new CareersDataContext();
            var list = db.Careers.Where(u => u.IsActive == 1).ToList();

            for (int i = 0; i < list.Count(); i++)
            {
                if (list.ElementAt(i).CareerName.ToLower().Equals(careerNameUrl))
                {
                    careerNameUrlFound = true;
                }
            }

            if (!careerNameUrlFound)
            {
                return(Redirect("/careers/administrative-assistant")); //anything after careers/ that is not valid will be redirected to aa.
            }
            ////////////////////////end redirect check/////////////////////////////////////////////

            //This is how you get the current user id.
            string userId         = "";
            var    claimsIdentity = User.Identity as ClaimsIdentity;

            if (claimsIdentity != null)
            {
                // the principal identity is a claims identity.
                // now we need to find the NameIdentifier claim
                var userIdClaim = claimsIdentity.Claims
                                  .FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);

                if (userIdClaim != null)
                {
                    userId = userIdClaim.Value;
                }
            }

            Stories stories = new Stories();

            stories.Title     = viewModelStories.Title;
            stories.Education = viewModelStories.Education;
            stories.Company   = viewModelStories.Company;
            stories.Salary    = viewModelStories.Salary;
            stories.Location  = viewModelStories.Location;
            stories.Story     = viewModelStories.Story;

            stories.CareerId   = getCareerId(RouteData.Values["careerName"].ToString().Replace("-", " "));
            stories.CareerName = RouteData.Values["careerName"].ToString().Replace("-", " ");
            stories.UserId     = userId; //MUST CHANGE to current user id!
            stories.Username   = User.Identity.Name;

            stories.PostDate         = DateTime.Now.ToString("M/dd/yy"); //account for time difference
            stories.StarCount        = 0;
            stories.PostCount        = 0;
            stories.FunnyCount       = 0;
            stories.InformativeCount = 0;
            stories.IsActive         = 1;

            if (stories.Title == null)
            {
                stories.Title = "";
            }
            if (stories.Education == null)
            {
                stories.Education = "";
            }
            if (stories.Company == null)
            {
                stories.Company = "";
            }

            if (ModelState.IsValid)
            {
                //Save to Database
                db = new CareersDataContext();
                db.Stories.Add(stories);
                db.SaveChanges();

                var id = stories.Id; //grabs the recently added story's id.

                //use StringBuilder here for optimization
                return(Redirect(@"~\" + "careers/" + RouteData.Values["careerName"].ToString() + "/" + id + "/" + stories.Title));
            }

            return(Create()); //returns to the Get Create action
        }