Skip to content

Commit

Permalink
Fixed TgStorage tables for EF Core
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianMorozov committed Nov 5, 2024
1 parent 4df3ed1 commit fbda5e8
Show file tree
Hide file tree
Showing 16 changed files with 818 additions and 290 deletions.
229 changes: 145 additions & 84 deletions Core/TgStorage/Common/TgEfRepositoryBase.cs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Core/TgStorage/Contracts/ITgEfRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ namespace TgStorage.Contracts;

#endregion

#region Public and private methods

Task<IDbContextTransaction> BeginTransactionAsync();
Task CommitTransactionAsync();
Task RollbackTransactionAsync();

#endregion

#region Public and private methods - Read

public TgEfStorageResult<TEntity> Get(TEntity item, bool isNoTracking);
Expand Down
24 changes: 12 additions & 12 deletions Core/TgStorage/Domain/Apps/TgEfAppRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override TgEfStorageResult<TgEfAppEntity> Get(TgEfAppEntity item, bool is
//.DefaultIfEmpty()
.SingleOrDefault();
return itemFind is not null
? new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.IsExists, itemFind)
? new(TgEnumEntityState.IsExists, itemFind)
: new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.NotExists, item);
}

Expand All @@ -49,7 +49,7 @@ public override async Task<TgEfStorageResult<TgEfAppEntity>> GetAsync(TgEfAppEnt
.SingleOrDefaultAsync()
.ConfigureAwait(false);
return itemFind is not null
? new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.IsExists, itemFind)
? new(TgEnumEntityState.IsExists, itemFind)
: new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.NotExists, item);
}

Expand All @@ -65,7 +65,7 @@ public override TgEfStorageResult<TgEfAppEntity> GetFirst(bool isNoTracking)
//.DefaultIfEmpty()
.FirstOrDefault();
return item is null
? new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.NotExists)
? new(TgEnumEntityState.NotExists)
: new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.IsExists, item);
}

Expand All @@ -81,7 +81,7 @@ public override async Task<TgEfStorageResult<TgEfAppEntity>> GetFirstAsync(bool
//.DefaultIfEmpty()
.FirstOrDefaultAsync().ConfigureAwait(false);
return item is null
? new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.NotExists)
? new(TgEnumEntityState.NotExists)
: new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.IsExists, item);
}

Expand All @@ -108,7 +108,7 @@ public override TgEfStorageResult<TgEfAppEntity> GetList(int take, int skip, boo
.Include(x => x.Proxy)
.ToList();
}
return new TgEfStorageResult<TgEfAppEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override async Task<TgEfStorageResult<TgEfAppEntity>> GetListAsync(int take, int skip, bool isNoTracking)
Expand All @@ -135,7 +135,7 @@ public override async Task<TgEfStorageResult<TgEfAppEntity>> GetListAsync(int ta
.Include(x => x.Proxy)
.ToList();
}
return new TgEfStorageResult<TgEfAppEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override TgEfStorageResult<TgEfAppEntity> GetList(int take, int skip, Expression<Func<TgEfAppEntity, bool>> where, bool isNoTracking)
Expand Down Expand Up @@ -165,7 +165,7 @@ public override TgEfStorageResult<TgEfAppEntity> GetList(int take, int skip, Exp
.Include(x => x.Proxy)
.ToList();
}
return new TgEfStorageResult<TgEfAppEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override async Task<TgEfStorageResult<TgEfAppEntity>> GetListAsync(int take, int skip, Expression<Func<TgEfAppEntity, bool>> where, bool isNoTracking)
Expand Down Expand Up @@ -196,7 +196,7 @@ public override async Task<TgEfStorageResult<TgEfAppEntity>> GetListAsync(int ta
.Include(x => x.Proxy)
.ToList();
}
return new TgEfStorageResult<TgEfAppEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override int GetCount() => EfContext.Apps.AsNoTracking().Count();
Expand Down Expand Up @@ -229,7 +229,7 @@ public override TgEfStorageResult<TgEfAppEntity> DeleteAll()
Delete(item, isSkipFind: true);
}
}
return new TgEfStorageResult<TgEfAppEntity>(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
return new(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
}

public override async Task<TgEfStorageResult<TgEfAppEntity>> DeleteAllAsync()
Expand All @@ -242,7 +242,7 @@ public override async Task<TgEfStorageResult<TgEfAppEntity>> DeleteAllAsync()
await DeleteAsync(item, isSkipFind: true).ConfigureAwait(false);
}
}
return new TgEfStorageResult<TgEfAppEntity>(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
return new(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
}

#endregion
Expand All @@ -258,7 +258,7 @@ public TgEfStorageResult<TgEfAppEntity> GetCurrentApp()
//.DefaultIfEmpty()
.FirstOrDefault();
return item is not null
? new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.IsExists, item)
? new(TgEnumEntityState.IsExists, item)
: new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.NotExists, new TgEfAppEntity());
}

