Beispiel #1
0
        public async Task<IHttpActionResult> GetUser(string userId)
        {
            ApplicationUser user = await UserManager.FindByIdAsync(userId);
            UserDTO dto = new UserDTO()
            {
                //userId = User.Identity.GetUserId(),
                firstName = user.firstName,
                photo = user.photo,
                userName = user.UserName,
                //authServer = user.Logins.First().LoginProvider,
                //authServerId = user.Logins.First().ProviderKey
            };

            return Ok(dto);
        }
        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);
        }