Пример #1
0
        public IActionResult AddAnimal(string animalName,
                                       string animalType, string info, string images,
                                       string shelterId, string ownerName, string phoneNumbers,
                                       string age)
        {
            var shelterIdNum = 1;

            if (shelterId != null)
            {
                shelterIdNum = Int32.Parse(shelterId);
            }
            AnimalType animalTypeEnum;

            Enum.TryParse(animalType, out animalTypeEnum);
            if (animalName == null)
            {
                animalName = "";
            }
            if (info == null)
            {
                info = "";
            }
            if (images == null)
            {
                images = "";
            }

            DateTime?birthday = null;
            int      months;

            if (Int32.TryParse(age, out months))
            {
                birthday = DateTime.UtcNow.AddMonths(-months);
            }
            phoneNumbers = RemoveExtraText(phoneNumbers);

            using (var session = sessionFactory.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var shelter    = session.QueryOver <Shelter>().List().Where(s => s.Id == shelterIdNum).First();
                    var imagesList = images.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    var animal     = new ShelterAnimal
                    {
                        Name         = animalName.ToLower(),
                        AnimalType   = animalTypeEnum,
                        OwnerName    = ownerName,
                        Info         = info,
                        BirthDay     = birthday,
                        Images       = imagesList,
                        PhoneNumbers = phoneNumbers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries),
                        Created      = DateTime.UtcNow,
                        Shelter      = shelter
                    };

                    session.Save(animal);
                    transaction.Commit();
                }
            return(RedirectToAction("New"));
        }
        public QueueAnimalShelter Enqueue(string name, Species species)
        {
            var newAnimal = new ShelterAnimal(name, species, animalIndex++);

            if (newAnimal.Species == Species.Cat)
            {
                cats.Enqueue(newAnimal);
            }
            else
            {
                dogs.Enqueue(newAnimal);
            }

            return(this);
        }