public int? UpdateChallenge(Guid providerUserKey, ChallengeModel model)
        {
            using (var context = new greenMoneyEntities())
            {
                var user = context.Users1.FirstOrDefault(u => u.Id == providerUserKey);
                var challenge = context.Challenges.FirstOrDefault(r => r.Id == model.Id);

                if (user != null && challenge != null)
                {

                    Utils.CopyProperties(model, challenge);
                    // challenge.State = model.AustraliaState;

                    challenge.Owner_Id = user.Id;
                    challenge.Instance_Id = user.Instance_Id;
                    challenge.LogoImageId = model.LogoImageId;

                    if (model.ProfileImages.Count > 0)
                    {
                        if (model.ProfileImages[0] != null && model.ProfileImages[0].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[0]);
                            challenge.ImageId1 = upload.UploadId;
                        }
                        else if (model.ImageId1 == null)
                        {
                            Uploads uploadOld = context.Uploads.SingleOrDefault(x => x.Id == challenge.ImageId1);
                            if (uploadOld != null)
                            {
                                context.Uploads.Remove(uploadOld);
                                challenge.ImageId1 = null;
                            }
                        }

                        if (model.ProfileImages[1] != null && model.ProfileImages[1].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[1]);
                            challenge.ImageId2 = upload.UploadId;
                        }
                        else if (model.ImageId2 == null)
                        {
                            Uploads uploadOld = context.Uploads.SingleOrDefault(x => x.Id == challenge.ImageId2);
                            if (uploadOld != null)
                            {
                                context.Uploads.Remove(uploadOld);
                                challenge.ImageId2 = null;
                            }
                        }

                        if (model.ProfileImages[2] != null && model.ProfileImages[2].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[2]);
                            challenge.ImageId3 = upload.UploadId;
                        }
                        else if (model.ImageId3 == null)
                        {
                            Uploads uploadOld = context.Uploads.SingleOrDefault(x => x.Id == challenge.ImageId3);
                            if (uploadOld != null)
                            {
                                context.Uploads.Remove(uploadOld);
                                challenge.ImageId3 = null;
                            }
                        }

                        if (model.ProfileImages[3] != null && model.ProfileImages[3].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[3]);
                            challenge.ImageId4 = upload.UploadId;
                        }
                        else if (model.ImageId4 == null)
                        {
                            Uploads uploadOld = context.Uploads.SingleOrDefault(x => x.Id == challenge.ImageId4);
                            if (uploadOld != null)
                            {
                                context.Uploads.Remove(uploadOld);
                                challenge.ImageId4 = null;
                            }
                        }
                    }

                    try
                    {
                        context.SaveChanges();

                        return challenge.Id;
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
                                    validationErrors.Entry.Entity.GetType().FullName,
                                    validationError.PropertyName,
                                    validationError.ErrorMessage);
                            }
                        }

                        return null;
                    }
                }
            }

            return null;
        }
        public int? UpdateReward(Guid providerUserKey, RewardModel model)
        {
            using (var context = new greenMoneyEntities())
            {
                var user = context.Users1.FirstOrDefault(u => u.Id == providerUserKey);
                var reward = context.Rewards.FirstOrDefault(r => r.Id == model.Id);

                if (user != null && reward != null)
                {
                    reward.PartnerEmail = model.PartnerEmail;
                    Utils.CopyProperties(model, reward);

                    //CopyProperties doesn't work for null values...
                    reward.ImageSmallId = model.ImageSmallId;
                    reward.VoucherBarcodeId = model.VoucherBarcodeId;

                    if (model.ProfileImages.Count > 0)
                    {
                        if (model.ProfileImages[0] != null && model.ProfileImages[0].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[0]);
                            reward.ImageId = upload.UploadId;
                        }
                        else if (model.ImageId == null)
                        {
                            Uploads uploadOld = context.Uploads.SingleOrDefault(x => x.Id == reward.ImageId);
                            if (uploadOld != null)
                            {
                                context.Uploads.Remove(uploadOld);
                                reward.ImageId = null;
                            }
                        }

                        if (model.ProfileImages[1] != null && model.ProfileImages[1].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[1]);
                            reward.Image2Id = upload.UploadId;
                        }
                        else if (model.Image2Id == null)
                        {
                            Uploads uploadOld = context.Uploads.SingleOrDefault(x => x.Id == reward.Image2Id);
                            if (uploadOld != null)
                            {
                                context.Uploads.Remove(uploadOld);
                                reward.Image2Id = null;
                            }
                        }

                        if (model.ProfileImages[2] != null && model.ProfileImages[2].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[2]);
                            reward.Image3Id = upload.UploadId;
                        }
                        else if (model.Image3Id == null)
                        {
                            Uploads uploadOld = context.Uploads.SingleOrDefault(x => x.Id == reward.Image3Id);
                            if (uploadOld != null)
                            {
                                context.Uploads.Remove(uploadOld);
                                reward.Image3Id = null;
                            }
                        }

                        if (model.ProfileImages[3] != null && model.ProfileImages[3].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[3]);
                            reward.Image4Id = upload.UploadId;
                        }
                        else if (model.Image4Id == null)
                        {
                            Uploads uploadOld = context.Uploads.SingleOrDefault(x => x.Id == reward.Image4Id);
                            if (uploadOld != null)
                            {
                                context.Uploads.Remove(uploadOld);
                                reward.Image4Id = null;
                            }
                        }
                    }

                    try
                    {

                        context.SaveChanges();

                        return reward.Id;
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
                                    validationErrors.Entry.Entity.GetType().FullName,
                                    validationError.PropertyName,
                                    validationError.ErrorMessage);
                            }
                        }

                        return null;
                    }
                }
            }

            return null;
        }
        //challenge
        public int? CreateChallenge(Guid providerUserKey, ChallengeModel model)
        {
            using (var context = new greenMoneyEntities())
            {
                var user = context.Users1.FirstOrDefault(u => u.Id == providerUserKey);
                if (user != null)
                {
                    Challenges challenge = new Challenges();

                    Utils.CopyProperties(model, challenge);

                    challenge.Owner_Id = user.Id;
                    challenge.Instance_Id = user.Instance_Id;

                    challenge.DateAdded = DateTime.Now;
                    challenge.Popularity = 0;
                    //  challenge.State = model.AustraliaState;

                    if (model.ProfileImages.Count > 0)
                    {
                        if (model.ProfileImages[0] != null && model.ProfileImages[0].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[0]);
                            challenge.ImageId1 = upload.UploadId;
                        }

                        if (model.ProfileImages[1] != null && model.ProfileImages[1].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[1]);
                            challenge.ImageId2 = upload.UploadId;
                        }

                        if (model.ProfileImages[2] != null && model.ProfileImages[2].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[2]);
                            challenge.ImageId3 = upload.UploadId;
                        }

                        if (model.ProfileImages[3] != null && model.ProfileImages[3].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[3]);
                            challenge.ImageId4 = upload.UploadId;
                        }

                    }

                    context.Challenges.Add(challenge);
                    try
                    {
                        context.SaveChanges();

                        return challenge.Id;

                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
                                    validationErrors.Entry.Entity.GetType().FullName,
                                    validationError.PropertyName,
                                    validationError.ErrorMessage);
                            }
                        }

                        return null;
                    }
                }
            }
            return null;
        }
        public int? CreateReward(Guid providerUserKey, RewardModel model)
        {
            using (var context = new greenMoneyEntities())
            {
                var user = context.Users1.FirstOrDefault(u => u.Id == providerUserKey);

                if (user != null)
                {
                    Rewards reward = new Rewards();
                    reward.PartnerEmail = model.PartnerEmail;
                    reward.State = (int)RewardState.Incomplete;
                    reward.Owner_Id = user.Id;
                    reward.Instance_Id = user.Instance_Id;
                    reward.Instance = user.Instance;

                    Utils.CopyProperties(model, reward);

                    reward.DateAdded = DateTime.Now;

                    if (model.ProfileImages.Count > 0)
                    {
                        if (model.ProfileImages[0] != null && model.ProfileImages[0].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[0]);
                            reward.ImageId = upload.UploadId;
                        }

                        if (model.ProfileImages[1] != null && model.ProfileImages[1].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[1]);
                            reward.Image2Id = upload.UploadId;
                        }

                        if (model.ProfileImages[2] != null && model.ProfileImages[2].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[2]);
                            reward.Image3Id = upload.UploadId;
                        }

                        if (model.ProfileImages[3] != null && model.ProfileImages[3].FileName != null)
                        {
                            var upload = new UploadsRepository().UploadFile(providerUserKey.ToString(), model.ProfileImages[3]);
                            reward.Image4Id = upload.UploadId;
                        }

                    }

                    context.Rewards.Add(reward);

                    try
                    {
                        context.SaveChanges();

                        return reward.Id;

                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
                                    validationErrors.Entry.Entity.GetType().FullName,
                                    validationError.PropertyName,
                                    validationError.ErrorMessage);
                            }
                        }

                        return null;
                    }

                }
            }

            return null;
        }