Skip to content

Commit 347af3f

Browse files
authored
Merge pull request #90 from boolean-uk/80-backend---insert-missing-responsedtos-in-update-user-endpoint
80 backend insert missing responsedtos in update user endpoint
2 parents 86e7f4a + 59943bc commit 347af3f

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

exercise.tests/IntegrationTests/UserTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public async Task UpdateUserSuccess()
184184
}
185185

186186
[Test]
187-
public async Task UpdateUserNoContent()
187+
public async Task UpdateUserNullFieldsOnly()
188188
{
189189
var fieldsToUpdate = new Dictionary<string, object?>{};
190190

@@ -194,7 +194,7 @@ public async Task UpdateUserNoContent()
194194
int userId = 1;
195195
var response = await _client.PatchAsync($"/users/{userId}", content);
196196

197-
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
197+
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
198198
}
199199

200200
[Test]

exercise.wwwapi/Endpoints/UserEndpoints.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ private static IResult Register(RegisterRequestDTO request, IRepository<User> se
6464
// syntax checks
6565
// check valid password
6666
string validationResult = Validator.Password(request.password);
67-
if (validationResult != "Accepted") return TypedResults.BadRequest(new ResponseDTO<Object>() { Message = validationResult });
67+
if (validationResult != "Accepted") return TypedResults.BadRequest(new ResponseDTO<string>() { Message = validationResult });
6868
// check valid email
6969
string emailValidation = Validator.Email(request.email);
70-
if (emailValidation != "Accepted") return TypedResults.BadRequest(new ResponseDTO<Object>() { Message = emailValidation });
70+
if (emailValidation != "Accepted") return TypedResults.BadRequest(new ResponseDTO<string>() { Message = emailValidation });
7171

7272
// check if email is in database
7373
var emailExists = service.GetAllFiltered(q => q.Email == request.email);
74-
if (emailExists.Count() != 0) return Results.Conflict(new ResponseDTO<Object>() { Message = "Fail" });
74+
if (emailExists.Count() != 0) return Results.Conflict(new ResponseDTO<string>() { Message = "Fail" });
7575

7676

7777
string passwordHash = BCrypt.Net.BCrypt.HashPassword(request.password);
@@ -95,6 +95,7 @@ private static IResult Register(RegisterRequestDTO request, IRepository<User> se
9595

9696
[ProducesResponseType(StatusCodes.Status200OK)]
9797
[ProducesResponseType(StatusCodes.Status400BadRequest)]
98+
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
9899
private static IResult Login(LoginRequestDTO request, IRepository<User> service, IConfigurationSettings config, IMapper mapper)
99100
{
100101
//if (string.IsNullOrEmpty(request.username)) request.username = request.email;
@@ -108,7 +109,7 @@ private static IResult Login(LoginRequestDTO request, IRepository<User> service,
108109
//email doesn't exist, should probably be 404 user not found, but should maybe just say invalid email or password
109110
//check if email is in database
110111
var emailExists = service.GetAllFiltered(q => q.Email == request.email);
111-
if (emailExists.Count() == 0) return TypedResults.BadRequest(new ResponseDTO<Object>() { Message = "Invalid email and/or password provided"});
112+
if (emailExists.Count() == 0) return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "Invalid email and/or password provided"});
112113

113114

114115

@@ -117,8 +118,11 @@ private static IResult Login(LoginRequestDTO request, IRepository<User> service,
117118

118119
if (!BCrypt.Net.BCrypt.Verify(request.password, user.PasswordHash))
119120
{
120-
// should probably be 401 unauthorized
121-
return Results.BadRequest(new ResponseDTO<Object>() { Message = "Invalid email and/or password provided" });
121+
// TypedResults.Unauthorized did not support Message. "Custom" solution which includes message.
122+
return Results.Json(new ResponseDTO<string>
123+
{
124+
Message = "Invalid email and/or password provided"
125+
}, statusCode: StatusCodes.Status401Unauthorized);
122126
}
123127

124128
string token = CreateToken(user, config);
@@ -142,7 +146,7 @@ private static IResult Login(LoginRequestDTO request, IRepository<User> service,
142146
public static async Task<IResult> GetUserById(IRepository<User> service, int id, IMapper mapper)
143147
{
144148
var user = service.GetById(id);
145-
if (user == null) return TypedResults.NotFound();
149+
if (user == null) return TypedResults.NotFound(new ResponseDTO<string> { Message = "User not found" });
146150

147151
ResponseDTO<UserDTO> response = new ResponseDTO<UserDTO>
148152
{
@@ -154,48 +158,48 @@ public static async Task<IResult> GetUserById(IRepository<User> service, int id,
154158
}
155159

156160
[ProducesResponseType(StatusCodes.Status200OK)]
157-
[ProducesResponseType(StatusCodes.Status204NoContent)]
158161
[ProducesResponseType(StatusCodes.Status400BadRequest)]
159162
[ProducesResponseType(StatusCodes.Status404NotFound)]
160-
public static async Task<IResult> UpdateUser(IRepository<User> repository, int id, UserPatchDTO userPatch)
163+
public static async Task<IResult> UpdateUser(IRepository<User> repository, int id, UserPatchDTO userPatch, IMapper mapper)
161164
{
162-
if (userPatch.GetType().GetProperties().Length > 0 && userPatch.GetType().GetProperties().All((p) => p.GetValue(userPatch) == null)) return TypedResults.NoContent();
165+
if (userPatch.GetType().GetProperties().Length > 0 && userPatch.GetType().GetProperties().All((p) => p.GetValue(userPatch) == null))
166+
return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "Provide at least one field for update" });
163167

164168
var user = repository.GetById(id);
165169

166-
if (user == null) return TypedResults.NotFound();
170+
if (user == null) return TypedResults.NotFound(new ResponseDTO<string> { Message = "User not found" });
167171

168172
if (userPatch.Username != null && userPatch.Username != user.Username)
169173
{
170174
// Validate username
171-
if (Validator.Username(userPatch.Username) != "Accepted") return TypedResults.BadRequest("Invalid username");
175+
if (Validator.Username(userPatch.Username) != "Accepted") return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "Invalid username" });
172176
var usernameExists = repository.GetAllFiltered(q => q.Username == userPatch.Username);
173-
if (usernameExists.Count() != 0) return TypedResults.BadRequest("Username is already in use");
177+
if (usernameExists.Count() != 0) return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "Username is already in use" });
174178
// Update
175179
user.Username = userPatch.Username;
176180
}
177181
if (userPatch.GithubUsername != null && userPatch.GithubUsername != user.GithubUsername)
178182
{
179183
// Validate github username
180-
if (Validator.Username(userPatch.GithubUsername) != "Accepted") return TypedResults.BadRequest("Invalid GitHub username");
184+
if (Validator.Username(userPatch.GithubUsername) != "Accepted") return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "Invalid GitHub username" });
181185
var gitUsernameExists = repository.GetAllFiltered(q => q.GithubUsername == userPatch.GithubUsername);
182-
if (gitUsernameExists.Count() != 0) return TypedResults.BadRequest("GitHub username is already in use");
186+
if (gitUsernameExists.Count() != 0) return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "GitHub username is already in use" });
183187
// Update
184188
user.GithubUsername = userPatch.GithubUsername;
185189
}
186190
if (userPatch.Email != null && userPatch.Email != user.Email)
187191
{
188-
// Validate username
189-
if (Validator.Email(userPatch.Email) != "Accepted") return TypedResults.BadRequest("Invalid email");
192+
// Validate email
193+
if (Validator.Email(userPatch.Email) != "Accepted") return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "Invalid email" });
190194
var emailExists = repository.GetAllFiltered(q => q.Email == userPatch.Email);
191-
if (emailExists.Count() != 0) return TypedResults.BadRequest("Email is already in use");
195+
if (emailExists.Count() != 0) return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "Email is already in use" });
192196
// Update
193197
user.Email = userPatch.Email;
194198
}
195199
if (userPatch.Password != null)
196200
{
197201
// Validate username
198-
if (Validator.Password(userPatch.Password) != "Accepted") return TypedResults.BadRequest("Invalid password");
202+
if (Validator.Password(userPatch.Password) != "Accepted") return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "Invalid password" });
199203
// Hash
200204
string passwordHash = BCrypt.Net.BCrypt.HashPassword(userPatch.Username);
201205
// Update
@@ -208,7 +212,7 @@ public static async Task<IResult> UpdateUser(IRepository<User> repository, int i
208212
{
209213
if (userPatch.Role == 0) { user.Role = Roles.student; }
210214
else if (userPatch.Role == 1) { user.Role = Roles.teacher; }
211-
else {return TypedResults.BadRequest("Role does not exist");}
215+
else {return TypedResults.BadRequest(new ResponseDTO<string>() { Message = "Role does not exist" });}
212216
}
213217
if (userPatch.Specialism != null) user.Specialism = userPatch.Specialism;
214218
// TODO: Add cohort support after implementing the Cohort model and adding it to user.
@@ -220,7 +224,13 @@ public static async Task<IResult> UpdateUser(IRepository<User> repository, int i
220224
repository.Update(user);
221225
repository.Save();
222226

223-
return TypedResults.Ok(userPatch);
227+
ResponseDTO<UserDTO> response = new ResponseDTO<UserDTO>
228+
{
229+
Message = "success",
230+
Data = mapper.Map<UserDTO>(user)
231+
};
232+
233+
return TypedResults.Ok(response);
224234
}
225235
private static string CreateToken(User user, IConfigurationSettings config)
226236
{

0 commit comments

Comments
 (0)