Esempio n. 1
0
        private IEnumerable <LinkDto> CreateMessagesLinks(MessagesParams messagesParams, bool hasPrevious, bool hasNext)
        {
            var links = new List <LinkDto>();

            links.Add(
                new LinkDto(CreateMessageResourceUri(messagesParams, UriTypeEnum.Current),
                            "self",
                            "GET"));

            if (hasNext)
            {
                links.Add(
                    new LinkDto(CreateMessageResourceUri(messagesParams, UriTypeEnum.NextPage),
                                "nextPage",
                                "GET")
                    );
            }

            if (hasPrevious)
            {
                links.Add(
                    new LinkDto(CreateMessageResourceUri(messagesParams, UriTypeEnum.PreviousPage),
                                "previousPage",
                                "GET")
                    );
            }


            return(links);
        }
Esempio n. 2
0
        public async Task <PagedList <Message> > GetMessageForUser(MessagesParams messagesParams)
        {
            var messages = _context.Messages
                           .Include(u => u.Sender).ThenInclude(p => p.Photos)
                           .Include(u => u.Recipient).ThenInclude(p => p.Photos)
                           .AsQueryable();

            switch (messagesParams.MessageContainer)
            {
            case "Inbox":
                messages = messages.Where(u => u.RecipientId == messagesParams.UserId && u.RecipientDeleted == false);
                break;

            case "Outbox":
                messages = messages.Where(u => u.SenderId == messagesParams.UserId && u.SenderDeleted == false);
                break;

            default:
                messages = messages.Where(u => u.RecipientId == messagesParams.UserId && u.RecipientDeleted == false && u.IsRead == false);
                break;
            }

            messages = messages.OrderByDescending(d => d.MessageSent);
            return(await PagedList <Message> .CreateAsync(messages, messagesParams.PageNumber, messagesParams.PageSize));
        }
Esempio n. 3
0
        private string CreateMessageResourceUri(MessagesParams messagesParams, UriTypeEnum uriType)
        {
            switch (uriType)
            {
            case UriTypeEnum.PreviousPage:
                return(Url.Link("GetMessages",
                                new
                {
                    pageNumber = messagesParams.PageNumber - 1,
                    pageSize = messagesParams.PageSize
                }));

            case UriTypeEnum.NextPage:
                return(Url.Link("GetMessages",
                                new
                {
                    pageNumber = messagesParams.PageNumber + 1,
                    pageSize = messagesParams.PageSize
                }));

            case UriTypeEnum.Current:
            default:
                return(Url.Link("GetMessages",
                                new
                {
                    pageNumber = messagesParams.PageNumber,
                    pageSize = messagesParams.PageSize
                }));
            }
        }
Esempio n. 4
0
        public async Task <PagedList <MessageDTO> > GetMessagesForUser(MessagesParams messagesParams)
        {
            var query = _context.Messages
                        .OrderByDescending(x => x.DateSent)
                        .ProjectTo <MessageDTO>(_mapper.ConfigurationProvider)
                        .AsQueryable();

            query = messagesParams.Container switch
            {
                "Inbox" => query.Where(x => x.RecipientUsername == messagesParams.Username && !x.RecipientDeleted),
                "Outbox" => query.Where(x => x.SenderUsername == messagesParams.Username && !x.SenderDeleted),
                _ => query.Where(x => x.RecipientUsername == messagesParams.Username && x.DateRead == null && !x.RecipientDeleted)
            };

            return(await PagedList <MessageDTO> .CreateAsync(query, messagesParams.PageNumber, messagesParams.PageSize));
        }
Esempio n. 5
0
        public async Task <PagedList <MessageDto> > GetMessagesForUser(MessagesParams messagesParams)
        {
            var query = _context.Messages
                        .OrderByDescending(xx => xx.MessageSent)
                        .AsQueryable();

            query = messagesParams.Container switch
            {
                "inbox" => query.Where(u => u.RecipientUsername == messagesParams.Username && u.RecipientDeleted == false),
                "outbox" => query.Where(u => u.SenderUsername == messagesParams.Username && u.SenderDeleted == false),
                _ => query.Where(u => u.RecipientUsername == messagesParams.Username && u.RecipientDeleted == false && u.DateRead == null)
            };

            var messages = query.ProjectTo <MessageDto>(_mapper.ConfigurationProvider);

            return(await PagedList <MessageDto> .CreateAsync(messages, messagesParams.PageNumber, messagesParams.PageSize));
        }
