Skip to content

Commit 0280af9

Browse files
authored
Merge pull request #11 from IgniteUI/DNenchev/26762-implementing-new-endpoints-and-refactor-northwind
Extending Northwind API
2 parents ee3fcb2 + ab47d77 commit 0280af9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+4623
-247
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Xml.Linq;
2+
3+
namespace NorthwindCRUD.Constants
4+
{
5+
public class StringTemplates
6+
{
7+
public const string InvalidEntityMessage = "{0} with id {1} does not exist!";
8+
}
9+
}

NorthwindCRUD/Controllers/AuthController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
66
using NorthwindCRUD.Models.DbModels;
7-
using NorthwindCRUD.Models.InputModels;
7+
using NorthwindCRUD.Models.Dtos;
88
using NorthwindCRUD.Services;
99

1010
[ApiController]
@@ -26,7 +26,7 @@ public AuthController(IConfiguration configuration, AuthService authService, IMa
2626

2727
[AllowAnonymous]
2828
[HttpPost("Login")]
29-
public ActionResult<string> Login(LoginInputModel userModel)
29+
public ActionResult<string> Login(LoginDto userModel)
3030
{
3131
try
3232
{
@@ -52,7 +52,7 @@ public ActionResult<string> Login(LoginInputModel userModel)
5252

5353
[AllowAnonymous]
5454
[HttpPost("Register")]
55-
public ActionResult<string> Register(RegisterInputModel userModel)
55+
public ActionResult<string> Register(RegisterDto userModel)
5656
{
5757
try
5858
{
@@ -68,7 +68,7 @@ public ActionResult<string> Register(RegisterInputModel userModel)
6868
return BadRequest("User does not exists!");
6969
}
7070

71-
var mappedModel = this.mapper.Map<RegisterInputModel, UserDb>(userModel);
71+
var mappedModel = this.mapper.Map<RegisterDto, UserDb>(userModel);
7272
var user = this.authService.RegisterUser(mappedModel);
7373

7474
if (user != null)

NorthwindCRUD/Controllers/CategoryController.cs renamed to NorthwindCRUD/Controllers/CategoriesController.cs

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,34 @@ namespace NorthwindCRUD.Controllers
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
66
using NorthwindCRUD.Models.DbModels;
7-
using NorthwindCRUD.Models.InputModels;
7+
using NorthwindCRUD.Models.Dtos;
88
using NorthwindCRUD.Services;
99

1010
[ApiController]
1111
[Route("[controller]")]
12-
public class CategoryController : ControllerBase
12+
public class CategoriesController : ControllerBase
1313
{
1414
private readonly CategoryService categoryService;
15+
private readonly ProductService productService;
1516
private readonly IMapper mapper;
1617
private readonly ILogger logger;
1718

18-
public CategoryController(CategoryService categoryService, IMapper mapper, ILogger logger)
19+
public CategoriesController(CategoryService categoryService, ProductService productService, IMapper mapper, ILogger logger)
1920
{
2021
this.categoryService = categoryService;
22+
this.productService = productService;
2123
this.mapper = mapper;
2224
this.logger = logger;
2325
}
2426

2527
[HttpGet]
2628
[Authorize]
27-
public ActionResult<CategoryInputModel[]> GetAll()
29+
public ActionResult<CategoryDto[]> GetAll()
2830
{
2931
try
3032
{
3133
var categories = this.categoryService.GetAll();
32-
return Ok(this.mapper.Map<CategoryDb[], CategoryInputModel[]>(categories));
34+
return base.Ok(this.mapper.Map<CategoryDb[], CategoryDto[]>(categories));
3335
}
3436
catch (Exception error)
3537
{
@@ -41,14 +43,14 @@ public ActionResult<CategoryInputModel[]> GetAll()
4143

4244
[HttpGet("{id}")]
4345
[Authorize]
44-
public ActionResult<CategoryInputModel> GetById(int id)
46+
public ActionResult<CategoryDto> GetById(int id)
4547
{
4648
try
4749
{
4850
var category = this.categoryService.GetById(id);
4951
if (category != null)
5052
{
51-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
53+
return base.Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
5254
}
5355

5456
return NotFound();
@@ -60,17 +62,55 @@ public ActionResult<CategoryInputModel> GetById(int id)
6062
}
6163
}
6264

65+
66+
[HttpGet("{id}/Details")]
67+
[Authorize]
68+
public ActionResult<CategoryDetailsDto> GetDetailsById(int id)
69+
{
70+
try
71+
{
72+
var category = this.categoryService.GetById(id);
73+
if (category != null)
74+
{
75+
return Ok(this.mapper.Map<CategoryDb, CategoryDetailsDto>(category));
76+
}
77+
78+
return NotFound();
79+
}
80+
catch (Exception error)
81+
{
82+
logger.LogError(error.Message);
83+
return StatusCode(500);
84+
}
85+
}
86+
87+
[HttpGet("{id}/Products")]
88+
[Authorize]
89+
public ActionResult<ProductDto[]> GetProductsByCategoryId(int id)
90+
{
91+
try
92+
{
93+
var products = this.productService.GetAllByCategoryId(id);
94+
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
95+
}
96+
catch (Exception error)
97+
{
98+
logger.LogError(error.Message);
99+
return StatusCode(500);
100+
}
101+
}
102+
63103
[HttpPost]
64104
[Authorize]
65-
public ActionResult<CategoryInputModel> Create(CategoryInputModel model)
105+
public ActionResult<CategoryDetailsDto> Create(CategoryDto model)
66106
{
67107
try
68108
{
69109
if (ModelState.IsValid)
70110
{
71-
var mappedModel = this.mapper.Map<CategoryInputModel, CategoryDb>(model);
111+
var mappedModel = this.mapper.Map<CategoryDto, CategoryDb>(model);
72112
var category = this.categoryService.Create(mappedModel);
73-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
113+
return Ok(this.mapper.Map<CategoryDb, CategoryDetailsDto>(category));
74114
}
75115

76116
return BadRequest(ModelState);
@@ -84,18 +124,18 @@ public ActionResult<CategoryInputModel> Create(CategoryInputModel model)
84124

85125
[HttpPut]
86126
[Authorize]
87-
public ActionResult<CategoryInputModel> Update(CategoryInputModel model)
127+
public ActionResult<CategoryDto> Update(CategoryDto model)
88128
{
89129
try
90130
{
91131
if (ModelState.IsValid)
92132
{
93-
var mappedModel = this.mapper.Map<CategoryInputModel, CategoryDb>(model);
133+
var mappedModel = this.mapper.Map<CategoryDto, CategoryDb>(model);
94134
var category = this.categoryService.Update(mappedModel);
95135

96136
if (category != null)
97137
{
98-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
138+
return base.Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
99139
}
100140

101141
return NotFound();
@@ -112,14 +152,14 @@ public ActionResult<CategoryInputModel> Update(CategoryInputModel model)
112152

113153
[HttpDelete("{id}")]
114154
[Authorize]
115-
public ActionResult<CategoryInputModel> Delete(int id)
155+
public ActionResult<CategoryDto> Delete(int id)
116156
{
117157
try
118158
{
119159
var category = this.categoryService.Delete(id);
120160
if (category != null)
121161
{
122-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
162+
return base.Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
123163
}
124164

125165
return NotFound();

NorthwindCRUD/Controllers/CustomerController.cs renamed to NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,34 @@
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
66
using NorthwindCRUD.Models.DbModels;
7-
using NorthwindCRUD.Models.InputModels;
7+
using NorthwindCRUD.Models.Dtos;
88
using NorthwindCRUD.Services;
99

1010
[ApiController]
1111
[Route("[controller]")]
12-
public class CustomerController : Controller
12+
public class CustomersController : Controller
1313
{
1414
private readonly CustomerService customerService;
15+
private readonly OrderService orderService;
1516
private readonly IMapper mapper;
1617
private readonly ILogger logger;
1718

18-
public CustomerController(CustomerService customerService, IMapper mapper, ILogger logger)
19+
public CustomersController(CustomerService customerService, OrderService orderService, IMapper mapper, ILogger logger)
1920
{
2021
this.customerService = customerService;
22+
this.orderService = orderService;
2123
this.mapper = mapper;
2224
this.logger = logger;
2325
}
2426

2527
[HttpGet]
2628
[Authorize]
27-
public ActionResult<CustomerInputModel[]> GetAll()
29+
public ActionResult<CustomerDto[]> GetAll()
2830
{
2931
try
3032
{
3133
var customers = this.customerService.GetAll();
32-
return Ok(this.mapper.Map<CustomerDb[], CustomerInputModel[]>(customers));
34+
return Ok(this.mapper.Map<CustomerDb[], CustomerDto[]>(customers));
3335
}
3436
catch (Exception error)
3537
{
@@ -40,15 +42,15 @@ public ActionResult<CustomerInputModel[]> GetAll()
4042

4143
[HttpGet("{id}")]
4244
[Authorize]
43-
public ActionResult<CustomerInputModel> GetById(string id)
45+
public ActionResult<CustomerDto> GetById(string id)
4446
{
4547
try
4648
{
4749
var customer = this.customerService.GetById(id);
4850

4951
if (customer != null)
5052
{
51-
return Ok(this.mapper.Map<CustomerDb, CustomerInputModel>(customer));
53+
return Ok(this.mapper.Map<CustomerDb, CustomerDto>(customer));
5254
}
5355

5456
return NotFound();
@@ -60,17 +62,33 @@ public ActionResult<CustomerInputModel> GetById(string id)
6062
}
6163
}
6264

65+
[HttpGet("{id}/Orders")]
66+
[Authorize]
67+
public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id)
68+
{
69+
try
70+
{
71+
var orders = this.orderService.GetOrdersByCustomerId(id);
72+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
73+
}
74+
catch (Exception error)
75+
{
76+
logger.LogError(error.Message);
77+
return StatusCode(500);
78+
}
79+
}
80+
6381
[HttpPost]
6482
[Authorize]
65-
public ActionResult<CustomerInputModel> Create(CustomerInputModel model)
83+
public ActionResult<CustomerDto> Create(CustomerDto model)
6684
{
6785
try
6886
{
6987
if (ModelState.IsValid)
7088
{
71-
var mappedModel = this.mapper.Map<CustomerInputModel, CustomerDb>(model);
89+
var mappedModel = this.mapper.Map<CustomerDto, CustomerDb>(model);
7290
var customer = this.customerService.Create(mappedModel);
73-
return Ok(this.mapper.Map<CustomerDb, CustomerInputModel>(customer));
91+
return Ok(this.mapper.Map<CustomerDb, CustomerDto>(customer));
7492
}
7593

7694
return BadRequest(ModelState);
@@ -84,18 +102,18 @@ public ActionResult<CustomerInputModel> Create(CustomerInputModel model)
84102

85103
[HttpPut]
86104
[Authorize]
87-
public ActionResult<CustomerInputModel> Update(CustomerInputModel model)
105+
public ActionResult<CustomerDto> Update(CustomerDto model)
88106
{
89107
try
90108
{
91109
if (ModelState.IsValid)
92110
{
93-
var mappedModel = this.mapper.Map<CustomerInputModel, CustomerDb>(model);
111+
var mappedModel = this.mapper.Map<CustomerDto, CustomerDb>(model);
94112
var customer = this.customerService.Update(mappedModel);
95113

96114
if (customer != null)
97115
{
98-
return Ok(this.mapper.Map<CustomerDb, CustomerInputModel>(customer));
116+
return Ok(this.mapper.Map<CustomerDb, CustomerDto>(customer));
99117
}
100118

101119
return NotFound();
@@ -112,14 +130,14 @@ public ActionResult<CustomerInputModel> Update(CustomerInputModel model)
112130

113131
[HttpDelete("{id}")]
114132
[Authorize]
115-
public ActionResult<CustomerInputModel> Delete(string id)
133+
public ActionResult<CustomerDto> Delete(string id)
116134
{
117135
try
118136
{
119137
var customer = this.customerService.Delete(id);
120138
if (customer != null)
121139
{
122-
return Ok(this.mapper.Map<CustomerDb, CustomerInputModel>(customer));
140+
return Ok(this.mapper.Map<CustomerDb, CustomerDto>(customer));
123141
}
124142

125143
return NotFound();

0 commit comments

Comments
 (0)