Skip to content

Commit b98af7c

Browse files
committed
Merge branch 'main' of https://github.com/Svetloslav15/NorthwindAPI into main
2 parents 86bd00a + 576bf14 commit b98af7c

File tree

92 files changed

+28031
-1249
lines changed

Some content is hidden

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

92 files changed

+28031
-1249
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+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
namespace NorthwindCRUD.Controllers
2+
{
3+
using AutoMapper;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
using NorthwindCRUD.Models.DbModels;
7+
using NorthwindCRUD.Models.Dtos;
8+
using NorthwindCRUD.Services;
9+
10+
[ApiController]
11+
[Route("[controller]")]
12+
public class AuthController : Controller
13+
{
14+
private readonly IConfiguration configuration;
15+
private readonly AuthService authService;
16+
private readonly IMapper mapper;
17+
private readonly ILogger logger;
18+
19+
public AuthController(IConfiguration configuration, AuthService authService, IMapper mapper, ILogger logger)
20+
{
21+
this.configuration = configuration;
22+
this.authService = authService;
23+
this.mapper = mapper;
24+
this.logger = logger;
25+
}
26+
27+
[AllowAnonymous]
28+
[HttpPost("Login")]
29+
public ActionResult<string> Login(LoginDto userModel)
30+
{
31+
try
32+
{
33+
if (ModelState.IsValid)
34+
{
35+
if (this.authService.IsAuthenticated(userModel.Email, userModel.Password))
36+
{
37+
var token = this.authService.GenerateJwtToken(userModel.Email);
38+
39+
return Ok(token);
40+
}
41+
return BadRequest("Email or password are not correct!");
42+
}
43+
44+
return BadRequest(ModelState);
45+
}
46+
catch (Exception error)
47+
{
48+
logger.LogError(error.Message);
49+
return StatusCode(500);
50+
}
51+
}
52+
53+
[AllowAnonymous]
54+
[HttpPost("Register")]
55+
public ActionResult<string> Register(RegisterDto userModel)
56+
{
57+
try
58+
{
59+
if (ModelState.IsValid)
60+
{
61+
if (userModel.Password != userModel.ConfirmedPassword)
62+
{
63+
return BadRequest("Passwords does not match!");
64+
}
65+
66+
if (this.authService.DoesUserExists(userModel.Email))
67+
{
68+
return BadRequest("User does not exists!");
69+
}
70+
71+
var mappedModel = this.mapper.Map<RegisterDto, UserDb>(userModel);
72+
var user = this.authService.RegisterUser(mappedModel);
73+
74+
if (user != null)
75+
{
76+
var token = this.authService.GenerateJwtToken(user.Email);
77+
return Ok(token);
78+
79+
}
80+
81+
return BadRequest("Email or password are not correct!");
82+
}
83+
84+
return BadRequest(ModelState);
85+
}
86+
catch (Exception error)
87+
{
88+
logger.LogError(error.Message);
89+
return StatusCode(500);
90+
}
91+
}
92+
}
93+
}

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

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
namespace NorthwindCRUD.Controllers
22
{
33
using AutoMapper;
4+
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
56
using NorthwindCRUD.Models.DbModels;
6-
using NorthwindCRUD.Models.InputModels;
7+
using NorthwindCRUD.Models.Dtos;
78
using NorthwindCRUD.Services;
89

910
[ApiController]
1011
[Route("[controller]")]
11-
public class CategoryController : ControllerBase
12+
public class CategoriesController : ControllerBase
1213
{
1314
private readonly CategoryService categoryService;
15+
private readonly ProductService productService;
1416
private readonly IMapper mapper;
1517
private readonly ILogger logger;
1618

17-
public CategoryController(CategoryService categoryService, IMapper mapper, ILogger logger)
19+
public CategoriesController(CategoryService categoryService, ProductService productService, IMapper mapper, ILogger logger)
1820
{
1921
this.categoryService = categoryService;
22+
this.productService = productService;
2023
this.mapper = mapper;
2124
this.logger = logger;
2225
}
2326

2427
[HttpGet]
25-
public ActionResult<CategoryInputModel[]> GetAll()
28+
public ActionResult<CategoryDto[]> GetAll()
2629
{
2730
try
2831
{
2932
var categories = this.categoryService.GetAll();
30-
return Ok(this.mapper.Map<CategoryDb[], CategoryInputModel[]>(categories));
33+
return base.Ok(this.mapper.Map<CategoryDb[], CategoryDto[]>(categories));
3134
}
3235
catch (Exception error)
3336
{
@@ -38,15 +41,35 @@ public ActionResult<CategoryInputModel[]> GetAll()
3841
}
3942

4043
[HttpGet("{id}")]
41-
public ActionResult<CategoryInputModel> GetById(int id)
44+
public ActionResult<CategoryDto> GetById(int id)
4245
{
4346
try
4447
{
4548
var category = this.categoryService.GetById(id);
49+
if (category != null)
50+
{
51+
return base.Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
52+
}
53+
54+
return NotFound();
55+
}
56+
catch (Exception error)
57+
{
58+
logger.LogError(error.Message);
59+
return StatusCode(500);
60+
}
61+
}
4662

63+
64+
[HttpGet("{id}/Details")]
65+
public ActionResult<CategoryDetailsDto> GetDetailsById(int id)
66+
{
67+
try
68+
{
69+
var category = this.categoryService.GetById(id);
4770
if (category != null)
4871
{
49-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
72+
return Ok(this.mapper.Map<CategoryDb, CategoryDetailsDto>(category));
5073
}
5174

5275
return NotFound();
@@ -58,16 +81,32 @@ public ActionResult<CategoryInputModel> GetById(int id)
5881
}
5982
}
6083

84+
[HttpGet("{id}/Products")]
85+
public ActionResult<ProductDto[]> GetProductsByCategoryId(int id)
86+
{
87+
try
88+
{
89+
var products = this.productService.GetAllByCategoryId(id);
90+
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
91+
}
92+
catch (Exception error)
93+
{
94+
logger.LogError(error.Message);
95+
return StatusCode(500);
96+
}
97+
}
98+
6199
[HttpPost]
62-
public ActionResult<CategoryInputModel> Create(CategoryInputModel model)
100+
[Authorize]
101+
public ActionResult<CategoryDetailsDto> Create(CategoryDto model)
63102
{
64103
try
65104
{
66105
if (ModelState.IsValid)
67106
{
68-
var mappedModel = this.mapper.Map<CategoryInputModel, CategoryDb>(model);
107+
var mappedModel = this.mapper.Map<CategoryDto, CategoryDb>(model);
69108
var category = this.categoryService.Create(mappedModel);
70-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
109+
return Ok(this.mapper.Map<CategoryDb, CategoryDetailsDto>(category));
71110
}
72111

73112
return BadRequest(ModelState);
@@ -80,15 +119,22 @@ public ActionResult<CategoryInputModel> Create(CategoryInputModel model)
80119
}
81120

82121
[HttpPut]
83-
public ActionResult<CategoryInputModel> Update(CategoryInputModel model)
122+
[Authorize]
123+
public ActionResult<CategoryDto> Update(CategoryDto model)
84124
{
85125
try
86126
{
87127
if (ModelState.IsValid)
88128
{
89-
var mappedModel = this.mapper.Map<CategoryInputModel, CategoryDb>(model);
129+
var mappedModel = this.mapper.Map<CategoryDto, CategoryDb>(model);
90130
var category = this.categoryService.Update(mappedModel);
91-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
131+
132+
if (category != null)
133+
{
134+
return base.Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
135+
}
136+
137+
return NotFound();
92138
}
93139

94140
return BadRequest(ModelState);
@@ -101,15 +147,15 @@ public ActionResult<CategoryInputModel> Update(CategoryInputModel model)
101147
}
102148

103149
[HttpDelete("{id}")]
104-
public ActionResult<CategoryInputModel> Delete(int id)
150+
[Authorize]
151+
public ActionResult<CategoryDto> Delete(int id)
105152
{
106153
try
107154
{
108155
var category = this.categoryService.Delete(id);
109-
110156
if (category != null)
111157
{
112-
return Ok(this.mapper.Map<CategoryDb, CategoryInputModel>(category));
158+
return base.Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
113159
}
114160

115161
return NotFound();

0 commit comments

Comments
 (0)