public SocialType(string type) { Socials = new List <Social>(); Type = type; SocialTypes.Add(this); }
private bool TrySaveSocialType(int profileId, SocialTypes socialType) { bool success = false; try { using (UserProfileDatabaseContext dbContext = new UserProfileDatabaseContext()) { // Get the SocialManager handle first UserProfile currentUser = dbContext.UserProfiles.FirstOrDefault(u => u.UserName == User.Identity.Name); SocialManager socialRecord = dbContext.SocialManager.Where(x => x.SocialType == socialType && x.User.Id == currentUser.Id).FirstOrDefault(); DateTime currentTime = DateTime.UtcNow; int days = socialRecord != null ? ((TimeSpan)currentTime.Subtract((DateTime)socialRecord.TimeStamp)).Days : 0; int? socialId; if (days <= ThresholdDays && socialRecord != null) { if (socialRecord.Count < ThresholdSocialTypeCount) { success = socialType == SocialTypes.Request ? TrySaveRequest(profileId, currentUser, dbContext, out socialId) : TrySaveMessage(profileId, currentUser, out socialId); if (!success) { return(success); } socialRecord.Count++; socialRecord.Update(dbContext); } else { // No. of counts exceeded for the given period. success = false; } } else { success = socialType == SocialTypes.Request ? TrySaveRequest(profileId, currentUser, dbContext, out socialId) : TrySaveMessage(profileId, currentUser, out socialId); if (!success) { return(success); } if (socialRecord != null) { socialRecord.TimeStamp = DateTime.UtcNow; socialRecord.SocialId = socialId.GetValueOrDefault(); socialRecord.Count = 1; socialRecord.Update(dbContext); } else { SocialManager newSocialRecord = new SocialManager { User = currentUser, SocialId = socialId.GetValueOrDefault(), SocialType = socialType, TimeStamp = DateTime.UtcNow, Count = 1, }; dbContext.SocialManager.Add(newSocialRecord); } } dbContext.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.Write("Operation failed with following ex : {0}", ex.ToString()); return(false); } return(success); }