Skip to content

Commit

Permalink
chore: code cleanup, refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: russkyc <[email protected]>
  • Loading branch information
russkyc committed Jun 10, 2023
1 parent f631764 commit 9ffee79
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 85 deletions.
18 changes: 9 additions & 9 deletions Russkyc.GroomWise/Models/Abstractions/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public abstract class Repository<T> : Interfaces.Repository.IRepository<T>
where T : class, IEquatable<T>, new()
{
private readonly IDatabaseServiceAsync _databaseService;
private readonly Queue<Task> _changes;
private readonly List<T> _collection;
private readonly object _collectionLock = new object();
public readonly Queue<Task> Changes;
private readonly List<T> _collection;

public Repository(IDatabaseServiceAsync databaseService)
{
_changes = new Queue<Task>();
Changes = new Queue<Task>();
_databaseService = databaseService;
_collection = Task.Run(databaseService.GetCollection<T>).Result.ToList();
}
Expand All @@ -25,7 +25,7 @@ public void Add(T entity)
lock (_collectionLock)
{
_collection.Add(entity);
_changes.Enqueue(new Task(() => _databaseService.Add(entity)));
Changes.Enqueue(new Task(() => _databaseService.Add(entity)));
}
}

Expand All @@ -35,7 +35,7 @@ public void AddRange(IEnumerable<T> entities)
{
var collection = entities as T[] ?? entities.ToArray();
_collection.AddRange(collection);
_changes.Enqueue(new Task(() => _databaseService.AddMultiple(collection.ToList())));
Changes.Enqueue(new Task(() => _databaseService.AddMultiple(collection.ToList())));
}
}

Expand Down Expand Up @@ -69,7 +69,7 @@ public bool Remove(T entity)
{
var result = _collection.Remove(entity);
if (result)
_changes.Enqueue(new Task(() => _databaseService.Delete<T>(t => entity == t)));
Changes.Enqueue(new Task(() => _databaseService.Delete<T>(t => entity == t)));
return result;
}
}
Expand All @@ -80,16 +80,16 @@ public int RemoveAll(Predicate<T> filter)
{
var result = _collection.RemoveAll(filter);
if (result > 0)
_changes.Enqueue(new Task(() => _databaseService.Delete<T>(t => filter(t))));
Changes.Enqueue(new Task(() => _databaseService.Delete<T>(t => filter(t))));
return result;
}
}

