Ejemplo n.º 1
0
 public Animal(string animalType, uint age, AnimalOwner animalOwner, string animalName = Nameless)
 {
     Type  = animalType;
     Age   = age;
     Owner = animalOwner;
     Name  = animalName;
 }
Ejemplo n.º 2
0
        private ActionResult InternalSaveOwner(AnimalOwner o, FormCollection fields)
        {
            try
            {
                TryUpdateModel(o, new string[] { "IsPrimary", "Starting", "Ending" });

                if (string.IsNullOrEmpty(fields["pid_a"]))
                {
                    ModelState.AddModelError("Owner", "Required. Please pick from list.");
                }
                else
                {
                    Guid   personId = new Guid(fields["pid_a"]);
                    Member member   = (from m in this.db.Members where m.Id == personId select m).First();
                    o.Owner = member;
                }

                if (ModelState.IsValid)
                {
                    this.db.SaveChanges();
                    TempData["message"] = "Saved";
                    return(RedirectToAction("ClosePopup"));
                }
            }
            catch (RuleViolationsException ex)
            {
                this.CollectRuleViolations(ex, fields);
            }
            return(InternalEditOwner(o));
        }
Ejemplo n.º 3
0
        public void Update(AnimalRegistration animal)
        {
            db.Entry(animal).State = EntityState.Modified;
            db.SaveChanges();

            AnimalOwner animalOwner = db.AnimalOwners.FirstOrDefault(m => m.AnimalId == animal.id && m.OwnerId == animal.ownerId);

            if (animalOwner != null)
            {
                db.AnimalOwners.Remove(animalOwner);
                db.SaveChanges();
            }


            if (animal.ownerId != null)
            {
                AnimalOwner aniOwner = new AnimalOwner();
                aniOwner.Owner              = db.OwnerKeeper.Find(Convert.ToInt32(animal.ownerId));
                aniOwner.OwnerId            = Convert.ToInt32(animal.ownerId);
                aniOwner.AnimalId           = animal.id;
                aniOwner.AnimalRegistration = animal;
                db.AnimalOwners.Add(aniOwner);
            }

            db.SaveChanges();
        }
Ejemplo n.º 4
0
        public ActionResult DeleteOwner(Guid id, FormCollection fields)
        {
            AnimalOwner o = (from ao in this.db.AnimalOwners where ao.Id == id select ao).First();

            this.db.AnimalOwners.Remove(o);
            this.db.SaveChanges();

            return(RedirectToAction("ClosePopup"));
        }
Ejemplo n.º 5
0
        public void WhenEnforceConstrainFromTypeYouUseThatType()
        {
            var paul = new AnimalOwner <Person, Cat> (new Person {
                Name = "Paul"
            });

            //In the generic, in describe method, you can use owner.Name property
            //because we know that owner MUST implement IHasName interface that has a Name property
            Assert.AreEqual("Paul loves Paul pets", paul.Describe());
        }
Ejemplo n.º 6
0
        public void CanComposeWithMultipleGenerics()
        {
            var paul = new AnimalOwner <Person, Cat> (new Person {
                Name = "Paul"
            });
            var expected = new[] { typeof(Person), typeof(Cat) };

            Assert.AreEqual(2, paul.GetType().GetGenericArguments().Length);
            Assert.AreEqual(expected, paul.GetType().GetGenericArguments());
        }
Ejemplo n.º 7
0
        public void WhenEnforceConstrainFromTypeYouUseThatType()
        {
            var paul = new AnimalOwner <Person, Cat> (new Person {
                Name = "Manu"
            });

            // En el genèric, en la descripció del mètode, podeu utilitzar la propietat owner.Name
            // perquè sabem que el propietari HA d'implementar la interfície IHasName, que té la propietat Name.
            //Assert.AreEqual (FILL_ME_IN, paul.Describe ());
            Assert.AreEqual("Manu estima Manu les mascotes", paul.Describe());
        }
