Пример #1
0
        // PUT api/Chat/5
        public async Task<IHttpActionResult> PutChat(int id, Chat chat)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != chat.Id)
            {
                return BadRequest();
            }

            db.Entry(chat).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ChatExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Пример #2
0
 //end
 public void AddMessage(Chat msg)
 {
     if (Context.User.Identity.IsAuthenticated)
     {
         var userId = Context.User.Identity.GetUserId();
         msg.time = DateTime.UtcNow;
         msg.sentFrom = Context.User.Identity.GetUserId();
         db.Chats.Add(msg);
         try
         {
             db.SaveChanges();
         }
         catch (Exception e)
         {
             string s = e.ToString();
         }
         var ret = (from chat in db.Chats
                   where chat.Id.Equals(msg.Id)
                   select new
                   {
                       id = chat.Id,
                       sentFrom = chat.sentFrom,
                       sentTo = chat.sentTo,
                       sentFromName = chat.AspNetUser1.Email,
                       sentToName = chat.AspNetUser.Email,
                       message = chat.message,
                       time = chat.time,
                   }).FirstOrDefault();
        // var ret = db.Chats.FirstOrDefault(x => x.Id == msg.Id);
         Clients.Group(msg.sentFrom).loadNewMessage(ret);
         Clients.Group(msg.sentTo).loadNewMessage(ret);
     }
    // Clients.Caller.loadNewMessage(ret);
   //  Clients.All.loadNewMessage(ret);
 }
Пример #3
0
 public async Task<IHttpActionResult> PostChat(Chat chat)
 {
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     if (User.Identity.IsAuthenticated)
     {
         chat.sentFrom = User.Identity.GetUserId();
         if (chat.sentFrom == chat.sentTo)
         {
             return BadRequest("You cannot send message to Yourself!");
         }
     }
     else
     {
         return BadRequest();
     }
     chat.time = DateTime.UtcNow;
     db.Chats.Add(chat);
     await db.SaveChangesAsync();
     var ret = from msg in db.Chats
               where msg.Id.Equals(chat.Id)
               select new
               {
                   time = msg.time,
                   sentTo = msg.sentTo,
                   sentFrom = msg.sentFrom,
                   messgae = msg.message,
               };
     return Ok(ret);
 }