public IHttpActionResult CreateChannel(ChannelBindingModel channelData)
        {
            if (channelData == null)
            {
                return BadRequest("Missing channel data.");
            }

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (db.Channels.All().Any(c => c.Name == channelData.Name))
            {
                return this.Content(HttpStatusCode.Conflict,
                    new { Message = "Duplicated channel name: " + channelData.Name } );
            }

            var channel = new Channel() { Name = channelData.Name };
            db.Channels.Add(channel);
            db.SaveChanges();

            return this.CreatedAtRoute(
                "DefaultApi", 
                new { controller = "channels", id = channel.Id },
                new { channel.Id, channel.Name });
        }
        public IHttpActionResult CreateChannel([FromBody] ChannelsBindingModel m)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            if (string.IsNullOrEmpty(m.Name))
                return BadRequest();

            if (Data.Channels.Any(c => c.Name == m.Name))
                return Conflict();

            var channel = new Channel
            {
                Name = m.Name
            };

            Data.Channels.Add(channel);

            Data.SaveChanges();

            return Created(HttpResponseHeader.Location.ToString(), channel);
        }
Exemplo n.º 3
0
 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);
 }