Expand All @@ -271,7 +271,7 @@ public async Task<TgEfStorageResult<TgEfAppEntity>> GetCurrentAppAsync()
//.DefaultIfEmpty()
.FirstOrDefaultAsync();
return item is not null
? new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.IsExists, item)
? new(TgEnumEntityState.IsExists, item)
: new TgEfStorageResult<TgEfAppEntity>(TgEnumEntityState.NotExists, new TgEfAppEntity());
}

Expand Down
2 changes: 1 addition & 1 deletion Core/TgStorage/Domain/Documents/TgEfDocumentEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public sealed class TgEfDocumentEntity : ITgDbEntity, ITgDbFillEntity<TgEfDocume
[DefaultValue("")]
[ConcurrencyCheck]
[MaxLength(256)]
[Column(TgEfConstants.ColumnFileName, TypeName = "NVARCHAR(100)")]
[Column(TgEfConstants.ColumnFileName, TypeName = "NVARCHAR(256)")]
public string FileName { get; set; } = default!;

[DefaultValue(0)]
Expand Down
20 changes: 10 additions & 10 deletions Core/TgStorage/Domain/Documents/TgEfDocumentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override TgEfStorageResult<TgEfDocumentEntity> Get(TgEfDocumentEntity ite
//.DefaultIfEmpty()
.SingleOrDefault();
return itemFind is not null
? new TgEfStorageResult<TgEfDocumentEntity>(TgEnumEntityState.IsExists, itemFind)
? new(TgEnumEntityState.IsExists, itemFind)
: new TgEfStorageResult<TgEfDocumentEntity>(TgEnumEntityState.NotExists, item);
}

Expand All @@ -51,7 +51,7 @@ public override async Task<TgEfStorageResult<TgEfDocumentEntity>> GetAsync(TgEfD
.SingleOrDefaultAsync()
.ConfigureAwait(false);
return itemFind is not null
? new TgEfStorageResult<TgEfDocumentEntity>(TgEnumEntityState.IsExists, itemFind)
? new(TgEnumEntityState.IsExists, itemFind)
: new TgEfStorageResult<TgEfDocumentEntity>(TgEnumEntityState.NotExists, item);
}

Expand All @@ -67,7 +67,7 @@ public override TgEfStorageResult<TgEfDocumentEntity> GetFirst(bool isNoTracking
//.DefaultIfEmpty()
.FirstOrDefault();
return item is null
? new TgEfStorageResult<TgEfDocumentEntity>(TgEnumEntityState.NotExists)
? new(TgEnumEntityState.NotExists)
: new TgEfStorageResult<TgEfDocumentEntity>(TgEnumEntityState.IsExists, item);
}

Expand All @@ -83,7 +83,7 @@ public override async Task<TgEfStorageResult<TgEfDocumentEntity>> GetFirstAsync(
//.DefaultIfEmpty()
.FirstOrDefaultAsync().ConfigureAwait(false);
return item is null
? new TgEfStorageResult<TgEfDocumentEntity>(TgEnumEntityState.NotExists)
? new(TgEnumEntityState.NotExists)
: new TgEfStorageResult<TgEfDocumentEntity>(TgEnumEntityState.IsExists, item);
}

