Esempio n. 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" } };
            }
        }
Esempio n. 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();
        }
Esempio n. 3
0
 public HomeController()
 {
     dbContext = new MessimContext();
 }
 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;
     }
 }
Esempio n. 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();
        }
Esempio n. 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;
        }
Esempio n. 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" } };
 }
Esempio n. 8
0
 public UserController()
 {
     dbContext = new MessimContext();
 }
Esempio n. 9
0
 public MessageController()
 {
     dbContext = new MessimContext();
 }