コード例 #1
0
ファイル: HomeController.cs プロジェクト: Alerter000/NewsBlog
 public ActionResult AddComment(NewsAndComments comments)
 {
     if (Session["LoginUserID"] != null)
     {
         using (UnitOfWork unit = new UnitOfWork())
         {
             UserRepository userRep = new UserRepository(unit.DataContext);
             NewsRepository newsRep = new NewsRepository(unit.DataContext);
             CommentRepository comRep = new CommentRepository(unit.DataContext);
             //News news = newsRep.GetNewsById(id);
             User user = userRep.GetUserById(new Guid((Session["LoginUserID"]).ToString()));
             CommentService comServ = new CommentService(unit.DataContext);
             comments.News = newsRep.GetNewsById(new Guid((Session["NewsId"]).ToString()));
             //comments.News.Comments = comRep.GetCommentsByPublicationId(new Guid((Session["NewsId"]).ToString())).ToList();
             //comments.News.Owner = user;
             comServ.Create(user, comments.News, comments.CurrentComment);
             unit.Commit();
             comments.Comments = newsRep.GetNewsById(comments.News.NewsId).Comments;
         }
     }
     if (Request.IsAjaxRequest())
     {
         return PartialView("_GridForComments", comments);
     }
     return RedirectToAction("ReadMore/" + comments.News.NewsId.ToString());
 }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: Alerter000/NewsBlog
        public ActionResult AddNews(News news, HttpPostedFileBase upload)
        {
            byte[] file = null;
            if (ModelState.IsValid)
            {
                if (upload != null)
                {
                    file = ConverterFileToBytes.Convert(upload);
                }
                using (UnitOfWork unit = new UnitOfWork())
                {
                    UserRepository userRep = new UserRepository(unit.DataContext);
                    User user = userRep.GetUserById(new Guid(Session["LoginUserID"].ToString()));
                    if (user != null)
                    {
                        NewsService newServ = new NewsService(unit.DataContext);
                        newServ.Create(user, news.Title, news.Content, news.Category, file);
                        unit.Commit();
                    }
                }
            }

            return RedirectToAction("Index");
        }
コード例 #3
0
ファイル: HomeController.cs プロジェクト: Alerter000/NewsBlog
        public ActionResult Register(User user)
        {
            if (ModelState.IsValid)
            {
                using (UnitOfWork unit = new UnitOfWork())
                {
                    UserRepository userRep = new UserRepository(unit.DataContext);
                    User u = userRep.GetUserByName(user.Name);
                    if (u != null)
                    {
                        ViewBag.Message = "Пользователь с таким именем уже есть";
                        return View(user);
                    }
                    UserService userServ = new UserService(unit.DataContext);
                    userServ.Create(user.Name, user.Password, user.EMail);
                    unit.Commit();
                    Session["LoginUserID"] = user.UserId;
                    Session["LogedUserName"] = user.Name;
                    ModelState.Clear();
                    user = null;
                    ViewBag.Message = "Регистрация успешно завершена";
                    return RedirectToAction("Index");
                }

            }
            return View(user);
        }
コード例 #4
0
ファイル: HomeController.cs プロジェクト: Alerter000/NewsBlog
 public ActionResult Login(User user)
 {
     if (ModelState.IsValid)
     {
         using (UnitOfWork unit = new UnitOfWork())
         {
             UserRepository userRep = new UserRepository(unit.DataContext);
             var authUser = userRep.GetUserByNameAndPass(user.Name, user.Password);
             if (authUser != null)
             {
                 Session["LoginUserID"] = authUser.UserId.ToString();
                 Session["LogedUserName"] = authUser.Name;
                 return RedirectToAction("Index");
             }
         }
     }
     return View(user);
 }
コード例 #5
0
ファイル: HomeController.cs プロジェクト: Alerter000/NewsBlog
 public ActionResult Edit(News news, HttpPostedFileBase upload)
 {
     if (ModelState.IsValid && Session["LoginUserID"] != null)
     {
         byte[] file = null;
         if (upload != null)
             file = ConverterFileToBytes.Convert(upload);
         using (UnitOfWork unit = new UnitOfWork())
         {
             NewsService newsServ = new NewsService(unit.DataContext);
             NewsRepository newsRep = new NewsRepository(unit.DataContext);
             UserRepository userRep = new UserRepository(unit.DataContext);
             CommentRepository comRep = new CommentRepository(unit.DataContext);
             news.NewsId = new Guid(Session["NewsId"].ToString());
             news.Image = file;
             news.DatePublication = newsRep.GetNewsById(news.NewsId).DatePublication;
             news.Owner = userRep.GetUserById(new Guid(Session["LoginUserID"].ToString()));
             news.Comments = comRep.GetCommentsByPublicationId(new Guid(Session["LoginUserID"].ToString())).ToList();
             newsServ.Edit(news);
             unit.Commit();
         }
         return RedirectToAction("Index");
     }
     return HttpNotFound();
 }