Example #1
0
        public void DeletePetsClient(PetsEntity petEntity, int?userId = null)
        {
            if (petEntity == null)
            {
                throw new ArgumentNullException("petEntity", "petEntity is null");
            }

            if (petEntity.Id <= 0)
            {
                throw new ArgumentNullException("petEntity.Id", "petEntity.ID is null");
            }

            if (petEntity.ClientId <= 0)
            {
                throw new ArgumentNullException("petEntity.ClientId", "ClientID is null");
            }

            using (Petz_dbEntities db = new Petz_dbEntities())
            {
                //update
                Boolean isPetClient = false;
                if (petEntity.ClientId > 0)
                {
                    isPetClient = db.petz_Client_Pet.Count(x => x.pet_id == petEntity.Id && x.client_id == petEntity.ClientId) == 1;
                }

                if (isPetClient)
                {
                    petz_Pets pet = db.petz_Pets.FirstOrDefault(x => x.pet_id == petEntity.Id);
                    if (pet != null)
                    {
                        if (userId != null && userId > 0)
                        {
                            pet.update_user_id = userId.Value;
                        }
                        else
                        {
                            pet.update_client_id = petEntity.ClientId;
                        }
                        pet.date_delete = DateTime.Now;
                        db.SaveChanges();
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException("petEntity.Id", "This Pet is not of this client");
                }
            }
        }
Example #2
0
        public void SetPetClientPicture(int clientId, int petId, byte[] byteArrayImage)
        {
            if (clientId <= 0)
            {
                throw new ArgumentNullException("clientId", "ClientID is null");
            }

            if (petId <= 0)
            {
                throw new ArgumentNullException("petId", "PetID is null");
            }


            if (byteArrayImage.Length > 512000)  // 512KB = 500 * 1024
            {
                throw new Exception("File too large! Limit of 512KB!");
            }

            using (Petz_dbEntities db = new Petz_dbEntities())
            {
                Boolean isPetClient = db.petz_Client_Pet.Count(x => x.pet_id == petId && x.client_id == clientId) == 1;
                if (isPetClient)
                {
                    petz_Pets pet = db.petz_Pets.FirstOrDefault(x => x.pet_id == petId);
                    if (pet != null)
                    {
                        pet.update_client_id = clientId;
                        pet.pet_picture      = Compressor.Compress(ImageHelper.ConvertImage(byteArrayImage, System.Drawing.Imaging.ImageFormat.Jpeg));
                    }
                    db.SaveChanges();
                }
                else
                {
                    throw new ArgumentOutOfRangeException("petId", "This Pet is not of this client");
                }
            }
        }
Example #3
0
        public void SetPetsClient(PetsEntity petEntity, int?userId = null)
        {
            if (petEntity == null)
            {
                throw new ArgumentNullException("petEntity", "petEntity is null");
            }

            if (petEntity.SubSpecies == null || petEntity.SubSpecies.Id <= 0)
            {
                throw new ArgumentNullException("petEntity.SubSpecies", "petEntity.SubSpecies is null");
            }

            if (petEntity.Size == null || petEntity.Size.Id <= 0)
            {
                throw new ArgumentNullException("petEntity.Size", "petEntity.Size is null");
            }

            if (petEntity.ClientId <= 0)
            {
                throw new ArgumentNullException("petEntity.ClientId", "ClientID is null");
            }

            using (Petz_dbEntities db = new Petz_dbEntities())
            {
                if (petEntity.Id <= 0)
                {
                    //Novo
                    petz_Pets pet = new petz_Pets();

                    if (userId != null && userId > 0)
                    {
                        pet.insert_user_id = userId.Value;
                    }
                    else
                    {
                        pet.insert_client_id = petEntity.ClientId;
                    }

                    pet.pet_birthday         = petEntity.Birthday;
                    pet.pet_color            = petEntity.Color;
                    pet.pet_document         = petEntity.Document;
                    pet.pet_name             = petEntity.Name;
                    pet.pet_nickname         = petEntity.NickName;
                    pet.pet_profile_facebook = petEntity.Facebook;
                    pet.pet_weight           = petEntity.Weight;
                    pet.size_id        = petEntity.Size.Id;
                    pet.sub_species_id = petEntity.SubSpecies.Id;
                    if (petEntity.Breed != null)
                    {
                        pet.breed_id = petEntity.Breed.Id;
                    }
                    if (petEntity.Sex == EnumSex.Female)
                    {
                        pet.pet_sex = "F";
                    }
                    if (petEntity.Sex == EnumSex.Male)
                    {
                        pet.pet_sex = "M";
                    }
                    if (petEntity.Sex == EnumSex.Other)
                    {
                        pet.pet_sex = null;
                    }
                    pet.date_insert = DateTime.Now;

                    db.petz_Pets.Add(pet);
                    db.SaveChanges();

                    int petId = pet.pet_id;
                    if (petEntity.ClientId > 0)
                    {
                        db.petz_Client_Pet.Add(new petz_Client_Pet()
                        {
                            pet_id = petId, client_id = petEntity.ClientId
                        });
                        db.SaveChanges();
                    }
                }
                else
                {
                    //update
                    Boolean isPetClient = false;
                    if (petEntity.ClientId > 0)
                    {
                        isPetClient = db.petz_Client_Pet.Count(x => x.pet_id == petEntity.Id && x.client_id == petEntity.ClientId) == 1;
                    }

                    if (isPetClient)
                    {
                        //Update
                        petz_Pets pet = db.petz_Pets.FirstOrDefault(x => x.pet_id == petEntity.Id);
                        if (pet != null)
                        {
                            if (userId != null && userId > 0)
                            {
                                pet.update_user_id = userId.Value;
                            }
                            else
                            {
                                pet.update_client_id = petEntity.ClientId;
                            }

                            pet.pet_birthday         = petEntity.Birthday;
                            pet.pet_color            = petEntity.Color;
                            pet.pet_document         = petEntity.Document;
                            pet.pet_name             = petEntity.Name;
                            pet.pet_nickname         = petEntity.NickName;
                            pet.pet_profile_facebook = petEntity.Facebook;
                            pet.pet_weight           = petEntity.Weight;
                            pet.size_id        = petEntity.Size.Id;
                            pet.sub_species_id = petEntity.SubSpecies.Id;
                            if (petEntity.Breed != null)
                            {
                                pet.breed_id = petEntity.Breed.Id;
                            }
                            if (petEntity.Sex == EnumSex.Female)
                            {
                                pet.pet_sex = "F";
                            }
                            if (petEntity.Sex == EnumSex.Male)
                            {
                                pet.pet_sex = "M";
                            }
                            if (petEntity.Sex == EnumSex.Other)
                            {
                                pet.pet_sex = null;
                            }
                            pet.date_update = DateTime.Now;

                            db.SaveChanges();
                        }
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException("petEntity.Id", "This Pet is not of this client");
                    }
                }
            }
        }