Skip to content

Commit 75b1305

Browse files
committed
User queries added (Check if email exists and get user bu email)
1 parent d610f4f commit 75b1305

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

API.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Azure.Functions.Worker;
88
using Microsoft.Azure.Functions.Worker.Http;
99
using Microsoft.Extensions.Logging;
10+
using System.Web;
1011

1112
namespace MODSI_SQLRestAPI
1213
{
@@ -431,6 +432,49 @@ public async Task<HttpResponseData> UpdateUserById([HttpTrigger(AuthorizationLev
431432
}
432433
}
433434

435+
[Function("EmailUserExists")]
436+
437+
public async Task<HttpResponseData> EmailUserExists([HttpTrigger(AuthorizationLevel.Function, "get", Route = "User/EmailExists/{email}")] HttpRequestData req, string email)
438+
{
439+
try
440+
{
441+
_logger.LogInformation($"Checking if email exists: {email}");
442+
var exists = await _databaseHandler.EmailUserExistsAsync(email);
443+
var response = req.CreateResponse(HttpStatusCode.OK);
444+
response.Headers.Add("Content-Type", "application/json; charset=utf-8");
445+
await response.WriteStringAsync(JsonSerializer.Serialize(new { Exists = exists }));
446+
return response;
447+
}
448+
catch (Exception ex)
449+
{
450+
_logger.LogError(ex, $"An error occurred while checking if email exists: {email}");
451+
return req.CreateResponse(HttpStatusCode.InternalServerError);
452+
}
453+
}
454+
455+
[Function("GetUserByEmail")]
456+
public async Task<HttpResponseData> GetUserByEmail([HttpTrigger(AuthorizationLevel.Function, "get", Route = "User/GetByEmail/{email}")] HttpRequestData req, string email)
457+
{
458+
try
459+
{
460+
_logger.LogInformation($"Retrieving user with email: {email}");
461+
var user = await _databaseHandler.GetUserByEmailAsync(email);
462+
if (user == null)
463+
{
464+
return req.CreateResponse(HttpStatusCode.NotFound);
465+
}
466+
var response = req.CreateResponse(HttpStatusCode.OK);
467+
response.Headers.Add("Content-Type", "application/json; charset=utf-8");
468+
await response.WriteStringAsync(JsonSerializer.Serialize(user));
469+
return response;
470+
}
471+
catch (Exception ex)
472+
{
473+
_logger.LogError(ex, $"An error occurred while retrieving user with email {email}.");
474+
return req.CreateResponse(HttpStatusCode.InternalServerError);
475+
}
476+
}
477+
434478
#endregion
435479

436480

DatabaseHandler.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,55 @@ public async Task UpdateUserByIdAsync(User user)
370370
}
371371
}
372372

373+
public async Task<bool> EmailUserExistsAsync(string email)
374+
{
375+
using (SqlConnection conn = new SqlConnection(_connectionString))
376+
{
377+
await conn.OpenAsync();
378+
var query = "SELECT COUNT(*) FROM [user] WHERE Email = @Email";
379+
using (SqlCommand cmd = new SqlCommand(query, conn))
380+
{
381+
cmd.Parameters.AddWithValue("@Email", email);
382+
int count = (int)await cmd.ExecuteScalarAsync();
383+
return count > 0;
384+
}
385+
}
386+
}
387+
// Get user by email
388+
public async Task<User> GetUserByEmailAsync(string email)
389+
{
390+
User user = null;
391+
using (SqlConnection conn = new SqlConnection(_connectionString))
392+
{
393+
await conn.OpenAsync();
394+
var query = $"SELECT ID, Name, Email, Password, Username, Role, CreatedAt, IsActive FROM [user] WHERE Email = @Email";
395+
using (SqlCommand cmd = new SqlCommand(query, conn))
396+
{
397+
cmd.Parameters.AddWithValue("@Email", email);
398+
using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
399+
{
400+
if (await reader.ReadAsync())
401+
{
402+
user = new User
403+
{
404+
ID = reader.GetInt32(0),
405+
Name = reader.GetString(1),
406+
Email = reader.GetString(2),
407+
Password = reader.GetString(3),
408+
Username = reader.GetString(4),
409+
Role = reader.GetString(5),
410+
CreatedAt = reader.GetDateTime(6),
411+
IsActive = reader.GetString(7)
412+
};
413+
}
414+
}
415+
}
416+
}
417+
return user;
418+
}
419+
420+
421+
373422
#endregion
374423

375424
}

0 commit comments

Comments
 (0)