Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/products management #12

Merged
merged 9 commits into from
Jan 24, 2024
9 changes: 1 addition & 8 deletions Domus.Api/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Domus.Api.Controllers.Base;
using Domus.Service.Interfaces;
using Domus.Service.Models.Requests;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Authorization;
using Domus.Service.Models.Requests.Authentication;
using Microsoft.AspNetCore.Mvc;

namespace Domus.Api.Controllers;
Expand Down Expand Up @@ -44,7 +40,4 @@ public async Task<IActionResult> RefreshToken(RefreshTokenRequest request)
async () => await _authService.RefreshTokenAsync(request).ConfigureAwait(false)
).ConfigureAwait(false);
}



}
72 changes: 72 additions & 0 deletions Domus.Api/Controllers/ProductDetailsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Domus.Api.Controllers.Base;
using Domus.Service.Constants;
using Domus.Service.Interfaces;
using Domus.Service.Models.Requests.Base;
using Domus.Service.Models.Requests.ProductDetails;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Domus.Api.Controllers;

[Authorize(Roles = $"{UserRoleConstants.ADMIN},{UserRoleConstants.STAFF}", AuthenticationSchemes = "Bearer")]
[Route("api/[controller]")]
public class ProductDetailsController : BaseApiController
{
private readonly IProductDetailService _productDetailService;

public ProductDetailsController(IProductDetailService productDetailService)
{
_productDetailService = productDetailService;
}

[AllowAnonymous]
[HttpGet]
public async Task<IActionResult> GetPaginatedProductDetails([FromQuery] BasePaginatedRequest request)
{
return await ExecuteServiceLogic(
async () => await _productDetailService.GetPaginatedProductDetails(request).ConfigureAwait(false)
).ConfigureAwait(false);
}

[AllowAnonymous]
[HttpGet("all")]
public async Task<IActionResult> GetAllProductDetails()
{
return await ExecuteServiceLogic(
async () => await _productDetailService.GetAllProductDetails().ConfigureAwait(false)
).ConfigureAwait(false);
}

[AllowAnonymous]
[HttpGet("{id:guid}")]
public async Task<IActionResult> GetProductDetail(Guid id)
{
return await ExecuteServiceLogic(
async () => await _productDetailService.GetProductDetailById(id).ConfigureAwait(false)
).ConfigureAwait(false);
}

[HttpPost]
public async Task<IActionResult> CreateProductDetail(CreateProductDetailRequest request)
{
return await ExecuteServiceLogic(
async () => await _productDetailService.CreateProductDetail(request).ConfigureAwait(false)
).ConfigureAwait(false);
}

[HttpPut("{id:guid}")]
public async Task<IActionResult> UpdateProductDetail(UpdateProductDetailRequest request, Guid id)
{
return await ExecuteServiceLogic(
async () => await _productDetailService.UpdateProductDetail(request, id).ConfigureAwait(false)
).ConfigureAwait(false);
}

[HttpDelete("{id:guid}")]
public async Task<IActionResult> DeleteProductDetail(Guid id)
{
return await ExecuteServiceLogic(
async () => await _productDetailService.DeleteProductDetail(id).ConfigureAwait(false)
).ConfigureAwait(false);
}
}
72 changes: 72 additions & 0 deletions Domus.Api/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Domus.Api.Controllers.Base;
using Domus.Service.Constants;
using Domus.Service.Interfaces;
using Domus.Service.Models.Requests.Base;
using Domus.Service.Models.Requests.Products;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Domus.Api.Controllers;

