コード例 #1
0
        public IActionResult Index()
        {
            List <CatViewModel> catsList = new List <CatViewModel>();
            var connectionString         = "Server=(localdb)\\mssqllocaldb;Integrated Security=True;" +
                                           "Database=FluffyCatDB;Trusted_Connection=True;MultipleActiveResultSets=true";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                //SqlDataReader
                connection.Open();
                Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
                Console.WriteLine("State: {0}", connection.Database);

                string     sql     = "Select * From Cats";
                SqlCommand command = new SqlCommand(sql, connection);

                using (SqlDataReader dataReader = command.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        CatViewModel cat = new CatViewModel();
                        cat.CatId = Convert.ToInt32(dataReader["CatId"]);
                        cat.Name  = Convert.ToString(dataReader["Name"]);
                        cat.Age   = Convert.ToInt32(dataReader["Age"]);
                        cat.Breed = Convert.ToString(dataReader["Breed"]);
                        cat.Url   = Convert.ToString(dataReader["ImageUrl"]);
                        catsList.Add(cat);
                    }
                }
                connection.Close();
            }
            return(View(catsList));
        }
コード例 #2
0
        public IActionResult Cat(int id)
        {
            if (!ModelState.IsValid)
            {
                return(this.Redirect("/"));
            }

            var cat = this.context
                      .Cats
                      .Where(x => x.Id == id)
                      .FirstOrDefault();

            if (cat == null)
            {
                return(this.Redirect("/"));
            }

            var catViewModel = new CatViewModel()
            {
                Name     = cat.Name,
                Age      = cat.Age,
                Breed    = cat.Breed,
                ImageUrl = cat.ImageUrl
            };

            return(this.View(catViewModel));
        }
コード例 #3
0
 public static Entity ToModel(this CatViewModel cat)
 {
     return(new Entity(cat.Identifier)
     {
         ImageUrl = cat.ImageUrl, Votes = cat.Votes
     });
 }
コード例 #4
0
        public IActionResult Details(CatViewModel model)
        {
            var cat = this.catsService.GetAll().FirstOrDefault(x => x.Id == model.Id);

            var catModel = this.mapper.Map <CatViewModel>(cat);

            return(this.View(catModel));
        }
コード例 #5
0
        public ActionResult Update([DataSourceRequest] DataSourceRequest request, CatViewModel item)
        {
            if (item != null && ModelState.IsValid)
            {
                repo.Update(item);
            }

            return(Json(new[] { item }.ToDataSourceResult(request, ModelState)));
        }
コード例 #6
0
        public ActionResult Destroy([DataSourceRequest] DataSourceRequest request, CatViewModel item)
        {
            if (item != null)
            {
                repo.Destroy(item);
            }

            return(Json(new[] { item }.ToDataSourceResult(request, ModelState)));
        }
コード例 #7
0
        public IActionResult Index()
        {
            var cat  = this._context.Cats.ToList();
            var cats = new CatViewModel
            {
                Cats = cat
            };

            return(View(cats));
        }
コード例 #8
0
        public ActionResult Edit(int id, CatViewModel catViewModel)
        {
            var cat = _catRepository.GetById(id);
            catViewModel.Update(cat, ModelState);

            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }

            return View("Edit", catViewModel);
        }
コード例 #9
0
        public ActionResult Index(CatViewModel catViewModel)
        {
            var cat = catViewModel.CreateCat(ModelState);

            if (ModelState.IsValid)
            {
                _catRepository.Save(cat);
                return RedirectToAction("Index");
            }

            return View("New", catViewModel);
        }
コード例 #10
0
        public ActionResult ViewCats()
        {
            CatViewModel CatVm = new CatViewModel();

            List <Animal> TempList = _service.fetchAllAnimals().Where(a => a.Type == BaseType.Cat).ToList();
            List <Cat>    CatList  = TempList.Cast <Cat>().ToList();

            CatVm.CatList = CatList;


            return(View(CatVm));
        }
コード例 #11
0
        public void AddCat(CatViewModel model)
        {
            Cat cat = new Cat
            {
                Name  = model.Name,
                Age   = model.Age,
                Breed = model.Breed,
                Url   = model.Url
            };

            _db.Cats.Add(cat);
            _db.SaveChanges();
        }
コード例 #12
0
ファイル: CatsController.cs プロジェクト: Ljubo6/SoftUni-1
        public IActionResult Details(string id)
        {
            var cat       = this.db.Cats.SingleOrDefault(c => c.Id == id);
            var viewModel = new CatViewModel
            {
                Name     = cat.Name,
                Age      = cat.Age,
                Breed    = cat.Breed,
                ImageUrl = cat.ImageUrl
            };

            return(this.View(viewModel));
        }
