예제 #1
0
        public IActionResult Add(CreateCatViewModel model)
        {
            if (this.catService.IsCatExists(model.Name))
            {
                return(this.View());
            }

            this.catService.AddCat(model);
            return(this.Redirect("/"));
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("Name,Age,ImageUrl,BreedId")] CreateCatViewModel catModel)
        {
            bool isCreated = await catService.CreateCat(catModel);

            if (!isCreated)
            {
                return(NotFound());
            }
            ViewData["BreedId"] = new SelectList(context.Breeds, "Id", "Name", catModel.BreedId);
            return(Redirect("/Cats/All"));
        }
        public void AddCat(CreateCatViewModel catModel)
        {
            var cat = new Cat
            {
                Name  = catModel.Name,
                Age   = catModel.Age,
                Breed = catModel.Breed,
                Url   = catModel.Url
            };

            this.context.Cats.Add(cat);
            this.context.SaveChanges();
        }
예제 #4
0
        public async Task <bool> CreateCat(CreateCatViewModel catModel)
        {
            Breed breed = await context.Breeds.FirstOrDefaultAsync(b => b.Id == catModel.BreedId);

            if (breed == null)
            {
                return(false);
            }
            Cat newCat = new Cat()
            {
                Name     = catModel.Name,
                Age      = catModel.Age,
                BreedId  = catModel.BreedId,
                Breed    = breed,
                ImageUrl = catModel.ImageUrl
            };

            context.Add(newCat);
            await context.SaveChangesAsync();

            return(true);
        }