示例#1
0
        public IHttpActionResult EditExistingChannel(int id, AddChannelBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest("Name is required!cannot be empty."));
            }

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

            var channel = this.Data.Channels
                          .FirstOrDefault(ch => ch.Id == id);

            if (channel == null)
            {
                return(this.NotFound());
            }

            if (this.Data.Channels
                .Any(ch => ch.Name == model.Name && ch.Id != channel.Id))
            {
                return(this.Conflict());
            }

            channel.Name = model.Name;
            this.Data.SaveChanges();
            return(this.Ok(new
            {
                Message = string.Format("Channel #{0} edited successfully.", channel.Id)
            }));
        }
示例#2
0
        public IHttpActionResult CreateNewChannel(AddChannelBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest("Name is required!Cannot be empty."));
            }

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

            if (this.Data.Channels.Any(ch => ch.Name == model.Name))
            {
                return(this.Conflict());
            }

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

            this.Data.Channels.Add(channel);
            this.Data.SaveChanges();

            var result = new ChannelViewModel()
            {
                Id   = channel.Id,
                Name = channel.Name
            };

            // return status 201, Location and (channel Id and name)
            return(this.CreatedAtRoute("DefaultApi", new { id = channel.Id }, result));
        }