public IHttpActionResult PutStudentPost(int id, StudentPost studentPost)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != studentPost.StudentId)
            {
                return(BadRequest());
            }

            db.Entry(studentPost).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentPostExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PostStudentPost(StudentPost studentPost)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.StudentPosts.Add(studentPost);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (StudentPostExists(studentPost.StudentId))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = studentPost.StudentId }, studentPost));
        }
        public IHttpActionResult GetStudentPost(int id)
        {
            StudentPost studentPost = db.StudentPosts.Find(id);

            if (studentPost == null)
            {
                return(NotFound());
            }

            return(Ok(studentPost));
        }
        public IHttpActionResult SetVoted(int studentId, int id)
        {
            StudentPost studentPost = db.StudentPosts.FirstOrDefault(p => p.StudentId == studentId && p.PostId == id);

            if (studentPost == null)
            {
                return(NotFound());
            }
            studentPost.Voted = true;
            db.SaveChanges();
            return(Ok(studentPost));
        }
        public IHttpActionResult DeleteStudentPost(int id)
        {
            StudentPost studentPost = db.StudentPosts.Find(id);

            if (studentPost == null)
            {
                return(NotFound());
            }

            db.StudentPosts.Remove(studentPost);
            db.SaveChanges();

            return(Ok(studentPost));
        }
        public IHttpActionResult GetVoted(int studentId, int id)
        {
            StudentPost studentPost = db.StudentPosts.FirstOrDefault(p => p.StudentId == studentId && p.PostId == id);

            if (studentPost == null)
            {
                return(NotFound());
            }
            StudentPostDto dto = new StudentPostDto()
            {
                PostId    = studentPost.PostId,
                StudentId = studentPost.StudentId,
                Voted     = studentPost.Voted
            };

            return(Ok(dto));
        }
Ejemplo n.º 7
0
        public ActionResult Create(Post post)
        {
            if (ModelState.IsValid)
            {
                string  currentStudentId = User.Identity.GetUserId();
                Student student          = db.Students.FirstOrDefault(s => s.UserId.Equals(currentStudentId));
                post.StudentId = student.Id;
                post.Student   = student;
                post.TimeAsked = DateTime.Now;
                StudentPost sp = new StudentPost()
                {
                    Post    = post,
                    Student = student,
                    Voted   = false
                };
                db.Posts.Add(post);
                db.StudentPosts.Add(sp);
                db.SaveChanges();
                return(RedirectToAction("ViewPostsForStudent", "Students"));
            }

            ViewBag.SubjectId = new SelectList(db.Subjects, "Id", "Name", post.SubjectId);
            return(View(post));
        }
Ejemplo n.º 8
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var u = UserManager.FindByEmail(model.Email);
                if (u != null)
                {
                    ModelState.AddModelError("Username", "This username already exists.");
                    return(View(model));
                }
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    UserManager.AddToRole(user.Id, "User");
                    Student student = new Student()
                    {
                        FirstName = model.FirstName,
                        LastName  = model.LastName,
                        UserName  = model.Email,
                        City      = model.City,
                        UserId    = UserManager.FindByEmail(model.Email).Id
                    };
                    List <Post>    posts    = db.Posts.ToList();
                    List <Comment> comments = db.Comments.ToList();
                    foreach (Post post in posts)
                    {
                        StudentPost sp = new StudentPost()
                        {
                            Post    = post,
                            Student = student,
                            Voted   = false
                        };
                        db.StudentPosts.Add(sp);
                    }
                    foreach (Comment comment in comments)
                    {
                        StudentComment sc = new StudentComment()
                        {
                            Comment = comment,
                            Student = student,
                            Voted   = false
                        };
                        db.StudentComments.Add(sc);
                    }
                    db.Students.Add(student);
                    db.SaveChanges();
                    return(RedirectToAction("Login", "Account"));
                }
                AddErrors(result);
            }

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