public async Task SetProfileInfoAsync_AffectGetProfileAsync_When_PassingProfileInfo(long userId, ProfileEntriesCollection profileEntries) { await this.subject.SetProfileEntriesCollectionAsync(userId, profileEntries); Profile profile = await this.subject.GetProfileAsync(userId); Assert.AreEqual(profile.Login, this.validLogin); Assert.AreEqual(profile.ProfileEntriesCollection.Count(), profileEntries.Count()); foreach (var entry in profile.ProfileEntriesCollection) { Assert.AreEqual(profileEntries.Where(v => v.Equals(entry)).Count(), 1); } }
public async Task SetProfileInfoAsync_NotAffectGetProfileAsync_For_OtherUsers() { Account account1 = await this.accountRepository.CreateAccountAsync(this.validLogin, this.validPassword); Account account2 = await this.accountRepository.CreateAccountAsync(this.anotherValidLogin, this.anotherValidPassword); var profileEntries1 = new ProfileEntriesCollection { new ProfileEntry { Type = ProfileEntryType.FirstName, Value = "a", IsPublic = false }, new ProfileEntry { Type = ProfileEntryType.LastName, Value = "b", IsPublic = true }, new ProfileEntry { Type = ProfileEntryType.MiddleName, Value = "c", IsPublic = false }, }; var profileEntries2 = new ProfileEntriesCollection { new ProfileEntry { Type = ProfileEntryType.MiddleName, Value = "c", IsPublic = false }, new ProfileEntry { Type = ProfileEntryType.City, Value = "d", IsPublic = true }, new ProfileEntry { Type = ProfileEntryType.Description, Value = "e", IsPublic = false }, }; await this.subject.SetProfileEntriesCollectionAsync(account1.UserId, profileEntries1); await this.subject.SetProfileEntriesCollectionAsync(account2.UserId, profileEntries2); Profile profile1 = await this.subject.GetProfileAsync(account1.UserId); Assert.AreEqual(profile1.Login, this.validLogin); Assert.AreEqual(profile1.ProfileEntriesCollection.Count(), profileEntries1.Count()); foreach (var entry in profile1.ProfileEntriesCollection) { Assert.AreEqual(profileEntries1.Where(v => v.Equals(entry)).Count(), 1); } Profile profile2 = await this.subject.GetProfileAsync(account2.UserId); Assert.AreEqual(profile2.Login, this.anotherValidLogin); Assert.AreEqual(profile2.ProfileEntriesCollection.Count(), profileEntries1.Count()); foreach (var entry in profile2.ProfileEntriesCollection) { Assert.AreEqual(profileEntries2.Where(v => v.Equals(entry)).Count(), 1); } await this.subject.SetProfileEntriesCollectionAsync(account2.UserId, new ProfileEntriesCollection()); profile1 = await this.subject.GetProfileAsync(account1.UserId); Assert.AreEqual(profile1.Login, this.validLogin); Assert.AreEqual(profile1.ProfileEntriesCollection.Count(), profileEntries1.Count()); foreach (var entry in profile1.ProfileEntriesCollection) { Assert.AreEqual(profileEntries1.Where(v => v.Equals(entry)).Count(), 1); } profile2 = await this.subject.GetProfileAsync(account2.UserId); Assert.AreEqual(profile2.Login, this.anotherValidLogin); Assert.AreEqual(profile2.ProfileEntriesCollection.Count(), 0); }
public async Task <bool> SetProfileEntriesCollectionAsync(long userId, ProfileEntriesCollection profileEntriesCollection) { if (userId < 0) { throw new ArgumentOutOfRangeException("userId"); } if (profileEntriesCollection == null) { throw new ArgumentNullException("profileEntriesCollection"); } using (var connection = await this.connectionFactory.GetConnectionAsync()) using (var transaction = connection.BeginTransaction()) { try { string sqlQuery = $"DELETE FROM {ProfileInfoEntriesTableName} WHERE UserId=@UserId"; await connection.ExecuteAsync(sqlQuery, new { UserId = userId }, transaction : transaction); if (profileEntriesCollection.Count() == 0) { sqlQuery = $"SELECT UserId FROM {AccountsTableName} WHERE UserId=@UserId"; var foundUsers = await connection.QueryAsync <long>(sqlQuery, new { UserId = userId }, transaction : transaction); bool userExists = foundUsers.Count() == 1; if (userExists) { transaction.Commit(); } return(userExists); } var dbProfileInfoEntries = from entry in profileEntriesCollection where entry.Value != null && entry.Value.Length > 0 select new DbProfileEntry { UserId = userId, Type = ToDbProfileInfoTypesMapping[entry.Type], Value = entry.Value, IsPublic = entry.IsPublic }; sqlQuery = $"INSERT INTO {ProfileInfoEntriesTableName} (UserId, Value, IsPublic, TypeId) " + $"VALUES (@UserId, @Value, @IsPublic, (SELECT Id FROM {ProfileInfoTypesTableName} WHERE Type=@Type))"; int inserted = await connection.ExecuteAsync(sqlQuery, dbProfileInfoEntries, transaction : transaction); bool completed = inserted == profileEntriesCollection.Count(); if (completed) { transaction.Commit(); } return(completed); } catch (SqlException ex) when(ex.Number == 547) { return(false); } } }