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

Added update functionality to Product. #73

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
}
}