public VoucherModel GetVoucher(Guid id) { using (var context = new greenMoneyEntities()) { var voucher = context.Vouchers.Find(id); VoucherModel model = new VoucherModel(); model.VoucherId = voucher.Id; model.Issued = voucher.Issued; model.UserFirstName = voucher.Users1.FirstName; model.UserLastName = voucher.Users1.LastName; RewardModel rewardModel = new RewardModel(); Utils.CopyProperties(voucher.Rewards, rewardModel); model.RewardModel = rewardModel; return model; } }
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; }
public RewardsListModel GetSupplierRewards(Guid providerUserKey, int page, int pageSize) { using (var context = new greenMoneyEntities()) { var rewards = context.Rewards.Where(r => r.Owner_Id == providerUserKey); //var uploads = context.Uploads.Where(u => u.UploadedBy_Id == providerUserKey); //Include pagination var rewardsList = rewards.OrderByDescending(r => r.DateAdded).Skip((page - 1) * pageSize).Take(pageSize); List<RewardModel> list = new List<RewardModel>(); foreach (var reward in rewardsList) { RewardModel rewardModel = new RewardModel(); Utils.CopyProperties(reward, rewardModel); list.Add(rewardModel); } RewardsListModel model = new RewardsListModel(); model.RewardsList = list; model.NumRewards = rewards.Count(); model.Page = page; model.NumPages = (int)Math.Ceiling((double)rewards.Count() / pageSize); return model; } }
public RewardsListModel GetRewards(int? instanceId, int? excludeInstanceId, string sortBy, string category, string filter, int page, int pageSize) { using (var context = new greenMoneyEntities()) { if (pageSize == 0) { pageSize = 10; } //Get all rewards var rewards = context.Rewards .Where(r => r.AvailableFrom == null || r.AvailableFrom.Value <= DateTime.Now) .Where(r => r.AvailableTo == null || r.AvailableTo.Value > DateTime.Now) .Where(r => r.Display) .Where( r => r.State == null || (int)r.State == (int)RewardState.Approved || (int)r.State == (int)RewardState.NotModerated).ToList(); if (instanceId != null && instanceId != 0) { rewards = rewards.Where(r => r.Instance_Id == instanceId).ToList(); } else if (excludeInstanceId != null) { rewards = rewards.Where(r => r.Instance_Id != excludeInstanceId).ToList(); } //Filter all by text query if (!string.IsNullOrEmpty(filter)) rewards = rewards.Where( r => r.Name.ToLower().IndexOf(filter.ToLower()) > -1 || r.Description.ToLower().IndexOf(filter.ToLower()) > -1).ToList(); List<Rewards> selected = new List<Rewards>(); //Filter all by selected category RewardCategories rewardCategory; if (category != null) { rewardCategory = context.RewardCategories.FirstOrDefault( c => category.Equals(c.Slug, StringComparison.OrdinalIgnoreCase)); if (rewardCategory == null) { throw new Exception("reward category not found"); } //get all items, for selected and for subcategories if (rewardCategory.ParentId == 0) { var allAvailableCategories = context.RewardCategories .Where(c => c.Rewards.Any( r => (r.AvailableFrom == null || r.AvailableFrom.Value <= DateTime.Now) && (r.AvailableTo == null || r.AvailableTo.Value > DateTime.Now))); List<int> subCategoriesIds = allAvailableCategories .Where(c => c.ParentId == rewardCategory.Id).Select(c => c.Id).ToList(); subCategoriesIds.Add(rewardCategory.Id); foreach (var subCategoriesId in subCategoriesIds) { selected.AddRange(rewards.Where(r => r.CategoryId == subCategoriesId).ToList()); } } else { selected = rewards.Where(r => r.CategoryId == rewardCategory.Id).ToList(); } } else { selected = rewards.ToList(); } switch (sortBy) { case "newest": selected = selected.OrderByDescending(r => r.AvailableFrom).ThenByDescending(r => r.Id).ToList(); break; case "oldest": selected = selected.OrderBy(r => r.AvailableFrom).ThenBy(r => r.Id).ToList(); break; case "cheapest": selected = selected.OrderBy(r => r.Price).ToList(); break; case "expensive": selected = selected.OrderByDescending(r => r.Price).ToList(); break; case "popular": selected = selected.OrderByDescending(r => r.Popularity).ToList(); break; default: selected = selected.OrderByDescending(r => r.Popularity).ToList(); break; } //Include pagination var list = selected.Skip((page - 1) * pageSize).Take(pageSize); List<RewardModel> models = new List<RewardModel>(); foreach (var item in list) { RewardModel m = new RewardModel(); Utils.CopyProperties(item, m); models.Add(m); } RewardsListModel model = new RewardsListModel { RewardsList = models, NumPages = (int)Math.Ceiling((double)selected.Count() / pageSize), NumRewards = selected.Count(), Page = page }; return model; } }
public RewardModel GetRewardDetails(int id) { using (var context = new greenMoneyEntities()) { var reward = context.Rewards.SingleOrDefault(x => x.Id == id); RewardModel model = new RewardModel(); Utils.CopyProperties(reward, model); return model; } }
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; }
public int? UpdateReward(Guid providerUserKey, RewardModel model) { return new RewardsRepository().UpdateReward(providerUserKey, model); }