public Dog ViewModelToDog(int id, DogViewModel dog) { return(new Dog() { Id = id, Name = dog.Name, Breed = dog.Breed }); }
public Dog Edit(int id, DogViewModel dog) { return(_dogsRepository.Edit(new Dog() { Id = id, Name = dog.Name, Breed = dog.Breed })); }
public bool EditDog(DogViewModel dogToBeEdited) { try { Dog dog = new Dog() { DogID = dogToBeEdited.DogID, DogOwnerID = dogToBeEdited.DogOwnerID, DogName = dogToBeEdited.DogName, DogSize = dogToBeEdited.DogSize, Description = dogToBeEdited.Description }; dogRepo.Update(dog); unitOfWork.Commit(); return(true); } catch (EntityException ex) { Debug.WriteLine(ex.Message); return(false); } finally { if (unitOfWork != null) { unitOfWork.Dispose(); } } }
// GET: DogManager/Edit/5 public ActionResult Edit(int id) { DogViewModel dogVM = new DogViewModel(); dogVM.ThisDog = _dogRepo.GetDogById(id); if (dogVM.ThisDog.Statuses.Count > 0) { dogVM.currentStatus = dogVM.ThisDog.Statuses.Last(); } else { dogVM.currentStatus = new Status(); dogVM.currentStatus.Date = DateTime.Now; dogVM.currentStatus.DogStatus = ""; dogVM.currentStatus.DogId = dogVM.ThisDog.Id; dogVM.currentStatus.Id = 0; } dogVM.Breeds = new SelectList(_breedRepo.GetBreedList(), "Name", "Name"); if (dogVM.ThisDog.Images.Count == 0) { //Image newImage = new Image() //{ // Name = "noPhoto.jpg", // DogId = dogVM.ThisDog.Id //}; //dogVM.ThisDog.Images.Add(newImage); } return(View(dogVM)); }
public ActionResult Edit(DogViewModel dvm) { DogClient dc = new DogClient(); dc.Edit(dvm.dog); return(RedirectToAction("Index")); }
public ActionResult Create(DogViewModel model, HttpPostedFileBase file) { bool saveSuccess = true; if (ModelState.IsValid) { string path = Path.Combine(Server.MapPath("~/dogimages"), model.PhotoPath); List <string> errors = homeOrchestrator.ValidateDogPhoto(file, path); if (errors.Count > 0) { errors.ForEach(e => ModelState.AddModelError(string.Empty, e)); saveSuccess = false; } else { saveSuccess = homeOrchestrator.InsertDog(MapModelToDto(model)); } } if (saveSuccess) { return(RedirectToAction("Index", "Home")); } else { //Repopulate color and breed list for view ViewData["dogColors"] = homeOrchestrator.GetDogColors(); ViewData["dogBreeds"] = homeOrchestrator.GetDogBreeds(); return(View(model)); } }
public CollectionViewChallengePage() { InitializeComponent(); vm = new DogViewModel(); BindingContext = vm; }
// GET: DogManager/Create public ActionResult Create() { DogViewModel dogVM = new DogViewModel(); dogVM.Breeds = new SelectList(_breedRepo.GetBreedList(), "Name", "Name"); dogVM.ThisDog = new Dog(); return(View(dogVM)); }
public ActionResult Edit(int id) { DogClient dc = new DogClient(); DogViewModel dvm = new DogViewModel(); dvm.dog = dc.find(id); return(View("Edit", dvm)); }
private void MainWindow_Loaded(object sender, RoutedEventArgs e) { // For test: LOAD & SET your DataContext here // var myDogViewmodel = new DogViewModel(); myDogViewModel.LoadData(); this.DataContext = myDogViewmodel; }
public IActionResult DeleteDog(int id) { Dogs dog = adminRepository.GetDogs(id); DogViewModel model = new DogViewModel { Name = $"{dog.Name}" }; return(PartialView("~/Views/Administrator/_DeleteDog.cshtml", model)); }
public async Task <DogViewModel> GetDogViewModel() { var result = await _dogService.GetDog(); var viewModel = new DogViewModel(); viewModel.ImageUrl = result.Message; return(viewModel); }
public ActionResult ViewDogs() { DogViewModel DogVm = new DogViewModel(); List <Animal> TempList = _service.fetchAllAnimals().Where(a => a.Type == BaseType.Dog).ToList(); List <Dog> DogList = TempList.Cast <Dog>().ToList(); DogVm.DogList = DogList; return(View(DogVm)); }
public static Dog Map(DogViewModel viewModel) => new Dog { Id = viewModel.Id, Name = viewModel.Name, Breed = viewModel.Breed, Owner = new Account { Email = viewModel.OwnerEmail, Name = viewModel.OwnerName } };
public async Task <IActionResult> Create([Bind("DogId,Name,DateOfBirth,HereSince,Description,DogRaceId")] DogViewModel dog) { if (ModelState.IsValid) { _context.Add(dog); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["DogRaceId"] = new SelectList(_context.DogRaces, "DogRaceId", "DogRaceId", dog.DogRaceId); return(View(dog)); }
public ActionResult ViewDogDetails(DogViewModel model) { DogViewModel dog = new DogViewModel(homeOrchestrator.GetDogById(model.Id)); if (dog != null) { return(PartialView("_DogDetailsPartial", dog)); } else { return(RedirectToAction("index", "home")); } }
public void ViewModelInit_DoesNotThrows() { Assert.DoesNotThrow(() => { var dogModel = new DogModel { Name = "name" }; var vm = new DogViewModel(dogModel); Assert.AreEqual(vm.Name, dogModel.Name); }); }
public DogViewModel GetDog(int DogID) { var dogy = dogRepo.GetByID(DogID); DogViewModel dogForEdit = new DogViewModel() { DogID = dogy.DogID, DogOwnerID = dogy.DogOwnerID, DogName = dogy.DogName, DogSize = dogy.DogSize, Description = dogy.Description }; return(dogForEdit); }
public IActionResult AddEditDog(int?id, DogViewModel model, IFormFile file) { try { if (ModelState.IsValid) { bool isNew = !id.HasValue; Dogs dogs = isNew ? new Dogs { } : adminRepository.GetDogs(id.Value); //var Id = breedRepository.GetBreedsIdByName(model.Breeds); if (!breedRepository.CheckIfBreedExists(model.Breeds)) { breedRepository.AddBreed(model.BreedId, model.Breeds); } //if (!gendersRepository.CheckIfGenderExists(model.GenderId)) //{ // gendersRepository.AddGender(model.GenderId, model.Genders); //} dogs.Name = model.Name; dogs.Age = model.Age; dogs.Image = model.Image; dogs.BreedId = breedRepository.GetBreedsIdByName(model.Breeds); List <Breeds> breeds = breedRepository.getAllBreeds(); dogs.GenderId = gendersRepository.GetGendersIdByName(model.Genders); List <Genders> genders = gendersRepository.getAllGenders(); dogs.Description = model.Description; if (isNew) { adminRepository.Add(dogs); UploadFile(file, dogs.DogId); //dogs.Image = file.FileName; } else { adminRepository.Update(dogs); UploadFile(file, dogs.DogId); //dogs.Image = file.FileName; } } } catch (Exception ex) { throw ex; } return(RedirectToAction("Admin")); }
public IActionResult CreateDog([FromBody] DogViewModel dogModel) { int id = _service.Add(dogModel); if (id > 0) { dogModel.Id = id; return(Ok(dogModel)); } else { return(BadRequest()); } }
[Authorize(Roles = "Administrator, Moderator")] // must be one of these roles public async Task <ActionResult> Delete(int id) { HttpResponseMessage response = await _httpClient.GetAsync($"{_dogsUrl}/{id}"); if (!response.IsSuccessStatusCode) { return(View("Error", new ErrorViewModel())); } Dog dog = await response.Content.ReadAsAsync <Dog>(); DogViewModel model = Mapper.Map(dog); return(View(model)); }
public async Task <ActionResult> Create(DogViewModel viewModel) { try { if (!ModelState.IsValid) { return(View(viewModel)); } string email; if (User.IsInRole("Administrator")) { email = viewModel.OwnerEmail; } else { email = User.FindFirst(ClaimTypes.Email).Value; } var dog = new Dog { Name = viewModel.Name, Breed = viewModel.Breed, Owner = new Account { Email = email } }; HttpResponseMessage response = await _httpClient.PostAsync( _dogsUrl, dog, new JsonMediaTypeFormatter()); if (!response.IsSuccessStatusCode) { // could check specifically for 400 and look inside // body for the model errors, to then add to the ModelState here. ModelState.AddModelError("", "Invalid data"); return(View(viewModel)); } // i don't actually need the response body return(RedirectToAction(nameof(Index))); } catch { return(View(viewModel)); } }
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); }
public ActionResult EditDog(DogViewModel DogToBeEdited) { if (!ModelState.IsValid) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } else { if (dogService.EditDog(DogToBeEdited)) { return(Json(true)); } else { return(Json(false)); } } }
public async Task <ActionResult> Edit(int id, DogViewModel viewModel) { try { var dog = new Dog { Name = viewModel.Name, Breed = viewModel.Breed, Owner = new Account() }; if (User.IsInRole("Administrator")) { dog.Owner.Email = viewModel.OwnerEmail; } else { dog.Owner.Email = User.FindFirst(ClaimTypes.Email).Value; } HttpResponseMessage response = await _httpClient.PutAsync( $"{_dogsUrl}/{id}", dog, new JsonMediaTypeFormatter()); // if the user is not allowed to edit this dog, he will be denied by the API if (!response.IsSuccessStatusCode) { if (response.StatusCode == HttpStatusCode.Forbidden) { return(RedirectToAction(nameof(AccountController.AccessDenied), "Account")); } // could check specifically for 400 and look inside // body for the model errors, to then add to the ModelState here. ModelState.AddModelError("", "Invalid data"); return(View(viewModel)); } return(RedirectToAction(nameof(Index))); } catch { ModelState.AddModelError("", "An error occurred"); return(View(viewModel)); } }
private DogViewModel GetDogWhereIdIsFirstOrDefault(int id) { DogDAL dogDAL = _doggyDayCareContext.Dogs .Where(dog => dog.DogID == id) .FirstOrDefault(); // will create error page later //if (dogDAL != null) //{ //} var dog = new DogViewModel(); dog.ID = dogDAL.DogID; dog.Name = dogDAL.Name; dog.DayOfTheWeek = dogDAL.DayOfTheWeek; return(dog); }
public IActionResult Edit(int id, DogViewModel dog) { if (ModelState.IsValid) { Dog doggy = _dogsService.Edit(id, dog); if (doggy == null) { ModelState.AddModelError(string.Empty, "Unable to save changes."); return(View(_dogsService.ViewModelToDog(id, dog))); } return(RedirectToAction(nameof(Index))); } else { return(View(_dogsService.ViewModelToDog(id, dog))); } }
public IActionResult Create(DogViewModel dog) { if (ModelState.IsValid) { Dog doggy = _dogsService.Create(dog.Name, dog.Breed); if (doggy == null) { ModelState.AddModelError(string.Empty, "Unable to create."); return(View(dog)); } return(RedirectToAction(nameof(Index))); } else { return(View(dog)); } }
public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } var dog = await _context.Dogs .Include(d => d.Race) .SingleOrDefaultAsync(m => m.DogId == id); if (dog == null) { return(NotFound()); } int numberOfEvents = 0; List <Event> events = _context.Events.ToList(); List <Dog> dogs = _context.Dogs.ToList(); //LINQ JOIN var eventsQuery = from e in events join d in dogs on e.Doge.DogId equals d.DogId into eq select new { Key = e.Doge.DogId, Items = eq }; foreach (var item in eventsQuery) { if (item.Key == id) { numberOfEvents++; } } //numberOfEvents = eventsQuery.Count(); DogViewModel dogViewModel = new DogViewModel(); dogViewModel.Name = dog.Name; dogViewModel.DateOfBirth = dog.DateOfBirth; dogViewModel.Description = dog.Description; dogViewModel.DogId = dog.DogId; dogViewModel.Race = dog.Race; dogViewModel.HereSince = dog.HereSince; dogViewModel.NumberOfEvents = numberOfEvents; return(View(dogViewModel)); }
public IActionResult My() { var dogs = new List <Dog>(); using (context) { dogs = context.Dogs.Where(d => d.OwnerId == userManager.GetUserId(User)) .Include(d => d.Owner) .Include(d => d.Vet) .Include(d => d.Images) .ToList(); } var result = new List <DogViewModel>(); foreach (var dog in dogs) { var dogModel = new DogViewModel { Id = dog.Id, Name = dog.Name, Gender = dog.Gender, BirthDate = dog.BirthDate, Province = dog.Province, IsVaccinated = dog.IsVaccinated, IsDisinfected = dog.IsDisinfected, IsCastrated = dog.IsCastrated, Owner = dog.Owner.FirstName + " " + dog.Owner.LastName, OwnerNotes = dog.OwnerNotes, Vet = dog.Vet != null ? dog.Vet.FirstName + " " + dog.Vet.LastName : "No vet", Breed = dog.Breed }; foreach (var pic in dog.Images) { dogModel.Images.Add(pic.Id, pic.ImageUrl); } result.Add(dogModel); } return(View("SearchResults", result)); }