コード例 #13
0
        public IActionResult Details(int id)
        {
            Cat cat = service.GetById(id);

            if (cat == null)
            {
                this.RedirectToAction("Home", "Index");
            }

            CatViewModel model = new CatViewModel(cat.Name, cat.Age, cat.Breed, cat.ImageUrl);

            return(View(model));
        }
コード例 #14
0
        public async Task <IActionResult> Details(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            CatViewModel cat = await catService.GetCat(id);

            if (cat == null)
            {
                return(NotFound());
            }
            return(View(cat));
        }
コード例 #15
0
        public IActionResult Details(int id)
        {
            CatViewModel cat = this.db.Cats
                               .Where(c => c.Id == id)
                               .Select(c => new CatViewModel
            {
                Name     = c.Name,
                Age      = c.Age,
                Breed    = c.Breed,
                ImageUrl = c.ImageUrl
            })
                               .FirstOrDefault();

            return(this.View(cat));
        }
コード例 #16
0
        public IActionResult AddCat(CatViewModel model)
        {
            var cat = new Cat
            {
                Name     = model.Name,
                Age      = model.Age,
                Breed    = model.Breed,
                ImageUrl = model.ImageUrl
            };

            this.Context.Cats.Add(cat);

            this.Context.SaveChanges();
            return(RedirectToAction());
        }
コード例 #17
0
        public IActionResult Add(CatViewModel model)
        {
            var cat = new Cat
            {
                Name     = model.Name,
                Age      = model.Age,
                Breed    = model.Breed,
                ImageUrl = model.ImageUrl
            };

            this.db.Cats.Add(cat);
            this.db.SaveChanges();

            return(this.Redirect("/"));
        }
コード例 #18
0
ファイル: CatUtility.cs プロジェクト: WamdahH/HelloWeb
        public static CatViewModel GetViewModel(this Cat cat)
        {
            var catVM = new CatViewModel()
            {
                Age        = cat.Age,
                Color      = cat.Color,
                CreatedAt  = cat.CreatedAt,
                ID         = cat.ID,
                Name       = cat.Name,
                PictureURL = cat.PictureURL,
                Size       = cat.Size
            };

            return(catVM);
        }
コード例 #19
0
        public async Task <IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            ViewData["BreedId"] = new SelectList(context.Breeds, "Id", "Name");

            CatViewModel cat = await catService.GetCat(id);

            if (cat == null)
            {
                return(NotFound());
            }
            return(View(cat));
        }
コード例 #20
0
        public async Task <ActionResult <Cat> > GetParents()
        {
            var query = from cat in _db.Cats
                        where cat.Parent == true
                        select cat;

            var result = await query.ToListAsync();

            if (result == null)
            {
                return(NotFound());
            }

            var returnList = new List <CatViewModel>();

            foreach (Cat cat in result)
            {
                CatViewModel catViewModel = new CatViewModel();

                var imageQuery = from images in _db.Images
                                 join ci in _db.Cat_Image on images.Id equals ci.ImageId
                                 where ci.CatId == cat.Id && images.DisplayPicture == true
                                 select images;

                catViewModel.Images = await imageQuery.ToListAsync();

                catViewModel.Id         = cat.Id;
                catViewModel.Name       = cat.Name;
                catViewModel.Notes      = cat.Notes;
                catViewModel.Parent     = cat.Parent;
                catViewModel.Pedigree   = cat.Pedigree;
                catViewModel.Sex        = cat.Sex;
                catViewModel.Vaccinated = cat.Vaccinated;
                catViewModel.Age        = cat.Age;
                catViewModel.BirthDate  = cat.BirthDate;
                catViewModel.Breed      = cat.Breed;
                catViewModel.CatLitter  = cat.CatLitter;
                catViewModel.Chipped    = cat.Chipped;
                catViewModel.Color      = cat.Color;

                returnList.Add(catViewModel);
            }

            return(Ok(returnList));
        }
コード例 #21
0
        private AnimalViewModel CreateRandomAnimal()
        {
            AnimalViewModel animal = null;

            var name  = _names[_random.Next(0, _names.Length - 1)];
            var color = _colors[_random.Next(0, _colors.Length - 1)];

            switch (_random.Next(0, 3))
            {
            case 0:     // Cat
                var cat = new CatViewModel
                {
                    Name          = name,
                    ImageUrl      = _catImageUrls[_random.Next(0, _catImageUrls.Length - 1)],
                    FavoriteColor = color
                };

                animal = cat;
                break;

            case 1:     // Dog
                var dog = new DogViewModel
                {
                    Name          = name,
                    ImageUrl      = _dogImageUrls[_random.Next(0, _dogImageUrls.Length - 1)],
                    FavoriteColor = color
                };

                animal = dog;
                break;

            case 2:     // Monkey
                var monkey = new MonkeyViewModel
                {
                    Name          = name,
                    ImageUrl      = _monkeyImageUrls[_random.Next(0, _monkeyImageUrls.Length - 1)],
                    FavoriteColor = color
                };

                animal = monkey;
                break;
            }

            return(animal);
        }
