Skip to content

Commit

Permalink
Merge pull request #23 from PasinduPrabhashitha/products-crud
Browse files Browse the repository at this point in the history
feat✨: change crud logics based on custom entity ids
  • Loading branch information
pasindu-pr authored Apr 8, 2023
2 parents 9d6e185 + 08eff7d commit 9d66ddf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/services/Florage.Products/Dtos/CreateProductDto.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Florage.Products.Dtos
{
public class CreateProductDto
{
{
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Image { get; set; } = string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion src/services/Florage.Products/Dtos/GetProductDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class GetProductDto
{
public string Id { get; set; } = string.Empty;
public string ProductId { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Image { get; set; } = string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion src/services/Florage.Products/Florage.Products.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Florage.Shared" Version="0.1.4" />
<PackageReference Include="Florage.Shared" Version="0.1.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions src/services/Florage.Products/Models/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Florage.Products.Models
{
public class Product: BaseEntity
{
public string ProductId { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Image { get; set; } = string.Empty;
Expand Down
16 changes: 12 additions & 4 deletions src/services/Florage.Products/Services/ProductsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ProductsService : IProductsService
{
private readonly IRepository<Product> _repository;
private readonly IMapper _mapper;
private readonly string _customIdentifierAttribute = "ProductId";

public ProductsService(IRepository<Product> genericRepository, IMapper mapper)
{
Expand All @@ -20,12 +21,15 @@ public ProductsService(IRepository<Product> genericRepository, IMapper mapper)
public async Task CreateAsync(CreateProductDto productDto)
{
Product product = _mapper.Map<Product>(productDto);

// Generate custom product id
product.ProductId = Guid.NewGuid().ToString();
await _repository.CreateAsync(product);
}

public async Task DeleteAsync(string productId)
{
await _repository.DeleteAsync(productId);
await _repository.DeleteAsync(_customIdentifierAttribute, productId);
}

public async Task<IReadOnlyCollection<GetProductDto>> GetAllAsync()
Expand All @@ -37,16 +41,20 @@ public async Task<IReadOnlyCollection<GetProductDto>> GetAllAsync()

public async Task<GetProductDto> GetByIdAsync(string id)
{
Product product = await _repository.GetByIdAsync(id);
// Get product by custom product id
Product product = await _repository.GetOneAsync(_customIdentifierAttribute, id);
GetProductDto mappedProduct = _mapper.Map<GetProductDto>(product);
return mappedProduct;
}

public async Task UpdateAsync(string productId,UpdateProductDto updateProductDto)
{
Product productToUpdate = _mapper.Map<Product>(updateProductDto);
productToUpdate.Id = productId;
await _repository.UpdateAsync(productId, productToUpdate);
Product product = await _repository.GetOneAsync(_customIdentifierAttribute, productId);

productToUpdate.Id = product.Id;
productToUpdate.ProductId = productId;
await _repository.UpdateAsync(_customIdentifierAttribute, productId,productToUpdate);
}
}
}

0 comments on commit 9d66ddf

Please sign in to comment.