public async Task <ActionResult> AddComment(CommentViewModel model)
        {
//            ViewBag.str = model.Reply_to + ' ' + model.Text + ' ' + model.Theme + ' ' + model.Author.Id;

            if (!ModelState.IsValid)
            {
                return(PartialView(new CommentViewModel()));
            }

            var  db = new GeeksForLessTestDBEntities();
            long temp;

            long.TryParse(model.Reply_to.ToString(), out temp);

            var message = new Themes_messages()
            {
                Theme            = model.Theme,
                Text             = model.Text,
                Reply_to         = temp == 0 ? null : (long?)temp,
                Publication_date = DateTime.Now,
                Author           = User.Identity.GetIdOfUser()
            };

            db.Themes_messages.Add(message);
            await db.SaveChangesAsync();

            return(RedirectToAction("GetTheme", "Themes", new { ID = model.Theme }));
        }
Esempio n. 2
0
        public ActionResult GetTheme(long ID = 0)
        {
            if (ID == 0)
            {
                return(View("Error"));
            }

            var db = new GeeksForLessTestDBEntities();

            var theme = db.Themes.FirstOrDefault(Theme => Theme.Id == ID);
            var likes = db.Likes.Where(Like => Like.Target == theme.Id);

            db.Configuration.LazyLoadingEnabled = false;

            var Messages     = db.Themes_messages.Where(themeMessage => themeMessage.Theme == theme.Id).Include(t => t.AspNetUsers);
            var MessageLikes = new List <CommentLikesView>();

            foreach (var message in Messages)
            {
                var Likes = db.Likes.Where(Like => Like.Target == message.Id /* && Like.Target_type == 1*/);
                MessageLikes.Add(new CommentLikesView()
                {
                    Comment = message, Likes = Likes
                });
            }

            ViewBag.user = db.AspNetUsers.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
            var themeThemeMessage = new ThemeMessagesModel()
            {
                Theme = theme, MessagesLikes = MessageLikes, Likes = likes
            };

            return(View(themeThemeMessage));
        }
        public async Task <ActionResult> UpdateAvatar(string ID)
        {
            var db         = new GeeksForLessTestDBEntities();
            var userAvatar = db.AspNetUsers.FirstOrDefault(m => m.Id == ID);


            if (string.IsNullOrEmpty(ID) || Request.Files["avatar"] == null)
            {
                return(PartialView("UserAvatar", userAvatar.Avatar));
            }

            var    file = @Request.Files["avatar"];
            string fileName;
            string filePath;
            string json = string.Empty;

            fileName = file.FileName;
            Directory.CreateDirectory(Server.MapPath("~/Content/img/"));
            filePath = Path.Combine(Server.MapPath("~/Content/img/" + fileName));
            file.SaveAs(filePath);



            userAvatar.Avatar = "/Content/img/" + fileName;
            await db.SaveChangesAsync();

            return(PartialView("UserAvatar", userAvatar.Avatar ?? ""));
        }
Esempio n. 4
0
        public async Task <ActionResult> AddTheme(AddThemeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            using (var db = new GeeksForLessTestDBEntities())
            {
                long temp;
                long.TryParse(model.MainThemeId, out temp);
                string authorID = db.AspNetUsers.Where(user => user.UserName == User.Identity.Name).FirstOrDefault().Id;
                var    theme    = new Themes
                {
                    Name             = model.Name,
                    Text             = model.Text,
                    Main_theme       = temp == 0 ? null : (long?)temp,
                    Publication_date = DateTime.Now,
                    Author           = authorID
                };

                db.Themes.Add(theme);
                await db.SaveChangesAsync();

                return(RedirectToAction("GetTheme", "Themes", new { ID = theme.Id }));
            }
        }
Esempio n. 5
0
        public async Task <ActionResult> RemoveTheme(long ID)
        {
            var db = new GeeksForLessTestDBEntities();

            db.Themes.Remove(db.Themes.FirstOrDefault(theme => theme.Id == ID));

            await db.SaveChangesAsync();

            return(RedirectToAction("Index", "Themes"));
        }
        public async Task <ActionResult> RemoveComment(long?ID)
        {
            var db      = new GeeksForLessTestDBEntities();
            var comment = db.Themes_messages.Find(ID);
            var theme   = comment.Themes;

            db.Themes_messages.Remove(comment);

            await db.SaveChangesAsync();

            return(RedirectToAction("GetTheme", "Themes", new { ID = theme.Id }));
        }
        public ActionResult Index(string ID)
        {
            if (string.IsNullOrEmpty(ID))
            {
                return(RedirectToAction("Index", "Themes"));
            }

            var db = new GeeksForLessTestDBEntities();

            var user = db.AspNetUsers.FirstOrDefault(m => m.Id == ID);

            return(View(user));
        }
Esempio n. 8
0
        public ActionResult _PopularThemes()
        {
            var db         = new GeeksForLessTestDBEntities();
            var themesList = new List <ThemeModel>();

            foreach (var theme in db.Themes)
            {
                var themeMessages = db.Themes_messages.Where(themeMessage => themeMessage.Theme == theme.Id);
                var themeLikes    = db.Likes.Where(Like => Like.Target == theme.Id /* && Like.Target_type == 1*/);

                themesList.Add(new ThemeModel()
                {
                    Theme = theme, Messages = themeMessages, Likes = themeLikes
                });
            }
            return(PartialView(themesList));
        }
        public ActionResult AddComment(long?ID)
        {
            if (!ID.HasValue)
            {
                return(PartialView(new CommentViewModel()));
            }

            var db = new GeeksForLessTestDBEntities();

            var theme = db.Themes.Where(Theme => Theme.Id == ID).FirstOrDefault();
            var user  = db.AspNetUsers.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();

            var themeThemeMessage = new CommentViewModel()
            {
                Theme = theme.Id, Author = user
            };

            return(PartialView(themeThemeMessage));
        }
Esempio n. 10
0
        public ActionResult ChangeTheme(long ID = 0)
        {
            if (ID == 0)
            {
                return(RedirectToAction("Index", "Themes"));
            }

            var db = new GeeksForLessTestDBEntities();

            var Theme = db.Themes.Join(db.AspNetUsers,
                                       T => T.Author,
                                       A => A.Id,
                                       (T, A) => new { Theme = T, Author = A }
                                       ).Where(themes => themes.Theme.Id == ID)
                        .Select(TA => new ChangeThemeViewModel()
            {
                Text = TA.Theme.Text,
                Name = TA.Theme.Name
            })
                        .FirstOrDefault();

            var themeView = new ChangeThemeViewModel();

            themeView.MainTheme = new List <SelectListItem> ()
            {
                new SelectListItem()
                {
                    Text = "Привязать к теме", Value = "0", Selected = true, Disabled = true
                }
            };
            themeView.MainTheme = themeView.MainTheme.Concat(db.Themes.Where(theme => theme.AspNetUsers.UserName == User.Identity.Name && theme.Id != ID)
                                                             .Select(themes => new SelectListItem()
            {
                Text     = themes.Name,
                Value    = themes.Id.ToString(),
                Selected = (themeView.MainThemeId.HasValue && themeView.MainThemeId.Value == themes.Id ? true : false)
            }));
            themeView.Id   = ID;
            themeView.Name = Theme.Name;
            themeView.Text = Theme.Text;

            return(View(themeView));
        }
Esempio n. 11
0
        public ActionResult AddTheme()
        {
            var db        = new GeeksForLessTestDBEntities();
            var themeView = new AddThemeViewModel();

            themeView.MainTheme = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "Привязать к теме", Value = "0", Selected = true, Disabled = true
                }
            };
            themeView.MainTheme = themeView.MainTheme.Concat(db.Themes.Where(theme => theme.AspNetUsers.UserName == User.Identity.Name)
                                                             .Select(themes => new SelectListItem()
            {
                Text  = themes.Name,
                Value = themes.Id.ToString()
            }));
            return(View(themeView));
        }
