public async Task CreateUser(Guid userId, string userName, string email, string imageCloudUrl) { Guard.ArgumentNotNullOrEmpty(userName, nameof(userName)); Guard.ArgumentNotNullOrEmpty(email, nameof(email)); using (var context = new InstaContext()) { await context.UserRecords.AddAsync( new UserRecord { UserId = userId, Email = email, CreatedAt = DateTimeOffset.Now, EncryptedPassword = string.Empty, IsEmailConfirmed = true, Profile = new ProfileRecord { UserName = userName, IsVisible = true, ImageCloudUrl = imageCloudUrl } }); await context.SaveChangesAsync(); } }
public async Task <int> AddPost(PostEntity post) { Guard.ArgumentNotNull(post, nameof(post)); using (var context = new InstaContext()) { var postRecord = new PostRecord { Description = new DescriptionRecord { Text = post.Description }, Geolocation = post.Location, UserId = post.UserId, CreatedAt = DateTimeOffset.Now, }; await context.PostRecords.AddAsync(postRecord); await context.SaveChangesAsync(); var entityRecord = new EntityRecord { ExternalEntityId = postRecord.PostId, EntityTypeId = (int)EntityType.Post }; await context.EntityRecords.AddAsync(entityRecord); await context.SaveChangesAsync(); return(postRecord.PostId); } }
public async Task <int> AddLike(Guid userId, int postId) { await LikeSemaphore.WaitAsync(); try { using (var context = new InstaContext()) { var like = context.LikeRecords.FirstOrDefault(x => x.UserId == userId && x.EntityId == postId); if (like != null) { context.LikeRecords.Remove(like); } else { context.LikeRecords.Add( new LikeRecord { EntityId = postId, UserId = userId, CreatedAt = DateTimeOffset.Now }); } await context.SaveChangesAsync(); return(await context.LikeRecords.CountAsync(x => x.EntityId == postId)); } } finally { LikeSemaphore.Release(1); } }
public ScheduleItemView(ScheduleModel item) { InitializeComponent(); using (var context = new InstaContext()) { item = context.Schedules.Include("Tasks").Include("User").First(s => item.Id == s.Id); } lblName.Content = item.Name; lblStartDate.Content = item.StartDate.ToString("YYYY-mm-dd"); lblEndDate.Content = item.EndDate.ToString("YYYY-mm-dd"); lblDays.Content = ""; item.GetDays().ForEach(d => { lblDays.Content += d.ToString() + ", "; }); lblRunningTasks.Content = item.Tasks.Where(t => t.Exectued < t.Repeat).Count(); lblCompletedTasks.Content = item.Tasks.Where(t => t.Exectued >= t.Repeat).Count(); lblStatues.Content = item.Active ? "Active" : "Sleeping"; }
private async void btnCreate_Click(object sender, RoutedEventArgs e) { try { var createdSearch = new SearchModel { Name = txtName.Text, InStrings = JsonConvert.SerializeObject(this.InTexts), ExStrings = JsonConvert.SerializeObject(this.ExTexts), InPosts = checkInPosts.IsChecked ?? false, InHashtags = checkInHashtags.IsChecked ?? false, InUsers = checkInUsers.IsChecked ?? false, InFollowingMe = checkInFollowingMe.IsChecked ?? false, StartDate = dateStart.SelectedDate ?? DateTime.Now, EndDate = dateStart.SelectedDate ?? DateTime.Now }; createdSearch.SetInLocations(InLocations); createdSearch.SetExLocations(ExLocations); using (var context = new InstaContext()) { var user = context.Users.Include("Searches").FirstOrDefault(u => u.Id == this.User.Id); user.Searches.Add(createdSearch); await context.SaveChangesAsync(); MessageBox.Show("Search: " + createdSearch.Name + " created Successfully."); Reset(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void Reset() { using (var context = new InstaContext()) { this.User = context.Users.Include("Searches").Include("Schedules").Include("Tasks").FirstOrDefault(u => u.Id == this.User.Id); } pnlSearches.Children.Clear(); this.User.Searches.ForEach(s => pnlSearches.Children.Add(new SearchItemView(s))); txtName.Text = ""; txtSearchText.Text = ""; txtSearchLocation.Text = ""; InLocations = new List <Location>(); lstLocationInclude.ItemsSource = null; lstLocationInclude.ItemsSource = InLocations; ExLocations = new List <Location>(); lstLocationExclude.ItemsSource = null; lstLocationExclude.ItemsSource = ExLocations; InTexts = new List <string>(); lstTextInclude.ItemsSource = null; lstTextInclude.ItemsSource = InTexts; ExTexts = new List <string>(); lstTextExclude.ItemsSource = null; lstTextExclude.ItemsSource = ExTexts; checkInUsers.IsChecked = false; checkInHashtags.IsChecked = false; checkInPosts.IsChecked = false; checkInFollowingMe.IsChecked = false; }
public async Task <Container <PostEntity> > GetPosts(Guid userId, int page, int take) { using (var context = new InstaContext()) { var totalPostsCount = await context.PostRecords .AsNoTracking() .CountAsync(x => x.UserId == userId); var posts = await context.PostRecords .AsNoTracking() .Where(x => x.UserId == userId) .OrderByDescending(x => x.CreatedAt) .Skip(page * take) .Take(take) .Include(x => x.Tags) .ThenInclude(x => x.UniqueTag) .Include(x => x.Description) .Include(x => x.User) .ThenInclude(x => x.Profile) .ToArrayAsync(); var postEntities = new List <PostEntity>(); foreach (var post in posts) { postEntities.Add(await this.ToPostEntity(post)); } return(new Container <PostEntity> { Entities = postEntities.ToArray(), Page = page, TotalEntities = totalPostsCount }); } }
public async Task UpdateUser(UserEntity userEditModel) { Guard.ArgumentNotNull(userEditModel, nameof(userEditModel)); using (var context = new InstaContext()) { var user = await context.UserRecords .Include(x => x.Profile) .FirstAsync(n => n.UserId == userEditModel.Id); user.Profile.Age = userEditModel.Age; user.Profile.Description = userEditModel.Description; user.Profile.FirstName = userEditModel.FirstName; user.Profile.LastName = userEditModel.LastName; user.Profile.Gender = (int)userEditModel.Gender; user.Profile.IsVisible = userEditModel.IsVisible; if (!string.IsNullOrEmpty(userEditModel.ProfileImageCloudUrl)) { user.Profile.ImageCloudUrl = userEditModel.ProfileImageCloudUrl; } await context.SaveChangesAsync(); } }
private void btnCreate_Click(object sender, RoutedEventArgs e) { btnCreate.IsEnabled = false; btnReset.IsEnabled = false; try { using (var context = new InstaContext()) { var task = new TaskModel { Name = txtName.Text, TaskType = (TaskType)comboTaskType.SelectedItem, Searches = SelectedSearches, Schedule = context.Schedules.FirstOrDefault(sc => sc.Id == (int)comboSchedule.SelectedValue), Repeat = int.Parse(txtRepeatCount.Text) }; task.SetValues(this.Values); var user = context.Users.Include("Tasks").FirstOrDefault(u => u.Id == this.User.Id); user.Tasks.Add(task); context.SaveChanges(); MessageBox.Show("Task: " + task.Name + " created successfully."); } Reset(); } catch (Exception ex) { MessageBox.Show(ex.Message); } btnCreate.IsEnabled = true; btnReset.IsEnabled = true; }
private void Refresh() { using (var context = new InstaContext()) { this.Exectued = context.Tasks.First(t => t.Id == this.Id).Exectued; } }
public async void TaskExecuted() { using (var context = new InstaContext()) { context.Tasks.First(t => t.Id == this.Id).Exectued++; await context.SaveChangesAsync(); } }
public async Task <bool> IsFollowing(Guid userId, Guid otherUserId) { using (var context = new InstaContext()) { return(await context.SubscriptionRecords.AnyAsync(x => x.SubscriberUserId == userId && x.SubscribedToUserId == otherUserId)); } }
public async Task <Container <PostEntity> > FindPostsByTags(Guid userId, string tag, int page, int take) { Guard.ArgumentNotNullOrEmpty(tag, nameof(tag)); using (var context = new InstaContext()) { var allTags = context.TagRecords .AsNoTracking() .Where(x => x.UniqueTag.Text.Contains(tag) && x.Post.UserId != userId); var totalPostsCount = await allTags.CountAsync(); var postIds = await allTags.OrderByDescending(x => x.Post.CreatedAt) .Skip(page * take) .Take(take) .Select(x => x.PostId) .ToArrayAsync(); var posts = await context.PostRecords.AsNoTracking() .Include(x => x.Tags) .ThenInclude(x => x.UniqueTag) .Include(x => x.Description) .Include(x => x.User) .ThenInclude(x => x.Profile) .Include(x => x.Comments) .ThenInclude(x => x.User) .ThenInclude(x => x.Profile) .Where(x => postIds.Contains(x.PostId)) .ToArrayAsync(); var postEntities = new List <PostEntity>(); foreach (var post in posts) { var entity = await context.EntityRecords .Include(x => x.Photos) .Include(x => x.Likes) .FirstAsync(x => x.ExternalEntityId == post.PostId); var photos = entity.Photos.Select(x => x.ToEntity()).ToArray(); var postEntity = post.ToEntity(); postEntity.Photos = photos; postEntity.Likes = entity.Likes.Count; postEntities.Add(postEntity); } return(new Container <PostEntity> { Entities = postEntities.ToArray(), Page = page, TotalEntities = totalPostsCount }); } }
public async Task <UserEntity> GetUser(Guid userId) { using (var context = new InstaContext()) { var user = await context.UserRecords.AsNoTracking() .Include(x => x.Profile) .FirstOrDefaultAsync(n => n.UserId == userId); return(user.ToEntity()); } }
public async Task AddPhotos(PhotoEntity[] photos) { Guard.ArgumentNotNull(photos, nameof(photos)); using (var context = new InstaContext()) { await context.PhotoRecords.AddRangeAsync(photos.Select(x => x.ToRecord())); await context.SaveChangesAsync(); } }
private void BtnStop_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show("Are you sure you want to stop this task premenantly?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) { using (var context = new InstaContext()) { context.Tasks.FirstOrDefault(t => t.Id == this.Item.Id).Exectued = this.Item.Repeat + 1; context.SaveChanges(); } } }
protected virtual void Dispose(bool disposing) { if (disposing) { if (this.db != null) { this.db.Dispose(); this.db = null; } } }
private async void Window_Loaded(object sender, RoutedEventArgs e) { this.Title = "Home - Feed"; DisableButtons(); var _user = await Api.GetCurrentUserAsync(); if (_user.Succeeded) { this.CurrentUser = _user.Value; using (var context = new InstaContext()) { if (!context.Users.Any(u => u.PK == this.CurrentUser.Pk)) { var user = new UserModel { PK = this.CurrentUser.Pk, LastLogin = DateTime.Now }; context.Users.Add(user); context.SaveChanges(); } this.UserModel = context.Users.Include("Schedules").Include("Searches").Include("Tasks").First(u => this.CurrentUser.Pk == u.PK); this.UserModel.LastLogin = DateTime.Now; context.SaveChanges(); } try { TaskWorker.StartBackgroundWorker(this.UserModel.Id, this.Api); } catch (Exception ex) { MessageBox.Show("TaskWorker.StartBackgroundWorker" + Environment.NewLine + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } EnableButtons(); profileImage.Fill = new ImageBrush(new BitmapImage(new Uri(this.CurrentUser.ProfilePicture))); lblUsername.Content = this.CurrentUser.UserName; progressBar.Visibility = Visibility.Hidden; } else { MessageBox.Show(_user.Info.Message); this.Close(); } pnlMainContent.Children.Clear(); pnlMainContent.Children.Add(new FeedView(this.Api)); }
public async Task AddSubscription(Guid userId, Guid subscribedToUserId) { using (var context = new InstaContext()) { var subscriber = new SubscriptionRecord { SubscribedToUserId = subscribedToUserId, SubscriberUserId = userId }; context.SubscriptionRecords.Add(subscriber); await context.SaveChangesAsync(); } }
public async Task DeleteSubscription(Guid userId, Guid subscribedToUserId) { using (var context = new InstaContext()) { var subscriber = await context.SubscriptionRecords.FirstOrDefaultAsync( n => n.SubscribedToUserId == subscribedToUserId && n.SubscriberUserId == userId); if (subscriber != null) { context.SubscriptionRecords.Remove(subscriber); await context.SaveChangesAsync(); } } }
public bool FindUniqueTags(string tag) { using (var context = new InstaContext()) { var uniqueTag = context.UniqueTagRecords.FirstOrDefault(u => u.Text == tag); if (uniqueTag != null) { return(true); } else { return(false); } } }
public async Task AddTags(int postId, string[] tags) { Guard.ArgumentNotNull(tags, nameof(tags)); using (var context = new InstaContext()) { var post = await context.PostRecords .Include(x => x.Tags) .FirstOrDefaultAsync(n => n.PostId == postId); if (post != null) { var existingTags = await context.UniqueTagRecords .Where(x => tags.Contains(x.Text)) .ToArrayAsync(); post.Tags.AddRange( existingTags .Where(x => !post.Tags.Select(t => t.UniqueTagId).Contains(x.UniqueTagId)) .Select( x => new TagRecord { UniqueTagId = x.UniqueTagId })); var newTags = tags .Where(x => !existingTags.Select(t => t.Text).Contains(x, StringComparer.OrdinalIgnoreCase)) .Select( x => new UniqueTagRecord { Text = x }) .ToArray(); await context.UniqueTagRecords.AddRangeAsync(newTags); await context.SaveChangesAsync(); post.Tags.AddRange(newTags.Select(x => new TagRecord { UniqueTagId = x.UniqueTagId })); await context.SaveChangesAsync(); } } }
public async Task <UserEntity[]> GetFollowers(Guid userId) { using (var context = new InstaContext()) { var following = await context.SubscriptionRecords.AsNoTracking() .Where(x => x.SubscribedToUserId == userId) .Include(x => x.SubscriberUser) .ThenInclude(x => x.Profile) .ToArrayAsync(); var users = following.Select(x => x.SubscriberUser.ToEntity()).ToArray(); users.ForEach(x => x.CanSubscribe = false); return(users); } }
public async Task Delete(int postId, Guid userId) { using (var context = new InstaContext()) { var post = await context.PostRecords.FirstOrDefaultAsync(x => x.UserId == userId && x.PostId == postId); if (post == null) { return; } context.PostRecords.Remove(post); await context.SaveChangesAsync(); } }
public async Task AddComment(CommentEntity comment) { Guard.ArgumentNotNull(comment, nameof(comment)); using (var context = new InstaContext()) { context.CommentRecords.Add(new CommentRecord { PostId = comment.PostId, UserId = comment.UserId, Text = comment.Text, CreatedAt = DateTimeOffset.Now }); await context.SaveChangesAsync(); } }
public async Task <SocialStats> GetSocialStats(Guid userId) { using (var context = new InstaContext()) { var following = await context.SubscriptionRecords.AsNoTracking() .CountAsync(x => x.SubscriberUserId == userId); var followers = await context.SubscriptionRecords.AsNoTracking() .CountAsync(x => x.SubscribedToUserId == userId); return(new SocialStats { Followers = followers, Following = following }); } }
private void Reset() { txtName.Text = ""; dateStartDate.SelectedDate = DateTime.Now; dateEndDate.SelectedDate = DateTime.Now.AddMonths(1); comboStartTime_Hour.SelectedIndex = 8; comboStartTime_Min.SelectedIndex = 0; comboStartTime_TOD.SelectedIndex = 0; comboEndTime_Hour.SelectedIndex = 4; comboEndTime_Min.SelectedIndex = 0; comboEndTime_TOD.SelectedIndex = 1; checkEveryDay.IsEnabled = true; checkSunday.IsEnabled = false; checkMonday.IsEnabled = false; checkTuseday.IsEnabled = false; checkWednesday.IsEnabled = false; checkThursday.IsEnabled = false; checkFriday.IsEnabled = false; checkSaturday.IsEnabled = false; checkEveryDay.IsChecked = true; checkSunday.IsChecked = false; checkMonday.IsChecked = false; checkTuseday.IsChecked = false; checkWednesday.IsChecked = false; checkThursday.IsChecked = false; checkFriday.IsChecked = false; checkSaturday.IsChecked = false; txtDialyLimit.Text = 100.ToString(); using (var context = new InstaContext()) { this.User = context.Users.Include("Schedules").Include("Searches").Include("Tasks").FirstOrDefault(u => u.Id == this.User.Id); } pnlSchedules.Children.Clear(); this.User.Schedules.ForEach(s => pnlSchedules.Children.Add(new ScheduleItemView(s))); }
private async Task <PostEntity> ToPostEntity(PostRecord post) { using (var context = new InstaContext()) { var entity = await context.EntityRecords .AsNoTracking() .Include(x => x.Photos) .Include(x => x.Likes) .FirstAsync(x => x.ExternalEntityId == post.PostId); var photos = entity.Photos .Select(x => x.ToEntity()) .ToArray(); var postEntity = post.ToEntity(); postEntity.Photos = photos; postEntity.Likes = entity.Likes.Count; return(postEntity); } }
public async Task <UserEntity[]> FindUsers(Guid userId, string text, int page, int take) { Guard.ArgumentNotNull(text, nameof(text)); using (var context = new InstaContext()) { var users = await context.UserRecords .AsNoTracking() .Include(x => x.Profile) .Where( n => n.UserId != userId && n.Profile.IsVisible && (n.Profile.UserName.Contains(text) || n.Profile.FirstName.Contains(text) || n.Profile.LastName.Contains(text))) .OrderBy(x => x.Profile.UserName) .Skip(page * take) .Take(take) .ToArrayAsync(); var subscriptions = await context.SubscriptionRecords .AsNoTracking() .Where(x => x.SubscriberUserId == userId) .Select(x => x.SubscribedToUserId) .ToArrayAsync(); var userEntities = users.Select(x => x.ToEntity()).ToArray(); foreach (var user in userEntities) { var userEntity = subscriptions.FirstOrDefault(x => x == user.Id); if (userEntity != Guid.Empty) { user.CanSubscribe = false; } } return(userEntities); } }
public async Task <PostEntity> GetPost(int postId) { using (var context = new InstaContext()) { var post = await context.PostRecords.AsNoTracking() .Include(x => x.Tags) .ThenInclude(x => x.UniqueTag) .Include(x => x.Description) .Include(x => x.User) .ThenInclude(x => x.Profile) .Include(x => x.Comments) .ThenInclude(x => x.User) .ThenInclude(x => x.Profile) .FirstOrDefaultAsync(x => x.PostId == postId); var entity = await context.EntityRecords .Include(x => x.Photos) .Include(x => x.Likes) .FirstAsync(x => x.ExternalEntityId == post.PostId); var photos = entity.Photos.Select(x => x.ToEntity()).ToArray(); var postEntity = post.ToEntity(); var comments = post.Comments.Select( x => new CommentEntity { CreatedAt = x.CreatedAt, Text = x.Text, UserName = x.User.Profile.UserName, PostId = x.PostId, UserProfileCloudUrl = x.User.Profile.ImageCloudUrl }).ToArray(); postEntity.Photos = photos; postEntity.Likes = entity.Likes.Count; postEntity.Comments = comments; return(postEntity); } }