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> GetRequest(int id) { Request r = await db.Requests.FindAsync(id); if (r == null) { return NotFound(); } RequestDTO requestDTO = new RequestDTO() { id = r.id, ownerId = r.ownerId, UserName = r.owner.UserName, firstName = r.owner.firstName, photo = r.owner.photo, fromCity = r.fromCity, fromCountry = r.fromCountry, toCity = r.toCity, toCountry = r.toCountry, title= r.title, price = r.price, estimatedWeight = r.estimatedWeight, explanation = r.explanation }; return Ok(requestDTO); }