Ejemplo n.º 8
0
        public void Delete(int id)
        {
            var         animal      = db.animalRegistration.Find(id);
            AnimalOwner animalOwner = db.AnimalOwners.FirstOrDefault(m => m.AnimalId == animal.id && m.OwnerId == animal.ownerId);

            if (animalOwner != null)
            {
                db.AnimalOwners.Remove(animalOwner);
                db.SaveChanges();
            }
            db.animalRegistration.Remove(animal);
            db.SaveChanges();
        }
Ejemplo n.º 9
0
        public ActionResult CreateOwner(Guid id)
        {
            ViewData["PageTitle"] = "New Owner";

            AnimalOwner o = new AnimalOwner();

            o.Animal   = (from a in this.db.Animals where a.Id == id select a).First();
            o.Starting = DateTime.Today;
            //s.Person = (from p in this.db.Members where p.Id == personId select p).First();
            //s.Activated = DateTime.Today;

            Session.Add("NewOwnerGuid", o.Id);
            ViewData["NewOwnerGuid"] = Session["NewOwnerGuid"];

            return(InternalEditOwner(o));
        }
Ejemplo n.º 10
0
        public ActionResult CreateOwner(Guid id, FormCollection fields)
        {
            if (Session["NewOwnerGuid"] != null && Session["NewOwnerGuid"].ToString() != fields["NewOwnerGuid"])
            {
                throw new InvalidOperationException("Invalid operation. Are you trying to re-create an ownership?");
            }
            Session.Remove("NewOwnerGuid");

            ViewData["PageTitle"] = "New Owner";

            AnimalOwner o = new AnimalOwner();

            o.Animal = (from a in this.db.Animals where a.Id == id select a).First();
            //    um.Person = (from p in this.db.Members where p.Id == personId select p).First();
            this.db.AnimalOwners.Add(o);
            return(InternalSaveOwner(o, fields));
        }
Ejemplo n.º 11
0
        public void Insert(AnimalRegistration animal)
        {
            db.animalRegistration.Add(animal);

            db.SaveChanges();
            keeper keeper = db.keeper.FirstOrDefault(m => m.id == animal.keeperId);

            animal.keeper = keeper;
            animal        = db.animalRegistration.LastOrDefault();
            if (db.OwnerKeeper.Any(m => m.id == animal.ownerId))
            {
                AnimalOwner animalowner = new AnimalOwner();
                animalowner.Owner              = db.OwnerKeeper.Find(Convert.ToInt32(animal.ownerId));
                animalowner.OwnerId            = Convert.ToInt32(animal.ownerId);
                animalowner.AnimalId           = animal.id;
                animalowner.AnimalRegistration = animal;

                db.AnimalOwners.Add(animalowner);
                db.SaveChanges();
            }
        }
Ejemplo n.º 12
0
        public void ReportFormat()
        {
            string   animalName = "Animal Bob";
            DateTime starting   = DateTime.Now;
            DateTime?ending     = null;
            var      owner      = new Member {
                FirstName = "Fred", LastName = "Rider"
            };
            var ao = new AnimalOwner {
                Owner  = owner,
                Animal = new Animal {
                    Name = animalName
                },
                Starting = starting,
                Ending   = ending
            };

            var html = ao.GetReportHtml();

            Assert.IsTrue(html.Contains(animalName), "animal name");
            Assert.IsTrue(html.Contains(owner.FullName), "owner name");
            Assert.IsTrue(html.Contains(starting.ToString()), "starting");
        }
