예제 #1
0
        public ActionResult Like(int messageId)
        {
            using (var db = new MessimContext())
            {
                var message = db.Messages.SingleOrDefault(x => x.ID == messageId);

                if (message == null)
                {
                    //FIXME: log error
                    return new JsonResult() { Data = new { ConsoleMessage = "no such message in DB" } };
                }

                message.LikeAmount += 1;
                db.Entry(message).State = EntityState.Modified;
                db.SaveChanges();
                return new JsonResult { Data = new { ConsoleMessage = "Message with " + messageId + " liked" } };
            }
        }
예제 #2
0
        public ActionResult Index()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Login");
            }
            var db = new MessimContext();

            var user = db.Users.Single(x => x.Username == User.Identity.Name);

            ViewData["PostNumber"] = db.Messages.Count(x => x.Sender.ID == user.ID);
            var userMessages = db.Messages.Where(x => x.Sender.ID == user.ID).ToList();
            var subscipsionsMessages = (from msg in db.Messages
                                        from usr in user.Subscribents
                                        where msg.Sender.ID == usr.ID
                                        select msg).ToList();

            var messageUnion = userMessages.Union(subscipsionsMessages).ToList();
            messageUnion.Sort((x, y) => y.Date.CompareTo(x.Date));
            ViewData["DisplayMessages"] = messageUnion;
            ViewData["Sender"] = user.ID;

            return View();
        }
예제 #3
0
 public HomeController()
 {
     dbContext = new MessimContext();
 }
예제 #4
0
 public override bool ValidateUser(string username, string password)
 {
     if (string.IsNullOrEmpty(password.Trim())) return false;
     string hash = SHA.CreateSHA1Hash(password);
     using (var _db = new MessimContext())
     {
         User user = _db.Users.SingleOrDefault(x => x.Username == username);
         if (user == null) return false;
         if (user.Password == hash)
         {
             return true;
         }
         return false;
     }
 }
예제 #5
0
        public ActionResult Register(string username, string password, string passwordAgain)
        {
            if (!ValidateRegister(username, password, passwordAgain))
            {
                return View();
            }
            using (var _db = new MessimContext())
            {
                User newUser = new User { Username = username, Password = SHA.CreateSHA1Hash(password) };
                _db.Users.Add(newUser);
                _db.SaveChanges();
            }

            ViewBag.Success = true;
            return View();
        }
예제 #6
0
        private bool ValidateRegister(string username, string password, string passwordAgain)
        {
            if (String.IsNullOrEmpty(username))
            {
                ModelState.AddModelError("username", "Musisz podać nazwę użytkownika");
            }
            if (String.IsNullOrEmpty(password))
            {
                ModelState.AddModelError("password", "Musisz podać hasło");
            }
            if (password != passwordAgain)
            {
                ModelState.AddModelError("_FORM", "Hasła muszą być takie same");
            }
            using (var _db = new MessimContext())
            {
                if (_db.Users.ToList().Any(x => x.Username == username))
                {
                    ModelState.AddModelError("username", "Nazwa użytkownika jest już zajęta");
                }

            }
            return ModelState.IsValid;
        }
예제 #7
0
 public JsonResult Send(string messageText, HttpPostedFileBase messageImage)
 {
     string path = null;
     if (messageImage.ContentLength > 0)
     {
         var fileName = Path.GetFileName(messageImage.FileName);
         path = Path.Combine(Server.MapPath("~/Content/uploads"), fileName);
         messageImage.SaveAs(path);
     }
     var image = new Bitmap(path);
     using (var db = new MessimContext())
     {
         var user = db.Users.Single(x => x.Username == User.Identity.Name);
         var newImage = new Image { URL = "/Content/uploads/" + Path.GetFileName(messageImage.FileName), Width = image.Width, Height = image.Height };
         var newMessage = new Message { Text = messageText, Date = DateTime.Now, LikeAmount = 0, Sender = user, Image = newImage };
         db.Messages.Add(newMessage);
         db.SaveChanges();
     }
     // Return JSON
     return new JsonResult { Data = new { Msg = "Success" } };
 }
예제 #8
0
 public UserController()
 {
     dbContext = new MessimContext();
 }
예제 #9
0
 public MessageController()
 {
     dbContext = new MessimContext();
 }