Пример #1
0
        public ActionResult DeleteNote(int noteId)
        {
            User currentUser = GetCurrentUser();

            if (currentUser == null || !UserHasLoggedIn(currentUser.Id))
            {
                return(View("LogIn", new LogInModel()));
            }

            using (DrawMapModel context = new DrawMapModel())
            {
                Note note = context.Notes.SingleOrDefault(n => n.Id == noteId);
                if (note == null || note.UserId != currentUser.Id)
                {
                    ViewBag.ErrorMessage = "Note with id = '" + noteId +
                                           "' does not exist or it is unavailable for the current user";
                    return(View("Error"));
                }

                context.Notes.Remove(note);
                context.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
Пример #2
0
        public ActionResult LogIn(string email, string password)
        {
            using (DrawMapModel context = new DrawMapModel())
            {
                User dbUser = context.Users.SingleOrDefault(u => u.Email == email);

                if (dbUser == null)
                {
                    dbUser = new User {
                        Email = email, Password = password
                    };
                    context.Users.Add(dbUser);
                    context.SaveChanges();
                }
                else if (dbUser.Password != password)
                {
                    return(View("Login",
                                new LogInModel
                    {
                        HasError = true,
                        ErrorMessage = "User with the same login exists, but the password is incorrect"
                    }));
                }

                Response.Cookies.Add(new HttpCookie("authorization", String.Concat(email, " - ", password)));
                Session.Add(dbUser.Id.ToString(), true);

                return(RedirectToAction("Index"));
            }
        }
Пример #3
0
        public ActionResult ViewMap(int noteId)
        {
            using (DrawMapModel context = new DrawMapModel())
            {
                Note note = context.Notes.SingleOrDefault(n => n.Id == noteId);
                if (note != null)
                {
                    return(View("ViewMap", note));
                }

                return(View("Error"));
            }
        }
Пример #4
0
        //
        // GET: /Home/

        public ActionResult Index()
        {
            using (DrawMapModel context = new DrawMapModel())
            {
                User currentUser = GetCurrentUser();
                if (currentUser == null || !UserHasLoggedIn(currentUser.Id))
                {
                    return(View("Login", new LogInModel()));
                }

                return(View(context.Notes.Where(n => n.UserId == currentUser.Id).ToList()));
            }
        }
Пример #5
0
        public ActionResult ViewMap(int noteId)
        {
            using (DrawMapModel context = new DrawMapModel())
            {
                Note note = context.Notes.SingleOrDefault(n => n.Id == noteId);
                if (note != null)
                {
                    return(View("ViewMap", note));
                }

                ViewBag.ErrorMessage = "Note with id = '" + noteId + "' does not exist";
                return(View("Error"));
            }
        }
Пример #6
0
        //private ActionResult ReturnErrorView(string message)
        //{
        //    return
        //}

        public ActionResult AddNote(string name, string content)
        {
            User currentUser = GetCurrentUser();

            if (currentUser == null)
            {
                return(View("Error"));
            }

            using (DrawMapModel context = new DrawMapModel())
            {
                context.Notes.Add(new Note {
                    Name = name, Content = content, UserId = currentUser.Id
                });
                context.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Пример #7
0
        private User GetCurrentUser()
        {
            HttpCookie authorizationCookie = Request.Cookies.Get("authorization");

            if (authorizationCookie == null)
            {
                return(null);
            }

            int separatorPosition = authorizationCookie.Value.IndexOf(" - ");

            string email    = authorizationCookie.Value.Substring(0, separatorPosition);
            string password = authorizationCookie.Value.Substring(separatorPosition + 3);

            using (DrawMapModel context = new DrawMapModel())
            {
                return(context.Users.SingleOrDefault(u => u.Email == email && u.Password == password));
            }
        }
Пример #8
0
        //
        // GET: /Home/

        public ActionResult Index()
        {
            using (DrawMapModel context = new DrawMapModel())
            {
                HttpCookie authorizationCookie = Request.Cookies.Get("authorization");

                if (authorizationCookie == null)
                {
                    return(View("Login"));
                }

                User currentUser = GetCurrentUser();
                if (currentUser == null)
                {
                    return(View("Error"));
                }

                return(View(context.Notes.Where(n => n.UserId == currentUser.Id).ToList()));
            }
        }
Пример #9
0
        public ActionResult LogIn(string email, string password)
        {
            using (DrawMapModel context = new DrawMapModel())
            {
                User dbUser = context.Users.SingleOrDefault(u => u.Email == email);

                if (dbUser == null)
                {
                    context.Users.Add(new User {
                        Email = email, Password = password
                    });
                    context.SaveChanges();
                }
                else if (dbUser.Password != password)
                {
                    return(View("Error"));
                }

                Response.Cookies.Add(new HttpCookie("authorization", String.Concat(email, " - ", password)));
                return(RedirectToAction("Index"));
            }
        }
Пример #10
0
        public ActionResult RenameNote(int noteId, string name)
        {
            User currentUser = GetCurrentUser();

            if (currentUser == null)
            {
                return(View("Error"));
            }

            using (DrawMapModel context = new DrawMapModel())
            {
                Note note = context.Notes.SingleOrDefault(n => n.Id == noteId);
                if (note == null || note.UserId != currentUser.Id)
                {
                    return(View("Error"));
                }

                note.Name = name;
                context.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }