public async Task InsertOrUpdateAsync(IApplicationSettings settings)
        {
            using (var context = new EFContext())
            {
                if (settings == null)
                    throw new ArgumentNullException(nameof(settings));

                var oldSettings = await context.ApplicationSettings.SingleOrDefaultAsync();

                if (oldSettings != null)
                {
                    oldSettings.VkUserId = settings.VkUserId;
                }
                else
                {
                    var newSettings = new ApplicationSettings
                    {
                        Id = Guid.NewGuid(),
                        VkUserId = settings.VkUserId
                    };

                    context.ApplicationSettings.Add(newSettings);
                }

                await context.SaveChangesAsync();
            }
        }
 public async Task<IApplicationSettings> GetAsync()
 {
     using (var context = new EFContext())
     {
         return await context.ApplicationSettings.SingleOrDefaultAsync();
     }
 }
Esempio n. 3
0
        public async Task InsertOrUpdateAsync(IUserInfo userInfo)
        {
            using (var context = new EFContext())
            {
                if (userInfo == null)
                    throw new ArgumentNullException(nameof(userInfo));

                var oldUserInfo = await context.UserInfos
                    .FirstOrDefaultAsync(x => x.VkUserId == userInfo.VkUserId);

                if (oldUserInfo != null)
                {
                    oldUserInfo.FirstName = userInfo.FirstName;
                    oldUserInfo.LastName = userInfo.LastName;
                    oldUserInfo.Photo100 = userInfo.Photo100;
                }
                else
                {
                    var newUserInfo = new UserInfo
                    {
                        Id = Guid.NewGuid(),
                        VkUserId = userInfo.VkUserId,
                        FirstName = userInfo.FirstName,
                        LastName = userInfo.LastName,
                        Photo100 = userInfo.Photo100
                    };

                    context.UserInfos.Add(newUserInfo);
                }

                await context.SaveChangesAsync();
            }
        }
Esempio n. 4
0
 public async Task<IUserInfo> GetByIdAsync(int userId)
 {
     using (var context = new EFContext())
     {
         return await context.UserInfos.FirstOrDefaultAsync(x => x.VkUserId == userId);
     }
 }
 public async Task<IVkSessionInfo> GetByUserIdAsync(int userId)
 {
     using (var context = new EFContext())
     {
         return await context.SessionInfo.SingleOrDefaultAsync(x => x.UserId == userId);
     }
 }
        public async Task InsertOrUpdateAsync(IVkSessionInfo sessionInfo)
        {
            using (var context = new EFContext())
            {
                if (sessionInfo == null)
                    throw new ArgumentNullException(nameof(sessionInfo));

                var oldSessionInfo = await context.SessionInfo.SingleOrDefaultAsync(x => x.UserId == sessionInfo.UserId);

                if (oldSessionInfo != null)
                {
                    oldSessionInfo.AccessToken = sessionInfo.AccessToken;
                }
                else
                {
                    var newSessionInfo = new VkSessionInfo
                    {
                        Id = Guid.NewGuid(),
                        AccessToken = sessionInfo.AccessToken,
                        UserId = sessionInfo.UserId
                    };

                    context.SessionInfo.Add(newSessionInfo);
                }

                await context.SaveChangesAsync();
            }
        }
Esempio n. 7
0
        public async Task InsertOrUpdateAsync(IAvatar avatar)
        {
            using (var context = new EFContext())
            {
                if (avatar == null)
                    throw new ArgumentNullException(nameof(avatar));

                var oldAvatar = await context.Avatars
                    .FirstOrDefaultAsync(x => x.VkUserId == avatar.VkUserId
                                              && x.VkOwnerId == avatar.VkOwnerId
                                              && x.Size == avatar.Size);

                if (oldAvatar != null)
                {
                    oldAvatar.Url = avatar.Url;
                    oldAvatar.Content = avatar.Content;
                }
                else
                {
                    var newAvatar = new Avatar
                    {
                        Id = Guid.NewGuid(),
                        VkUserId = avatar.VkUserId,
                        VkOwnerId = avatar.VkOwnerId,
                        Url = avatar.Url,
                        Content = avatar.Content,
                        Size = avatar.Size
                    };

                    context.Avatars.Add(newAvatar);
                }

                await context.SaveChangesAsync();
            }
        }
Esempio n. 8
0
        public async Task InsertOrUpdateAsync(int vkOwnerId, AvatarSize size, IEnumerable<IAvatar> avatars)
        {
            using (var context = new EFContext())
            {
                if (avatars == null)
                    throw new ArgumentNullException(nameof(avatars));

                var newAvatars = avatars
                    .Where(x => x.VkOwnerId == vkOwnerId
                                && x.Size == size)
                    .ToList();

                var duplicatedAvatar = newAvatars.FirstOrDefault(x => newAvatars
                    .Any(y => y.VkUserId == x.VkUserId
                              && x != y));

                if (duplicatedAvatar != null)
                    throw new EFClientException<DuplicatedVkIdExceptionArgs>(
                        new DuplicatedVkIdExceptionArgs(duplicatedAvatar.VkUserId),
                        "В списке avatars имеются записи, с дублирующимся VkUserId");

                var oldAvatars = await context.Avatars.Where(x => x.VkOwnerId == vkOwnerId
                                                                  && x.Size == size)
                    .ToListAsync();

                var oldAvatarIds = oldAvatars.Select(x => x.VkUserId).ToArray();

                var avatarsToCreate = (from avatarToCreate in newAvatars
                    where !oldAvatarIds.Contains(avatarToCreate.VkUserId)
                    select new Avatar
                    {
                        Id = Guid.NewGuid(),
                        VkOwnerId = vkOwnerId,
                        VkUserId = avatarToCreate.VkUserId,
                        Content = avatarToCreate.Content,
                        Url = avatarToCreate.Url
                    });

                var queryToUpdate = (from avatarToUpdate in oldAvatars
                    let newAvatar = newAvatars.FirstOrDefault(x => x.VkUserId == avatarToUpdate.VkUserId)
                    where newAvatar != null
                    select new {NewAvatar = newAvatar, AvatarToUpdate = avatarToUpdate});

                foreach (var item in queryToUpdate)
                {
                    var newAvatar = item.NewAvatar;
                    var oldAvatar = item.AvatarToUpdate;

                    oldAvatar.Url = newAvatar.Url;
                    oldAvatar.Content = newAvatar.Content;
                }

                context.Avatars.AddRange(avatarsToCreate);
                await context.SaveChangesAsync();
            }
        }
Esempio n. 9
0
 public async Task<IEnumerable<IFriend>> GetAllFriendsByUserId(int vkOwnerId)
 {
     using (var context = new EFContext())
     {
         return await context.Friends.Where(x => x.VkOwnerId == vkOwnerId)
             .ToListAsync();
     }
 }
Esempio n. 10
0
        public async Task InsertOrUpdateOrDeleteAsync(int vkOwnerId, IEnumerable<IFriend> friends)
        {
            using (var context = new EFContext())
            {
                if (friends == null)
                    throw new ArgumentNullException(nameof(friends));

                var newFriends = friends
                    .Where(x => x.VkOwnerId == vkOwnerId)
                    .ToList();

                var duplicatedFriend = newFriends.FirstOrDefault(x => newFriends.Any(y => y.VkId == x.VkId
                                                                                          && x != y));

                if (duplicatedFriend != null)
                    throw new EFClientException<DuplicatedVkIdExceptionArgs>(
                        new DuplicatedVkIdExceptionArgs(duplicatedFriend.VkId),
                        "В списке friends имеются записи, с дублирующимся VkId");

                var oldFriends = await context.Friends.Where(x => x.VkOwnerId == vkOwnerId)
                    .ToListAsync();

                var oldFriendVkIds = oldFriends.Select(x => x.VkId).ToArray();
                var newFriendVkIds = newFriends.Select(x => x.VkId).ToArray();

                var friendsToCreate = (from friendToCreate in newFriends
                    where !oldFriendVkIds.Contains(friendToCreate.VkId)
                    select new Friend
                    {
                        Id = Guid.NewGuid(),
                        VkOwnerId = vkOwnerId,
                        VkId = friendToCreate.VkId,
                        FirstName = friendToCreate.FirstName,
                        LastName = friendToCreate.LastName,
                        Gender = friendToCreate.Gender,
                        Online = friendToCreate.Online,
                        Photo50 = friendToCreate.Photo50,
                        Photo100 = friendToCreate.Photo100
                    });

                var queryToUpdate = (from friendToUpdate in oldFriends
                    let newFriend = newFriends.FirstOrDefault(x => x.VkId == friendToUpdate.VkId)
                    where newFriend != null
                    select new {NewFriend = newFriend, FriendToUpdate = friendToUpdate});

                foreach (var item in queryToUpdate)
                {
                    var newFriend = item.NewFriend;
                    var oldFriend = item.FriendToUpdate;

                    oldFriend.FirstName = newFriend.FirstName;
                    oldFriend.LastName = newFriend.LastName;
                    oldFriend.Online = newFriend.Online;
                    oldFriend.Photo50 = newFriend.Photo50;
                    oldFriend.Photo100 = newFriend.Photo100;
                    oldFriend.Gender = newFriend.Gender;
                }

                var friendsToDelete = (from friendToDelete in oldFriends
                    where !newFriendVkIds.Contains(friendToDelete.VkId)
                    select friendToDelete);

                context.Friends.AddRange(friendsToCreate);
                context.Friends.RemoveRange(friendsToDelete);

                await context.SaveChangesAsync();
            }
        }
Esempio n. 11
0
 public async Task<IAvatar> GetAvatarAsync(int vkUserId, int vkOwnerId, AvatarSize size)
 {
     using (var context = new EFContext())
     {
         return await context.Avatars.FirstOrDefaultAsync(x => x.VkUserId == vkUserId
                                                               && x.VkOwnerId == vkOwnerId
                                                               && x.Size == size);
     }
 }