示例#1
0
 public static CategoryViewModel Create(Category category)
 {
     return new CategoryViewModel
     {
         Id = category.Id,
         Name = category.Name
     };
 }
        public static CategoryViewModel ConvertToCategoryViewModel(Category category)
        {
            CategoryViewModel categoryViewModel = new CategoryViewModel
            {
                Id = category.Id,
                Name = category.Name
            };

            return categoryViewModel;
        } 
        public IHttpActionResult Create(BookBindingModel bookBindingModel)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var author = this.data.Authors.Find(bookBindingModel.AuthorId);
            if (author == null)
            {
                return this.BadRequest("Author does not exist - invalid id!");
            }

            Book book = new Book()
            {
                Title = bookBindingModel.Title,
                Description = bookBindingModel.Description,
                Price = bookBindingModel.Price,
                Copies = bookBindingModel.Copies,
                EditionType = bookBindingModel.EditionType,
                AgeRestriction = bookBindingModel.AgeRestriction,
                ReleaseDate = bookBindingModel.ReleaseDate,
                Author = author
            };

            string[] categories = bookBindingModel.Categories.Split(new [] {' ', ','}, StringSplitOptions.RemoveEmptyEntries);
            foreach (var categoryName in categories)
            {
                Category category = this.data
                    .Categories
                    .All()
                    .FirstOrDefault(c => c.Name == categoryName);

                if (category == null)
                {
                    category = new Category
                    {
                        Name = categoryName
                    };

                    this.data.Categories.Add(category);
                }

                book.Categories.Add(category);
            }

            this.data.Books.Add(book);
            this.data.Save();

            BookViewModel bookViewModel = BookViewModel.ConvertToBookViewModel(book);
            return this.Ok(bookViewModel);
        }
        public IHttpActionResult ChangeCategorieById(int id, Category category)
        {
            var categoryToChange = this.Data.Categories
                .First(c => c.Id == id);
            if (categoryToChange == null)
            {
                return this.NotFound();
            }

            categoryToChange.Name = category.Name;
            this.Data.SaveChanges();
            return this.Ok("successful change");
        }
        public IHttpActionResult Post([FromUri]string name)
        {
            if (ctx.Categories.Where(c => c.Name == name).Any())
            {
                return this.BadRequest("A Category with name " + name + " already exists.");
            }

            var category = new Category() { Name = name };

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

            return this.Ok();
        }
        public IHttpActionResult PostCategory(AddOrChangeCategoryModelBinder model)
        {
            var exisitngCategory = this.context.Categories
                .FirstOrDefault(c => c.Name == model.Name);

            if (exisitngCategory != null)
            {
                return this.BadRequest("Duplicate category name!");
            }

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

            var newCategory = new Category { Name = model.Name };
            this.context.Categories.Add(newCategory);
            this.context.SaveChanges();

            return this.Ok(newCategory);
        }
        public IHttpActionResult PostCategory([FromBody]CategoryBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var categoryExists = context.Categories.FirstOrDefault(c => c.Name == model.Name);

            if (categoryExists != null)
            {
                return this.BadRequest("Category already exists!");
            }

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

            context.Categories.Add(categoryToAdd);

            context.SaveChanges();

            var data = new CategoryViewModel(categoryToAdd);

            return this.Ok(data);
        }
        // POST: api/Categories
        public IHttpActionResult PostCategory(CategoryBindingModel categoryModel)
        {
            if (categoryModel == null)
            {
                this.ModelState.AddModelError("categoryModel", "Category model can not be empty!");
            }

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

            var checkExistCategory = db.Categories.FirstOrDefault(c => c.Name == categoryModel.Name);
            if(checkExistCategory != null)
            {
                return this.BadRequest("Category exists");
            }

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

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

            return this.Ok("Category successfully added");
        }
 public CategoryViewModel(Category category)
 {
     Name = category.Name;
     Id = category.Id;
 }
