public IHttpActionResult GetChannel(int id) { Channel channel = db.Channels.All().FirstOrDefault(c => c.Id == id); if (channel == null) { return NotFound(); } var channelView = new ChannelViewModel() { Id = channel.Id, Name = channel.Name }; return Ok(channelView); }
public IHttpActionResult CreateChannel(ChannelBindingModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var existingChannel = this.Data.Channels.All() .Where(c => c.Name.Equals(model.Name)) .Select(ChannelViewModel.Create) .FirstOrDefault(); if (existingChannel != null) { return this.Conflict(); } var newChannel = new Channel { Name = model.Name }; this.Data.Channels.Add(newChannel); this.Data.SaveChanges(); var channelViewModel = new ChannelViewModel { Id = newChannel.Id, Name = newChannel.Name }; return this.CreatedAtRoute( "DefaultApi", new { controller = "channels", newChannel.Id }, channelViewModel); }
public IHttpActionResult CreateNewChannel([FromBody]ChannelBindingModel model) { if (model == null) { return this.BadRequest("Invalid input data"); } Channel nameChannel = this.Data.Channels.FirstOrDefault(c => c.Name == model.Name); if (nameChannel != null) { return this.Conflict(); } var newChannel = new Channel() { Name = model.Name }; this.Data.Channels.Add(newChannel); this.Data.SaveChanges(); var viewChannel = new ChannelViewModel() { Name = newChannel.Name, Id = newChannel.Id }; return CreatedAtRoute("DefaultApi", new {id = viewChannel.Id},viewChannel); }
public IHttpActionResult GetChannelById(int id) { Channel searChannel = this.Data.Channels.FirstOrDefault(c => c.Id == id); if (searChannel == null) { return NotFound(); } var channel = new ChannelViewModel() { Id=searChannel.Id, Name=searChannel.Name }; return Ok(channel); }
public IHttpActionResult CreateChannel([FromBody] ChannelBindingModel model) { if (model == null) { return this.BadRequest("Missing channel data."); } if (!ModelState.IsValid) { return BadRequest(ModelState); } if (this.db.Channels.All().Any(c => c.Name == model.Name)) { return this.Content(HttpStatusCode.Conflict, new {Message = "Duplicated channel name: " + model.Name}); } var channel = new Channel() { Name = model.Name }; db.Channels.Add(channel); db.SaveChanges(); var channelView = new ChannelViewModel() { Id = channel.Id, Name = channel.Name }; return CreatedAtRoute("DefaultApi", new { id = channel.Id }, channelView); }