Skip to content

Commit

Permalink
Merge pull request #73 from trihugger/main
Browse files Browse the repository at this point in the history
Added update functionality to Product.
  • Loading branch information
iammukeshm committed Sep 27, 2021
2 parents b2c1217 + 6ec132c commit cd356cc
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 1 deletion.
35 changes: 34 additions & 1 deletion postman/dotnet.webapi.boilerplate.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "7bc4b7a3-1161-4775-946b-1c2d70f3c34b",
"_postman_id": "2059afd5-1d5c-4093-a29b-c3e16a45c202",
"name": "dotnet.webapi.boilerplate",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
Expand Down Expand Up @@ -252,6 +252,39 @@
}
},
"response": []
},
{
"name": "update-product",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"name\":\"Product Test to max name length to be exactly or around 75 which is the max\",\r\n \"description\":\"Something more Cool!\",\r\n \"rate\":5\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/v1/products?id=a5d1b2e5-295c-4ebd-a80e-fb6b82b1cb55",
"host": [
"{{url}}"
],
"path": [
"v1",
"products"
],
"query": [
{
"key": "id",
"value": "a5d1b2e5-295c-4ebd-a80e-fb6b82b1cb55"
}
]
}
},
"response": []
}
]
},
Expand Down
6 changes: 6 additions & 0 deletions src/Bootstrapper/Controllers/v1/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public async Task<IActionResult> CreateAsync(CreateProductRequest request)
return Ok(await _service.CreateProductAsync(request));
}

[HttpPut]
public async Task<IActionResult> UpdateAsync(CreateProductRequest request, Guid id)
{
return Ok(await _service.UpdateProductAsync(request, id));
}

[HttpDelete]
public async Task<IActionResult> DeleteAsync(Guid id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IProductService : ITransientService
Task<Result<ProductDetailsDto>> GetByIdUsingDapperAsync(Guid id);
Task<PaginatedResult<ProductDetailsDto>> GetListAsync(ProductListFilter filter);
Task<Result<object>> CreateProductAsync(CreateProductRequest request);
Task<Result<object>> UpdateProductAsync(CreateProductRequest request, Guid id);
Task<Result<Guid>> DeleteProductAsync(Guid id);
}
}
25 changes: 25 additions & 0 deletions src/Core/Application/Services/Catalog/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ public async Task<Result<object>> CreateProductAsync(CreateProductRequest reques
return await Result<object>.SuccessAsync(productId);
}

public async Task<Result<object>> UpdateProductAsync(CreateProductRequest request, Guid id)
{
var product = await _repository.GetByIdAsync<Product>(id);

if(product == null)
{
return await Result<object>.FailAsync("Product Id provided is invalid, it doesn't return a product.");
}

string productImagePath = string.Empty;

// If image is provided process upload
if (request.Image != null)
{
productImagePath = await _file.UploadAsync<Product>(request.Image, FileType.Image);
}

var updateProduct = product.Update(request.Name, request.Description, request.Rate, productImagePath);

await _repository.UpdateAsync<Product>(updateProduct);
await _repository.SaveChangesAsync();

return await Result<object>.SuccessAsync(updateProduct);
}

public async Task<Result<Guid>> DeleteProductAsync(Guid id)
{
await _repository.RemoveByIdAsync<Product>(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DN.WebApi.Application.Validators.General;
using DN.WebApi.Shared.DTOs.Catalog;
using FluentValidation;

namespace DN.WebApi.Application.Validators.Catalog
{
public class UpdateProductRequestValidator : CustomValidator<UpdateProductRequest>
{
public UpdateProductRequestValidator()
{
RuleFor(p => p.Name).MaximumLength(75).NotEmpty();
RuleFor(p => p.Rate).GreaterThanOrEqualTo(1).NotEqual(0);
RuleFor(p => p.Image).SetValidator(new FileUploadRequestValidator());
}
}
}
15 changes: 15 additions & 0 deletions src/Core/Domain/Entities/Catalog/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ protected Product()
{
}

public Product Update(string name, string description, decimal rate, string imagePath)
{
Name = name;
Description = description;
Rate = rate;

// Update image path if one is provided
if (!string.IsNullOrEmpty(imagePath))
{
ImagePath = imagePath;
}

return this;
}

public string Name { get; private set; }

public string Description { get; private set; }
Expand Down
12 changes: 12 additions & 0 deletions src/Shared/Shared.DTOs/Catalog/UpdateProductRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using DN.WebApi.Shared.DTOs.General.Requests;

namespace DN.WebApi.Shared.DTOs.Catalog
{
public class UpdateProductRequest : IMustBeValid
{
public string Name { get; set; }
public string Description { get; set; }
public decimal Rate { get; set; }
public FileUploadRequest Image { get; set; }
}
}

0 comments on commit cd356cc

Please sign in to comment.