示例#10
0
        public IHttpActionResult PostCategory([FromBody]CategoryBindingModel category)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            foreach (var currentCategory in db.Categories)
            {
                if (currentCategory.Name == category.Name)
                {
                    return this.BadRequest();
                }
            }

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

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

            return this.Ok(new CategoryViewModel(newCategory));
        }
        public IHttpActionResult AddCategory(CategoryBindingModel categoryModel)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var existingCategory = this.BookShopData.Categories.All()
                .FirstOrDefault(c => c.Name.Equals(categoryModel.Name));

            if (existingCategory != null)
            {
                return this.BadRequest("Category with the specified name already exists.");
            }

            var newCategory = new Category
            {
                Name = categoryModel.Name
            };

            this.BookShopData.Categories.Add(newCategory);
            this.BookShopData.SaveChanges();

            var categoryViewModel = CategoryViewModel.CreateViewModel(newCategory);

            return this.Ok(categoryViewModel);
        }
        public IHttpActionResult PostBook(AddBookBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newBook = new Book
            {
                Title = model.Title,
                Description = model.Description,
                Price = model.Price,
                Copies = model.Copies,
                EditionType = model.Edition,
                AgeRestriction = model.AgeRestriction,
                ReleaseDate = model.ReleaseDate
            };

            var authorId = new Guid(model.AuthorId);
            var author = this.context.Authors
                .FirstOrDefault(a => a.Id == authorId);

            if (author == null)
            {
                return this.BadRequest("Invalid author id");
            }

            newBook.AuthorId = authorId;

            char[] separator = new char[] { ' ' };
            string[] categories = model.Categories.Split(separator, StringSplitOptions.RemoveEmptyEntries);

            foreach (var categoryName in categories)
            {
                var categoryInDb = this.context.Categories
                    .FirstOrDefault(c => c.Name == categoryName);

                if (categoryInDb == null)
                {
                    Category newCategory = new Category { Name = categoryName };
                    this.context.Categories.Add(newCategory);
                    this.context.SaveChanges();
                    categoryInDb = newCategory;
                }

                newBook.Categories.Add(categoryInDb);
            }

            this.context.Books.Add(newBook);
            this.context.SaveChanges();
            var bookToReturn = this.context.Books
                .Where(b => b.Id == newBook.Id)
                .Select(BookDataModel.DataModel)
                .FirstOrDefault();

            return this.Ok(bookToReturn);
        }
 public CategoryBooksViewModel(Category category)
 {
     this.Name = category.Name;
 }
        public IHttpActionResult AddCategorie(CategoryBindingModel model)
        {
            if (model == null)
            {
                this.ModelState.AddModelError("model", "The model cannot be null - (request is empty).");
            }

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

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

            this.Data.Categories.Add(category);
            this.Data.SaveChanges();
            return this.Ok("created category  with id = " + category.Id);
        }
        public IHttpActionResult AddBook(BookBindingModel model)
        {
            if (model == null)
            {
                this.ModelState.AddModelError("model", "The model cannot be null - (request 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 category = new Category()
                    {
                        Name = categoryName
                    };

                    categoryList.Add(category);
                }
            }

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

            };

            this.Data.Books.Add(book);
            this.Data.SaveChanges();
            return this.Ok("created book  with id = " + book.Id);
        }
 public BookCategoryViewModel(Category category)
 {
     Category = category.Name;
 }
 public BookCategoriesViewModel(Category category)
 {
     this.Name = category.Name;
 }
示例#18
0
 public CategoryViewModel(Category category)
 {
     this.Id = category.Id;
     this.Name = category.Name;
 }
        public IHttpActionResult CreateCategory([FromBody] AddCategoryBindingModel categoryModel)
        {
            if (!ModelState.IsValid)
            {
                return this.BadRequest();
            }

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

            unitOfWork.CategoryRepository.Insert(category);

            unitOfWork.Save();

            return this.Ok(category);
        }