public async Task <IHttpActionResult> Add(MessageCreateVM messageCreateVM) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _userManager.FindByIdAsync(User.Identity.GetUserId()); var message = new Message { Body = messageCreateVM.Body, CreatedUtc = DateTime.UtcNow, UserId = user.Id, User = user }; _messagesManager.Create(message); var viewModel = new MessageFullVM { Id = message.Id, Body = message.Body, CreatedUtc = message.CreatedUtc, User = new UserVM { Id = message.User.Id, UserName = message.User.UserName } }; return(Ok(viewModel)); }
internal object Messages(MessageCreateVM message) { if (string.IsNullOrEmpty(message.Message)) { return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Message} : {Token.FiledIsRequired}")); } if (message.UserReciveId <= 0) { return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.UserRecivedId} : {Token.FiledIsRequired}")); } if (message.Message.Length > 500) { return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Message} : {Token.MaxLength} = 500")); } if (!db.Users.Any(c => c.Id == message.UserReciveId)) { return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.UserRecive} : {Token.NotFound} = 500")); } int UserLoggedId = AccessToken.GetUserId(); var Chat = db.Chats.SingleOrDefault(c => (c.FkUser_Id_Recived == UserLoggedId && c.FkUser_Id_Starting == message.UserReciveId) || (c.FkUser_Id_Starting == UserLoggedId && c.FkUser_Id_Recived == message.UserReciveId) ); var Message = new ChatMessage { Message = message.Message, FkUser_Id = UserLoggedId, DateTime = DateTime.Now }; if (Chat == null) { //This Is New Chat db.Chats.Add(new Chat { FkUser_Id_Starting = UserLoggedId, FkUser_Id_Recived = message.UserReciveId, ChatMessages = new List <ChatMessage> { Message } }); } else { //Save Message To Chat Messaging Chat.ChatMessages.Add(Message); } return(null); }
public ActionResult Create(MessageCreateVM viewModel) { if (ModelState.IsValid) { var message = new Message { Body = viewModel.Body, UserId = User.Identity.GetUserId() }; _messagesManager.Create(message); return(RedirectToAction("List")); } return(View(viewModel)); }
public object Create(MessageCreateVM message) { try { object ObjectReturn = this.CreateS.Messages(message); if (ObjectReturn != null) { return(ObjectReturn); } db.SaveChanges(); return(new ResponseVM(RequestTypeEnumVM.Success, Token.Sent, db.SelectLastMessage(AccessToken.GetUserId(), message.UserReciveId) .Select(v => GetMessageInformation(v)).ToList())); } catch (Exception ex) { return(new ResponseVM(RequestTypeEnumVM.Error, Token.SomeErrorInServer, ex)); } }
public IHttpActionResult Create(MessageCreateVM cont) { return(Json(MessagesData.Create(cont))); }
private bool IsMessageValid(MessageCreateVM viewModel) { return(true); }
public ActionResult Create() { var viewModel = new MessageCreateVM(); return(View(viewModel)); }