Esempio n. 12
0
        public async Task <ActionResult> ChangeTheme(ChangeThemeViewModel model, string returnUrl)
        {
            if (model == null)
            {
                return(RedirectToLocal(returnUrl));
            }

            var db    = new GeeksForLessTestDBEntities();
            var Theme = db.Themes.FirstOrDefault(m => m.Id == model.Id);

            if (Theme != null)
            {
                Theme.Main_theme = model.MainThemeId;
                Theme.Name       = model.Name;
                Theme.Text       = model.Text;
            }
            await db.SaveChangesAsync();

            return(RedirectToAction("GetTheme", "Themes", new { ID = model.Id }));
        }
Esempio n. 13
0
        public ActionResult Index(int?page)
        {
            var db         = new GeeksForLessTestDBEntities();
            var themesList = new List <ThemeModel>();


            foreach (var theme in db.Themes)
            {
                var themeMessages = db.Themes_messages.Where(themeMessage => themeMessage.Theme == theme.Id);
                var themeLikes    = db.Likes.Where(Like => Like.Target == theme.Id /* && Like.Target_type == 1*/);

                themesList.Add(new ThemeModel()
                {
                    Theme = theme, Messages = themeMessages, Likes = themeLikes
                });
            }
            int pageNumber = (page ?? 1);

            return(PartialView(themesList.ToPagedList(pageNumber, Properties.Settings.Default.GET_ITEMS_COUNT)));
        }
        public ActionResult ChangeComment(long?ID)
        {
            var returnUrl = Request.UrlReferrer.LocalPath;

            if (ID.HasValue)
            {
                var db = new GeeksForLessTestDBEntities();
                var messageRepeatTo = new ChangeCommentViewModel();
                var message         = db.Themes_messages.FirstOrDefault(m => m.Id == ID.Value);

                messageRepeatTo.Message     = message;
                messageRepeatTo.ReplyToList = new List <SelectListItem>()
                {
                    new SelectListItem()
                    {
                        Text = "Выбрать ответ", Value = "null", Selected = true
                    }
                };
                messageRepeatTo.ReplyToList = messageRepeatTo.ReplyToList.Concat(db.Themes_messages
                                                                                 .Where(m => m.Id != ID.Value && m.Theme == message.Theme && m.Publication_date < message.Publication_date)
                                                                                 .Select(rTo => new SelectListItem()
                {
                    Text     = rTo.Text,
                    Value    = rTo.Id.ToString(),
                    Selected = message.Reply_to == rTo.Id ? true : false
                }));
                if (messageRepeatTo.ReplyToList.Where(m => m.Selected == true).ToList().Count > 1)
                {
                    messageRepeatTo.ReplyToList.First().Selected = false;
                    messageRepeatTo.ReplyToId = int.Parse(messageRepeatTo.ReplyToList.Last().Value);
                }
                messageRepeatTo.Id = ID.Value;

                ViewBag.ReturnUrl = returnUrl;
                return(PartialView(messageRepeatTo));
            }
            return(PartialView(null));
        }
        public async Task <ActionResult> ChangeComment(ChangeCommentViewModel model, string returnUrl)
        {
            if (model == null || !model.Id.HasValue)
            {
                return(RedirectToLocal(returnUrl));
            }

            var db      = new GeeksForLessTestDBEntities();
            var Comment = db.Themes_messages.FirstOrDefault(m => m.Id == model.Id.Value);

            if (Comment != null)
            {
                Comment.Reply_to = model.ReplyToId;
                Comment.Text     = model.Message.Text;
            }
            await db.SaveChangesAsync();


            var theme        = Comment.Themes;
            var Messages     = db.Themes_messages.Where(themeMessage => themeMessage.Theme == theme.Id);
            var MessageLikes = new List <CommentLikesView>();

            foreach (var message in Messages)
            {
                var Likes = db.Likes.Where(Like => Like.Target == message.Id /* && Like.Target_type == 1*/);
                MessageLikes.Add(new CommentLikesView()
                {
                    Comment = message, Likes = Likes
                });
            }

            ViewBag.ReturnUrl = returnUrl;
            ViewBag.user      = db.AspNetUsers.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
            ViewBag.Theme     = theme;
            return(PartialView("Messages", MessageLikes));
        }