Esempio n. 1
0
        // Возвращает только что добавленную запись
        public UserImage AddUserImage(int user_id, int image_id)
        {
            UserImage userImage = new UserImage();
            if (user_id != null && image_id != null)
            {
                UserImageContext userImageContext = new UserImageContext();
                userImage.user_id = user_id;
                userImage.image_id = image_id;

                userImageContext.UserImages.Add(userImage);
                userImageContext.SaveChanges();
                var imId = userImageContext.UserImages.Select(id => id.id).Max();
                userImage.id = imId;
                return userImage;
            }
            else return null;
        }
Esempio n. 2
0
        public ActionResult ChangeProfile() //Обработка данных с формы изменения профиля пользователя
        {
            if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account");

            IUsersContext _user = new UsersContext();
            IImagesContext _image = new ImagesContext();
            IUserImageContext _userImage = new UserImageContext();
            User user = new User();
            user = _user.GetUserByLogin((string)Session["login"]);

            ViewBag.user = user;

            user.name = Request.Form["name"];
            user.last_name = Request.Form["last_name"];
            user.gender = Request.Form["gender"];
            user.e_mail = Request.Form["e_mail"];

            var file = Request.Files["image"];

            if (file.ContentLength != 0)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "images/users/";
                string filename = user.id.ToString() + Path.GetExtension(file.FileName);
                if (filename != null) file.SaveAs(path + filename);

                if (_userImage.GetImageByUserId(user.id) == null)
                {
                    Image image = new Image();
                    image.image_path = filename;
                    image = _image.AddImage(image.image_path);
                    _userImage.AddUserImage(user.id, image.id);
                }
                else
                {
                    Image image = new Image();
                    image = _userImage.GetImageByUserId(user.id);
                    image.image_path = filename;
                    image = _image.EditImage(image);
                }
            }

            /*-------------- Обращение к БД ------------------------*/

            try
            {
                _user.EditUser(user);
                return RedirectToAction("userProfile", "Account");
            }
            catch
            {
                return View();
            } 
        }
Esempio n. 3
0
        public ActionResult UserProfile() // Загрузка представленя страницы пользователя
        {
            if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account");
            IUsersContext _user = new UsersContext();
            IPostsContext postsContext = new PostsContext();
            IUserImageContext userImageContext = new UserImageContext();
            ICategoriesContext catContext = new CategoriesContext();

            List<Categories> categories = new List<Categories>();
            if (catContext.GetAllCategories() != null)
            {
                categories = catContext.GetAllCategories().ToList();
                ViewBag.categories = categories;
            }
            ViewBag.catContext = catContext;

            int s = 0;
            if (Request.QueryString.Count == 0) s = 0;
            else
                s = Convert.ToInt32(Request.QueryString["user"]);
            if (s == null || s == 0) s = _user.GetUserByLogin(Convert.ToString(Session["login"])).id;
            User user = new User();
            user = _user.GetUserById(s);

            ViewBag.login = user.login;
            ViewBag.user = user;
            if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false)
                ViewBag.sessionUser = _user.GetUserByLogin(Session["login"].ToString());

            IEnumerable<Post> userPosts;
            userPosts = postsContext.GetPostsByUserId(user.id);

            IPostImageContext pic = new PostImageContext();
            List<Image> postImageList = new List<Image>();
            Image userImage = new Image();
            userImage = userImageContext.GetImageByUserId(user.id);
            if (userImage != null)
                ViewBag.userImage = userImage.image_path;
            else
                ViewBag.userImage = "default.png";

            if (userPosts != null)
            {
                foreach (var p in userPosts)
                {
                    postImageList.Add(pic.GetImageByPostId(p.id));
                }
            }
            ViewBag.posts = userPosts;
            if (postImageList != null)
                ViewBag.postImageList = postImageList;
            return View();
        }