Exemplo n.º 1
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" } };
 }
Exemplo n.º 2
0
        private void SendMessageToDatabase(string messageText, HttpPostedFileBase messageImage, int? replyTo = null)
        {
            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);
            var user = dbContext.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,
                                     WhoLikes = new List<User>(),
                                     Sender = user,
                                     Image = newImage
                                 };

            if (replyTo != null)
            {
                var messageReplaingTo = dbContext.Messages.First(x => x.ID == replyTo);
                newMessage.ReplyTo = messageReplaingTo;
            }

            dbContext.Messages.Add(newMessage);
            dbContext.SaveChanges();
        }