Skip to content

Commit 6add03b

Browse files
Refactor Put methods to simplify update logic
1 parent 537dfe6 commit 6add03b

File tree

9 files changed

+18
-52
lines changed

9 files changed

+18
-52
lines changed

src/Modules/Grand.Module.Api/Controllers/BrandController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,14 @@ public async Task<IActionResult> Post([FromBody] BrandDto model)
7070

7171
[EndpointDescription("Update entity in Brand")]
7272
[EndpointName("UpdateBrand")]
73-
[HttpPut("{key}")]
73+
[HttpPut]
7474
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
7575
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BrandDto))]
7676
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
77-
[ProducesResponseType((int)HttpStatusCode.NotFound)]
78-
public async Task<IActionResult> Put([FromRoute] string key, [FromBody] BrandDto model)
77+
public async Task<IActionResult> Put([FromBody] BrandDto model)
7978
{
8079
if (!await _permissionService.Authorize(PermissionSystemName.Brands)) return Forbid();
8180

82-
var brand = await _mediator.Send(new GetGenericQuery<BrandDto, Brand>(key));
83-
if (!brand.Any()) return NotFound();
84-
8581
model = await _mediator.Send(new UpdateBrandCommand { Model = model });
8682
return Ok(model);
8783
}

src/Modules/Grand.Module.Api/Controllers/CategoryController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,14 @@ public async Task<IActionResult> Post([FromBody] CategoryDto model)
7272

7373
[EndpointDescription("Update entity in Category")]
7474
[EndpointName("UpdateCategory")]
75-
[HttpPut("{key}")]
75+
[HttpPut]
7676
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
7777
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CategoryDto))]
7878
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
79-
[ProducesResponseType((int)HttpStatusCode.NotFound)]
80-
public async Task<IActionResult> Put([FromRoute] string key, [FromBody] CategoryDto model)
79+
public async Task<IActionResult> Put([FromBody] CategoryDto model)
8180
{
8281
if (!await _permissionService.Authorize(PermissionSystemName.Categories)) return Forbid();
8382

84-
var category = await _mediator.Send(new GetGenericQuery<CategoryDto, Category>(key));
85-
if (!category.Any()) return NotFound();
86-
8783
model = await _mediator.Send(new UpdateCategoryCommand { Model = model });
8884
return Ok(model);
8985
}

src/Modules/Grand.Module.Api/Controllers/CollectionController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,14 @@ public async Task<IActionResult> Post([FromBody] CollectionDto model)
7070

7171
[EndpointDescription("Update entity in Collection")]
7272
[EndpointName("UpdateCollection")]
73-
[HttpPut("{key}")]
73+
[HttpPut]
7474
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
7575
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CollectionDto))]
7676
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
77-
[ProducesResponseType((int)HttpStatusCode.NotFound)]
78-
public async Task<IActionResult> Put([FromRoute] string key, [FromBody] CollectionDto model)
77+
public async Task<IActionResult> Put([FromBody] CollectionDto model)
7978
{
8079
if (!await _permissionService.Authorize(PermissionSystemName.Collections)) return Forbid();
8180

82-
var collection = await _mediator.Send(new GetGenericQuery<CollectionDto, Collection>(key));
83-
if (!collection.Any()) return NotFound();
84-
8581
model = await _mediator.Send(new UpdateCollectionCommand { Model = model });
8682
return Ok(model);
8783
}

src/Modules/Grand.Module.Api/Controllers/CustomerController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,14 @@ public async Task<IActionResult> Post([FromBody] CustomerDto model)
6666

6767
[EndpointDescription("Update entity in Customer")]
6868
[EndpointName("UpdateCustomer")]
69-
[HttpPut("{email}")]
69+
[HttpPut]
7070
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
7171
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerDto))]
7272
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
73-
[ProducesResponseType((int)HttpStatusCode.NotFound)]
74-
public async Task<IActionResult> Put([FromRoute] string email, [FromBody] CustomerDto model)
73+
public async Task<IActionResult> Put([FromBody] CustomerDto model)
7574
{
7675
if (!await _permissionService.Authorize(PermissionSystemName.Customers)) return Forbid();
7776

78-
var customer = await _mediator.Send(new GetCustomerQuery { Email = email });
79-
if (customer == null) return NotFound();
80-
8177
model = await _mediator.Send(new UpdateCustomerCommand { Model = model });
8278
return Ok(model);
8379
}

src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,14 @@ public async Task<IActionResult> Post([FromBody] CustomerGroupDto model)
7070

