Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public async Task<IHttpActionResult> GetTravel(int id)
        {
            Travel t = await db.Travels.FindAsync(id);
            if (t == null)
            {
                return NotFound();
            }

            TravelDTO travelDto = new TravelDTO() {
                id = t.id,
                ownerId = t.ownerId,
                UserName = t.owner.UserName,
                firstName = t.owner.firstName,
                photo = t.owner.photo,
                fromCity = t.fromCity,
                fromCountry = t.fromCountry,
                toCity = t.toCity,
                toCountry = t.toCountry,
                availableWeight = t.availableWeight,
                availableVolume = t.availableVolume,
                startDate = t.startDate,
                finishDate = t.finishDate,
                itemType = t.itemType,
                price = t.price,
                explanation = t.explanation
            };

            return Ok(travelDto);
        }