Skip to content

Commit 270acc5

Browse files
Update Iban index and related queries
The uniqueness index for Iban in the AddressBookRecord model has been extended to include the IbanType. Previously, the uniqueness was only enforced on the Iban. Now it is on the combination of OwnerClientId, Iban, and IbanType. This change has also been reflected in related queries and services, and the database migration file has been created for this adjustment.
1 parent 5927d04 commit 270acc5

7 files changed

Lines changed: 162 additions & 27 deletions

File tree

src/Service.AddressBook.Domain/IAddressBookRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IAddressBookRepository
99
Task<AddressBookRecord> GetAsync(string contactId);
1010
Task<AddressBookRecord> GetByNicknameAsync(string ownerClientId, string nickname);
1111
Task<AddressBookRecord> GetByNameAsync(string ownerClientId, string name);
12-
Task<AddressBookRecord> GetByIbanAsync(string ownerClientId, string iban);
12+
Task<AddressBookRecord> GetByIbanAsync(string ownerClientId, string iban, IbanType type);
1313

1414
Task<List<AddressBookRecord>> GetListAsync(string ownerClientId, int skip, int take, bool withIban,
1515
bool withNickname, IbanType? requestIbanType = null);

src/Service.AddressBook.Postgres/DatabaseContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2929

3030
modelBuilder.Entity<AddressBookRecord>().HasIndex(e => new {e.OwnerClientId, e.Nickname}).IsUnique();
3131
modelBuilder.Entity<AddressBookRecord>().HasIndex(e => new {e.OwnerClientId, e.Name}).IsUnique();
32-
modelBuilder.Entity<AddressBookRecord>().HasIndex(e => new {e.OwnerClientId, e.Iban}).IsUnique();
32+
modelBuilder.Entity<AddressBookRecord>().HasIndex(e => new {e.OwnerClientId, e.Iban, e.IbanType}).IsUnique();
3333

3434
base.OnModelCreating(modelBuilder);
3535
}

src/Service.AddressBook.Postgres/Migrations/20231212154538_Index.Designer.cs

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace Service.AddressBook.Postgres.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class Index : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.DropIndex(
14+
name: "IX_addressbook_OwnerClientId_Iban",
15+
schema: "addressbook",
16+
table: "addressbook");
17+
18+
migrationBuilder.CreateIndex(
19+
name: "IX_addressbook_OwnerClientId_Iban_IbanType",
20+
schema: "addressbook",
21+
table: "addressbook",
22+
columns: new[] { "OwnerClientId", "Iban", "IbanType" },
23+
unique: true);
24+
}
25+
26+
/// <inheritdoc />
27+
protected override void Down(MigrationBuilder migrationBuilder)
28+
{
29+
migrationBuilder.DropIndex(
30+
name: "IX_addressbook_OwnerClientId_Iban_IbanType",
31+
schema: "addressbook",
32+
table: "addressbook");
33+
34+
migrationBuilder.CreateIndex(
35+
name: "IX_addressbook_OwnerClientId_Iban",
36+
schema: "addressbook",
37+
table: "addressbook",
38+
columns: new[] { "OwnerClientId", "Iban" },
39+
unique: true);
40+
}
41+
}
42+
}

src/Service.AddressBook.Postgres/Migrations/DatabaseContextModelSnapshot.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1818
#pragma warning disable 612, 618
1919
modelBuilder
2020
.HasDefaultSchema("addressbook")
21-
.HasAnnotation("ProductVersion", "7.0.13")
21+
.HasAnnotation("ProductVersion", "8.0.0")
2222
.HasAnnotation("Relational:MaxIdentifierLength", 63);
2323

2424
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
@@ -72,15 +72,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
7272

7373
b.HasKey("ContactId");
7474

75-
b.HasIndex("OwnerClientId", "Iban")
76-
.IsUnique();
77-
7875
b.HasIndex("OwnerClientId", "Name")
7976
.IsUnique();
8077