[Authorize(Roles = $"{UserRoleConstants.ADMIN},{UserRoleConstants.STAFF}", AuthenticationSchemes = "Bearer")]
[Route("api/[controller]")]
public class ProductsController : BaseApiController
{
private IProductService _productService;

public ProductsController(IProductService productService)
{
_productService = productService;
}

[AllowAnonymous]
[HttpGet]
public async Task<IActionResult> GetPaginatedProducts([FromQuery] BasePaginatedRequest request)
{
return await ExecuteServiceLogic(
async () => await _productService.GetPaginatedProducts(request).ConfigureAwait(false)
).ConfigureAwait(false);
}

[AllowAnonymous]
[HttpGet("all")]
public async Task<IActionResult> GetAllProducts()
{
return await ExecuteServiceLogic(
async () => await _productService.GetAllProducts().ConfigureAwait(false)
).ConfigureAwait(false);
}

[AllowAnonymous]
[HttpGet("{id:guid}")]
public async Task<IActionResult> GetProduct(Guid id)
{
return await ExecuteServiceLogic(
async () => await _productService.GetProduct(id).ConfigureAwait(false)
).ConfigureAwait(false);
}

[HttpPost]
public async Task<IActionResult> CreateProduct(CreateProductRequest request)
{
return await ExecuteServiceLogic(
async () => await _productService.CreateProduct(request).ConfigureAwait(false)
).ConfigureAwait(false);
}

[HttpPut("{id:guid}")]
public async Task<IActionResult> UpdateProduct(UpdateProductRequest request, Guid id)
{
return await ExecuteServiceLogic(
async () => await _productService.UpdateProduct(request, id).ConfigureAwait(false)
).ConfigureAwait(false);
}

[HttpDelete("{id:guid}")]
public async Task<IActionResult> DeleteProduct(Guid id)
{
return await ExecuteServiceLogic(
async () => await _productService.DeleteProduct(id).ConfigureAwait(false)
).ConfigureAwait(false);
}
}
5 changes: 5 additions & 0 deletions Domus.Api/Exceptions/MissingGoogleSettingsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Domus.Api.Exceptions;

