private IEnumerable <Guid> GetGuidsFromUsers(IEnumerable <string> users) { using (var ctx = new OpenGroovesEntities()) { var userIds = from u in ctx.Users join i in users on u.UserName equals i select u.UserId; return(userIds.AsEnumerable()); } }
public void SaveData(IDictionary <string, string> data) { using (var ctx = new OpenGroovesEntities()) { var settings = ctx.UserSettings.ToList().Where(s => data.ContainsKey(s.Key) && s.UserId == _userId); settings.ToList().ForEach(s => ctx.UserSettings.DeleteObject(s)); data.ToList().ForEach(s => ctx.AddToUserSettings(new UserSetting { Key = s.Key, Value = s.Value, UserId = _userId })); ctx.SaveChanges(); } }
public T GetValue <T>(string key) { using (var ctx = new OpenGroovesEntities()) { var setting = ctx.UserSettings.SingleOrDefault(s => s.Key == key && s.UserId == _userId); if (setting != null) { var converter = TypeDescriptor.GetConverter(typeof(T)); return((T)converter.ConvertFromInvariantString(setting.Value)); } return(default(T)); } }
public IDictionary <string, string> GetData() { // using EF directly, cool? EDIT: No, not cool using (var ctx = new OpenGroovesEntities()) { var settings = ctx.UserSettings.Where(s => s.UserId == _userId).ToDictionary(u => u.Key, u => u.Value); if (settings != null) { return(settings); } return(null); } }
public override void SendNotifications(NotificationType type, IEnumerable <Guid> userIds, MessageData data, bool ignoreSettings = false) { using (var ctx = new OpenGroovesEntities()) { string typeStr = type.ToString(); var emails = (from s in ctx.UserSettings join u in userIds on s.UserId equals u where ignoreSettings ? true : (s.Value == "True" && s.Key.Equals(typeStr, StringComparison.OrdinalIgnoreCase)) select s.User.Email).Distinct().ToList(); var replacements = BuildReplacements(data); var msg = TemplateBuilder.LoadTemplate(type, replacements); SendMessage(msg, emails); } }
public override void SendBandNotifications(NotificationType type, Guid bandId, BandMessageData data) { using (var ctx = new OpenGroovesEntities()) { string typeStr = type.ToString(); // Get list of users who are following this band and have notifications on to receive ctx.ContextOptions.LazyLoadingEnabled = true; var emails = (from r in ctx.UsersBands join s in ctx.UserSettings on r.UserId equals s.UserId where r.BandId == bandId && s.Value == "True" && s.Key.Equals(typeStr, StringComparison.OrdinalIgnoreCase) select s.User.Email).Distinct().AsEnumerable(); var replacements = BuildReplacements(data); var msg = TemplateBuilder.LoadTemplate(type, replacements); SendMessage(msg, emails); } }
public ActionResult SignUpPost(SignUpModel m) { // check city/state if (!m.City.IsNullOrWhiteSpace() && m.State.Name.IsNullOrWhiteSpace()) { ModelState.AddModelError("CityNoState", "If entering a city, please select your state."); } if (ModelState.IsValid) { using (var ctx = new OpenGroovesEntities()) { m.Email = m.Email.Trim(); bool hasBand = !m.BandName.IsNullOrWhiteSpace(); MembershipCreateStatus status; var username = EntityNameHelper.CreateUrl(m.Email); // check for email if (Membership.FindUsersByEmail(m.Email).Count > 0 || Membership.FindUsersByName(username).Count > 0) { ModelState.AddModelError("EmailExists", "This email address is already registered"); } // check for band name if (hasBand && ctx.Bands.Any(b => b.Name == m.BandName.Trim())) { ModelState.AddModelError("BandExists", String.Format("The band, \"{0}\", has already registered. But don't worry, you can leave this field blank and request to join this band later.", m.BandName)); ModelState.AddModelError("BandExistsHelp", "If this is in error, please contact [email protected]."); m.BandName = String.Empty; } // if we're here, we're good to go! if (ModelState.IsValid) { var user = Membership.CreateUser(username, "thisisafakepassword", m.Email, null, null, false, out status); // if membership create is good if (status == MembershipCreateStatus.Success) { var guid = (Guid)user.ProviderUserKey; var existingUser = ctx.Users.SingleOrDefault(u => u.Email == m.Email); existingUser.RealName = m.RealName; existingUser.Email = m.Email; existingUser.SetupRequired = true; var relation = new UsersBand { RelationId = Guid.NewGuid(), RelationTypeId = 1, Date = DateTime.Now }; if (!m.City.IsNullOrWhiteSpace() && !m.State.Name.IsNullOrWhiteSpace()) { existingUser.City = m.City; existingUser.State = m.State.Name; } var address = LocationHelper.CityStateOrZip(m.City, m.State.Name); var location = ObjectFactory.GetInstance <ILocationService>(); var loc = location.GetLocation(address); if (loc != null) { existingUser.Latitude = loc.Coordinate.Latitude; existingUser.Longitude = loc.Coordinate.Longitude; existingUser.State = loc.State; existingUser.City = loc.City; } if (hasBand) { var band = new Band { BandId = Guid.NewGuid(), Name = m.BandName, City = m.City, Searchable = EntityNameHelper.StripUselessWords(m.BandName), State = m.State.Name, UrlName = Core.Helpers.EntityNameHelper.CreateUrl(m.BandName), Date = DateTime.Now }; if (loc != null) { band.Latitude = loc.Coordinate.Latitude; band.Longitude = loc.Coordinate.Longitude; band.City = loc.City; band.State = loc.State; } relation.Band = band; existingUser.UsersBands.Add(relation); } ctx.SaveChanges(); NotifyNewSignup(m); return(View("ThankYou", m)); } else if (status == MembershipCreateStatus.DuplicateEmail) { ModelState.AddModelError("DupEmail", "This email address already exists."); } else { ModelState.AddModelError("OtherError", "Your account could not be created."); } } } } return(View(m)); }