public IHttpActionResult ChangeCategorieById(int id, Category cat)
 {
     var categoryToChange = db.Categories.First(c => c.Id == id);
     if (categoryToChange==null)
     {
         return this.NotFound();
     }
     categoryToChange.Name = cat.Name;
     db.SaveChanges();
     return this.Ok("changes has been made");
 }
        public IHttpActionResult CreateCategory([FromBody] CategoryModel categoryModel)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var category = new Category() { Name = categoryModel.Name };
            this.Data.Categories.Add(category);
            this.Data.SaveChanges();

            categoryModel.Id = category.Id;
            return this.Ok(categoryModel);
        }
        public IHttpActionResult AddBook(AddBooksBindingModels  model)
        {
            if (model == null)
            {
                this.ModelState.AddModelError("model", "the model is empty");
            }
            if (!ModelState.IsValid)
            {
                return this.BadRequest(ModelState);
            }

            var categories = model.Categories.Split(' ') ;
            var categoryList = new List<Category>();

            if (categories.Count() != 0)
            {

                foreach (var categoryName in categories)
                {
                    Category cat = new Category()
                    {
                        Name = categoryName
                    };
                    categoryList.Add(cat);
                }
            }

            var book = new Book()
            {
                Title = model.Title,
                Description = model.Description ?? null,
                Edition = model.Edition,
                Price = model.Price,
                AgeRestriction = model.Restriction,
                Copies = model.Copies,
                ReleaseDate = model.ReleaseDate,
                Author = db.Authors.First(a => a.Id==model.AuthorId) ?? null,
                RelatedBooks = db.Books.Where(b => b.Id == model.BookId).ToList().Count == 0 ? null : db.Books.Where(b => b.Id == model.BookId).ToList(),
                Categories = categoryList ?? null

            };
            db.Books.Add(book);
            db.SaveChanges();
            return this.Ok("created book  with id = "+ book.Id);
        }
 public IHttpActionResult AddCategorie(AddCategorieBindingModels model)
 {
     if (model == null)
     {
         this.ModelState.AddModelError("model","the model is empty");
     }
     if (!ModelState.IsValid)
     {
         return this.BadRequest(ModelState);
     }
     var category = new Category()
     {
         Name = model.Name,
     };
     db.Categories.Add(category);
     db.SaveChanges();
     return this.CreatedAtRoute("DefaultApi", new { id = category.Id }, category);
 }
        public IHttpActionResult PutCategory(int id, PutCategoryBindingModel categoryBindingModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var category = new Category(id, categoryBindingModel.Name);

            context.Entry(category).State = EntityState.Modified;

            try
            {
                context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public IHttpActionResult PostCategory([FromBody]AddCategoryBindingModel categoryBindingModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var category = new Category(categoryBindingModel.Name);

            context.Categories.Add(category);
            context.SaveChanges();

            var categoryViewModel = new CategoryViewModel(category);

            return CreatedAtRoute("DefaultApi", new { id = categoryViewModel.Id }, categoryViewModel);
        }
 public CategoryViewModel(Category category)
 {
     Name = category.Name;
 }
 public CategoryViewModel(Category category)
 {
     this.Id = category.Id;
     this.Name = category.Name;
 }
 public AllCategoriesViewModel(Category category)
 {
     Id = category.Id;
     Name = category.Name;
 }
        public IHttpActionResult PostCategory([FromBody] CategoryBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var duplicate = this.db.Categories.Any(c => c.Name == model.Name);
            if (duplicate == true)
            {
                var message = string.Format("Category with {0} name already exist!", model.Name);
                return this.BadRequest(message);
            }

            var category = new Category()
            {
                Name = model.Name
            };

            db.Categories.Add(category);
            db.SaveChanges();

            var viewModel = new CategoryViewModel()
            {
                Name = model.Name
            };

            return this.Ok(viewModel);
        }
  //POST/api/categories
 public void PostCategory(string name)
  {
      var categories = this.Data.Categories.Where(c=>c.Name==name).ToList();
      if (categories.Count == 0)
      {
          Category newCategory = new Category()
          {
              Name = name
          };
          this.Data.Categories.Add(newCategory);
          this.Data.SaveChanges();
      }
  }
 public CategoryDetailedViewModel(Category category)
 {
     this.Id = category.Id;
     this.Name = category.Name;
     //this.Books = category.Books.Select(b => b.Title).ToList();
 }
 public CategorySimpleViewModel(Category category)
 {
     this.Name = category.Name;
 }
        public IHttpActionResult AddNewCategory([FromBody]CategoryBindingModel bindingCategory)
        {
            var categoryNames = context.Categories
                .Select(c => c.Name).ToList();
            if (categoryNames.Contains(bindingCategory.Name))
            {
                return this.BadRequest("Category already exist.");
            }

            var newCategory = new Category() { Name = bindingCategory.Name };
            context.Categories.Add(newCategory);
            context.SaveChanges();
            return this.Ok("Category {" + newCategory.Name + "} has been created.");
        }