-
Notifications
You must be signed in to change notification settings - Fork 0
create and update product functionality added #2
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,9 @@ | |
*.log | ||
node_modules/ | ||
.env | ||
*.dll | ||
*.json | ||
secrets.json | ||
*.pdb | ||
*.cache | ||
*.exe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using AutoMapper; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ProductPOC.Dto; | ||
using ProductPOC.Models; | ||
using ProductPOC.Service; | ||
|
||
namespace ProductPOC.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class ProductController : ControllerBase | ||
{ | ||
private readonly IProductService _productService; | ||
private readonly IMapper _mapper; | ||
public ProductController(IProductService productService, IMapper mapper) | ||
{ | ||
_productService = productService; | ||
_mapper = mapper; | ||
} | ||
|
||
[HttpGet] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
public async Task<ActionResult<IEnumerable<ProductDto>>> GetProducts() | ||
{ | ||
var products = await _productService.GetAllProductsAsync(); | ||
var productDtos= _mapper.Map<IEnumerable<ProductDto>>(products); | ||
return Ok(productDtos); | ||
} | ||
|
||
[HttpGet] | ||
[Route("{id}")] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
public async Task<ActionResult<IEnumerable<ProductDto>>> GetByIdProduct([FromRoute] Guid id) | ||
{ | ||
var product = await _productService.GetByIdProductAsync(id); | ||
var productDtos = _mapper.Map<ProductDto>(product); | ||
return Ok(productDtos); | ||
} | ||
|
||
[HttpPost] | ||
[ProducesResponseType(StatusCodes.Status201Created)] | ||
public async Task<IActionResult> CreateProduct([FromBody] CreateProductDto createProductDto) | ||
{ | ||
var product= _mapper.Map<Product>(createProductDto); | ||
product= await _productService.CreateProductAsync(product); | ||
var productDto= _mapper.Map<ProductDto>(product); | ||
return CreatedAtAction(nameof(GetByIdProduct), new { id = productDto.Id }, productDto); | ||
} | ||
|
||
[HttpPut] | ||
[Route("{id}")] | ||
[ProducesResponseType(StatusCodes.Status202Accepted)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
public async Task<IActionResult> UpdateProduct([FromRoute] Guid id, [FromBody] UpdateProductDto updateProductDto) | ||
{ | ||
var product = _mapper.Map<Product>(updateProductDto); | ||
product = await _productService.UpdateProductAsync(id, product); | ||
if (product == null) | ||
{ | ||
return NotFound(); | ||
} | ||
var productDto = _mapper.Map<ProductDto>(product); | ||
return AcceptedAtAction(nameof(GetByIdProduct), new { id = productDto.Id }, productDto); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using MongoDB.Driver; | ||
using ProductPOC.Models; | ||
|
||
namespace ProductPOC.DbContext | ||
{ | ||
public interface IProductDbContext | ||
{ | ||
IMongoCollection<Product> Products { get; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.Extensions.Options; | ||
using MongoDB.Driver; | ||
using ProductPOC.Models; | ||
|
||
namespace ProductPOC.DbContext | ||
{ | ||
public class ProductDbContext:IProductDbContext | ||
{ | ||
private readonly IMongoDatabase _database; | ||
private readonly string _productsCollectionName; | ||
|
||
public ProductDbContext() | ||
{ | ||
|
||
} | ||
public ProductDbContext(IConfiguration configuration) | ||
{ | ||
var connectionString = configuration["ConnectionStrings:MongoDb"]; | ||
var databaseName = configuration["ConnectionStrings:DatabaseName"]; | ||
_productsCollectionName = configuration["ConnectionStrings:ProductsCollectionName"]; | ||
var client = new MongoClient(connectionString); | ||
_database = client.GetDatabase(databaseName); | ||
|
||
} | ||
|
||
|
||
public virtual IMongoCollection<Product> Products => _database.GetCollection<Product>(_productsCollectionName); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace ProductPOC.Dto | ||
{ | ||
public class CreateProductDto | ||
{ | ||
public string Name { get; set; } | ||
public string Description { get; set; } | ||
public decimal Price { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace ProductPOC.Dto | ||
{ | ||
public class ProductDto | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public string Description { get; set; } | ||
public decimal Price { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace ProductPOC.Dto | ||
{ | ||
public class UpdateProductDto | ||
{ | ||
public string Name { get; set; } | ||
public string Description { get; set; } | ||
public decimal Price { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using AutoMapper; | ||
using ProductPOC.Dto; | ||
using ProductPOC.Models; | ||
namespace ProductPOC.Mappings | ||
{ | ||
public class AutoMapperProfiles:Profile | ||
{ | ||
public AutoMapperProfiles() | ||
{ | ||
CreateMap<Product,ProductDto>().ReverseMap(); | ||
CreateMap<Product,CreateProductDto>().ReverseMap(); | ||
CreateMap<Product,UpdateProductDto>().ReverseMap(); | ||
} | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using MongoDB.Bson.Serialization.Attributes; | ||
using MongoDB.Bson; | ||
|
||
namespace ProductPOC.Models | ||
{ | ||
public class Product | ||
{ | ||
[BsonId] | ||
[BsonRepresentation(BsonType.String)] | ||
public Guid Id { get; set; } | ||
|
||
[BsonElement("name")] | ||
public string Name { get; set; } | ||
|
||
[BsonElement("description")] | ||
public string Description { get; set; } | ||
|
||
[BsonElement("price")] | ||
public decimal Price { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UserSecretsId>e8774c96-ba4d-4773-b837-687e85197ffd</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="13.0.1" /> | ||
<PackageReference Include="MongoDB.Driver" Version="2.25.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Controllers\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using ProductPOC.Models; | ||
|
||
namespace ProductPOC.Repository | ||
{ | ||
public interface IProductRepository | ||
{ | ||
Task<IEnumerable<Product>> GetAllAsync(); | ||
Task<Product> GetByIdAsync(Guid id); | ||
Task<Product> CreateAsync(Product product); | ||
Task<Product> UpdateAsync(Guid id,Product product); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using MongoDB.Driver; | ||
using ProductPOC.DbContext; | ||
using ProductPOC.Models; | ||
|
||
namespace ProductPOC.Repository | ||
{ | ||
public class ProductRepository : IProductRepository | ||
{ | ||
private readonly IMongoCollection<Product> _products; | ||
public ProductRepository(ProductDbContext context) | ||
{ | ||
_products = context.Products; | ||
} | ||
|
||
public async Task<Product> CreateAsync(Product product) | ||
{ | ||
await _products.InsertOneAsync(product); | ||
return product; | ||
} | ||
|
||
public async Task<IEnumerable<Product>> GetAllAsync() => | ||
await _products.Find(p => true).ToListAsync(); | ||
|
||
public async Task<Product> GetByIdAsync(Guid id) | ||
{ | ||
return await _products.Find(x=>x.Id==id).FirstOrDefaultAsync(); | ||
} | ||
|
||
public async Task<Product> UpdateAsync(Guid id, Product product) | ||
{ | ||
var existingProduct = await _products.Find(x => x.Id == id).FirstOrDefaultAsync(); | ||
if (existingProduct == null) | ||
{ | ||
return null; | ||
} | ||
existingProduct.Name = product.Name; | ||
existingProduct.Description = product.Description; | ||
existingProduct.Price = product.Price; | ||
var result = await _products.ReplaceOneAsync(x => x.Id == id, existingProduct); | ||
return existingProduct; | ||
} | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using ProductPOC.Models; | ||
|
||
namespace ProductPOC.Service | ||
{ | ||
public interface IProductService | ||
{ | ||
Task<IEnumerable<Product>> GetAllProductsAsync(); | ||
Task<Product> GetByIdProductAsync(Guid id); | ||
Task<Product> CreateProductAsync(Product product); | ||
Task<Product> UpdateProductAsync(Guid id,Product product); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using ProductPOC.Models; | ||
using ProductPOC.Repository; | ||
|
||
namespace ProductPOC.Service | ||
{ | ||
public class ProductService : IProductService | ||
{ | ||
private readonly IProductRepository _productRepository; | ||
|
||
public ProductService(IProductRepository productRepository) | ||
{ | ||
_productRepository = productRepository; | ||
} | ||
|
||
public async Task<Product> CreateProductAsync(Product product) | ||
{ | ||
if(product.Id == Guid.Empty) | ||
{ | ||
product.Id = Guid.NewGuid(); | ||
} | ||
return await _productRepository.CreateAsync(product); | ||
} | ||
|
||
public async Task<IEnumerable<Product>> GetAllProductsAsync() | ||
{ | ||
return await _productRepository.GetAllAsync(); | ||
} | ||
|
||
public async Task<Product> GetByIdProductAsync(Guid id) | ||
{ | ||
return await _productRepository.GetByIdAsync(id); | ||
} | ||
|
||
public async Task<Product> UpdateProductAsync(Guid id, Product product) | ||
{ | ||
return await _productRepository.UpdateAsync(id, product); | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.