Advertisement
Venciity

Untitled

Apr 18th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. //GET: api/channel-messages/{channelName}
  2.         [Route("channel-messages/{channelName}")]
  3.         public IHttpActionResult GetChannelMessages(string channelName, [FromUri]string limit = null)
  4.         {
  5.             var channel = db.Channels.FirstOrDefault(c => c.Name == channelName);
  6.  
  7.             if (channel == null)
  8.             {
  9.                 return this.NotFound();
  10.             }
  11.  
  12.             var channelMessages = db.ChannelMessages
  13.                 .Where(m => m.ChannelId == channel.Id)
  14.                 .OrderByDescending(m => m.DateSent)
  15.                 .ThenByDescending(m => m.Text)
  16.                 .Select(m => new ChannelMessageOutputModel()
  17.                 {
  18.                     Id = m.Id,
  19.                     Text = m.Text,
  20.                     DateSent = m.DateSent,
  21.                     User = (m.Sender) == null ? null : m.Sender.UserName
  22.                 });
  23.  
  24.             if (limit != null)
  25.             {
  26.                 int limitCount = 0;
  27.                 int.TryParse(limit, out limitCount);
  28.                 if (limitCount < 1 || limitCount > 1000)
  29.                 {
  30.                     return BadRequest("Limit should be integer int range [1...1000]");
  31.                 }
  32.                 else
  33.                 {
  34.                     channelMessages = channelMessages.Take(limitCount);
  35.                 }
  36.             }
  37.  
  38.             return Ok(channelMessages);
  39.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement