13
13
using ShareBook . Infra . CrossCutting . Identity . Interfaces ;
14
14
using ShareBook . Service ;
15
15
16
- namespace ShareBook . Api . Controllers {
17
- [ Route ( "api/[controller]" ) ]
18
- [ EnableCors ( "AllowAllHeaders" ) ]
16
+ namespace ShareBook . Api . Controllers
17
+ {
18
+ [ Route ( "api/[controller]" ) ]
19
+ [ EnableCors ( "AllowAllHeaders" ) ]
19
20
[ GetClaimsFilter ]
20
- public class AccountController : Controller {
21
+ public class AccountController : Controller
22
+ {
21
23
private readonly IUserService _userService ;
22
24
private readonly IApplicationSignInManager _signManager ;
23
25
24
- public AccountController ( IUserService userService , IApplicationSignInManager signManager ) {
26
+ public AccountController ( IUserService userService , IApplicationSignInManager signManager )
27
+ {
25
28
_userService = userService ;
26
29
_signManager = signManager ;
27
30
}
28
31
29
32
#region GET
30
- [ Authorize ( "Bearer" ) ]
33
+ [ Authorize ( "Bearer" ) ]
31
34
[ HttpGet ]
32
- public UserVM Get ( ) {
33
- var id = new Guid ( Thread . CurrentPrincipal ? . Identity ? . Name ) ;
34
- var user = _userService . Find ( id ) ;
35
+ public UserVM Get ( )
36
+ {
37
+ var id = new Guid ( Thread . CurrentPrincipal ? . Identity ? . Name ) ;
38
+ var user = _userService . Find ( id ) ;
35
39
36
- var userVM = Mapper . Map < User , UserVM > ( user ) ;
40
+ var userVM = Mapper . Map < User , UserVM > ( user ) ;
37
41
return userVM ;
38
42
}
39
43
40
- [ Authorize ( "Bearer" ) ]
41
- [ HttpGet ( "Profile" ) ]
42
- public object Profile ( ) {
43
- var id = new Guid ( Thread . CurrentPrincipal ? . Identity ? . Name ) ;
44
- return new { profile = _userService . Find ( id ) . Profile . ToString ( ) } ;
44
+ [ Authorize ( "Bearer" ) ]
45
+ [ HttpGet ( "Profile" ) ]
46
+ public object Profile ( )
47
+ {
48
+ var id = new Guid ( Thread . CurrentPrincipal ? . Identity ? . Name ) ;
49
+ return new { profile = _userService . Find ( id ) . Profile . ToString ( ) } ;
45
50
}
46
51
47
- [ Authorize ( "Bearer" ) ]
48
- [ HttpGet ( "ListFacilitators/{userIdDonator}" ) ]
49
- public IActionResult ListFacilitators ( Guid userIdDonator ) {
50
- var facilitators = _userService . GetFacilitators ( userIdDonator ) ;
51
- var facilitatorsClean = Mapper . Map < List < UserFacilitatorVM > > ( facilitators ) ;
52
- return Ok ( facilitatorsClean ) ;
52
+ [ Authorize ( "Bearer" ) ]
53
+ [ HttpGet ( "ListFacilitators/{userIdDonator}" ) ]
54
+ public IActionResult ListFacilitators ( Guid userIdDonator )
55
+ {
56
+ var facilitators = _userService . GetFacilitators ( userIdDonator ) ;
57
+ var facilitatorsClean = Mapper . Map < List < UserFacilitatorVM > > ( facilitators ) ;
58
+ return Ok ( facilitatorsClean ) ;
53
59
}
54
60
#endregion
55
61
56
62
#region POST
57
- [ HttpPost ( "Register" ) ]
58
- [ ProducesResponseType ( typeof ( object ) , 200 ) ]
59
- [ ProducesResponseType ( 409 ) ]
60
- public IActionResult Post ( [ FromBody ] RegisterUserVM registerUserVM , [ FromServices ] SigningConfigurations signingConfigurations , [ FromServices ] TokenConfigurations tokenConfigurations ) {
63
+ [ HttpPost ( "Register" ) ]
64
+ [ ProducesResponseType ( typeof ( object ) , 200 ) ]
65
+ [ ProducesResponseType ( 409 ) ]
66
+ public IActionResult Post ( [ FromBody ] RegisterUserVM registerUserVM , [ FromServices ] SigningConfigurations signingConfigurations , [ FromServices ] TokenConfigurations tokenConfigurations )
67
+ {
61
68
if ( ! ModelState . IsValid )
62
- return BadRequest ( ) ;
69
+ return BadRequest ( ) ;
63
70
64
- var user = Mapper . Map < RegisterUserVM , User > ( registerUserVM ) ;
71
+ var user = Mapper . Map < RegisterUserVM , User > ( registerUserVM ) ;
65
72
66
- var result = _userService . Insert ( user ) ;
73
+ var result = _userService . Insert ( user ) ;
67
74
68
75
if ( result . Success )
69
- return Ok ( _signManager . GenerateTokenAndSetIdentity ( result . Value , signingConfigurations , tokenConfigurations ) ) ;
76
+ return Ok ( _signManager . GenerateTokenAndSetIdentity ( result . Value , signingConfigurations , tokenConfigurations ) ) ;
70
77
71
- return Conflict ( result ) ;
78
+ return Conflict ( result ) ;
72
79
}
73
80
74
- [ HttpPost ( "Login" ) ]
75
- [ ProducesResponseType ( typeof ( object ) , 200 ) ]
76
- [ ProducesResponseType ( 404 ) ]
77
- public IActionResult Login ( [ FromBody ] LoginUserVM loginUserVM , [ FromServices ] SigningConfigurations signingConfigurations , [ FromServices ] TokenConfigurations tokenConfigurations ) {
81
+ [ HttpPost ( "Login" ) ]
82
+ [ ProducesResponseType ( typeof ( object ) , 200 ) ]
83
+ [ ProducesResponseType ( 404 ) ]
84
+ public IActionResult Login ( [ FromBody ] LoginUserVM loginUserVM , [ FromServices ] SigningConfigurations signingConfigurations , [ FromServices ] TokenConfigurations tokenConfigurations )
85
+ {
78
86
if ( ! ModelState . IsValid )
79
- return BadRequest ( ) ;
87
+ return BadRequest ( ) ;
80
88
81
- var user = Mapper . Map < LoginUserVM , User > ( loginUserVM ) ;
89
+ var user = Mapper . Map < LoginUserVM , User > ( loginUserVM ) ;
82
90
83
- var result = _userService . AuthenticationByEmailAndPassword ( user ) ;
91
+ var result = _userService . AuthenticationByEmailAndPassword ( user ) ;
84
92
85
- if ( result . Success ) {
86
- var response = new Result {
87
- Value = _signManager . GenerateTokenAndSetIdentity ( result . Value , signingConfigurations , tokenConfigurations )
93
+ if ( result . Success )
94
+ {
95
+ var response = new Result
96
+ {
97
+ Value = _signManager . GenerateTokenAndSetIdentity ( result . Value , signingConfigurations , tokenConfigurations )
88
98
} ;
89
99
90
- return Ok ( response ) ;
100
+ return Ok ( response ) ;
91
101
}
92
102
93
- return NotFound ( result ) ;
103
+ return NotFound ( result ) ;
94
104
}
95
105
96
- [ HttpPost ( "ForgotMyPassword" ) ]
97
- [ ProducesResponseType ( typeof ( Result ) , 200 ) ]
98
- [ ProducesResponseType ( 404 ) ]
99
- public IActionResult ForgotMyPassword ( [ FromBody ] ForgotMyPasswordVM forgotMyPasswordVM ) {
100
- var result = _userService . GenerateHashCodePasswordAndSendEmailToUser ( forgotMyPasswordVM . Email ) ;
106
+ [ HttpPost ( "ForgotMyPassword" ) ]
107
+ [ ProducesResponseType ( typeof ( Result ) , 200 ) ]
108
+ [ ProducesResponseType ( 404 ) ]
109
+ public IActionResult ForgotMyPassword ( [ FromBody ] ForgotMyPasswordVM forgotMyPasswordVM )
110
+ {
111
+ var result = _userService . GenerateHashCodePasswordAndSendEmailToUser ( forgotMyPasswordVM . Email ) ;
101
112
102
113
if ( result . Success )
103
- return Ok ( result ) ;
114
+ return Ok ( result ) ;
104
115
105
- return NotFound ( result ) ;
116
+ return NotFound ( result ) ;
106
117
}
107
118
108
119
#endregion
109
120
110
121
#region PUT
111
- [ Authorize ( "Bearer" ) ]
122
+ [ Authorize ( "Bearer" ) ]
112
123
[ HttpPut ]
113
- [ ProducesResponseType ( typeof ( Result < User > ) , 200 ) ]
114
- [ ProducesResponseType ( 409 ) ]
115
- public IActionResult Update ( [ FromBody ] UpdateUserVM updateUserVM , [ FromServices ] SigningConfigurations signingConfigurations , [ FromServices ] TokenConfigurations tokenConfigurations ) {
124
+ [ ProducesResponseType ( typeof ( Result < User > ) , 200 ) ]
125
+ [ ProducesResponseType ( 409 ) ]
126
+ public IActionResult Update ( [ FromBody ] UpdateUserVM updateUserVM , [ FromServices ] SigningConfigurations signingConfigurations , [ FromServices ] TokenConfigurations tokenConfigurations )
127
+ {
116
128
if ( ! ModelState . IsValid )
117
- return BadRequest ( ) ;
129
+ return BadRequest ( ) ;
118
130
119
- var user = Mapper . Map < UpdateUserVM , User > ( updateUserVM ) ;
131
+ var user = Mapper . Map < UpdateUserVM , User > ( updateUserVM ) ;
120
132
121
- user . Id = new Guid ( Thread . CurrentPrincipal ? . Identity ? . Name ) ;
133
+ user . Id = new Guid ( Thread . CurrentPrincipal ? . Identity ? . Name ) ;
122
134
123
- var result = _userService . Update ( user ) ;
135
+ var result = _userService . Update ( user ) ;
124
136
125
137
if ( ! result . Success )
126
- return Conflict ( result ) ;
138
+ return Conflict ( result ) ;
127
139
128
- return Ok ( _signManager . GenerateTokenAndSetIdentity ( result . Value , signingConfigurations , tokenConfigurations ) ) ;
140
+ return Ok ( _signManager . GenerateTokenAndSetIdentity ( result . Value , signingConfigurations , tokenConfigurations ) ) ;
129
141
}
130
142
131
- [ Authorize ( "Bearer" ) ]
132
- [ HttpPut ( "ChangePassword" ) ]
133
- public Result < User > ChangePassword ( [ FromBody ] ChangePasswordUserVM changePasswordUserVM ) {
134
- var user = new User ( ) { Password = changePasswordUserVM . OldPassword } ;
135
- user . Id = new Guid ( Thread . CurrentPrincipal ? . Identity ? . Name ) ;
136
- return _userService . ValidOldPasswordAndChangeUserPassword ( user , changePasswordUserVM . NewPassword ) ;
143
+ [ Authorize ( "Bearer" ) ]
144
+ [ HttpPut ( "ChangePassword" ) ]
145
+ public Result < User > ChangePassword ( [ FromBody ] ChangePasswordUserVM changePasswordUserVM )
146
+ {
147
+ var user = new User ( ) { Password = changePasswordUserVM . OldPassword } ;
148
+ user . Id = new Guid ( Thread . CurrentPrincipal ? . Identity ? . Name ) ;
149
+ return _userService . ValidOldPasswordAndChangeUserPassword ( user , changePasswordUserVM . NewPassword ) ;
137
150
}
138
151
139
- [ HttpPut ( "ChangeUserPasswordByHashCode" ) ]
140
- [ ProducesResponseType ( typeof ( Result < User > ) , 200 ) ]
141
- [ ProducesResponseType ( 404 ) ]
142
- public IActionResult ChangeUserPasswordByHashCode ( [ FromBody ] ChangeUserPasswordByHashCodeVM changeUserPasswordByHashCodeVM ) {
143
- var result = _userService . ConfirmHashCodePassword ( changeUserPasswordByHashCodeVM . HashCodePassword ) ;
152
+ [ HttpPut ( "ChangeUserPasswordByHashCode" ) ]
153
+ [ ProducesResponseType ( typeof ( Result < User > ) , 200 ) ]
154
+ [ ProducesResponseType ( 404 ) ]
155
+ public IActionResult ChangeUserPasswordByHashCode ( [ FromBody ] ChangeUserPasswordByHashCodeVM changeUserPasswordByHashCodeVM )
156
+ {
157
+ var result = _userService . ConfirmHashCodePassword ( changeUserPasswordByHashCodeVM . HashCodePassword ) ;
144
158
if ( ! result . Success )
145
- return NotFound ( result ) ;
159
+ return NotFound ( result ) ;
146
160
var newPassword = changeUserPasswordByHashCodeVM . NewPassword ;
147
161
var user = _userService . Find ( ( result . Value as User ) . Id ) ;
148
162
user . Password = newPassword ;
149
163
150
164
var resultChangePasswordUser = _userService . ChangeUserPassword ( user , newPassword ) ;
151
-
165
+
152
166
if ( ! resultChangePasswordUser . Success )
153
- return BadRequest ( resultChangePasswordUser ) ;
167
+ return BadRequest ( resultChangePasswordUser ) ;
154
168
155
- return Ok ( resultChangePasswordUser ) ;
169
+ return Ok ( resultChangePasswordUser ) ;
156
170
}
157
171
#endregion
158
-
159
- [ HttpGet ]
160
- [ Route ( "ForceException" ) ]
161
- public IActionResult ForceException ( ) {
162
- var teste = 1 / Convert . ToInt32 ( "Teste" ) ;
163
- return BadRequest ( ) ;
164
- }
165
172
}
166
173
}
0 commit comments