コード例 #22
0
        public void MapperExtensions_ToModel()
        {
            //init

            var viewModel = new CatViewModel()
            {
                Identifier = "identifier",
                ImageUrl   = new Uri("http://myimg.com"),
                Votes      = 15
            };

            //system-under-test

            var model = viewModel.ToModel();

            //assert
            AssertModelEqualViewModel(model, viewModel);
        }
コード例 #23
0
        public async Task <ActionResult <Cat> > GetCat(int id)
        {
            var cat = await _db.Cats.FindAsync(id);

            if (cat == null)
            {
                return(NotFound());
            }
            else
            {
                var catViewModel = new CatViewModel
                {
                    Id         = cat.Id,
                    BirthDate  = cat.BirthDate,
                    Notes      = cat.Notes,
                    Age        = cat.Age,
                    Breed      = cat.Breed,
                    Color      = cat.Color,
                    Name       = cat.Name,
                    Sex        = cat.Sex,
                    Parent     = cat.Parent,
                    Pedigree   = cat.Pedigree,
                    Chipped    = cat.Chipped,
                    Vaccinated = cat.Vaccinated,
                    CatLitter  = cat.CatLitter
                };

                var imageQuery = from images in _db.Images
                                 join ci in _db.Cat_Image on images.Id equals ci.ImageId
                                 where ci.CatId == id
                                 select images;
                catViewModel.Images = await imageQuery.ToListAsync();

                //List<Image> imgs = await imageQuery.ToListAsync();

                //foreach (Image img in imgs)
                //{
                //    img.Value = img.DecompressImage(img.Value);
                //}
                //catViewModel.Images = imgs;

                return(Ok(catViewModel));
            }
        }
コード例 #24
0
        public IActionResult Details(int id)
        {
            var cat = this.Context.Cats.FirstOrDefault(x => x.Id == id);

            if (cat == default(Cat))
            {
                return(RedirectToAction("Index", "Home"));
            }

            CatViewModel catViewModel = new CatViewModel()
            {
                Name     = cat.Name,
                Age      = cat.Age.ToString(),
                Breed    = cat.Breed,
                ImageURL = cat.ImageURL
            };

            return(this.View(catViewModel));
        }
コード例 #25
0
        public HttpResponse Cats()
        {
            const string nameKey = "Name";
            const string ageKey  = "Age";

            var query = this.Request.Query;

            var catName = query.ContainsKey(nameKey) ? query[nameKey] : "the cats";

            var catAge = query.ContainsKey(ageKey) ? int.Parse(query[ageKey]) : 0;

            var viewModel = new CatViewModel()
            {
                Name = catName,
                Age  = catAge
            };

            return(View(viewModel));
        }
コード例 #26
0
        public async Task <CatViewModel> GetCat(string id)
        {
            Cat cat = await context.Cats.Include(c => c.Breed).FirstOrDefaultAsync(c => c.Id == id);

            CatViewModel catViewModel;

            if (cat != null)
            {
                catViewModel = new CatViewModel
                {
                    Id       = cat.Id,
                    Name     = cat.Name,
                    Age      = cat.Age,
                    BreedId  = cat.BreedId,
                    Breed    = cat.Breed,
                    ImageUrl = cat.ImageUrl
                };
                return(catViewModel);
            }
            return(null);
        }
コード例 #27
0
        // GET: Cat/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(StatusCode(400, Json("Bad request.")));
            }

            Cat cat = dbContext.Cats.Find(id);

            if (cat == null)
            {
                return(StatusCode(400, Json("Bad request.")));
            }
            CatViewModel catVM = new CatViewModel()
            {
                CatId = cat.CatId,
                Name  = cat.Name,
                Breed = cat.Breed,
                Age   = cat.Age,
                Url   = cat.ImageUrl
            };

            return(View(catVM));
        }
コード例 #28
0
 private void AssertModelEqualViewModel(Entity model, CatViewModel viewModel)
 {
     Assert.AreEqual(viewModel.Identifier, model.Identifier);
     Assert.AreEqual(viewModel.ImageUrl, model.ImageUrl);
     Assert.AreEqual(viewModel.Votes, model.Votes);
 }
コード例 #29
0
        public IActionResult Create(CatViewModel model)
        {
            _catService.AddCat(model);

            return(Redirect("/"));
        }
コード例 #30
0
 private static CatViewModel CreateCatViewModelFor(Cat cat)
 {
     var catViewModel = new CatViewModel();
     cat.RenderView(catViewModel);
     return catViewModel;
 }