8178
b.HasIndex("OwnerClientId", "Nickname")
8279
.IsUnique();
8380

81+
b.HasIndex("OwnerClientId", "Iban", "IbanType")
82+
.IsUnique();
83+
8484
b.ToTable("addressbook", "addressbook");
8585
});
8686
#pragma warning restore 612, 618

src/Service.AddressBook/Services/AddressBookRepositoryMemoryCache.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ public async Task<AddressBookRecord> GetByNameAsync(string ownerClientId, string
4343
return _records.FirstOrDefault(t => t.OwnerClientId == ownerClientId && t.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
4444
}
4545

46-
public async Task<AddressBookRecord> GetByIbanAsync(string ownerClientId, string iban)
46+
public async Task<AddressBookRecord> GetByIbanAsync(string ownerClientId, string iban, IbanType type)
4747
{
48-
return _records.FirstOrDefault(t => t.OwnerClientId == ownerClientId && t.Iban == iban);
48+
return _records.FirstOrDefault(
49+
t => t.OwnerClientId == ownerClientId && t.Iban == iban && t.IbanType == type);
4950
}
5051

5152
public async Task<List<AddressBookRecord>> GetListAsync(string ownerClientId, int skip, int take, bool withIban,

src/Service.AddressBook/Services/AddressBookService.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,17 @@ public async Task<OperationResponse> CreateWithIbanAsync(AddIbanRequest request)
245245
_logger.LogInformation("Requested create for client {clientId} and iban {iban}", request.OwnerClientId, request.Iban);
246246
try
247247
{
248-
// var existingRecord =
249-
// await _addressBookRepository.GetByIbanAsync(request.OwnerClientId, request.Iban);
250-
// if (existingRecord != null)
251-
// return new OperationResponse()
252-
// {
253-
// IsSuccess = false,
254-
// ErrorMessage = "Record already exists",
255-
// ErrorCode = GlobalSendErrorCode.IbanAlreadyUsed
256-
// };
248+
var existingRecord =
249+
await _addressBookRepository.GetByIbanAsync(request.OwnerClientId, request.Iban, request.IbanType);
250+
if (existingRecord != null)
251+
return new OperationResponse()
252+
{
253+
IsSuccess = false,
254+
ErrorMessage = "Record already exists",
255+
ErrorCode = GlobalSendErrorCode.IbanAlreadyUsed
256+
};
257257

258-
var existingRecord = await _addressBookRepository.GetByNameAsync(request.OwnerClientId, request.Name);
258+
existingRecord = await _addressBookRepository.GetByNameAsync(request.OwnerClientId, request.Name);
259259
if (existingRecord != null)
260260
return new OperationResponse()
261261
{
@@ -388,15 +388,15 @@ public async Task<OperationResponse> UpdateContactAsync(UpdateContactRequest req
388388

389389
if(!string.IsNullOrWhiteSpace(request.Iban))
390390
{
391-
// var existing = await _addressBookRepository.GetByIbanAsync(request.OwnerClientId, request.Iban);
392-
// if (existing != null && existing.ContactId != request.ContactId)
393-
// {
394-
// return new OperationResponse()
395-
// {
396-
// IsSuccess = false,
397-
// ErrorCode = GlobalSendErrorCode.IbanAlreadyUsed
398-
// };
399-
// }
391+
var existing = await _addressBookRepository.GetByIbanAsync(request.OwnerClientId, request.Iban, record.IbanType);
392+
if (existing != null && existing.ContactId != request.ContactId)
393+
{
394+
return new OperationResponse()
395+
{
396+
IsSuccess = false,
397+
ErrorCode = GlobalSendErrorCode.IbanAlreadyUsed
398+
};
399+
}
400400

401401
request.Iban = request.Iban.ToUpper().Replace(" ","");
402402

0 commit comments

Comments
 (0)