Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GET: api/channel-messages/{channelName}
- [Route("channel-messages/{channelName}")]
- public IHttpActionResult GetChannelMessages(string channelName, [FromUri]string limit = null)
- {
- var channel = db.Channels.FirstOrDefault(c => c.Name == channelName);
- if (channel == null)
- {
- return this.NotFound();
- }
- var channelMessages = db.ChannelMessages
- .Where(m => m.ChannelId == channel.Id)
- .OrderByDescending(m => m.DateSent)
- .ThenByDescending(m => m.Text)
- .Select(m => new ChannelMessageOutputModel()
- {
- Id = m.Id,
- Text = m.Text,
- DateSent = m.DateSent,
- User = (m.Sender) == null ? null : m.Sender.UserName
- });
- if (limit != null)
- {
- int limitCount = 0;
- int.TryParse(limit, out limitCount);
- if (limitCount < 1 || limitCount > 1000)
- {
- return BadRequest("Limit should be integer int range [1...1000]");
- }
- else
- {
- channelMessages = channelMessages.Take(limitCount);
- }
- }
- return Ok(channelMessages);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement