diff --git a/Ubee.Api/Middlewares/ExceptionHandlerMiddleware.cs b/Ubee.Api/Middlewares/ExceptionHandlerMiddleware.cs new file mode 100644 index 0000000..5e7c89c --- /dev/null +++ b/Ubee.Api/Middlewares/ExceptionHandlerMiddleware.cs @@ -0,0 +1,42 @@ +using System.Globalization; + +namespace Ubee.Api.Middlewares; + +public class ExceptionHandlerMiddleware +{ + private readonly RequestDelegate next; + private readonly ILogger logger; + + public ExceptionHandlerMiddleware(RequestDelegate next, ILogger 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 + }); + } + } +} diff --git a/Ubee.Api/Program.cs b/Ubee.Api/Program.cs index 48863a6..c0c8e28 100644 --- a/Ubee.Api/Program.cs +++ b/Ubee.Api/Program.cs @@ -16,6 +16,8 @@ app.UseSwaggerUI(); } +app.UseMiddleware(); + app.UseHttpsRedirection(); app.UseAuthorization(); diff --git a/Ubee.Data/IRepositories/IInfoRepository.cs b/Ubee.Data/IRepositories/IInfoRepository.cs deleted file mode 100644 index 1910839..0000000 --- a/Ubee.Data/IRepositories/IInfoRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Ubee.Domain.Configurations; -using Ubee.Domain.Entities; - -namespace Ubee.Data.IRepositories; - -public interface IInfoRepository -{ - ValueTask InsertInfoAsync(Info info); - ValueTask UpdateInfoAsync(Info info); - ValueTask DeleteInfoAysnyc(long id); - ValueTask SelectInfoAsync(Predicate predicate); - IQueryable SelectAllInfos(); -} diff --git a/Ubee.Data/IRepositories/IRepository.cs b/Ubee.Data/IRepositories/IRepository.cs new file mode 100644 index 0000000..8c3680f --- /dev/null +++ b/Ubee.Data/IRepositories/IRepository.cs @@ -0,0 +1,17 @@ +using System.Linq.Expressions; +using Ubee.Domain.Commons; + +namespace Ubee.Data.IRepositories; + +public interface IRepository where TEntity : Auditable +{ + ValueTask InsertAsync(TEntity entity); + + TEntity Update(TEntity entity); + IQueryable SelectAll(Expression> expression = null, string[] includes = null); + ValueTask SelectAsync(Expression> expression, string[] includes = null); + ValueTask DeleteAsync(Expression> expression); + bool DeleteMany(Expression> expression); + + ValueTask SaveAsync(); +} diff --git a/Ubee.Data/IRepositories/ITransactionRepository.cs b/Ubee.Data/IRepositories/ITransactionRepository.cs deleted file mode 100644 index 03823a6..0000000 --- a/Ubee.Data/IRepositories/ITransactionRepository.cs +++ /dev/null @@ -1,14 +0,0 @@ - - -using Ubee.Domain.Entities; - -namespace Ubee.Data.IRepositories; - -public interface ITransactionRepository -{ - ValueTask InsertTransactionAsync(Transaction transaction); - ValueTask UpdateTransactionAsync(Transaction transaction); - ValueTask DeleteTransactionAysnyc(long id); - ValueTask SelectTransactionById(Predicate predicate); - IQueryable SelectAllTransactions(); -} diff --git a/Ubee.Data/IRepositories/IUserRepository.cs b/Ubee.Data/IRepositories/IUserRepository.cs deleted file mode 100644 index 4285f02..0000000 --- a/Ubee.Data/IRepositories/IUserRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Ubee.Domain.Configurations; -using Ubee.Domain.Entities; - -namespace Ubee.Data.IRepositories; - -public interface IUserRepository -{ - ValueTask InsertUserAsync(User user); - ValueTask UpdateUserAsync(User user); - ValueTask DeleteUserAysnyc(long id); - ValueTask SelectUserAsync(Predicate predicate); - IQueryable SelectAllUsers(); -} diff --git a/Ubee.Data/IRepositories/IWalletRepository.cs b/Ubee.Data/IRepositories/IWalletRepository.cs deleted file mode 100644 index f3efaa5..0000000 --- a/Ubee.Data/IRepositories/IWalletRepository.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ubee.Domain.Entities; - -namespace Ubee.Data.IRepositories; - -public interface IWalletRepository -{ - ValueTask InsertWalletAsync(Wallet wallet); - ValueTask UpdateWalletAsync(Wallet wallet); - ValueTask DeleteWalletAysnyc(long id); - ValueTask SelectWalletAsync(Predicate wallet); - IQueryable SelectAllWallets(); -} diff --git a/Ubee.Data/Repositories/InfoRepository.cs b/Ubee.Data/Repositories/InfoRepository.cs deleted file mode 100644 index a0a039f..0000000 --- a/Ubee.Data/Repositories/InfoRepository.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Ubee.Data.Contexts; -using Ubee.Data.IRepositories; -using Ubee.Domain.Configurations; -using Ubee.Domain.Entities; - -namespace Ubee.Data.Repositories -{ - public class InfoRepository : IInfoRepository - - { - private readonly AppDbContext appDbContext = new AppDbContext(); - - public async ValueTask InsertInfoAsync(Info info) - { - EntityEntry entity = await this.appDbContext.Infos.AddAsync(info); - await appDbContext.SaveChangesAsync(); - return entity.Entity; - } - - public async ValueTask UpdateInfoAsync(Info info) - { - EntityEntry entity = this.appDbContext.Infos.Update(info); - await appDbContext.SaveChangesAsync(); - return entity.Entity; - } - - public async ValueTask DeleteInfoAysnyc(long id) - { - Info entity = - await this.appDbContext.Infos.FirstOrDefaultAsync(info => info.Id.Equals(id)); - if (entity is null) - return false; - - this.appDbContext.Infos.Remove(entity); - await this.appDbContext.SaveChangesAsync(); - return true; - } - - public async ValueTask SelectInfoAsync(Predicate predicate) => - await this.appDbContext.Infos.Where(info => info.IsActive).FirstOrDefaultAsync(info => predicate(info)); - - public IQueryable SelectAllInfos() => - this.appDbContext.Infos.Where(info => info.IsActive); - } -} diff --git a/Ubee.Data/Repositories/Repository.cs b/Ubee.Data/Repositories/Repository.cs new file mode 100644 index 0000000..56df95c --- /dev/null +++ b/Ubee.Data/Repositories/Repository.cs @@ -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 : IRepository where TEntity : Auditable +{ + protected readonly AppDbContext appDbContext; + protected readonly DbSet dbSet; + + public Repository(AppDbContext appDbContext, DbSet dbSet) + { + this.appDbContext = appDbContext; + this.dbSet = dbSet; + } + public async ValueTask DeleteAsync(Expression> expression) + { + var entity = await this.SelectAsync(expression); + + if(entity is null) + { + entity.IsDeleted = true; + return true; + } + + return false; + } + + public bool DeleteMany(Expression> 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 InsertAsync(TEntity entity) + { + EntityEntry entry = await this.dbSet.AddAsync(entity); + + return entry.Entity; + } + + public async ValueTask SaveAsync() + { + appDbContext.SaveChangesAsync(); + } + + public IQueryable SelectAll(Expression> expression = null, string[] includes = null) + { + IQueryable 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 SelectAsync(Expression> expression, string[] includes = null) + => await this.SelectAll(expression, includes).FirstOrDefaultAsync(); + + public TEntity Update(TEntity entity) + { + EntityEntry entryentity = this.appDbContext.Update(entity); + + return entryentity.Entity; + } +} diff --git a/Ubee.Data/Repositories/TransactionRepository.cs b/Ubee.Data/Repositories/TransactionRepository.cs deleted file mode 100644 index b6475b6..0000000 --- a/Ubee.Data/Repositories/TransactionRepository.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; -using Ubee.Data.Contexts; -using Ubee.Data.IRepositories; -using Ubee.Domain.Entities; -namespace Ubee.Data.Repositories; -public class TransactionRepository : ITransactionRepository -{ - private readonly AppDbContext appDbContext = new AppDbContext(); - - public async ValueTask InsertTransactionAsync(Transaction transaction) - { - EntityEntry entity = await this.appDbContext.Transactions.AddAsync(transaction); - await appDbContext.SaveChangesAsync(); - return entity.Entity; - } - - public async ValueTask UpdateTransactionAsync(Transaction transaction) - { - EntityEntry entity = this.appDbContext.Transactions.Update(transaction); - await appDbContext.SaveChangesAsync(); - return entity.Entity; - } - - public async ValueTask DeleteTransactionAysnyc(long id) - { - Transaction entity = - await this.appDbContext.Transactions.FirstOrDefaultAsync(tr => tr.Id.Equals(id)); - if (entity is null) - return false; - - this.appDbContext.Transactions.Remove(entity); - await this.appDbContext.SaveChangesAsync(); - return true; - } - - public async ValueTask SelectTransactionById(Predicate predicate) => - await this.appDbContext.Transactions.Where(info => info.IsActive).FirstOrDefaultAsync(tr => predicate(tr)); - - public IQueryable SelectAllTransactions() => - this.appDbContext.Transactions.Where(tr => tr.IsActive); -} diff --git a/Ubee.Data/Repositories/UserRepository.cs b/Ubee.Data/Repositories/UserRepository.cs deleted file mode 100644 index c43c763..0000000 --- a/Ubee.Data/Repositories/UserRepository.cs +++ /dev/null @@ -1,48 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; -using Ubee.Data.Contexts; -using Ubee.Data.IRepositories; -using Ubee.Domain.Entities; - -namespace Ubee.Data.Repositories; - -public class UserRepository : IUserRepository -{ - private readonly AppDbContext appDbContext = new AppDbContext(); - - public async ValueTask InsertUserAsync(User user) - { - EntityEntry entity = await this.appDbContext.Users.AddAsync(user); - await appDbContext.SaveChangesAsync(); - return entity.Entity; - } - - public async ValueTask UpdateUserAsync(User user) - { - EntityEntry entity = this.appDbContext.Users.Update(user); - await appDbContext.SaveChangesAsync(); - return entity.Entity; - } - - public async ValueTask DeleteUserAysnyc(long id) - { - User entity = - await this.appDbContext.Users.FirstOrDefaultAsync(user => user.Id.Equals(id)); - if (entity is null) - return false; - - this.appDbContext.Users.Remove(entity); - await this.appDbContext.SaveChangesAsync(); - return true; - } - - public async ValueTask SelectUserAsync(Predicate predicate) => - await this.appDbContext.Users.Where(user => user.IsActive).FirstOrDefaultAsync(user => predicate(user)); - - public IQueryable SelectAllUsers() - { - this.appDbContext.Users.Where(user => user.IsActive); - var query = "select * from \"Users\" where \"Firstname\" like '%o%'"; - return this.appDbContext.Users.FromSqlRaw(query); - } -} diff --git a/Ubee.Data/Repositories/WalletRepository.cs b/Ubee.Data/Repositories/WalletRepository.cs deleted file mode 100644 index a5411ab..0000000 --- a/Ubee.Data/Repositories/WalletRepository.cs +++ /dev/null @@ -1,44 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; -using Ubee.Data.Contexts; -using Ubee.Data.IRepositories; -using Ubee.Domain.Entities; - -namespace Ubee.Data.Repositories; - -public class WalletRepository : IWalletRepository -{ - private readonly AppDbContext appDbContext = new AppDbContext(); - - public async ValueTask InsertWalletAsync(Wallet wallet) - { - EntityEntry entity = await this.appDbContext.Wallets.AddAsync(wallet); - await appDbContext.SaveChangesAsync(); - return entity.Entity; - } - - public async ValueTask UpdateWalletAsync(Wallet wallet) - { - EntityEntry entity = this.appDbContext.Wallets.Update(wallet); - await appDbContext.SaveChangesAsync(); - return entity.Entity; - } - - public async ValueTask DeleteWalletAysnyc(long id) - { - Wallet entity = - await this.appDbContext.Wallets.FirstOrDefaultAsync(wallet => wallet.Id.Equals(id)); - if (entity is null) - return false; - - this.appDbContext.Wallets.Remove(entity); - await this.appDbContext.SaveChangesAsync(); - return true; - } - - public async ValueTask SelectWalletAsync(Predicate predicate) => - await this.appDbContext.Wallets.Where(wallet => wallet.IsActive).FirstOrDefaultAsync(wallet => predicate(wallet)); - - public IQueryable SelectAllWallets() => - this.appDbContext.Wallets.Where(wallet => wallet.IsActive); -} diff --git a/Ubee.Domain/Commons/Auditable.cs b/Ubee.Domain/Commons/Auditable.cs index edc2cce..1780171 100644 --- a/Ubee.Domain/Commons/Auditable.cs +++ b/Ubee.Domain/Commons/Auditable.cs @@ -3,7 +3,7 @@ public abstract class Auditable { public int Id { get; set; } - public DateTime CreatedAt { get; set; } + public DateTime CreatedAt { get; set; } = DateTime.UtcNow; public DateTime? UpdatedAt { get; set; } - public bool IsActive { get; set; } = true; + public bool IsDeleted { get; set; } } diff --git a/Ubee.Domain/Configurations/PaginationMetaData.cs b/Ubee.Domain/Configurations/PaginationMetaData.cs new file mode 100644 index 0000000..50dc707 --- /dev/null +++ b/Ubee.Domain/Configurations/PaginationMetaData.cs @@ -0,0 +1,17 @@ +namespace Ubee.Domain.Configurations; + +public class PaginationMetaData +{ + public int CurrentPage { get; set; } + public int TotalPages { get; set; } + public int TotalCount { get; set; } + public bool HasPrevious => CurrentPage > 1; + public bool HasNext => CurrentPage < TotalPages; + + public PaginationMetaData(int totalCount, PaginationParams @params) + { + TotalCount = totalCount; + TotalPages = (int)Math.Ceiling(totalCount / (double)@params.PageSize); + CurrentPage = @params.PageIndex; + } +} diff --git a/Ubee.Domain/Entities/Info.cs b/Ubee.Domain/Entities/Info.cs index 304a5d6..f55baa9 100644 --- a/Ubee.Domain/Entities/Info.cs +++ b/Ubee.Domain/Entities/Info.cs @@ -11,7 +11,6 @@ public class Info : Auditable public DateTime DailyExposeDate { get; set; } public decimal SumRecord { get; set; } public DateTime SumRecordDate { get; set; } - public long WalletId { get; set; } public Wallet Wallet { get; set; } } diff --git a/Ubee.Domain/Entities/User.cs b/Ubee.Domain/Entities/User.cs index ded4087..e3cf236 100644 --- a/Ubee.Domain/Entities/User.cs +++ b/Ubee.Domain/Entities/User.cs @@ -6,6 +6,7 @@ public class User : Auditable { public string Firstname { get; set; } public string Lastname { get; set; } + public string Email { get; set; } public string Phone { get; set; } public string Username { get; set; } public string Password { get; set; } diff --git a/Ubee.Domain/Entities/Wallet.cs b/Ubee.Domain/Entities/Wallet.cs index 88df613..e9663c4 100644 --- a/Ubee.Domain/Entities/Wallet.cs +++ b/Ubee.Domain/Entities/Wallet.cs @@ -1,5 +1,4 @@ using Ubee.Domain.Commons; -using Ubee.Domain.Enums; namespace Ubee.Domain.Entities; diff --git a/Ubee.Domain/Enums/TransactionType.cs b/Ubee.Domain/Enums/TransactionType.cs index c331983..65f5155 100644 --- a/Ubee.Domain/Enums/TransactionType.cs +++ b/Ubee.Domain/Enums/TransactionType.cs @@ -1,7 +1,7 @@ namespace Ubee.Domain.Enums; -public enum TransactionType +public enum TransactionType : byte { - Income = 1, - Outcome + Income = 10, + Outcome = 20 } diff --git a/Ubee.GraphQL/Controllers/WeatherForecastController.cs b/Ubee.GraphQL/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..ebcc6f9 --- /dev/null +++ b/Ubee.GraphQL/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Ubee.GraphQL.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} \ No newline at end of file diff --git a/Ubee.GraphQL/Program.cs b/Ubee.GraphQL/Program.cs new file mode 100644 index 0000000..48863a6 --- /dev/null +++ b/Ubee.GraphQL/Program.cs @@ -0,0 +1,25 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/Ubee.GraphQL/Properties/launchSettings.json b/Ubee.GraphQL/Properties/launchSettings.json new file mode 100644 index 0000000..5f1fc4d --- /dev/null +++ b/Ubee.GraphQL/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:20250", + "sslPort": 44358 + } + }, + "profiles": { + "Ubee.GraphQL": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7008;http://localhost:5110", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Ubee.GraphQL/Ubee.GraphQL.csproj b/Ubee.GraphQL/Ubee.GraphQL.csproj new file mode 100644 index 0000000..60bf9ea --- /dev/null +++ b/Ubee.GraphQL/Ubee.GraphQL.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/Ubee.GraphQL/WeatherForecast.cs b/Ubee.GraphQL/WeatherForecast.cs new file mode 100644 index 0000000..656ad5e --- /dev/null +++ b/Ubee.GraphQL/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace Ubee.GraphQL +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} \ No newline at end of file diff --git a/Ubee.GraphQL/appsettings.Development.json b/Ubee.GraphQL/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Ubee.GraphQL/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Ubee.GraphQL/appsettings.json b/Ubee.GraphQL/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Ubee.GraphQL/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Ubee.Service/DTOs/InfoDto.cs b/Ubee.Service/DTOs/Infos/InfoForCreationDto.cs similarity index 87% rename from Ubee.Service/DTOs/InfoDto.cs rename to Ubee.Service/DTOs/Infos/InfoForCreationDto.cs index f4035ff..a00266a 100644 --- a/Ubee.Service/DTOs/InfoDto.cs +++ b/Ubee.Service/DTOs/Infos/InfoForCreationDto.cs @@ -1,8 +1,8 @@ using Ubee.Domain.Entities; -namespace Ubee.Service.DTOs +namespace Ubee.Service.DTOs.Infos { - public class InfoDto + public class InfoForCreationDto { public decimal AnnualExpose { get; set; } public decimal MonthlyExpose { get; set; } diff --git a/Ubee.Service/DTOs/InfoCreationDto.cs b/Ubee.Service/DTOs/Infos/InfoForResultDto.cs similarity index 87% rename from Ubee.Service/DTOs/InfoCreationDto.cs rename to Ubee.Service/DTOs/Infos/InfoForResultDto.cs index 02d7c19..d4ea6f8 100644 --- a/Ubee.Service/DTOs/InfoCreationDto.cs +++ b/Ubee.Service/DTOs/Infos/InfoForResultDto.cs @@ -1,8 +1,8 @@ using Ubee.Domain.Entities; -namespace Ubee.Service.DTOs +namespace Ubee.Service.DTOs.Infos { - public class InfoCreationDto + public class InfoForResultDto { public decimal AnnualExpose { get; set; } public decimal MonthlyExpose { get; set; } diff --git a/Ubee.Service/DTOs/TransactionForCreationDto.cs b/Ubee.Service/DTOs/Transactions/TransactionForCreationDto.cs similarity index 84% rename from Ubee.Service/DTOs/TransactionForCreationDto.cs rename to Ubee.Service/DTOs/Transactions/TransactionForCreationDto.cs index b0d3985..b2c968b 100644 --- a/Ubee.Service/DTOs/TransactionForCreationDto.cs +++ b/Ubee.Service/DTOs/Transactions/TransactionForCreationDto.cs @@ -1,6 +1,6 @@ using Ubee.Domain.Enums; -namespace Ubee.Service.DTOs; +namespace Ubee.Service.DTOs.Transactions; public class TransactionForCreationDto { public decimal Amount { get; set; } diff --git a/Ubee.Service/DTOs/TransactionDto.cs b/Ubee.Service/DTOs/Transactions/TransactionForResultDto.cs similarity index 71% rename from Ubee.Service/DTOs/TransactionDto.cs rename to Ubee.Service/DTOs/Transactions/TransactionForResultDto.cs index 7cfb7f8..b29775f 100644 --- a/Ubee.Service/DTOs/TransactionDto.cs +++ b/Ubee.Service/DTOs/Transactions/TransactionForResultDto.cs @@ -1,7 +1,7 @@ using Ubee.Domain.Enums; -namespace Ubee.Service.DTOs; -public class TransactionDto +namespace Ubee.Service.DTOs.Transactions; +public class TransactionForResultDto { public decimal Amount { get; set; } public string Note { get; set; } diff --git a/Ubee.Service/DTOs/Users/UserForChangePasswordDto.cs b/Ubee.Service/DTOs/Users/UserForChangePasswordDto.cs new file mode 100644 index 0000000..4eaed8d --- /dev/null +++ b/Ubee.Service/DTOs/Users/UserForChangePasswordDto.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace Ubee.Service.DTOs.Users; + +public class UserForChangePasswordDto +{ + [Required(ErrorMessage = "Email is requaried!")] + public string Email { get; set; } + + [Required(ErrorMessage = "Old password must not be null or empty!")] + public string OldPassword { get; set; } + + [Required(ErrorMessage = "New password must not be null or empty!")] + public string NewPassword { get; set; } + + [Required(ErrorMessage = "Confirming password must not be null or empty!")] + public string ComfirmPassword { get; set; } +} diff --git a/Ubee.Service/DTOs/UserForCreationDto.cs b/Ubee.Service/DTOs/Users/UserForCreationDto.cs similarity index 81% rename from Ubee.Service/DTOs/UserForCreationDto.cs rename to Ubee.Service/DTOs/Users/UserForCreationDto.cs index 954f414..338846a 100644 --- a/Ubee.Service/DTOs/UserForCreationDto.cs +++ b/Ubee.Service/DTOs/Users/UserForCreationDto.cs @@ -1,11 +1,12 @@ using Ubee.Domain.Entities; -namespace Ubee.Service.DTOs; +namespace Ubee.Service.DTOs.Users; public class UserForCreationDto { public string Firstname { get; set; } public string Lastname { get; set; } + public string Email { get; set; } public string Username { get; set; } public string Password { get; set; } public string Phone { get; set; } diff --git a/Ubee.Service/DTOs/UserDto.cs b/Ubee.Service/DTOs/Users/UserForResultDto.cs similarity index 87% rename from Ubee.Service/DTOs/UserDto.cs rename to Ubee.Service/DTOs/Users/UserForResultDto.cs index 1622ede..59c6d41 100644 --- a/Ubee.Service/DTOs/UserDto.cs +++ b/Ubee.Service/DTOs/Users/UserForResultDto.cs @@ -1,8 +1,8 @@ using Ubee.Domain.Entities; -namespace Ubee.Service.DTOs; +namespace Ubee.Service.DTOs.Users; -public class UserDto +public class UserForResultDto { public string Firstname { get; set; } public string Lastname { get; set; } diff --git a/Ubee.Service/DTOs/WalletForCreationDto.cs b/Ubee.Service/DTOs/Wallets/WalletForCreationDto.cs similarity index 83% rename from Ubee.Service/DTOs/WalletForCreationDto.cs rename to Ubee.Service/DTOs/Wallets/WalletForCreationDto.cs index 04d96f7..8112157 100644 --- a/Ubee.Service/DTOs/WalletForCreationDto.cs +++ b/Ubee.Service/DTOs/Wallets/WalletForCreationDto.cs @@ -1,6 +1,6 @@ using Ubee.Domain.Entities; -namespace Ubee.Service.DTOs; +namespace Ubee.Service.DTOs.Wallets; public class WalletForCreationDto { diff --git a/Ubee.Service/DTOs/WalletDto.cs b/Ubee.Service/DTOs/Wallets/WalletForResultDto.cs similarity index 74% rename from Ubee.Service/DTOs/WalletDto.cs rename to Ubee.Service/DTOs/Wallets/WalletForResultDto.cs index 779d2d5..e3c0e00 100644 --- a/Ubee.Service/DTOs/WalletDto.cs +++ b/Ubee.Service/DTOs/Wallets/WalletForResultDto.cs @@ -1,8 +1,8 @@ using Ubee.Domain.Entities; -namespace Ubee.Service.DTOs; +namespace Ubee.Service.DTOs.Wallets; -public class WalletDto +public class WalletForResultDto { public string Name { get; set; } public decimal AvailableMoney { get; set; } diff --git a/Ubee.Service/Exceptions/UbeeException.cs b/Ubee.Service/Exceptions/UbeeException.cs new file mode 100644 index 0000000..300a3d5 --- /dev/null +++ b/Ubee.Service/Exceptions/UbeeException.cs @@ -0,0 +1,10 @@ +namespace Ubee.Service.Exceptions; + +public class UbeeException : Exception +{ + public int Code { get; set; } + public UbeeException(int code, string message) : base(message) + { + this.Code = code; + } +} diff --git a/Ubee.Service/Helpers/PasswordHelper.cs b/Ubee.Service/Helpers/PasswordHelper.cs new file mode 100644 index 0000000..992e819 --- /dev/null +++ b/Ubee.Service/Helpers/PasswordHelper.cs @@ -0,0 +1,13 @@ +namespace Ubee.Service.Helpers; + +public class PasswordHelper +{ + public static string Hash(string password) + { + return BCrypt.Net.BCrypt.HashPassword(password); + } + public static bool Verify(string password, string passwordHash) + { + return BCrypt.Net.BCrypt.Verify(password, passwordHash); + } +} diff --git a/Ubee.Service/Helpers/Response.cs b/Ubee.Service/Helpers/Response.cs deleted file mode 100644 index 0d1d729..0000000 --- a/Ubee.Service/Helpers/Response.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Ubee.Service.Helpers; - -public class Response -{ - public int Code { get; set; } - public string Message { get; set; } - public TResult Value { get; set; } -} diff --git a/Ubee.Service/Interfaces/IInfoService.cs b/Ubee.Service/Interfaces/IInfoService.cs index 8408b6a..ff7d0a8 100644 --- a/Ubee.Service/Interfaces/IInfoService.cs +++ b/Ubee.Service/Interfaces/IInfoService.cs @@ -1,14 +1,5 @@ -using Ubee.Service.DTOs; -using Ubee.Service.Helpers; +namespace Ubee.Service.Interfaces; -namespace Ubee.Service.Interfaces +public interface IInfoService { - public interface IInfoService - { - ValueTask> AddInfoAsync(InfoCreationDto infoCreationDto); - ValueTask> ModifyInfoAsync(long id, InfoCreationDto infoCreationDto); - ValueTask> DeleteInfoAsync(long id); - ValueTask> GetInfoAsync(long id); - ValueTask>> GetAllInfoAsync(); - } } diff --git a/Ubee.Service/Interfaces/ITransactionService.cs b/Ubee.Service/Interfaces/ITransactionService.cs index cbb83d9..4c7e902 100644 --- a/Ubee.Service/Interfaces/ITransactionService.cs +++ b/Ubee.Service/Interfaces/ITransactionService.cs @@ -1,14 +1,5 @@ -using Ubee.Domain.Configurations; -using Ubee.Service.DTOs; -using Ubee.Service.Helpers; - -namespace Ubee.Service.Interfaces; +namespace Ubee.Service.Interfaces; public interface ITransactionService { - ValueTask> AddTransactionAsync(TransactionForCreationDto transactionForCreationDto); - ValueTask> ModifyTransactionAsync(long id, TransactionForCreationDto transactionForCreationDto); - ValueTask> DeleteTransactionAsync(long id); - ValueTask> GetTransactionByIdAsync(long id); - ValueTask>> GetAllTransactionAsync(); } \ No newline at end of file diff --git a/Ubee.Service/Interfaces/IUserService.cs b/Ubee.Service/Interfaces/IUserService.cs index c48d836..009a9bf 100644 --- a/Ubee.Service/Interfaces/IUserService.cs +++ b/Ubee.Service/Interfaces/IUserService.cs @@ -1,14 +1,15 @@ using Ubee.Domain.Configurations; -using Ubee.Service.DTOs; -using Ubee.Service.Helpers; +using Ubee.Service.DTOs.Users; namespace Ubee.Service.Interfaces; public interface IUserService { - ValueTask> AddUserAsync(UserForCreationDto userForCreationDto); - ValueTask> ModifyUserAsync(long id, UserForCreationDto userForCreationDto); - ValueTask> DeleteUserAsync(long id); - ValueTask> GetUserByIdAsync(long id); - ValueTask>> GetAllUserAsync(PaginationParams @params, string search = null); + Task AddAsync(UserForCreationDto dto); + Task> RetrieveAllAsync(PaginationParams @params); + Task RetrieveByIdAsync(long id); + Task RemoveAsync(long id); + Task ModifyAsync(long id, UserForCreationDto dto); + Task ChangePasswordAsync(UserForChangePasswordDto dto); + } diff --git a/Ubee.Service/Interfaces/IWalletService.cs b/Ubee.Service/Interfaces/IWalletService.cs index 50417df..0264fe0 100644 --- a/Ubee.Service/Interfaces/IWalletService.cs +++ b/Ubee.Service/Interfaces/IWalletService.cs @@ -1,14 +1,5 @@ -using Ubee.Domain.Configurations; -using Ubee.Service.DTOs; -using Ubee.Service.Helpers; - -namespace Ubee.Service.Interfaces; +namespace Ubee.Service.Interfaces; public interface IWalletService { - ValueTask> AddWalletAsync(WalletForCreationDto walletForCreationDto); - ValueTask> ModifyWalletAsync(long id, WalletForCreationDto walletForCreationDto); - ValueTask> DeleteWalletAsync(long id); - ValueTask> GetWalletByIdAsync(long id); - ValueTask>> GetAllWalletAsync(); } diff --git a/Ubee.Service/Mappers/MappingProfile.cs b/Ubee.Service/Mappers/MappingProfile.cs index 7d06c55..4dfdbcf 100644 --- a/Ubee.Service/Mappers/MappingProfile.cs +++ b/Ubee.Service/Mappers/MappingProfile.cs @@ -1,6 +1,10 @@ using AutoMapper; using Ubee.Domain.Entities; using Ubee.Service.DTOs; +using Ubee.Service.DTOs.Infos; +using Ubee.Service.DTOs.Transactions; +using Ubee.Service.DTOs.Users; +using Ubee.Service.DTOs.Wallets; namespace Ubee.Service.Mappers; @@ -8,13 +12,20 @@ public class MappingProfile : Profile { public MappingProfile() { + // User CreateMap().ReverseMap(); - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + + // Wallet CreateMap().ReverseMap(); - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + + // Info + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + + // Transaction CreateMap().ReverseMap(); + CreateMap().ReverseMap(); } } diff --git a/Ubee.Service/Services/InfoService.cs b/Ubee.Service/Services/InfoService.cs index 5e774cb..4c6fbd8 100644 --- a/Ubee.Service/Services/InfoService.cs +++ b/Ubee.Service/Services/InfoService.cs @@ -1,122 +1,7 @@ -using AutoMapper; -using Microsoft.EntityFrameworkCore; -using Ubee.Data.IRepositories; -using Ubee.Domain.Entities; -using Ubee.Service.DTOs; -using Ubee.Service.Helpers; -using Ubee.Service.Interfaces; +using Ubee.Service.Interfaces; -namespace Ubee.Service.Services -{ - public class InfoService : IInfoService - { - private readonly IInfoRepository infoRepository; - private readonly IMapper mapper; - - public InfoService(IInfoRepository infoRepository, IMapper mapper) - { - this.infoRepository = infoRepository; - this.mapper = mapper; - } - - public async ValueTask> DeleteInfoAsync(long id) - { - var info = await this.infoRepository.SelectInfoAsync(i => i.Id == id); - if (info is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = false - }; - - await this.infoRepository.DeleteInfoAysnyc(id); - return new Response - { - Code = 200, - Message = "Success", - Value = true - }; - } - - public async ValueTask> AddInfoAsync(InfoCreationDto infoCreationDto) - { - var info = await this.infoRepository.SelectInfoAsync(i => i.WalletId == infoCreationDto.WalletId); - - if (info is null) - { - return new Response - { - Code = 404, - Message = "Not found", - Value = null, - }; - } - - var mappedInfo = this.mapper.Map(infoRepository); - var addedInfo = await this.infoRepository.InsertInfoAsync(mappedInfo); - var resultIInfoDto = this.mapper.Map (addedInfo); +namespace Ubee.Service.Services; - return new Response - { - Code = 200, - Message = "Success", - Value = resultIInfoDto, - }; - } - - public async ValueTask>> GetAllInfoAsync() - { - var infos = await infoRepository.SelectAllInfos().ToListAsync(); - var mappedInfos = mapper.Map>(infos); - - return new Response> - { - Code = 200, - Message = "Success", - Value = mappedInfos - }; - } - - public async ValueTask> GetInfoAsync(long id) - { - Info info = await this.infoRepository.SelectInfoAsync(i => i.Id == id); - if (info is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = null - }; - - var mappedInfo = this.mapper.Map(info); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedInfo - }; - } - - public async ValueTask> ModifyInfoAsync(long id, InfoCreationDto infoCreationDto) - { - Info info = await this.infoRepository.SelectInfoAsync(i => i.Id.Equals(id)); - if (info is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = null - }; - - var updatedInfo = await this.infoRepository.UpdateInfoAsync(info); - var mappedInfo = this.mapper.Map(updatedInfo); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedInfo - }; - } - } +public class InfoService : IInfoService +{ } diff --git a/Ubee.Service/Services/TransactionService.cs b/Ubee.Service/Services/TransactionService.cs index cd0bca3..526e68f 100644 --- a/Ubee.Service/Services/TransactionService.cs +++ b/Ubee.Service/Services/TransactionService.cs @@ -1,154 +1,6 @@ -using AutoMapper; -using Ubee.Data.IRepositories; -using Ubee.Data.Repositories; -using Ubee.Domain.Configurations; -using Ubee.Domain.Entities; -using Ubee.Service.DTOs; -using Ubee.Service.Extensions; -using Ubee.Service.Helpers; -using Ubee.Service.Interfaces; +using Ubee.Service.Interfaces; namespace Ubee.Service.Services; public class TransactionService : ITransactionService { - private readonly ITransactionRepository transactionRepository = new TransactionRepository(); - private readonly IWalletRepository walletRepository = new WalletRepository(); - private readonly IMapper mapper; - public TransactionService(IMapper mapper) - { - this.mapper = mapper; - } - - public async ValueTask> AddTransactionAsync(TransactionForCreationDto transactionForCreationDto) - { - if(transactionForCreationDto.Type == Domain.Enums.TransactionType.Income) - { - var res = await this.walletRepository.SelectWalletAsync(wl => wl.Id == transactionForCreationDto.WalletId); - if(res is null) - { - return new Response - { - Code = 404 , - Message = "Not found", - Value = null - }; - } - res.AvailableMoney += transactionForCreationDto.Amount; - await this.walletRepository.UpdateWalletAsync(res); - var mappedRes = mapper.Map(transactionForCreationDto); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedRes - }; - } - else - { - var res = await this.walletRepository.SelectWalletAsync(wl => wl.Id == transactionForCreationDto.WalletId); - if (res is null) - { - return new Response - { - Code = 404, - Message = "Not found", - Value = null - }; - } - if (res.AvailableMoney < transactionForCreationDto.Amount) - { - return new Response - { - Code = 404, - Message = "There are insufficient funds in your account", - Value = null - }; - } - res.AvailableMoney -= transactionForCreationDto.Amount; - await this.walletRepository.UpdateWalletAsync(res); - var mappedRes = mapper.Map(transactionForCreationDto); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedRes - }; - - } - - } - - public async ValueTask> DeleteTransactionAsync(long id) - { - Transaction user = await this.transactionRepository.SelectTransactionById(user => user.Id.Equals(id)); - if (user is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = false - }; - - await this.transactionRepository.DeleteTransactionAysnyc(id); - return new Response - { - Code = 200, - Message = "Success", - Value = true - }; - } - - public async ValueTask>> GetAllTransactionAsync() - { - var transactions = transactionRepository.SelectAllTransactions().ToList(); - var mappedWallets = mapper.Map>(transactions); - - return new Response> - { - Code = 200, - Message = "Success", - Value = mappedWallets - }; - } - - public async ValueTask> GetTransactionByIdAsync(long id) - { - Transaction user = await this.transactionRepository.SelectTransactionById(tr => tr.Id.Equals(id)); - if (user is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = null - }; - - var mappedUsers = this.mapper.Map(user); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedUsers - }; - } - - public async ValueTask> ModifyTransactionAsync(long id, TransactionForCreationDto transactionForCreationDto) - { - Transaction user = await this.transactionRepository.SelectTransactionById(tr => tr.Id.Equals(id)); - if (user is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = null - }; - - var updatedUser = await this.transactionRepository.UpdateTransactionAsync(user); - var mappedUsers = this.mapper.Map(updatedUser); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedUsers - }; - } } diff --git a/Ubee.Service/Services/UserService.cs b/Ubee.Service/Services/UserService.cs index 46864ea..891b3a1 100644 --- a/Ubee.Service/Services/UserService.cs +++ b/Ubee.Service/Services/UserService.cs @@ -1,10 +1,10 @@ using AutoMapper; using Microsoft.EntityFrameworkCore; using Ubee.Data.IRepositories; -using Ubee.Data.Repositories; using Ubee.Domain.Configurations; using Ubee.Domain.Entities; -using Ubee.Service.DTOs; +using Ubee.Service.DTOs.Users; +using Ubee.Service.Exceptions; using Ubee.Service.Extensions; using Ubee.Service.Helpers; using Ubee.Service.Interfaces; @@ -13,121 +13,92 @@ namespace Ubee.Service.Services; public class UserService : IUserService { - //I call the repository by constructor - private readonly IUserRepository userRepository; + private readonly IRepository userRepository; private readonly IMapper mapper; - public UserService(IUserRepository userRepository,IMapper mapper) + + public UserService(IRepository userRepository, IMapper mapper) { this.userRepository = userRepository; this.mapper = mapper; } - public async ValueTask> AddUserAsync(UserForCreationDto userForCreationDto) + public async Task AddAsync(UserForCreationDto dto) + { + var existUser = await this.userRepository.SelectAsync(u => u.Email == dto.Email); + if (existUser is not null && !existUser.IsDeleted) + throw new UbeeException(409, "User already exist"); + + var mapped = this.mapper.Map(dto); + mapped.CreatedAt = DateTime.UtcNow; + + var addedUser = await this.userRepository.InsertAsync(mapped); + await this.userRepository.SaveAsync(); + + return this.mapper.Map(addedUser); + } + + public async Task ChangePasswordAsync(UserForChangePasswordDto dto) { - var user = await this.userRepository.SelectUserAsync(user => - user.Username.Equals(userForCreationDto.Username) || - user.Phone.Equals(userForCreationDto.Phone)); - - if (user is not null) - return new Response - { - Code = 404, - Message = "User is already existed", - Value = (UserDto)user - }; - - - var mappedUser = this.mapper.Map(userForCreationDto); - mappedUser.Password = userForCreationDto.Password.Encrypt(); - var addedUser = await this.userRepository.InsertUserAsync(mappedUser); - var resultDto = this.mapper.Map(addedUser); - return new Response - { - Code = 200, - Message = "Success", - Value = resultDto - }; + var user = await this.userRepository.SelectAsync(u => u.Email == dto.Email); + if (user is null || user.IsDeleted) + throw new UbeeException(404, "User not found"); + + if (!dto.OldPassword.Equals(user.Password)) + throw new UbeeException(400, "Password is incorrect"); + + if (dto.NewPassword != dto.ComfirmPassword) + throw new UbeeException(400, "Confirm password is not equal to new password"); + + user.Password = PasswordHelper.Hash(dto.NewPassword); + + await this.userRepository.SaveAsync(); + + return this.mapper.Map(user); } - public async ValueTask> DeleteUserAsync(long id) + public async Task ModifyAsync(long id, UserForCreationDto dto) { - User user = await this.userRepository.SelectUserAsync(user => user.Id.Equals(id)); - if (user is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = false - }; - - await this.userRepository.DeleteUserAysnyc(id); - return new Response - { - Code = 200, - Message = "Success", - Value = true - }; + var user = await this.userRepository.SelectAsync(u => u.Id == id); + if (user is null || user.IsDeleted) + throw new UbeeException(404, "Couldn't find user for this given id"); + + var modifiedUser = this.mapper.Map(dto, user); + modifiedUser.UpdatedAt = DateTime.UtcNow; + + await this.userRepository.SaveAsync(); + + return this.mapper.Map(user); } - public async ValueTask>> GetAllUserAsync(PaginationParams @params, string search = null) + public async Task RemoveAsync(long id) { - var users = await this.userRepository.SelectAllUsers().ToPagedList(@params).ToListAsync(); - if (users.Any()) - return new Response> - { - Code = 404, - Message = "Success", - Value = null - }; - - var result = users.Where(user => user.Firstname.Contains(search, StringComparison.OrdinalIgnoreCase)); - var mappedUsers = this.mapper.Map>(result); - return new Response> - { - Code = 200, - Message = "Success", - Value = mappedUsers - }; + var user = await this.userRepository.SelectAsync(u => u.Id == id); + if (user is null || user.IsDeleted) + throw new UbeeException(404, "Couldn't find user for this given id"); + + await this.userRepository.DeleteAsync(u => u.Id == id); + + await this.userRepository.SaveAsync(); + + return true; } - public async ValueTask> GetUserByIdAsync(long id) + public async Task> RetrieveAllAsync(PaginationParams @params) { - User user = await this.userRepository.SelectUserAsync(user => user.Id.Equals(id)); - if (user is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = null - }; - - var mappedUsers = this.mapper.Map(user); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedUsers - }; + var users = await this.userRepository.SelectAll() + .Where(u => u.IsDeleted == false) + .ToPagedList(@params) + .ToListAsync(); + + return this.mapper.Map>(users); } - public async ValueTask> ModifyUserAsync(long id, UserForCreationDto userForCreationDto) + public async Task RetrieveByIdAsync(long id) { - User user = await this.userRepository.SelectUserAsync(user => user.Id.Equals(id)); - if (user is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = null - }; - - var updatedUser = await this.userRepository.UpdateUserAsync(user); - var mappedUsers = this.mapper.Map(updatedUser); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedUsers - }; + var user = await this.userRepository.SelectAsync(u => u.Id == id); + if (user is null || user.IsDeleted) + throw new UbeeException(404, "User not found"); + + return this.mapper.Map(user); } } diff --git a/Ubee.Service/Services/WalletService.cs b/Ubee.Service/Services/WalletService.cs index 7b4a65b..171b5bf 100644 --- a/Ubee.Service/Services/WalletService.cs +++ b/Ubee.Service/Services/WalletService.cs @@ -1,121 +1,7 @@ -using AutoMapper; -using Microsoft.EntityFrameworkCore; -using Ubee.Data.IRepositories; -using Ubee.Data.Repositories; -using Ubee.Domain.Entities; -using Ubee.Service.DTOs; -using Ubee.Service.Helpers; -using Ubee.Service.Interfaces; +using Ubee.Service.Interfaces; namespace Ubee.Service.Services; public class WalletService : IWalletService { - - //I call the repository by constructor - private readonly IWalletRepository walletRepository; - private readonly IMapper mapper; - public WalletService(IWalletRepository walletRepository, IMapper mapper) - { - this.walletRepository = walletRepository; - this.mapper = mapper; - } - - public async ValueTask> AddWalletAsync(WalletForCreationDto walletForCreationDto) - { - var user = await this.walletRepository.SelectWalletAsync(u => u.Id == walletForCreationDto.UserId); - - if (user is null) - return new Response - { - Code = 404, - Message = "Wallet not found", - Value = null - }; - - - var mappedWallet = this.mapper.Map(walletForCreationDto); - var addedWallet = await this.walletRepository.InsertWalletAsync(mappedWallet); - var resultDto = this.mapper.Map(addedWallet); - return new Response - { - Code = 200, - Message = "Success", - Value = resultDto - }; - } - - public async ValueTask> DeleteWalletAsync(long id) - { - Wallet wallet = await this.walletRepository.SelectWalletAsync(w => w.Id.Equals(id)); - if (wallet is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = false - }; - - await this.walletRepository.DeleteWalletAysnyc(id); - return new Response - { - Code = 200, - Message = "Success", - Value = true - }; - } - - public async ValueTask>> GetAllWalletAsync() - { - var wallets = await walletRepository.SelectAllWallets().ToListAsync(); - var mappedWallets = mapper.Map>(wallets); - - return new Response> - { - Code = 200, - Message = "Success", - Value = mappedWallets - }; - } - - public async ValueTask> GetWalletByIdAsync(long id) - { - Wallet wallet = await this.walletRepository.SelectWalletAsync(w => w.Id.Equals(id)); - if (wallet is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = null - }; - - var mappedWallets = this.mapper.Map(wallet); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedWallets - }; - } - - public async ValueTask> ModifyWalletAsync(long id, WalletForCreationDto walletForCreationDto) - { - Wallet wallet = await this.walletRepository.SelectWalletAsync(w => w.Id.Equals(id)); - if (wallet is null) - return new Response - { - Code = 404, - Message = "Couldn't find for given ID", - Value = null - }; - - var updatedWallet = await this.walletRepository.UpdateWalletAsync(wallet); - var mappedWallet = this.mapper.Map(updatedWallet); - return new Response - { - Code = 200, - Message = "Success", - Value = mappedWallet - }; - } } diff --git a/Ubee.Service/Ubee.Service.csproj b/Ubee.Service/Ubee.Service.csproj index 8f1736f..3ff6d80 100644 --- a/Ubee.Service/Ubee.Service.csproj +++ b/Ubee.Service/Ubee.Service.csproj @@ -8,6 +8,7 @@ + diff --git a/Ubee.sln b/Ubee.sln index ca010db..7a5438f 100644 --- a/Ubee.sln +++ b/Ubee.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ubee.Service", "Ubee.Servic EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ubee.Api", "Ubee.Api\Ubee.Api.csproj", "{ED3B1FB9-3AF7-42B4-9C2A-7AACC21294F2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ubee.GraphQL", "Ubee.GraphQL\Ubee.GraphQL.csproj", "{3D4ED376-F687-4FAB-8935-1F59EFA77FC1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {ED3B1FB9-3AF7-42B4-9C2A-7AACC21294F2}.Debug|Any CPU.Build.0 = Debug|Any CPU {ED3B1FB9-3AF7-42B4-9C2A-7AACC21294F2}.Release|Any CPU.ActiveCfg = Release|Any CPU {ED3B1FB9-3AF7-42B4-9C2A-7AACC21294F2}.Release|Any CPU.Build.0 = Release|Any CPU + {3D4ED376-F687-4FAB-8935-1F59EFA77FC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3D4ED376-F687-4FAB-8935-1F59EFA77FC1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3D4ED376-F687-4FAB-8935-1F59EFA77FC1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3D4ED376-F687-4FAB-8935-1F59EFA77FC1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE