public async Task<IHttpActionResult> PostConversation(Conversation conversation) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Conversations.Add(conversation); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = conversation.id }, conversation); }
public async Task<IHttpActionResult> GetConversation(int? id, string toUserId, int? travelId, int? requestId) { var userId = User.Identity.GetUserId(); Conversation conversation = null; if (id.HasValue) { conversation = await db.Conversations.FindAsync(id); } else { conversation = await db.Conversations.Where( t => t.secondUserId == toUserId && t.firstUserId == userId && ((requestId.HasValue && t.requestId == requestId) || (travelId.HasValue && t.travelId == travelId))) .FirstOrDefaultAsync(); } if (conversation == null) { conversation = new Conversation() { firstUserId = User.Identity.GetUserId(), secondUserId = toUserId, travelId = travelId, requestId = requestId }; db.Conversations.Add(conversation); await db.SaveChangesAsync(); conversation = db.Conversations .Include(t => t.firstUser) .Include(t => t.secondUser) .Include(t => t.travel) .Include(t => t.request) .FirstOrDefault(t => t.id == conversation.id); } UserDTO otherUser = new UserDTO(); if(conversation.firstUserId == userId) { otherUser.userId = conversation.secondUserId; otherUser.photo = conversation.secondUser.photo; otherUser.firstName = conversation.secondUser.firstName; } else { otherUser.userId = conversation.firstUserId; otherUser.photo = conversation.firstUser.photo; otherUser.firstName = conversation.firstUser.firstName; } TravelDTO travelDTO = null; if (conversation.travelId.HasValue) { travelDTO = new TravelDTO() { id = conversation.travel.id, explanation = conversation.travel.explanation, availableWeight = conversation.travel.availableWeight, price = conversation.travel.price, fromCity = conversation.travel.fromCity, toCity = conversation.travel.toCity, fromCountry = conversation.travel.fromCountry, toCountry = conversation.travel.toCountry, }; } RequestDTO requestDTO = null; if (conversation.requestId.HasValue) { requestDTO = new RequestDTO() { id = conversation.request.id, explanation = conversation.request.explanation, estimatedWeight = conversation.request.estimatedWeight, price = conversation.request.price, title = conversation.request.title, fromCity = conversation.request.fromCity, toCity = conversation.request.toCity, fromCountry = conversation.request.fromCountry, toCountry = conversation.request.toCountry, }; } ConversationDTO conversationDTO = new ConversationDTO() { id = conversation.id, otherUser = otherUser, travel = travelDTO, request = requestDTO, messages = conversation.messages.Select(t => new MessageDTO() { ownerId = t.ownerId, my = t.ownerId == userId, date = t.messageDate, messageBody = t.messageBody, ownerName = t.owner.firstName, receiverState = t.recieverState })//.Take(10) }; return Ok(conversationDTO); }
public async Task<IHttpActionResult> PutConversation(int id, Conversation conversation) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != conversation.id) { return BadRequest(); } db.Entry(conversation).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ConversationExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }