Skip to content

Commit 0fa7364

Browse files
committed
fix(put): update endpoints to include notfound
1 parent 36783e8 commit 0fa7364

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

NorthwindCRUD/Controllers/CategoryController.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public ActionResult<CategoryInputModel> GetById(int id)
4646
try
4747
{
4848
var category = this.categoryService.GetById(id);
49-
5049
if (category != null)
5150
{
5251
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
@@ -93,7 +92,13 @@ public ActionResult<CategoryInputModel> Update(CategoryInputModel model)
9392
{
9493
var mappedModel = this.mapper.Map<CategoryInputModel, CategoryDb>(model);
9594
var category = this.categoryService.Update(mappedModel);
96-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
95+
96+
if (category != null)
97+
{
98+
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
99+
}
100+
101+
return NotFound();
97102
}
98103

99104
return BadRequest(ModelState);
@@ -112,7 +117,12 @@ public ActionResult<CategoryInputModel> Delete(int id)
112117
try
113118
{
114119
var category = this.categoryService.Delete(id);
115-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
120+
if (category != null)
121+
{
122+
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
123+
}
124+
125+
return NotFound();
116126
}
117127
catch (Exception error)
118128
{

NorthwindCRUD/Controllers/CustomerController.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using AutoMapper;
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
6+
using NorthwindCRUD.Models.Contracts;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.InputModels;
89
using NorthwindCRUD.Services;
@@ -92,7 +93,13 @@ public ActionResult<CustomerInputModel> Update(CustomerInputModel model)
9293
{
9394
var mappedModel = this.mapper.Map<CustomerInputModel, CustomerDb>(model);
9495
var customer = this.customerService.Update(mappedModel);
95-
return Ok(this.mapper.Map<CustomerDb, CustomerInputModel>(customer));
96+
97+
if (customer != null)
98+
{
99+
return Ok(this.mapper.Map<CustomerDb, CustomerInputModel>(customer));
100+
}
101+
102+
return NotFound();
96103
}
97104

98105
return BadRequest(ModelState);
@@ -111,7 +118,12 @@ public ActionResult<CustomerInputModel> Delete(string id)
111118
try
112119
{
113120
var customer = this.customerService.Delete(id);
114-
return Ok(this.mapper.Map<CustomerDb, CustomerInputModel>(customer));
121+
if (customer != null)
122+
{
123+
return Ok(this.mapper.Map<CustomerDb, CustomerInputModel>(customer));
124+
}
125+
126+
return NotFound();
115127
}
116128
catch (Exception error)
117129
{

NorthwindCRUD/Controllers/EmployeeController.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using AutoMapper;
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
6+
using NorthwindCRUD.Models.Contracts;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.InputModels;
89
using NorthwindCRUD.Services;
@@ -111,7 +112,13 @@ public ActionResult<EmployeeInputModel> Delete(int id)
111112
try
112113
{
113114
var employee = this.employeeService.Delete(id);
114-
return Ok(this.mapper.Map<EmployeeDb, EmployeeInputModel>(employee));
115+
116+
if (employee != null)
117+
{
118+
return Ok(this.mapper.Map<EmployeeDb, EmployeeInputModel>(employee));
119+
}
120+
121+
return NotFound();
115122
}
116123
catch (Exception error)
117124
{

NorthwindCRUD/Controllers/OrderController.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ public ActionResult<OrderInputModel> Update(OrderInputModel model)
9292
{
9393
var mappedModel = this.mapper.Map<OrderInputModel, OrderDb>(model);
9494
var order = this.orderService.Update(mappedModel);
95-
return Ok(this.mapper.Map<OrderDb, OrderInputModel>(order));
95+
if (order != null)
96+
{
97+
return Ok(this.mapper.Map<OrderDb, OrderInputModel>(order));
98+
}
99+
100+
return NotFound();
96101
}
97102

98103
return BadRequest(ModelState);
@@ -111,7 +116,12 @@ public ActionResult<OrderInputModel> Delete(int id)
111116
try
112117
{
113118
var order = this.orderService.Delete(id);
114-
return Ok(this.mapper.Map<OrderDb, OrderInputModel>(order));
119+
if (order != null)
120+
{
121+
return Ok(this.mapper.Map<OrderDb, OrderInputModel>(order));
122+
}
123+
124+
return NotFound();
115125
}
116126
catch (Exception error)
117127
{

0 commit comments

Comments
 (0)