예제 #1
0
        public ActionResult Create(RechercheCreateEditViewModel vm)
        {
            if (ModelState.IsValid)
            {
                vm.Recherche.Animal.Type = db.TypeAnimaux.Find(vm.TypeId);
                db.Animaux.Add(vm.Recherche.Animal);
                db.Recherches.Add(vm.Recherche);

                if (Authentification.EstConnecte())
                {
                    var sessionUtilisateur = Authentification.GetSessionUtilisateur();
                    var utilisateur        = db.Utilisateurs.Find(sessionUtilisateur.Id);
                    vm.Recherche.Animal.Maitre = utilisateur;
                }

                var photoService    = new PhotosService();
                var photosRecherche = photoService.Add(new PhotoRechercheViewModel
                {
                    Photos    = vm.ImageFilesRecherche,
                    Recherche = vm.Recherche
                });

                var photosAnimal = photoService.Add(new PhotoRechercheViewModel
                {
                    Photos = vm.ImageFilesAnimal,
                    Animal = vm.Recherche.Animal
                });

                vm.Recherche.Photos        = photosRecherche;
                vm.Recherche.Animal.Photos = photosAnimal;

                db.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("Create"));
        }