コード例 #1
0
ファイル: PetService.cs プロジェクト: domco545/PetShop
 public List <Pet> GetPetsByType(string querry)
 {
     if (_validator.StringLenght(querry, 1, 100))
     {
         return(_petRepository.GetPetsByType(querry));
     }
     else
     {
         throw new InvalidDataException("querry string is too short or too long");
     }
 }
コード例 #2
0
        private Pet PetCreation()
        {
            var NameReady             = false;
            var TypeReady             = false;
            var BirthdateReady        = false;
            var SoldDateReady         = false;
            var ColorReady            = false;
            var OwnerFNameReady       = false;
            var OwnerLNameReady       = false;
            var OwnerAddressReady     = false;
            var OwnerPhoneNumberReady = false;
            var OwnerEmailReady       = false;
            var PriceReady            = false;

            var NewPet   = new Pet();
            var NewOwner = new Owner();

            #region Pet name
            Console.Clear();
            Console.WriteLine("1. enter pet name");
            while (!NameReady)
            {
                var PetName = Console.ReadLine();

                if (!_validatorService.StringLenght(PetName, 2, 20))
                {
                    Console.WriteLine("pet name is too long or too short");
                    Console.WriteLine("try again\n");
                }
                else
                {
                    NameReady   = true;
                    NewPet.Name = PetName;
                }
            }
            #endregion

            #region Pet type
            Console.Clear();
            Console.WriteLine("2. enter pet type");
            Console.WriteLine("printing types");
            var petTypes = Enum.GetValues(typeof(Pet.TypeOfPet));
            foreach (var petType in petTypes)
            {
                Console.WriteLine(petType);
            }
            while (!TypeReady)
            {
                var type = Console.ReadLine();
                if (Enum.TryParse(type, out Pet.TypeOfPet selected))
                {
                    NewPet.Type = selected;
                    TypeReady   = true;
                }
                else
                {
                    Console.WriteLine("didnt match any type");
                    Console.WriteLine("try again\n");
                }
            }
            #endregion

            #region Pet birthdate
            Console.Clear();
            Console.WriteLine("3. enter pet birthdate");
            Console.WriteLine("use format yyyy-mm-dd or dd/mm/yyyy");
            while (!BirthdateReady)
            {
                var read = Console.ReadLine();
                if (DateTime.TryParse(read, out DateTime res))
                {
                    NewPet.Birthdate = res;
                    BirthdateReady   = true;
                }
                else
                {
                    Console.WriteLine("wrong format");
                    Console.WriteLine("try again\n");
                }
            }
            #endregion

            #region Pet sold date
            Console.Clear();
            Console.WriteLine("4. enter pet sold date");
            Console.WriteLine("if you want to set sold time to now enter 1, if you want to set manual date enter 2");
            if (Console.ReadLine() == "1")
            {
                NewPet.SoldDate = DateTime.Now;
            }
            else if (Console.ReadLine() == "2")
            {
                Console.WriteLine("enter pet sold date");
                Console.WriteLine("use format yyyy-mm-dd or dd/mm/yyyy");
                while (!SoldDateReady)
                {
                    if (DateTime.TryParse(Console.ReadLine(), out DateTime res))
                    {
                        NewPet.SoldDate = res;
                        SoldDateReady   = true;
                    }
                    else
                    {
                        Console.WriteLine("Wrong format try again\n");
                    }
                }
            }
            #endregion

            #region pet color
            Console.Clear();
            Console.WriteLine("5. enter pet color");
            while (!ColorReady)
            {
                var read = Console.ReadLine();
                if (_validatorService.StringLenght(read, 1, 40))
                {
                    NewPet.Color = read;
                    ColorReady   = true;
                }
                else
                {
                    Console.WriteLine("color needs to be at least 1 letter long and maximum 40 letters long");
                    Console.WriteLine("try again\n");
                }
            }
            #endregion

            #region owner first name
            Console.Clear();
            Console.WriteLine("6. enter Owner first name");
            while (!OwnerFNameReady)
            {
                var read = Console.ReadLine();
                if (_validatorService.StringLenght(read, 1, 20))
                {
                    NewOwner.FirstName = read;
                    OwnerFNameReady    = true;
                }
                else
                {
                    Console.WriteLine("owner first name is too short or too long");
                    Console.WriteLine("try again\n");
                }
            }
            #endregion

            #region owner last name
            Console.Clear();
            Console.WriteLine("7. enter Owner last name");
            while (!OwnerLNameReady)
            {
                var read = Console.ReadLine();
                if (_validatorService.StringLenght(read, 1, 20))
                {
                    NewOwner.LastName = read;
                    OwnerLNameReady   = true;
                }
                else
                {
                    Console.WriteLine("owner last name is too short or too long");
                    Console.WriteLine("try again\n");
                }
            }
            #endregion

            #region owner address
            Console.Clear();
            Console.WriteLine("8. enter Owner address");
            while (!OwnerAddressReady)
            {
                var read = Console.ReadLine();
                if (_validatorService.StringLenght(read, 4, 40))
                {
                    NewOwner.Address  = read;
                    OwnerAddressReady = true;
                }
                else
                {
                    Console.WriteLine("owner address is too short or too long");
                    Console.WriteLine("try again\n");
                }
            }
            #endregion

            #region owner phone number
            Console.Clear();
            Console.WriteLine("9. enter Owner phone number");
            while (!OwnerPhoneNumberReady)
            {
                var read = Console.ReadLine();
                if (_validatorService.StringLenght(read, 4, 20))
                {
                    NewOwner.PhoneNumber  = read;
                    OwnerPhoneNumberReady = true;
                }
                else
                {
                    Console.WriteLine("owner phone number is too short or too long");
                    Console.WriteLine("try again\n");
                }
            }
            #endregion

            #region owner email
            Console.Clear();
            Console.WriteLine("10. enter Owner email");
            while (!OwnerEmailReady)
            {
                var read = Console.ReadLine();
                if (_validatorService.ValidEmail(read))
                {
                    NewOwner.Email  = read;
                    OwnerEmailReady = true;
                }
                else
                {
                    Console.WriteLine("invalid email");
                    Console.WriteLine("try again\n");
                }
            }
            #endregion

            #region pet price
            Console.Clear();
            Console.WriteLine("11. enter pet price");
            while (!PriceReady)
            {
                if (double.TryParse(Console.ReadLine(), out double res))
                {
                    NewPet.price = res;
                    PriceReady   = true;
                }
                else
                {
                    Console.WriteLine("wrong format");
                    Console.WriteLine("try again\n");
                }
            }
            #endregion

            NewPet.Owner = NewOwner;
            return(NewPet);
        }