Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Ubee.Api/Middlewares/ExceptionHandlerMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Globalization;

namespace Ubee.Api.Middlewares;

public class ExceptionHandlerMiddleware
{
private readonly RequestDelegate next;
private readonly ILogger<ExceptionHandlerMiddleware> logger;

public ExceptionHandlerMiddleware(RequestDelegate next, ILogger<ExceptionHandlerMiddleware> logger)
{
this.next = next;
this.logger = logger;
}

public async Task Invoke(HttpContext context)
{
try
{
await next(context);
}
catch (FleetFlowException exception)
{
context.Response.StatusCode = exception.Code;
await context.Response.WriteAsJsonAsync(new Response
{
Code = exception.Code,
Error = exception.Message
});
}
catch (Exception exception)
{
this.logger.LogError($"{exception}\n\n");
context.Response.StatusCode = 500;
await context.Response.WriteAsJsonAsync(new Response
{
Code = 500,
Error = exception.Message
});
}
}
}
2 changes: 2 additions & 0 deletions Ubee.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
app.UseSwaggerUI();
}

app.UseMiddleware<ExceptionHandlerMiddleware>();

app.UseHttpsRedirection();

app.UseAuthorization();
Expand Down
13 changes: 0 additions & 13 deletions Ubee.Data/IRepositories/IInfoRepository.cs

This file was deleted.

17 changes: 17 additions & 0 deletions Ubee.Data/IRepositories/IRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Linq.Expressions;
using Ubee.Domain.Commons;

namespace Ubee.Data.IRepositories;

public interface IRepository<TEntity> where TEntity : Auditable
{
ValueTask<TEntity> InsertAsync(TEntity entity);

TEntity Update(TEntity entity);
IQueryable<TEntity> SelectAll(Expression<Func<TEntity, bool>> expression = null, string[] includes = null);
ValueTask<TEntity> SelectAsync(Expression<Func<TEntity, bool>> expression, string[] includes = null);
ValueTask<bool> DeleteAsync(Expression<Func<TEntity, bool>> expression);
bool DeleteMany(Expression<Func<TEntity, bool>> expression);

ValueTask SaveAsync();
}
14 changes: 0 additions & 14 deletions Ubee.Data/IRepositories/ITransactionRepository.cs

This file was deleted.

13 changes: 0 additions & 13 deletions Ubee.Data/IRepositories/IUserRepository.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Ubee.Data/IRepositories/IWalletRepository.cs

This file was deleted.

52 changes: 0 additions & 52 deletions Ubee.Data/Repositories/InfoRepository.cs

This file was deleted.

83 changes: 83 additions & 0 deletions Ubee.Data/Repositories/Repository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Linq.Expressions;
using Ubee.Data.Contexts;
using Ubee.Data.IRepositories;
using Ubee.Domain.Commons;

namespace Ubee.Data.Repositories;
#pragma warning disable

public class Repository<TEntity> : IRepository<TEntity> where TEntity : Auditable
{
protected readonly AppDbContext appDbContext;
protected readonly DbSet<TEntity> dbSet;

public Repository(AppDbContext appDbContext, DbSet<TEntity> dbSet)
{
this.appDbContext = appDbContext;
this.dbSet = dbSet;
}
public async ValueTask<bool> DeleteAsync(Expression<Func<TEntity, bool>> expression)
{
var entity = await this.SelectAsync(expression);

if(entity is null)
{
entity.IsDeleted = true;
return true;
}

return false;
}

public bool DeleteMany(Expression<Func<TEntity, bool>> expression)
{
var entities = dbSet.Where(expression);
if(entities is null)
{
foreach (var entity in entities)
entity.IsDeleted = true;

return true;
}
return false;
}

public async ValueTask<TEntity> InsertAsync(TEntity entity)
{
EntityEntry<TEntity> entry = await this.dbSet.AddAsync(entity);

return entry.Entity;
}

public async ValueTask SaveAsync()
{
appDbContext.SaveChangesAsync();
}

public IQueryable<TEntity> SelectAll(Expression<Func<TEntity, bool>> expression = null, string[] includes = null)
{
IQueryable<TEntity> query = expression is null ? this.dbSet : this.dbSet.Where(expression);

if(includes is not null)
{
foreach (string include in includes)
{
query = query.Include(include);
}
}

return query;
}

public async ValueTask<TEntity> SelectAsync(Expression<Func<TEntity, bool>> expression, string[] includes = null)
=> await this.SelectAll(expression, includes).FirstOrDefaultAsync();

public TEntity Update(TEntity entity)
{
EntityEntry<TEntity> entryentity = this.appDbContext.Update(entity);

return entryentity.Entity;
}
}
42 changes: 0 additions & 42 deletions Ubee.Data/Repositories/TransactionRepository.cs

This file was deleted.

48 changes: 0 additions & 48 deletions Ubee.Data/Repositories/UserRepository.cs

This file was deleted.

44 changes: 0 additions & 44 deletions Ubee.Data/Repositories/WalletRepository.cs

This file was deleted.

Loading