Skip to content

Commit

Permalink
Add getting all and filtered messages endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ya7on committed May 12, 2019
1 parent 90a4a9b commit caf2b6a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
36 changes: 36 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* * * <a href="#get-roomsinfo">info</a>
* * <a href="#messages">Messages</a>
* * * <a href="#post-messagessend">send</a>
* * * <a href="#get-messagesget">get</a>
* <a href="#installation">Installation</a>

# Environment variables
Expand Down Expand Up @@ -209,6 +210,41 @@ __Response:__
}
```

### GET `/messages/get`

Getting all or filtered messages

__Headers:__
```
{
Authorization: 'yourAPItoken'
}
```

__GET arguments:__

offset [optional] - count of messages for offset

count [optional] - numeric value of count messages in response

__Response:__
```
[
{
id: 19
text: 'Hello. This is text of message'
created: 1557615906
room: {
id: 5
}
user: {
id: 14
}
}
]
```


# Installation

It's very easy. Follow the instructions:
Expand Down
3 changes: 2 additions & 1 deletion server/messages/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from engine.route import Router
from messages.views import send
from messages.views import send, get


router = Router()
router.post('send', send.Route)
router.get('get', get.Route)
38 changes: 38 additions & 0 deletions server/messages/views/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from engine.views import View
from users.models import UserProfile
from messages.models import Messages


def serializer(queryset, count, offset):
def mapping(item):
return {
'id': item.id,
'text': item.text,
'created': int(item.datetime.timestamp()),
'room': {
'id': item.room.id
},
'user': {
'id': item.user.id
}
}

items = map(mapping, queryset[offset:offset+count])
return list(items)


class Route(View):
def data(self):
count = int(self.request.GET.get('count') or '50')
offset = int(self.request.GET.get('offset') or '0')
if not self.request.META.get('HTTP_AUTHORIZATION'):
raise Exception('Authorization token is missing')
user = UserProfile.objects.decode_token(
self.request.META['HTTP_AUTHORIZATION']
)
room = user.room
return serializer(
Messages.objects.filter(room=room).order_by('-id'),
count,
offset
)

0 comments on commit caf2b6a

Please sign in to comment.