Expand All @@ -110,7 +110,7 @@ public override TgEfStorageResult<TgEfDocumentEntity> GetList(int take, int skip
.Include(x => x.Source)
.ToList();
}
return new TgEfStorageResult<TgEfDocumentEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override async Task<TgEfStorageResult<TgEfDocumentEntity>> GetListAsync(int take, int skip, bool isNoTracking)
Expand All @@ -137,7 +137,7 @@ public override async Task<TgEfStorageResult<TgEfDocumentEntity>> GetListAsync(i
.Include(x => x.Source)
.ToList();
}
return new TgEfStorageResult<TgEfDocumentEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override TgEfStorageResult<TgEfDocumentEntity> GetList(int take, int skip, Expression<Func<TgEfDocumentEntity, bool>> where, bool isNoTracking)
Expand Down Expand Up @@ -167,7 +167,7 @@ public override TgEfStorageResult<TgEfDocumentEntity> GetList(int take, int skip
.Include(x => x.Source)
.ToList();
}
return new TgEfStorageResult<TgEfDocumentEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override async Task<TgEfStorageResult<TgEfDocumentEntity>> GetListAsync(int take, int skip, Expression<Func<TgEfDocumentEntity, bool>> where, bool isNoTracking)
Expand Down Expand Up @@ -198,7 +198,7 @@ public override async Task<TgEfStorageResult<TgEfDocumentEntity>> GetListAsync(i
.Include(x => x.Source)
.ToList();
}
return new TgEfStorageResult<TgEfDocumentEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override int GetCount() => EfContext.Documents.AsNoTracking().Count();
Expand Down Expand Up @@ -231,7 +231,7 @@ public override TgEfStorageResult<TgEfDocumentEntity> DeleteAll()
Delete(item, isSkipFind: true);
}
}
return new TgEfStorageResult<TgEfDocumentEntity>(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
return new(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
}

public override async Task<TgEfStorageResult<TgEfDocumentEntity>> DeleteAllAsync()
Expand All @@ -244,7 +244,7 @@ public override async Task<TgEfStorageResult<TgEfDocumentEntity>> DeleteAllAsync
await DeleteAsync(item, isSkipFind: true).ConfigureAwait(false);
}
}
return new TgEfStorageResult<TgEfDocumentEntity>(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
return new(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion Core/TgStorage/Domain/Messages/TgEfMessageEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public sealed class TgEfMessageEntity : ITgDbEntity, ITgDbFillEntity<TgEfMessage

[DefaultValue(0)]
[ConcurrencyCheck]
[Column(TgEfConstants.ColumnSourceId, TypeName = "INT(20)")]
[Column(TgEfConstants.ColumnSourceId, TypeName = "LONG(20)")]
public long? SourceId { get; set; }

public TgEfSourceEntity? Source { get; set; }
Expand Down
20 changes: 10 additions & 10 deletions Core/TgStorage/Domain/Messages/TgEfMessageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override TgEfStorageResult<TgEfMessageEntity> Get(TgEfMessageEntity item,
//.DefaultIfEmpty()
.SingleOrDefault();
return itemFind is not null
? new TgEfStorageResult<TgEfMessageEntity>(TgEnumEntityState.IsExists, itemFind)
? new(TgEnumEntityState.IsExists, itemFind)
: new TgEfStorageResult<TgEfMessageEntity>(TgEnumEntityState.NotExists, item);
}

Expand All @@ -49,7 +49,7 @@ public override async Task<TgEfStorageResult<TgEfMessageEntity>> GetAsync(TgEfMe
.SingleOrDefaultAsync()
.ConfigureAwait(false);
return itemFind is not null
? new TgEfStorageResult<TgEfMessageEntity>(TgEnumEntityState.IsExists, itemFind)
? new(TgEnumEntityState.IsExists, itemFind)
: new TgEfStorageResult<TgEfMessageEntity>(TgEnumEntityState.NotExists, item);
}

Expand All @@ -65,7 +65,7 @@ public override TgEfStorageResult<TgEfMessageEntity> GetFirst(bool isNoTracking)
//.DefaultIfEmpty()
.FirstOrDefault();
return item is null
? new TgEfStorageResult<TgEfMessageEntity>(TgEnumEntityState.NotExists)
? new(TgEnumEntityState.NotExists)
: new TgEfStorageResult<TgEfMessageEntity>(TgEnumEntityState.IsExists, item);
}

Expand All @@ -81,7 +81,7 @@ public override async Task<TgEfStorageResult<TgEfMessageEntity>> GetFirstAsync(b
//.DefaultIfEmpty()
.FirstOrDefaultAsync().ConfigureAwait(false);
return item is null
? new TgEfStorageResult<TgEfMessageEntity>(TgEnumEntityState.NotExists)
? new(TgEnumEntityState.NotExists)
: new TgEfStorageResult<TgEfMessageEntity>(TgEnumEntityState.IsExists, item);
}

Expand All @@ -108,7 +108,7 @@ public override TgEfStorageResult<TgEfMessageEntity> GetList(int take, int skip,
.Include(x => x.Source)
.ToList();
}
return new TgEfStorageResult<TgEfMessageEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override async Task<TgEfStorageResult<TgEfMessageEntity>> GetListAsync(int take, int skip, bool isNoTracking)
Expand All @@ -135,7 +135,7 @@ public override async Task<TgEfStorageResult<TgEfMessageEntity>> GetListAsync(in
.Include(x => x.Source)
.ToList();
}
return new TgEfStorageResult<TgEfMessageEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override TgEfStorageResult<TgEfMessageEntity> GetList(int take, int skip, Expression<Func<TgEfMessageEntity, bool>> where, bool isNoTracking)
Expand Down Expand Up @@ -165,7 +165,7 @@ public override TgEfStorageResult<TgEfMessageEntity> GetList(int take, int skip,
.Include(x => x.Source)
.ToList();
}
return new TgEfStorageResult<TgEfMessageEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override async Task<TgEfStorageResult<TgEfMessageEntity>> GetListAsync(int take, int skip, Expression<Func<TgEfMessageEntity, bool>> where, bool isNoTracking)
Expand Down Expand Up @@ -198,7 +198,7 @@ public override async Task<TgEfStorageResult<TgEfMessageEntity>> GetListAsync(in
.Include(x => x.Source)
.ToList();
}
return new TgEfStorageResult<TgEfMessageEntity>(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
return new(items.Any() ? TgEnumEntityState.IsExists : TgEnumEntityState.NotExists, items);
}

public override int GetCount() => EfContext.Messages.AsNoTracking().Count();
Expand Down Expand Up @@ -231,7 +231,7 @@ public override TgEfStorageResult<TgEfMessageEntity> DeleteAll()
Delete(item, isSkipFind: true);
}
}
return new TgEfStorageResult<TgEfMessageEntity>(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
return new(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
}

public override async Task<TgEfStorageResult<TgEfMessageEntity>> DeleteAllAsync()
Expand All @@ -244,7 +244,7 @@ public override async Task<TgEfStorageResult<TgEfMessageEntity>> DeleteAllAsync(
await DeleteAsync(item, isSkipFind: true).ConfigureAwait(false);
}
}
return new TgEfStorageResult<TgEfMessageEntity>(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
return new(storageResult.IsExists ? TgEnumEntityState.IsDeleted : TgEnumEntityState.NotDeleted);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion Core/TgStorage/Domain/Proxies/TgEfProxyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Guid ProxyUid
set
{
TgEfStorageResult<TgEfProxyEntity> storageResult = ProxyRepository.Get(
new TgEfProxyEntity { Uid = value }, isNoTracking: false);
new() { Uid = value }, isNoTracking: false);
Item = storageResult.IsExists
? storageResult.Item
: ProxyRepository.GetNew(isNoTracking: false).Item;
Expand Down
2 changes: 1 addition & 1 deletion Core/TgStorage/Domain/Sources/TgEfSourceEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed class TgEfSourceEntity : ITgDbEntity, ITgDbFillEntity<TgEfSourceEn

[DefaultValue(1)]
[ConcurrencyCheck]
[Column(TgEfConstants.ColumnId, TypeName = "INT(20)")]
[Column(TgEfConstants.ColumnId, TypeName = "LONG(20)")]
public long Id { get; set; }

[DefaultValue("UserName")]
Expand Down
14 changes: 4 additions & 10 deletions Core/TgStorage/Domain/Sources/TgEfSourceValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ public sealed class TgEfSourceValidator : TgEfValidatorBase<TgEfSourceEntity>
public TgEfSourceValidator()
{
RuleFor(item => item.Id)
.GreaterThan(0);
RuleFor(item => item.UserName)
.NotNull();
.GreaterThan(0);
RuleFor(item => item.Title)
.NotNull();
.NotNull();
RuleFor(item => item.About)
.NotNull();
.NotNull();
RuleFor(item => item.Count)
.GreaterThan(0);
//RuleFor(item => item.Directory)
// .NotNull();
.GreaterThan(0);
RuleFor(item => item.FirstId)
.GreaterThan(0);
//RuleFor(item => item.IsAutoUpdate)
// .NotNull();
}

#endregion
Expand Down
3 changes: 3 additions & 0 deletions Core/TgStorage/Domain/Versions/TgEfVersionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ public void FillTableVersions()
case 23:
Save(new() { Version = 24, Description = "Updating the UID field in the versions table" });
break;
case 24:
Save(new() { Version = 25, Description = "Migrated to EF Core" });
break;
}
if (versionLast.Version >= LastVersion)
isLast = true;
Expand Down
Loading

0 comments on commit fbda5e8

Please sign in to comment.