public void WriteToDb()
{
while (_changes.Count != 0)
while (Changes.Count != 0)
{
_changes.Dequeue().Start();
Changes.Dequeue().Start();
}
}
}
2 changes: 0 additions & 2 deletions Russkyc.GroomWise/Russkyc.GroomWise.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Services\Converter\" />
<Folder Include="Services\Helper\" />
<Folder Include="Views\Resources\Themes\BaseThemes\" />
<Folder Include="Views\Resources\Themes\ColorThemes\" />
</ItemGroup>
Expand Down
51 changes: 15 additions & 36 deletions Russkyc.GroomWise/Services/Data/DataServiceProviderAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ public class DataServiceProviderAsync : IDatabaseServiceAsync
{
private readonly IFreeSql _db;

public DataServiceProviderAsync(IConfigProvider configProvider)
public DataServiceProviderAsync(
IConfigProvider configProvider,
IConnectionSourceProvider connectionSourceProvider
)
{
_db = new FreeSqlBuilder()
.UseConnectionString(
FreeSql.DataType.Sqlite,
new ConnectionSourceProvider().Build(
connectionSourceProvider.Build(
new ConnectionSource { Path = configProvider.Path },
DbProvider.Sqlite
)
Expand All @@ -29,47 +32,35 @@ public DataServiceProviderAsync(IConfigProvider configProvider)
public async Task<bool> Add<T>(T item)
where T : class, new()
{
return await _db
.Insert(item)
.ExecuteIdentityAsync() > 0;
return await _db.Insert(item).ExecuteIdentityAsync() > 0;
}

/// <inheritdoc />
public async Task<bool> AddMultiple<T>(ICollection<T> items)
where T : class, new()
{
return await _db
.Insert(items.ToList())
.ExecuteIdentityAsync() > 0;
return await _db.Insert(items.ToList()).ExecuteIdentityAsync() > 0;
}

/// <inheritdoc />
public async Task<T> Get<T>(Expression<Func<T, bool>> filter)
where T : class, new()
{
return await _db
.Select<T>()
.Where(filter)
.ToOneAsync();
return await _db.Select<T>().Where(filter).ToOneAsync();
}

/// <inheritdoc />
public async Task<ICollection<T>> GetMultiple<T>(Expression<Func<T, bool>> filter)
where T : class, new()
{
return await _db
.Select<T>()
.Where(filter)
.ToListAsync();
return await _db.Select<T>().Where(filter).ToListAsync();
}

/// <inheritdoc />
public async Task<ICollection<T>> GetCollection<T>()
where T : class, new()
{
return await _db
.Select<T>()
.ToListAsync();
return await _db.Select<T>().ToListAsync();
}

/// <inheritdoc />
Expand All @@ -79,22 +70,14 @@ public async Task<ICollection<T>> GetCollection<T>()
)
where T : class, new()
{
return await _db
.Update<T>()
.Set(t => action)
.Where(filter)
.ExecuteAffrowsAsync() > 0;
return await _db.Update<T>().Set(t => action).Where(filter).ExecuteAffrowsAsync() > 0;
}

/// <inheritdoc />
public async Task<bool> Update<T>(Expression<Func<T, bool>> filter, T item)
where T : class, new()
{
return await _db
.Update<T>()
.Set(t => item)
.Where(filter)
.ExecuteAffrowsAsync() > 0;
return await _db.Update<T>().Set(t => item).Where(filter).ExecuteAffrowsAsync() > 0;
}

/// <inheritdoc />
Expand All @@ -111,18 +94,14 @@ public async Task<bool> Update<T>(T item)
public async Task<bool> Delete<T>(Expression<Func<T, bool>> filter)
where T : class, new()
{
return await _db
.Delete<T>()
.Where(filter).ExecuteAffrowsAsync() > 0;
return await _db.Delete<T>().Where(filter).ExecuteAffrowsAsync() > 0;
}

/// <inheritdoc />
public async Task<bool> Delete<T>(object id)
where T : class, new()
{
return await _db
.Delete<T>()
.Where(t => ((IEntity)t).Id == (int)id)
.ExecuteAffrowsAsync() > 0;
return await _db.Delete<T>().Where(t => ((IEntity)t).Id == (int)id).ExecuteAffrowsAsync()
> 0;
}
}
90 changes: 54 additions & 36 deletions Russkyc.GroomWise/Services/Data/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,96 +16,114 @@ public UnitOfWork(IDatabaseServiceAsync databaseService)
}

private AccountRepository? _accountsRepository;

/// <inheritdoc />
public AccountRepository AccountsRepository => _accountsRepository ??= new AccountRepository(_databaseService);
public AccountRepository AccountsRepository =>
_accountsRepository ??= new AccountRepository(_databaseService);

private AddressRepository? _addressRepository;

/// <inheritdoc />
public AddressRepository AddressRepository => _addressRepository ??= new AddressRepository(_databaseService);
public AddressRepository AddressRepository =>
_addressRepository ??= new AddressRepository(_databaseService);

private AppointmentEmployeeRepository? _appointmentEmployeeRepository;

/// <inheritdoc />
public AppointmentEmployeeRepository AppointmentEmployeeRepository => _appointmentEmployeeRepository ??= new AppointmentEmployeeRepository(_databaseService);
public AppointmentEmployeeRepository AppointmentEmployeeRepository =>
_appointmentEmployeeRepository ??= new AppointmentEmployeeRepository(_databaseService);

private AppointmentPetRepository? _appointmentPetRepository;

/// <inheritdoc />
public AppointmentPetRepository AppointmentPetRepository => _appointmentPetRepository ??= new AppointmentPetRepository(_databaseService);
public AppointmentPetRepository AppointmentPetRepository =>
_appointmentPetRepository ??= new AppointmentPetRepository(_databaseService);

private AppointmentRepository? _appointmentRepository;

/// <inheritdoc />
public AppointmentRepository AppointmentRepository => _appointmentRepository ??= new AppointmentRepository(_databaseService);
public AppointmentRepository AppointmentRepository =>
_appointmentRepository ??= new AppointmentRepository(_databaseService);

private AppointmentServiceProductRepository? _appointmentServiceProductRepository;

/// <inheritdoc />
public AppointmentServiceProductRepository AppointmentServiceProductRepository => _appointmentServiceProductRepository ??= new AppointmentServiceProductRepository(_databaseService);
public AppointmentServiceProductRepository AppointmentServiceProductRepository =>
_appointmentServiceProductRepository ??= new AppointmentServiceProductRepository(
_databaseService
);

private AppointmentServiceRepository? _appointmentServiceRepository;

/// <inheritdoc />
public AppointmentServiceRepository AppointmentServiceRepository => _appointmentServiceRepository ??= new AppointmentServiceRepository(_databaseService);
public AppointmentServiceRepository AppointmentServiceRepository =>
_appointmentServiceRepository ??= new AppointmentServiceRepository(_databaseService);

private CustomerAddressRepository? _customerAddressRepository;

/// <inheritdoc />
public CustomerAddressRepository CustomerAddressRepository => _customerAddressRepository ??= new CustomerAddressRepository(_databaseService);
public CustomerAddressRepository CustomerAddressRepository =>
_customerAddressRepository ??= new CustomerAddressRepository(_databaseService);

private CustomerPetRepository? _customerPetRepository;

/// <inheritdoc />
public CustomerPetRepository CustomerPetRepository => _customerPetRepository ??= new CustomerPetRepository(_databaseService);
public CustomerPetRepository CustomerPetRepository =>
_customerPetRepository ??= new CustomerPetRepository(_databaseService);

private CustomerRepository? _customerRepository;

/// <inheritdoc />
public CustomerRepository CustomerRepository => _customerRepository ??= new CustomerRepository(_databaseService);
public CustomerRepository CustomerRepository =>
_customerRepository ??= new CustomerRepository(_databaseService);

private EmployeeAccountRepository? _employeeAccountRepository;

/// <inheritdoc />
public EmployeeAccountRepository EmployeeAccountRepository => _employeeAccountRepository ??= new EmployeeAccountRepository(_databaseService);
public EmployeeAccountRepository EmployeeAccountRepository =>
_employeeAccountRepository ??= new EmployeeAccountRepository(_databaseService);

private EmployeeAddressRepository? _employeeAddressRepository;

/// <inheritdoc />
public EmployeeAddressRepository EmployeeAddressRepository => _employeeAddressRepository ??= new EmployeeAddressRepository(_databaseService);
public EmployeeAddressRepository EmployeeAddressRepository =>
_employeeAddressRepository ??= new EmployeeAddressRepository(_databaseService);

private EmployeeRepository? _employeeRepository;

/// <inheritdoc />
public EmployeeRepository EmployeeRepository => _employeeRepository ??= new EmployeeRepository(_databaseService);
public EmployeeRepository EmployeeRepository =>
_employeeRepository ??= new EmployeeRepository(_databaseService);

private EmployeeRoleRepository? _employeeRoleRepository;

/// <inheritdoc />
public EmployeeRoleRepository EmployeeRoleRepository => _employeeRoleRepository ??= new EmployeeRoleRepository(_databaseService);
public EmployeeRoleRepository EmployeeRoleRepository =>
_employeeRoleRepository ??= new EmployeeRoleRepository(_databaseService);

private GroomingServiceRepository? _groomingServiceRepository;

/// <inheritdoc />
public GroomingServiceRepository GroomingServiceRepository => _groomingServiceRepository ??= new GroomingServiceRepository(_databaseService);
public GroomingServiceRepository GroomingServiceRepository =>
_groomingServiceRepository ??= new GroomingServiceRepository(_databaseService);

private PetRepository? _petRepository;

/// <inheritdoc />
public PetRepository PetRepository => _petRepository ??= new PetRepository(_databaseService);

private ProductRepository? _productRepository;

/// <inheritdoc />
public ProductRepository ProductRepository => _productRepository ??= new ProductRepository(_databaseService);
public ProductRepository ProductRepository =>
_productRepository ??= new ProductRepository(_databaseService);

private RoleRepository? _roleRepository;

/// <inheritdoc />
public RoleRepository RoleRepository => _roleRepository ??= new RoleRepository(_databaseService);
public RoleRepository RoleRepository =>
_roleRepository ??= new RoleRepository(_databaseService);


/// <inheritdoc />
public void SaveChanges()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

using CustomerCardViewModel = GroomWise.ViewModels.Customers.CustomerCardViewModel;

namespace GroomWise.Services.Factory;

public class CustomerCardViewModelFactory : Factory<CustomerCardViewModel> { }

0 comments on commit 9ffee79

Please sign in to comment.