コード例 #1
0
ファイル: HomeController.cs プロジェクト: AndreiMarhatau/Blog
        public async Task <IActionResult> SignIn(string Login, string Password)
        {
            try
            {
                Guid id = await userService.CheckUser(Login, Password);

                if (!(await tokenService.GetUserIdByToken(CookieController.GetOrGenerateToken(HttpContext))).Equals(id))
                {
                    await tokenService.AddToken(CookieController.GetOrGenerateToken(HttpContext), id);
                }
                return(RedirectToAction("Index", "Profile"));
            }
            catch (Exception e)
            {
                if (e is InvalidOperationException)
                {
                    ViewBag.Alert = "Неверный логин или пароль";
                    return(View());
                }
                else if (e is ArgumentNullException)
                {
                    ViewBag.Alert = "Одно или несколько полей пустые";
                    return(View());
                }
                else
                {
                    throw;
                }
            }
        }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: AndreiMarhatau/Blog
        public async Task <IActionResult> SignUp(string Login, string Name, string Surname, DateTime BornDate, string Email, string Password)
        {
            try
            {
                Guid id = await userService.AddUser(Login, Name, Surname, BornDate, Email, Password);

                await tokenService.AddToken(CookieController.GetOrGenerateToken(HttpContext), id);

                return(RedirectToAction("Index", "Profile"));
            }
            catch (Exception e)
            {
                if (e is ArgumentNullException)
                {
                    ViewBag.Alert = "Одно или несколько полей пустые";
                    return(View());
                }
                else if (e is ArgumentException)
                {
                    ViewBag.Alert = "Логин и(или) Email заняты";
                    return(View());
                }

                else
                {
                    throw;
                }
            }
        }
コード例 #3
0
ファイル: HomeController.cs プロジェクト: AndreiMarhatau/Blog
        public async Task <IActionResult> Sign(SignMethod sign)
        {
            string defaultView = sign == SignMethod.SignIn ? "signin" : "signup";

            if (!HttpContext.Request.Cookies.ContainsKey("Token"))
            {
                ViewBag.Token = CookieController.GetOrGenerateToken(HttpContext);
                return(View(defaultView));
            }
            //Check token in the cookie
            var token = CookieController.GetOrGenerateToken(HttpContext);

            try
            {
                //Check token and user in DB
                await userService.GetUserById(await tokenService.GetUserIdByToken(token));

                return(RedirectToAction("Index", "Profile"));
            }
            //Catching all exceptions for returning to user a view (DataBase is in safety)
            catch (Exception)
            {
                return(View(defaultView));
            }
        }
コード例 #4
0
        public async Task <IActionResult> SearchUsers(string Name, string Surname, string Login)
        {
            //Check authenticate before give access
            var token = CookieController.GetOrGenerateToken(HttpContext);

            try
            {
                await tokenService.GetUserIdByToken(token);
            }
            catch (Exception e)
            {
                if (e is ArgumentNullException || e is InvalidOperationException)
                {
                    return(RedirectToAction("SignIn", "Home"));
                }
                throw;
            }

            //Create result list of search
            List <User> resultList = await userService.SearchUsers(Login, Name, Surname);

            string pathBase = HttpContext.Request.PathBase;

            return(View("Search", new Tuple <List <User>, string>(resultList, pathBase)));
        }
コード例 #5
0
        public async Task <IActionResult> Index(string id)
        {
            var  token   = CookieController.GetOrGenerateToken(HttpContext);
            bool isOwner = false;

            var userId = await tokenService.GetUserIdByToken(token);

            Guid Id;

            //Check get data
            try
            {
                if (id == null || id == userId.ToString())
                {
                    Id      = userId;
                    isOwner = true;
                }
                else
                {
                    Id = Guid.Parse(id);
                }
            }
            catch (Exception e)
            {
                if (e is ArgumentNullException || e is InvalidOperationException)
                {
                    return(RedirectToAction("SignIn", "Home"));
                }
                throw;
            }

            //Add content on the page
            try
            {
                ViewBag.User = await userService.GetUserById(Id);
            }
            catch (Exception e)
            {
                if (e is ArgumentNullException || e is InvalidOperationException)
                {
                    return(RedirectToAction("SignIn", "Home"));
                }
                throw;
            }

            //Add list of posts and comments to view model
            Tuple <bool, List <Post> > Model =
                new Tuple <bool, List <Post> >(
                    isOwner,
                    await commentsAndPostsService.GetCommentsAndPostsByUserId(Id)
                    );

            return(View("Profile", Model));
        }
コード例 #6
0
        public async Task <IActionResult> AddOrRemoveLikeToComment(string UserId, string CommentId)
        {
            await likeService.AddOrRemoveLike(
                await tokenService
                .GetUserIdByToken(
                    CookieController.GetOrGenerateToken(HttpContext)),
                new Comment()
            {
                Id = Guid.Parse(CommentId)
            });

            return(RedirectToAction("Index", new { id = UserId }));
        }
コード例 #7
0
 public async Task <IActionResult> AddPost(string Text)
 {
     try
     {
         await postsService.AddPost(await tokenService.GetUserIdByToken(CookieController.GetOrGenerateToken(HttpContext)), Text);
     }
     catch (Exception e)
     {
         if (!(e is ArgumentNullException || e is InvalidOperationException))
         {
             throw;
         }
         return(RedirectToAction("SignIn", "Home"));
     }
     return(RedirectToAction("Index"));
 }
コード例 #8
0
 public async Task <IActionResult> AddComment(string PostId, string UserId, string Text, string CommentId)
 {
     try
     {
         await commentsService.AddComment(Guid.Parse(PostId), await tokenService.GetUserIdByToken(CookieController.GetOrGenerateToken(HttpContext)), Guid.Parse(CommentId), Text);
     }
     catch (Exception e)
     {
         if (!(e is ArgumentNullException || e is InvalidOperationException))
         {
             throw;
         }
         return(RedirectToAction("SignIn", "Home"));
     }
     return(RedirectToAction("Index", new { id = UserId }));
 }