7171
[EndpointDescription("Update entity in CustomerGroup")]
7272
[EndpointName("UpdateCustomerGroup")]
73-
[HttpPut("{key}")]
73+
[HttpPut]
7474
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
7575
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerGroupDto))]
7676
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
77-
[ProducesResponseType((int)HttpStatusCode.NotFound)]
78-
public async Task<IActionResult> Put([FromRoute] string key, [FromBody] CustomerGroupDto model)
77+
public async Task<IActionResult> Put([FromBody] CustomerGroupDto model)
7978
{
8079
if (!await _permissionService.Authorize(PermissionSystemName.Customers)) return Forbid();
8180

82-
var customerGroup = await _mediator.Send(new GetGenericQuery<CustomerGroupDto, CustomerGroup>(key));
83-
if (!customerGroup.Any()) return NotFound();
84-
8581
if (!model.IsSystem)
8682
{
8783
model = await _mediator.Send(new UpdateCustomerGroupCommand { Model = model });

src/Modules/Grand.Module.Api/Controllers/PictureController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,14 @@ public async Task<IActionResult> Post([FromBody] PictureDto model)
5555

5656
[EndpointDescription("Update entity in Picture")]
5757
[EndpointName("UpdatePicture")]
58-
[HttpPut("{key}")]
58+
[HttpPut]
5959
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
6060
[ProducesResponseType((int)HttpStatusCode.OK)]
6161
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
62-
[ProducesResponseType((int)HttpStatusCode.NotFound)]
63-
public async Task<IActionResult> Put([FromRoute] string key, [FromBody] PictureDto model)
62+
public async Task<IActionResult> Put([FromBody] PictureDto model)
6463
{
6564
if (!await _permissionService.Authorize(PermissionSystemName.Pictures)) return Forbid();
6665

67-
var picture = await _mediator.Send(new GetGenericQuery<PictureDto, Picture>(key));
68-
if (picture == null || !picture.Any()) return NotFound();
69-
7066
var result = await _mediator.Send(new UpdatePictureCommand { Model = model });
7167
return Ok(result);
7268
}

src/Modules/Grand.Module.Api/Controllers/ProductAttributeController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,14 @@ public async Task<IActionResult> Post([FromBody] ProductAttributeDto model)
7272

7373
[EndpointDescription("Update entity in ProductAttribute")]
7474
[EndpointName("UpdateProductAttribute")]
75-
[HttpPut("{key}")]
75+
[HttpPut]
7676
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
7777
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ProductAttributeDto))]
7878
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
79-
[ProducesResponseType((int)HttpStatusCode.NotFound)]
80-
public async Task<IActionResult> Put([FromRoute] string key, [FromBody] ProductAttributeDto model)
79+
public async Task<IActionResult> Put([FromBody] ProductAttributeDto model)
8180
{
8281
if (!await _permissionService.Authorize(PermissionSystemName.ProductAttributes)) return Forbid();
8382

84-
var productAttribute = await _mediator.Send(new GetGenericQuery<ProductAttributeDto, ProductAttribute>(key));
85-
if (!productAttribute.Any()) return NotFound();
86-
8783
model = await _mediator.Send(new UpdateProductAttributeCommand { Model = model });
8884
return Ok(model);
8985
}

src/Modules/Grand.Module.Api/Controllers/ProductController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,14 @@ public async Task<IActionResult> Post([FromBody] ProductDto model)
7272

7373
[EndpointDescription("Update entity in Product")]
7474
[EndpointName("UpdateProduct")]
75-
[HttpPut("{key}")]
75+
[HttpPut]
7676
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
7777
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ProductDto))]
78-
[ProducesResponseType((int)HttpStatusCode.NotFound)]
7978
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
80-
public async Task<IActionResult> Put([FromRoute] string key, [FromBody] ProductDto model)
79+
public async Task<IActionResult> Put([FromBody] ProductDto model)
8180
{
8281
if (!await _permissionService.Authorize(PermissionSystemName.Products)) return Forbid();
8382

84-
var product = await _mediator.Send(new GetGenericQuery<ProductDto, Product>(key));
85-
if (!product.Any()) return NotFound();
86-
8783
await _mediator.Send(new UpdateProductCommand { Model = model });
8884
return Ok();
8985
}

src/Modules/Grand.Module.Api/Controllers/SpecificationAttributeController.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,13 @@ public async Task<IActionResult> Post([FromBody] SpecificationAttributeDto model
7070

7171
[EndpointDescription("Update entity in SpecificationAttribute")]
7272
[EndpointName("UpdateSpecificationAttribute")]
73-
[HttpPut("{key}")]
73+
[HttpPut]
7474
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
7575
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SpecificationAttributeDto))]
7676
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
77-
public async Task<IActionResult> Put([FromRoute] string key, [FromBody] SpecificationAttributeDto model)
77+
public async Task<IActionResult> Put([FromBody] SpecificationAttributeDto model)
7878
{
7979
if (!await _permissionService.Authorize(PermissionSystemName.SpecificationAttributes)) return Forbid();
80-
var specification = await _mediator.Send(new GetGenericQuery<SpecificationAttributeDto, SpecificationAttribute>(key));
81-
if (!specification.Any()) return NotFound();
8280

8381
model = await _mediator.Send(new UpdateSpecificationAttributeCommand { Model = model });
8482
return Ok(model);

0 commit comments

Comments
 (0)