// PUT api/User/5 public HttpResponseMessage Putwx_user(int id, wx_user wx_user) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != wx_user.ID) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } db.Entry(wx_user).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public override System.Threading.Tasks.Task OnDisconnected() { // ownerId is the guid url part string ownerId = Context.QueryString["ownerId"]; using (var db = new cloudjunsqlEntities()) { var user = db.wx_user.First(u => u.GuidUrl == ownerId); user.SignalrID = string.Empty; db.SaveChanges(); } return(base.OnDisconnected()); }
// DELETE api/Chat/5 public HttpResponseMessage Deletewx_chat(long id) { using (var db = new cloudjunsqlEntities()) { wx_chat wx_chat = db.wx_chat.Find(id); if (wx_chat == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } db.wx_chat.Remove(wx_chat); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK, wx_chat)); } }
// PUT api/Chat/5 //public HttpResponseMessage Putwx_chat(long id, ChatDto chat) //{ // if (!ModelState.IsValid) // { // return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); // } // if (id != chat.ID) // { // return Request.CreateResponse(HttpStatusCode.BadRequest); // } // db.Entry(chat).State = EntityState.Modified; // try // { // db.SaveChanges(); // } // catch (DbUpdateConcurrencyException ex) // { // return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); // } // return Request.CreateResponse(HttpStatusCode.OK); //} // POST api/Chat public HttpResponseMessage Postwx_chat(ChatDto dto) { if (ModelState.IsValid) { System.Diagnostics.Trace.TraceInformation("Chat Message received: " + dto); // the OwnerUsername is Uni, change it to username dto.OwnerUsername = MvcApplication.UinUsernameCache[long.Parse(dto.OwnerUsername)]; wx_chat chat = ChatDto.GetChatFromDto(dto); // ignore if content is empty if (string.IsNullOrEmpty(chat.Content) || string.IsNullOrWhiteSpace(chat.Content)) { HttpResponseMessage resp = Request.CreateResponse(HttpStatusCode.OK); return(resp); } using (var db = new cloudjunsqlEntities()) { // check if the same message is already in the database long existingChatId = IsAlreadyInDb(chat); if (existingChatId == -1) { // new message db.wx_chat.Add(chat); db.SaveChanges(); // insert the new chat into the owner-chat map table var map = new wx_owner_chat_map { ChatID = chat.ID, OwnerID = chat.OwnerID, CounterPartyID = chat.CounterPartyID }; db.wx_owner_chat_map.Add(map); db.SaveChanges(); } else { // chat already exists, add one entry in the owner_chat_map table chat.ID = existingChatId; var map = new wx_owner_chat_map { ChatID = existingChatId, OwnerID = chat.OwnerID, CounterPartyID = chat.CounterPartyID }; db.wx_owner_chat_map.Add(map); db.SaveChanges(); } // update the web mvc client using signalr var chatDto = ChatDto.GetDtoFromChat(chat); // get the signalr client id from the database for this owner id var signalrId = db.wx_user.Single(u => u.ID == chat.OwnerID).SignalrID; if (!string.IsNullOrEmpty(signalrId)) { var hub = GlobalHost.ConnectionManager.GetHubContext("Chat"); try { hub.Clients.Client(signalrId).addMessage(chatDto); } catch (Exception ex) { System.Diagnostics.Trace.TraceInformation("SignalrID - {0} could be bad. Execption-{1}", signalrId, ex.Message); } } } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); return(response); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }