Ejemplo n.º 1
0
        public ActionResult CreateMessageForGroup(GroupMessageViewModel messageViewModel)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    HttpPostedFileBase fileUploaded = messageViewModel.Attachment;

                    Message message = new Message()
                    {
                        MessageType = messageViewModel.MessageType,
                        Mode = messageViewModel.Mode,
                        Msg = messageViewModel.Msg,
                        //React = messageViewModel.React,
                    };

                    GroupMessage groupMessage = new GroupMessage()
                    {
                        GroupId = messageViewModel.groupMessage.GroupId,
                        MessageId = message.MessageId,
                    };

                    if (fileUploaded != null && fileUploaded.ContentLength > 0)
                    {
                        Resource resource = new Resource()
                        {
                            ResourceName = DateTime.Now.ToString("yyyymmddMMss") + messageViewModel.Attachment.FileName,
                            Filename = messageViewModel.Attachment.FileName,
                        };
                        message.Attachment = resource;
                        db.Resources.Add(resource);
                    }

                    db.Messages.Add(message);
                    db.GroupMessages.Add(groupMessage);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(messageViewModel);
            }

            catch
            {
                return View();
            }
        }
Ejemplo n.º 2
0
        public ActionResult EditMessageForUser(int id, SendMessageViewModel messageViewModel)
        {
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    Resource resource = new Resource()
                    {
                        ResourceName = DateTime.Now.ToString("yyyymmddMMss") + messageViewModel.Attachment.FileName,
                        Filename = messageViewModel.Attachment.FileName,
                    };

                    Message message = new Message()
                    {
                        Attachment = resource,
                        MessageType = messageViewModel.MessageType,
                        Mode = messageViewModel.Mode,
                        Msg = messageViewModel.Msg,
                        //React = messageViewModel.React,
                    };

                    if (message == null)
                    {
                        return HttpNotFound();
                    }
                    messageViewModel.Attachment.SaveAs(Path.Combine(Server.MapPath("~/Uploads/"), message.Attachment.ResourceName));
                    db.Entry(resource).State = EntityState.Modified;
                    db.Entry(message).State = EntityState.Modified;
                    db.SaveChanges();

                    return RedirectToAction("Index");
                }
                return View(messageViewModel);
            }
            catch
            {
                return View();
            }
        }