public class MissingGoogleSettingsException : ArgumentNullException
{
}
17 changes: 10 additions & 7 deletions Domus.Api/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Text;
using AutoMapper;
using Azure.Storage.Blobs;
using Domus.Api.Constants;
using Domus.Api.Exceptions;
using Domus.Api.Settings;
using Domus.Common.Constants;
Expand Down Expand Up @@ -77,7 +75,7 @@ public static IServiceCollection AddJwtAuthentication(this IServiceCollection se

public static IServiceCollection AddGgAuthentication(this IServiceCollection services, IConfiguration configuration)
{
var googleSettings = configuration.GetSection(nameof(GoogleSettings)).Get<GoogleSettings>();
var googleSettings = configuration.GetSection(nameof(GoogleSettings)).Get<GoogleSettings>() ?? throw new MissingGoogleSettingsException();
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
Expand Down Expand Up @@ -123,23 +121,28 @@ public static IServiceCollection RegisterServices(this IServiceCollection servic
services.AddScoped<IUserTokenRepository, UserTokenRepository>();
services.AddScoped<IArticleRepository, ArticleRepository>();
services.AddScoped<IArticleCategoryRepository, ArticleCategoryRepository>();
services.AddScoped<IProductCategoryRepository, ProductCategoryRepository>();
services.AddScoped<IProductRepository, ProductRepository>();
services.AddScoped<IProductDetailRepository, ProductDetailRepository>();
services.AddScoped<IProductPriceRepository, ProductPriceRepository>();
services.AddScoped<IProductAttributeRepository, ProductAttributeRepository>();
services.AddScoped<IProductAttributeValueRepository, ProductAttributeValueRepository>();
services.AddScoped<IAuthService, AuthService>();
services.AddScoped<IArticleService, ArticleService>();
services.AddScoped<IJwtService, JwtService>();
services.AddScoped<IEmailService, EmailService>();
services.AddScoped<IProductService, ProductService>();
services.AddScoped<IProductDetailService, ProductDetailService>();
services.AddIdentity<DomusUser, IdentityRole>()
.AddEntityFrameworkStores<DomusContext>()
.AddDefaultTokenProviders();
services.AddScoped<IEmailService, EmailService>();
services.AddScoped<IFileService, FileService>();


var config = new MapperConfiguration(AutoMapperConfiguration.RegisterMaps);
var mapper = config.CreateMapper();
services.AddSingleton(mapper);

return services;
}



}
4 changes: 3 additions & 1 deletion Domus.DAL/Data/DomusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
if (optionsBuilder.IsConfigured)
return;

optionsBuilder.UseSqlServer(DataAccessHelper.GetDefaultConnectionString());
optionsBuilder.UseSqlServer(DataAccessHelper.GetDefaultConnectionString())
.EnableSensitiveDataLogging()
.LogTo(Console.WriteLine);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
48 changes: 25 additions & 23 deletions Domus.DAL/Implementations/GenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,43 @@ namespace Domus.DAL.Implementations;
public abstract class GenericRepository<T> : IGenericRepository<T> where T : class
{
private readonly IAppDbContext _dbContext;
private readonly DbSet<T> _dbSet;

protected GenericRepository(IAppDbContext dbContext)
{
_dbContext = dbContext;
_dbSet = _dbContext.CreateSet<T>();
}

public void Add(T entity)
{
_dbContext.CreateSet<T>().AddAsync(entity);
_dbSet.Add(entity);
}

public async Task AddAsync(T entity)
{
await _dbContext.CreateSet<T>().AddAsync(entity);
await _dbSet.AddAsync(entity);
}

public void AddMany(IEnumerable<T> entities)
{
_dbContext.CreateSet<T>().AddRange(entities);
_dbSet.AddRange(entities);
}

public async Task AddManyAsync(IEnumerable<T> entities)
{
await _dbContext.CreateSet<T>().AddRangeAsync(entities);
await _dbSet.AddRangeAsync(entities);
}

public void Update(T entity)
{
_dbContext.Update<T>(entity);
}

public Task UpdateAsync(T entity)
public async Task UpdateAsync(T entity)
{
_dbContext.Update<T>(entity);
return Task.CompletedTask;
await Task.CompletedTask;
}

public void UpdateMany(IEnumerable<T> entities)
Expand All @@ -54,88 +56,88 @@ public void UpdateMany(IEnumerable<T> entities)

public void UpdateMany(Expression<Func<T, bool>> predicate)
{
var entities = _dbContext.CreateSet<T>().Where(predicate);
var entities = _dbSet.Where(predicate);
foreach (var entity in entities)
{
_dbContext.Update<T>(entity);
}
}

public Task UpdateManyAsync(IEnumerable<T> entities)
public async Task UpdateManyAsync(IEnumerable<T> entities)
{
foreach (var entity in entities)
{
_dbContext.Update<T>(entity);
}

return Task.CompletedTask;
await Task.CompletedTask;
}

public async Task UpdateManyAsync(Expression<Func<T, bool>> predicate)
{
var entities = _dbContext.CreateSet<T>().Where(predicate);
var entities = _dbSet.Where(predicate);
await entities.ForEachAsync(c => _dbContext.Update<T>(c));
}

public void DeleteMany(Expression<Func<T, bool>> predicate)
{
var entities = _dbContext.CreateSet<T>().Where(predicate);
var entities = _dbSet.Where(predicate);
entities.ForEachAsync(c => _dbContext.SetDeleted<T>(c));
}

public async Task DeleteManyAsync(Expression<Func<T, bool>> predicate)
{
var entities = _dbContext.CreateSet<T>().Where(predicate);
var entities = _dbSet.Where(predicate);
await entities.ForEachAsync(c => _dbContext.SetDeleted<T>(c));
}

public IQueryable<T> GetAll()
{
return _dbContext.CreateSet<T>().AsQueryable();
return _dbSet.AsQueryable();
}

public async Task<IQueryable<T>> GetAllAsync()
{
return await Task.FromResult(_dbContext.CreateSet<T>().AsQueryable());
return await Task.FromResult(_dbSet.AsQueryable());
}

public T? Get(Expression<Func<T, bool>> predicate)
{
return _dbContext.CreateSet<T>().SingleOrDefault(predicate);
return _dbSet.SingleOrDefault(predicate);
}

public Task<T?> GetAsync(Expression<Func<T, bool>> predicate)
public async Task<T?> GetAsync(Expression<Func<T, bool>> predicate)
{
return _dbContext.CreateSet<T>().FirstOrDefaultAsync(predicate);
return await _dbSet.FirstOrDefaultAsync(predicate);
}

public IQueryable<T> Find(Expression<Func<T, bool>> predicate)
{
return _dbContext.CreateSet<T>().Where(predicate).AsQueryable();
return _dbSet.Where(predicate).AsQueryable();
}

public async Task<IQueryable<T>> FindAsync(Expression<Func<T, bool>> predicate)
{
return await Task.FromResult(_dbContext.CreateSet<T>().Where(predicate).AsQueryable());
return await Task.FromResult(_dbSet.Where(predicate).AsQueryable());
}

public long Count(Expression<Func<T, bool>> predicate)
{
return _dbContext.CreateSet<T>().Where(predicate).Count();
return _dbSet.Where(predicate).Count();
}

public async Task<long> CountAsync(Expression<Func<T, bool>> predicate)
{
return await _dbContext.CreateSet<T>().Where(predicate).CountAsync();
return await _dbSet.Where(predicate).CountAsync();
}

public bool Exists(Expression<Func<T, bool>> predicate)
{
return _dbContext.CreateSet<T>().Where(predicate).Any();
return _dbSet.Where(predicate).Any();
}

public async Task<bool> ExistsAsync(Expression<Func<T, bool>> predicate)
{
return await _dbContext.CreateSet<T>().Where(predicate).AnyAsync();
return await _dbSet.Where(predicate).AnyAsync();
}
}
Loading
Loading