Skip to content

Commit 1e6adef

Browse files
committed
remove reduntant checks
1 parent 8a1fc49 commit 1e6adef

File tree

8 files changed

+26
-62
lines changed

8 files changed

+26
-62
lines changed

NorthwindCRUD/Controllers/CategoriesController.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public CategoriesController(CategoryService categoryService, ProductService prod
2626

2727
[HttpGet]
2828
[Authorize]
29-
public ActionResult<Models.Dtos.CategoryDto[]> GetAll()
29+
public ActionResult<CategoryDto[]> GetAll()
3030
{
3131
try
3232
{
@@ -91,12 +91,7 @@ public ActionResult<ProductDto[]> GetProductsByCategoryId(int id)
9191
try
9292
{
9393
var products = this.productService.GetAllByCategoryId(id);
94-
if (products != null)
95-
{
96-
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
97-
}
98-
99-
return NotFound();
94+
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
10095
}
10196
catch (Exception error)
10297
{

NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id)
6969
try
7070
{
7171
var orders = this.orderService.GetOrdersByCustomerId(id);
72-
if (orders != null)
73-
{
74-
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
75-
}
76-
77-
return NotFound();
72+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
7873
}
7974
catch (Exception error)
8075
{

NorthwindCRUD/Controllers/EmployeesController.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,7 @@ public ActionResult<OrderDto[]> GetOrdersByEmployeeId(int id)
113113
try
114114
{
115115
var orders = this.ordersService.GetOrdersByEmployeeId(id);
116-
if (orders != null)
117-
{
118-
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
119-
}
120-
121-
return NotFound();
116+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
122117
}
123118
catch (Exception error)
124119
{
@@ -134,12 +129,7 @@ public ActionResult<EmployeeDto[]> GetTeritoriesByEmployeeId(int id)
134129
try
135130
{
136131
var teritories = this.employeeTerritoryService.GetTeritoriesByEmployeeId(id);
137-
if (teritories != null)
138-
{
139-
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(teritories));
140-
}
141-
142-
return NotFound();
132+
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(teritories));
143133
}
144134
catch (Exception error)
145135
{

NorthwindCRUD/Controllers/RegionsController.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ActionResult<RegionDto> GetById(int id)
6363
}
6464
}
6565

66-
[HttpGet("{id}/Territory")]
66+
[HttpGet("{id}/Territories")]
6767
[Authorize]
6868
public ActionResult<CustomerDto> GetTerritoryByRegionId(int id)
6969
{
@@ -73,10 +73,7 @@ public ActionResult<CustomerDto> GetTerritoryByRegionId(int id)
7373
if (region != null)
7474
{
7575
var territories = this.territoryService.GetTerritoriesByRegionId(id);
76-
if (territories != null)
77-
{
78-
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(territories));
79-
}
76+
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(territories));
8077
}
8178

8279
return NotFound();

NorthwindCRUD/Controllers/ShippersController.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ public ActionResult<OrderDto[]> GetOrdersByShipperId(int id)
7070
try
7171
{
7272
var orders = this.orderService.GetOrdersByShipperId(id);
73-
if (orders != null)
74-
{
75-
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
76-
}
77-
78-
return NotFound();
73+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
7974
}
8075
catch (Exception error)
8176
{

NorthwindCRUD/Controllers/SuppliersController.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ public ActionResult<ProductDto[]> GetProductsBySupplierId(int id)
7070
try
7171
{
7272
var products = this.productService.GetAllBySupplierId(id);
73-
if (products != null)
74-
{
75-
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
76-
}
77-
78-
return NotFound();
73+
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
7974
}
8075
catch (Exception error)
8176
{

NorthwindCRUD/Controllers/TerritoriesController.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,7 @@ public ActionResult<EmployeeDto[]> GetEmployeesByTerritory(string id)
7272
try
7373
{
7474
var employees = this.employeeTerritoryService.GetEmployeesByTerritoryId(id);
75-
76-
if (employees != null)
77-
{
78-
return Ok(this.mapper.Map<EmployeeDb[], EmployeeDto[]>(employees));
79-
}
80-
81-
return NotFound();
75+
return Ok(this.mapper.Map<EmployeeDb[], EmployeeDto[]>(employees));
8276
}
8377
catch (Exception error)
8478
{
@@ -98,7 +92,11 @@ public ActionResult<RegionDto[]> GetRegionByTerritory(string id)
9892
{
9993
var region = this.regionService.GetById(territory.RegionId);
10094

101-
return Ok(this.mapper.Map<RegionDb, RegionDto>(region));
95+
if (region != null)
96+
{
97+
return Ok(this.mapper.Map<RegionDb, RegionDto>(region));
98+
}
99+
102100
}
103101

104102
return NotFound();

NorthwindCRUD/DataContext.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,16 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3838
.WithMany(c => c.Products);
3939

4040
modelBuilder.Entity<ProductDb>()
41-
.HasOne(p => p.Supplier)
42-
.WithMany(c => c.Products);
41+
.HasOne(p => p.Supplier)
42+
.WithMany(c => c.Products);
4343

4444
modelBuilder.Entity<CustomerDb>()
4545
.HasOne(c => c.Address)
4646
.WithMany(a => a.Customers);
4747

48-
4948
modelBuilder.Entity<OrderDb>()
50-
.HasOne(o => o.ShipAddress)
51-
.WithMany(a => a.Orders);
49+
.HasOne(o => o.ShipAddress)
50+
.WithMany(a => a.Orders);
5251

5352
modelBuilder.Entity<OrderDb>()
5453
.HasOne(o => o.Customer)
@@ -63,20 +62,20 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
6362
.OnDelete(DeleteBehavior.Restrict);
6463

6564
modelBuilder.Entity<OrderDb>()
66-
.HasOne(o => o.Shipper)
67-
.WithMany(s => s.Orders);
65+
.HasOne(o => o.Shipper)
66+
.WithMany(s => s.Orders);
6867

6968
modelBuilder.Entity<OrderDetailDb>()
70-
.HasOne(od => od.Product)
71-
.WithMany(p => p.Details);
69+
.HasOne(od => od.Product)
70+
.WithMany(p => p.Details);
7271

7372
modelBuilder.Entity<OrderDetailDb>()
7473
.HasOne(o => o.Order)
7574
.WithMany(o => o.Details);
7675

7776
modelBuilder.Entity<EmployeeDb>()
78-
.HasOne(e => e.Address)
79-
.WithMany(a => a.Employees);
77+
.HasOne(e => e.Address)
78+
.WithMany(a => a.Employees);
8079

8180
modelBuilder.Entity<TerritoryDb>()
8281
.HasOne(t => t.Region)
@@ -96,7 +95,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
9695
.HasKey(et => new { et.EmployeeId, et.TerritoryId });
9796

9897
modelBuilder.Entity<OrderDetailDb>()
99-
.HasKey(et => new { et.ProductId, et.OrderId });
98+
.HasKey(et => new { et.ProductId, et.OrderId });
10099
}
101100
}
102101
}

0 commit comments

Comments
 (0)