Ejemplo n.º 13
0
        private static void Main(string[] args)
        {
            try
            {
                Input = new StreamReader("Files/input.txt");
            }
            catch
            {
                Console.WriteLine("There are some errors with the input file. Input file is not exists or it is empty");
                Console.ReadKey();
                return;
            }

            AnimalOwners = new Dictionary <string, AnimalOwner>();
            AnimalsTypes = new Dictionary <string, AnimalTypeData>();

            while (!Input.EndOfStream)
            {
                string[] BufferStringArray = Input.ReadLine().Split(',');

                if (!AnimalOwners.ContainsKey(BufferStringArray[0]))
                {
                    AnimalOwners[BufferStringArray[0]] = new AnimalOwner(BufferStringArray[0]);
                }

                if (!AnimalsTypes.ContainsKey(BufferStringArray[1]))
                {
                    AnimalsTypes[BufferStringArray[1]] = new AnimalTypeData();
                }


                Animal BufferAnimal = new Animal
                                      (
                    BufferStringArray[1],
                    uint.Parse(BufferStringArray[3]),
                    AnimalOwners[BufferStringArray[0]],
                    BufferStringArray[2] == string.Empty ? Animal.Nameless : BufferStringArray[2]
                                      );


                AnimalOwners[BufferStringArray[0]].OwnAnimals.Add(BufferAnimal);

                var CachedTypeData = AnimalsTypes[BufferStringArray[1]];
                CachedTypeData.Animals.Add(BufferAnimal);
                CachedTypeData.Owners.Add(BufferStringArray[0]);
                CachedTypeData.TypeAge.UpdateAge(BufferAnimal.Age);
            }

            Console.WriteLine("Type 1/2/3/4 to continue");
            var Key = Console.ReadKey().KeyChar;

            Console.WriteLine();

            switch (Key)
            {
            case '1':
            {
                foreach (var animalOwner in AnimalOwners.Values)
                {
                    int counter = 0;
                    HashSet <string> bufferTypes = new HashSet <string>();

                    foreach (var animal in animalOwner.OwnAnimals)
                    {
                        if (bufferTypes.Add(animal.Name))
                        {
                            counter++;
                        }
                    }

                    Console.WriteLine(string.Format("[{0}] has animals of [{1}] different types", animalOwner.Name, counter));
                }

                break;
            }

            case '2':
            {
                Console.WriteLine("Enter an animal type");
                string bufferTypeString = Console.ReadLine();

                if (!AnimalsTypes.ContainsKey(bufferTypeString))
                {
                    Console.WriteLine(string.Format("There are no animals of the selected type"));
                    break;
                }

                foreach (var owner in AnimalsTypes[bufferTypeString].Owners)
                {
                    Console.WriteLine(string.Format("Animal owner: [{0}]", owner));

                    foreach (var animal in AnimalOwners[owner].OwnAnimals)
                    {
                        if (animal.Type == bufferTypeString)
                        {
                            Console.WriteLine(string.Format("   Animal name: [{0}]", animal.Name));
                        }
                    }

                    Console.WriteLine();
                }

                break;
            }

            case '3':
            {
                Console.WriteLine("Enter an animal name");
                string bufferString = Console.ReadLine();

                HashSet <string> bufferTypes = new HashSet <string>();

                foreach (var animals in AnimalsTypes.Values)
                {
                    foreach (var animal in animals.Animals)
                    {
                        if (animal.Name == bufferString)
                        {
                            bufferTypes.Add(animal.Type);
                            break;
                        }
                    }
                }


                Console.WriteLine(string.Format("Such name have animals of [{0}] different types", bufferTypes.Count));

                break;
            }

            case '4':
            {
                foreach (var type in AnimalsTypes.Keys)
                {
                    Console.WriteLine(string.Format("Type [{0,8}]: MIN age = [{1}], MAX age = [{2}]",
                                                    type,
                                                    AnimalsTypes[type].TypeAge.Min,
                                                    AnimalsTypes[type].TypeAge.Max));
                }
                break;
            }

            default:
            {
                Console.WriteLine("Wrong input");
                break;
            }
            }


            Console.ReadKey();
        }
Ejemplo n.º 14
0
 private ActionResult InternalEditOwner(AnimalOwner o)
 {
     return(View("EditOwner", o));
 }
Ejemplo n.º 15
0
        public ActionResult EditOwner(Guid id, FormCollection fields)
        {
            AnimalOwner o = (from ao in this.db.AnimalOwners.Include("Animal").Include("Owner") where ao.Id == id select ao).First();

            return(InternalSaveOwner(o, fields));
        }
Ejemplo n.º 16
0
        public ActionResult EditOwner(Guid id)
        {
            AnimalOwner o = (from ao in this.db.AnimalOwners.Include("Animal").Include("Owner") where ao.Id == id select ao).First();

            return(InternalEditOwner(o));
        }