Ver código fonte

feat: add patch method

Mariano Z. 1 ano atrás
pai
commit
2ad74500ec

+ 13 - 6
Products.API/Controllers/ProductsController.cs

@@ -1,3 +1,4 @@
+using Microsoft.AspNetCore.JsonPatch;
 using Microsoft.AspNetCore.Mvc;
 using Products.Business.Service;
 using Products.Common.Dtos;
@@ -25,26 +26,32 @@ public class ProductsController : ControllerBase
     [HttpGet("{id}")]
     public ActionResult<ProductDto> Get(int id)
     {
-         return Ok(_productService.GetProduct(id));
-         
+        return Ok(_productService.GetProduct(id));
+
     }
-    
+
     [HttpPost]
     public ActionResult<ProductDto> Post(CreateProductDto product)
     {
         return Ok(_productService.AddProduct(product));
     }
-    
+
+    [HttpPatch("{id}")]
+    public ActionResult<ProductDto> Patch(int id, JsonPatchDocument<UpdateProductDto> product)
+    {
+        return Ok(_productService.PatchProduct(id, product));
+    }
+
     [HttpPut("{id}")]
     public ActionResult<ProductDto> Put(int id, UpdateProductDto product)
     {
         return Ok(_productService.UpdateProduct(id, product));
     }
-    
+
     [HttpDelete("{id}")]
     public ActionResult Delete(int id)
     {
         _productService.DeleteProduct(id);
         return Ok();
     }
-}
+}

+ 5 - 4
Products.API/Products.API.csproj

@@ -7,14 +7,15 @@
     </PropertyGroup>
 
     <ItemGroup>
-        <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3"/>
-        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2"/>
-        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
+        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.4" />
+        <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3" />
+        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
+        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
     </ItemGroup>
 
 
     <ItemGroup>
-        <Folder Include="Controllers\"/>
+        <Folder Include="Controllers\" />
     </ItemGroup>
 
 

+ 5 - 3
Products.API/Program.cs

@@ -14,14 +14,16 @@ internal class Program
 
 // Add services to the container.
 
-        builder.Services.AddControllers();
-// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
+        builder.Services.AddControllers().AddNewtonsoftJson();
+        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
         builder.Services.AddEndpointsApiExplorer();
         builder.Services.AddSwaggerGen();
         builder.Services.AddScoped<IProductService, ProductService>();
         builder.Services.AddScoped<IProductRepository, ProductRepository>();
         builder.Services.AddDbContext<DataContext>();
 
+
+
         builder.Services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
         builder.Services.AddCors(
             options =>
@@ -53,4 +55,4 @@ internal class Program
 
         app.Run();
     }
-}
+}

+ 0 - 0
Products.Business/AutoMapperProfiles/DomainToDtoProfile.cs


+ 0 - 0
Products.Business/AutoMapperProfiles/DtoToDomainProfile.cs


+ 2 - 1
Products.Business/Products.Business.csproj

@@ -11,7 +11,8 @@
     </ItemGroup>
 
     <ItemGroup>
-        <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.3"/>
+        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.4" />
+        <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.3" />
     </ItemGroup>
 
 </Project>

+ 3 - 1
Products.Business/Service/IProductService.cs

@@ -1,3 +1,4 @@
+using Microsoft.AspNetCore.JsonPatch;
 using Products.Common.Dtos;
 
 namespace Products.Business.Service;
@@ -7,6 +8,7 @@ public interface IProductService
     IEnumerable<ProductDto> GetProducts();
     ProductDto GetProduct(int id);
     ProductDto AddProduct(CreateProductDto product);
+    ProductDto PatchProduct(int id, JsonPatchDocument<UpdateProductDto> productDto);
     ProductDto UpdateProduct(int id, UpdateProductDto productDto);
     void DeleteProduct(int id);
-}
+}

+ 31 - 4
Products.Business/Service/ProductService.cs

@@ -1,4 +1,5 @@
-using Products.Business.Domain;
+using Microsoft.AspNetCore.JsonPatch;
+using Products.Business.Domain;
 using Products.Business.Repository;
 using Products.Common.Dtos;
 using Products.Common.Exceptions;
@@ -43,6 +44,15 @@ public class ProductService : IProductService
         return MapProductToProductDto(createdProduct);
     }
 
+
+    public void DeleteProduct(int id)
+    {
+        if (!_productRepository.ProductExists(id)) throw new RecordNotFoundException("Product not found");
+        _productRepository.DeleteProduct(id);
+    }
+
+
+
     public ProductDto UpdateProduct(int id, UpdateProductDto productDto)
     {
         var product = _productRepository.GetProduct(id);
@@ -57,12 +67,27 @@ public class ProductService : IProductService
         return GetProduct(id);
     }
 
-    public void DeleteProduct(int id)
+    public ProductDto PatchProduct(int id,
+            JsonPatchDocument<UpdateProductDto> productDto)
     {
-        if (!_productRepository.ProductExists(id)) throw new RecordNotFoundException("Product not found");
-        _productRepository.DeleteProduct(id);
+        var product = _productRepository.GetProduct(id);
+        if (product == null) throw new RecordNotFoundException("Product not found");
+
+        // Apply the patch
+        var update = new UpdateProductDto(product.Name, product.Color, product.Brand);
+        productDto.ApplyTo(update);
+
+        // Update the product
+        product.Name = update.Name;
+        product.Color = update.Color;
+        product.Brand = update.Brand;
+
+        _productRepository.UpdateProduct(product);
+
+        return this.GetProduct(id);
     }
 
+
     // TODO: This can be avoided using AutoMapper, also this should not be in this class
     private ProductDto MapProductToProductDto(Product product)
     {
@@ -73,4 +98,6 @@ public class ProductService : IProductService
             product.Id
         );
     }
+
+
 }

+ 0 - 2
Products.Common/Dtos/ProductDto.cs

@@ -1,5 +1,3 @@
-using Products.Common.Types;
-
 namespace Products.Common.Dtos;
 
 public record ProductDto(