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 PutChannel(int id, ChannelBindingModel model) { if (model == null) { return BadRequest(); } if (!ModelState.IsValid) { return BadRequest(ModelState); } Channel chanelForEdit = this.Data.Channels.FirstOrDefault(c => c.Id == id); if (chanelForEdit == null) { return NotFound(); } Channel nameChannel = this.Data.Channels.FirstOrDefault(c => c.Name == model.Name); if (nameChannel != null) { return this.Conflict(); } chanelForEdit.Name = model.Name; this.Data.SaveChanges(); return this.Ok(new {Message = "Channel # " + chanelForEdit.Id + " edited successfully."}); }
public IHttpActionResult EditChannel(int channelId, ChannelBindingModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var existingChannel = this.Data.Channels.Find(channelId); var existingChannelName = this.Data.Channels.All() .Any(c => c.Name.Equals(model.Name)); if (existingChannel == null) { return this.NotFound(); } if (existingChannelName) { return this.Conflict(); } existingChannel.Name = model.Name; this.Data.Channels.Update(existingChannel); this.Data.SaveChanges(); var responseMessage = string.Format("Channel {0} edited successfully.", existingChannel.Id); return this.Ok(responseMessage); }