Esempio n. 6
0
        public async Task <IActionResult> GetMessagesForUser(int userId, [FromQuery] MessagesParams messagesParams)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messagesParams.UserId = userId;

            var messageFromRepo = await _repo.GetMessageForUser(messagesParams);

            var messages = _mapper.Map <IEnumerable <MessagesToReturnDto> >(messageFromRepo);

            Response.AddPagination(messageFromRepo.CurrentPage, messageFromRepo.PageSize, messageFromRepo.TotalCount, messageFromRepo.TotalPages);

            return(Ok(messages));
        }
        public async Task <ActionResult <IEnumerable <MessageDTO> > > GetMessagesForUser([FromQuery] MessagesParams messagesParams)
        {
            messagesParams.Username = User.GetUsername();

            var messages = await _unitOfWork.MessagesRepository.GetMessagesForUser(messagesParams);

            Response.AddPaginationHeader(messages.CurrentPage, messages.PageSize, messages.TotalCount, messages.TotalPages);

            return(Ok(messages));
        }
Esempio n. 8
0
        public async Task <IActionResult> GetMessagesAsync(Guid boardId, Guid topicId, [FromQuery] MessagesParams messagesParams, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest());
            }

            PagedList <Message> messages = await _messageService.GetMessagesAsync(boardId, topicId, messagesParams);

            if (messages == null)
            {
                return(NotFound());
            }

            var previousPageLink = messages.HasPrevious ? CreateMessageResourceUri(messagesParams, UriTypeEnum.PreviousPage) : null;
            var nextPageLink     = messages.HasNext ? CreateMessageResourceUri(messagesParams, UriTypeEnum.NextPage) : null;

            var paginationMetaData = new
            {
                totalCount  = messages.TotalCount,
                pageSize    = messages.PageSize,
                currentPage = messages.CurrentPage,
                totalPages  = messages.TotalPages,
                previousPageLink,
                nextPageLink
            };

            var paginationMetaDataJson = JsonSerializer.Serialize(paginationMetaData);

            Response.Headers.Add("X-Pagination", paginationMetaDataJson);

            var includeLinks = parsedMediaType.SubTypeWithoutSuffix.EndsWith("hateoas", StringComparison.InvariantCultureIgnoreCase);

            if (includeLinks)
            {
                var messagesWithLinks = new List <ResponseMessageLinksDto>();

                foreach (var item in messages)
                {
                    IEnumerable <LinkDto> links = new List <LinkDto>();
                    links = CreateMessageLinks(boardId, item.TopicId, item.Id);
                    var messageWithLinks = _mapper.Map <ResponseMessageLinksDto>(item);
                    messageWithLinks.Links = links;

                    messagesWithLinks.Add(messageWithLinks);
                }

                var envelopeWithLinks = new EnvelopeResponseMessageLinksDto();
                envelopeWithLinks.Value = messagesWithLinks;
                envelopeWithLinks.Links = CreateMessagesLinks(messagesParams, messages.HasPrevious, messages.HasNext);

                return(Ok(envelopeWithLinks));
            }

            var responseDtos = _mapper.Map <IEnumerable <ResponseMessageDto> >(messages);

            return(Ok(responseDtos));
        }
Esempio n. 9
0
        public async Task <PagedList <Message> > GetMessagesAsync(Guid boardId, Guid topicId, MessagesParams messagesParams)
        {
            var collection = await _messageRepository.GetMessagesAsync();

            var pagedList = PagedList <Message> .Create(collection, messagesParams.PageNumber, messagesParams.PageSize);

            return(pagedList);
        }