Skip to content

Commit 7209e8b

Browse files
committed
Move id outside of the Put endpoint body for categories
1 parent 093fb69 commit 7209e8b

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

NorthwindCRUD/Controllers/CategoriesController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,16 @@ public ActionResult<CategoryDetailsDto> Create(CategoryDto model)
198198
}
199199
}
200200

201-
[HttpPut]
201+
[HttpPut("{id}")]
202202
[Authorize]
203-
public ActionResult<CategoryDto> Update(CategoryDto model)
203+
public ActionResult<CategoryDto> Update(int id, CategoryUpdateDto model)
204204
{
205205
try
206206
{
207207
if (ModelState.IsValid)
208208
{
209-
var mappedModel = this.mapper.Map<CategoryDto, CategoryDb>(model);
210-
var category = this.categoryService.Update(mappedModel);
209+
var mappedModel = this.mapper.Map<CategoryUpdateDto, CategoryDb>(model);
210+
var category = this.categoryService.Update(id, mappedModel);
211211

212212
if (category != null)
213213
{

NorthwindCRUD/ControllersGraphQL/CategoryController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public CategoryDto Create(CategoryDto model)
5050
}
5151

5252
[Mutation]
53-
public CategoryDto? Update(CategoryDto model)
53+
public CategoryDto? Update(int id, CategoryUpdateDto model)
5454
{
55-
var mappedModel = this.mapper.Map<CategoryDto, CategoryDb>(model);
56-
var category = this.categoryService.Update(mappedModel);
55+
var mappedModel = this.mapper.Map<CategoryUpdateDto, CategoryDb>(model);
56+
var category = this.categoryService.Update(id, mappedModel);
5757

5858
return category != null ? this.mapper.Map<CategoryDb, CategoryDto>(category) : null;
5959
}

NorthwindCRUD/Helpers/MappingProfiles.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class MappingProfiles : Profile
99
public MappingProfiles()
1010
{
1111
CreateMap<CategoryDto, CategoryDb>().ReverseMap();
12+
CreateMap<CategoryUpdateDto, CategoryDb>().ReverseMap();
1213
CreateMap<CategoryDb, CategoryDetailsDto>();
1314

1415
CreateMap<ProductDto, ProductDb>().ReverseMap();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace NorthwindCRUD.Models.Dtos
4+
{
5+
public class CategoryUpdateDto
6+
{
7+
[StringLength(500, ErrorMessage = "Description cannot exceed 500 characters.")]
8+
public string Description { get; set; }
9+
10+
[Required(ErrorMessage = "Name is required.")]
11+
[StringLength(50, MinimumLength = 3, ErrorMessage = "Name must be between 3 and 50 characters.")]
12+
public string Name { get; set; }
13+
}
14+
}

NorthwindCRUD/Services/CategoryService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public CategoryDb Create(CategoryDb model)
4848
return categoryEntity.Entity;
4949
}
5050

51-
public CategoryDb? Update(CategoryDb model)
51+
public CategoryDb? Update(int id, CategoryDb model)
5252
{
53-
var categoryEntity = this.dataContext.Categories.FirstOrDefault(c => c.CategoryId == model.CategoryId);
53+
var categoryEntity = this.dataContext.Categories.FirstOrDefault(c => c.CategoryId == id);
5454
if (categoryEntity != null)
5555
{
5656
categoryEntity.Description = model.Description != null ? model.Description : categoryEntity.Description;

0 commit comments

Comments
 (0)