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 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 <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); } }
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); } }
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(); } }
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 void TaskExecuted() { using (var context = new InstaContext()) { context.Tasks.First(t => t.Id == this.Id).Exectued++; await context.SaveChangesAsync(); } }
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(); } }
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 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<TEntity> UpdateAsync(TEntity entity) { if (entity == null) { throw new ArgumentNullException($"{nameof(UpdateAsync)} entity must not be null"); } try { _instaContext.Update(entity); await _instaContext.SaveChangesAsync(); return entity; } catch (Exception) { throw new Exception($"{nameof(entity)} could not be updated"); } }
private async void btnCreate_Click(object sender, RoutedEventArgs e) { LoadingState(); var createdSchedule = new ScheduleModel { Name = txtName.Text, StartDate = dateStartDate.SelectedDate.GetValueOrDefault(DateTime.Now), EndDate = dateEndDate.SelectedDate.GetValueOrDefault(DateTime.Now.AddMonths(1)), StartTime = new TimeModel { Hour = int.Parse(comboStartTime_Hour.Text), Minute = int.Parse(comboStartTime_Min.Text), IsMorning = comboStartTime_TOD.SelectedIndex == 0 }, EndTime = new TimeModel { Hour = int.Parse(comboEndTime_Hour.Text), Minute = int.Parse(comboEndTime_Min.Text), IsMorning = comboEndTime_TOD.SelectedIndex == 0 }, TaskLimit = int.Parse(txtDialyLimit.Text) }; createdSchedule.SetDays(GetDays()); using (var context = new InstaContext()) { var user = context.Users.Include("Schedules").FirstOrDefault(u => u.Id == User.Id); user.Schedules.Add(createdSchedule); await context.SaveChangesAsync(); MessageBox.Show("Schedule: " + createdSchedule.Name + " is created.", "Success", MessageBoxButton.OK, MessageBoxImage.Information); Reset(); } LoadedState(); }