diff --git a/Web.Api/Controllers/AdminController.cs b/Web.Api/Controllers/AdminController.cs index 6cec37d..8809bc5 100644 --- a/Web.Api/Controllers/AdminController.cs +++ b/Web.Api/Controllers/AdminController.cs @@ -28,63 +28,78 @@ public AdminController(IOptions statusChangeOptions, TaskManagerAp [HttpPost("/AddStatus", Name = "AddStatus")] public async Task> AddStatus() { - using (logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - Status pendingStatus = new() { Id = statusChange.PendingId, Name = statusChange.Pending, Code = statusChange.Code1 }; - Status completedStatus = new() { Id = statusChange.CompleteId, Name = statusChange.Complete, Code = statusChange.Code2 }; + using (logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + { + Status pendingStatus = new() { Id = statusChange.PendingId, Name = statusChange.Pending, Code = statusChange.Code1 }; + Status completedStatus = new() { Id = statusChange.CompleteId, Name = statusChange.Complete, Code = statusChange.Code2 }; - context.Add(pendingStatus); - context.Add(completedStatus); + context.Add(pendingStatus); + context.Add(completedStatus); - await context.SaveChangesAsync(); - logger.LogInformation("Pending and completed status saved to database"); - return Ok("Status' Added"); + await context.SaveChangesAsync(); + logger.LogInformation("Pending and completed status saved to database"); + return Ok("Status' Added"); + } + } + catch (Exception ex) + { + logger.LogError($"Adding status process failed: {ex.Message}"); + return StatusCode(500); } } [HttpPost("/Refresh", Name = "Refresh")] public async Task> AddDummyData() { - - using (logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - CancellationTokenSource source = new CancellationTokenSource(); - CancellationToken token = source.Token; + using (logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + { + CancellationTokenSource source = new CancellationTokenSource(); + CancellationToken token = source.Token; - context.Database.ExecuteSqlRaw(""" - delete from SubTasks - delete from TaskItemStatusHistory - delete from TaskWithinList - delete from TaskItemNotes - delete from Lists - delete from TaskItems - delete from Statuses - delete from users - """); - logger.LogInformation("Successfully removed all previous data in database"); + context.Database.ExecuteSqlRaw(""" + delete from SubTasks + delete from TaskItemStatusHistory + delete from TaskWithinList + delete from TaskItemNotes + delete from Lists + delete from TaskItems + delete from Statuses + delete from users + """); + logger.LogInformation("Successfully removed all previous data in database"); - Status pendingStatus = new() { Id = statusChange.PendingId, Name = statusChange.Pending, Code = statusChange.Code1 }; - Status completedStatus = new() { Id = statusChange.CompleteId, Name = statusChange.Complete, Code = statusChange.Code2 }; + Status pendingStatus = new() { Id = statusChange.PendingId, Name = statusChange.Pending, Code = statusChange.Code1 }; + Status completedStatus = new() { Id = statusChange.CompleteId, Name = statusChange.Complete, Code = statusChange.Code2 }; - context.Add(pendingStatus); - context.Add(completedStatus); - logger.LogInformation("Successfully added pending and completed status'"); + context.Add(pendingStatus); + context.Add(completedStatus); + logger.LogInformation("Successfully added pending and completed status'"); - UserDirector userDirector = new UserDirector(statusChange); - context.AddRange([ - userDirector.MakeAlexFarmerProfile(), - userDirector.MakeJessieHopkinsProfile(), - userDirector.MakeAprilRiceProfile(), - userDirector.MakeNikoLoganProfile(), - userDirector.MakeChuckFinleyProfile(), - userDirector.MakeIrenePetersonProfile(), - ]); - logger.LogInformation("Successfully created all dummy data"); + UserDirector userDirector = new UserDirector(statusChange); + context.AddRange([ + userDirector.MakeAlexFarmerProfile(), + userDirector.MakeJessieHopkinsProfile(), + userDirector.MakeAprilRiceProfile(), + userDirector.MakeNikoLoganProfile(), + userDirector.MakeChuckFinleyProfile(), + userDirector.MakeIrenePetersonProfile(), + ]); + logger.LogInformation("Successfully created all dummy data"); - await context.SaveChangesAsync(token); - logger.LogInformation("Successfully saved all changes to database"); - return Ok("Dummy Data Added"); + await context.SaveChangesAsync(token); + logger.LogInformation("Successfully saved all changes to database"); + return Ok("Dummy Data Added"); + } + } + catch (Exception ex) + { + logger.LogError($"Refresh process failed: {ex.Message}"); + return StatusCode(500); } } } diff --git a/Web.Api/Controllers/ListController.cs b/Web.Api/Controllers/ListController.cs index 5f28d1c..46a41ac 100644 --- a/Web.Api/Controllers/ListController.cs +++ b/Web.Api/Controllers/ListController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; @@ -25,39 +26,47 @@ public class ListController : ControllerBase [HttpPost(Name = "CreateList")] public async Task> CreateList([FromHeader] Guid userId, ListCreateDto createListDto) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation($"Initiating Create List method"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"User {userId} not authorized"); - return StatusCode(403); - } + _logger.LogInformation($"Initiating Create List method"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"User {userId} not authorized"); + return StatusCode(403); + } - List? createList = new List - { - Id = Guid.NewGuid(), - Name = createListDto.Name, - CreatedDate = DateTime.Now, - CreatedUserId = userId, - }; + List? createList = new List + { + Id = Guid.NewGuid(), + Name = createListDto.Name, + CreatedDate = DateTime.Now, + CreatedUserId = userId, + }; - await _unitOfWork.List.CreateList(createList); // add the list // sending information to the database - await _unitOfWork.SaveChangesAsync(); - _logger.LogInformation($"List creation is successful for user {userId}"); + await _unitOfWork.List.CreateList(createList); // add the list // sending information to the database + await _unitOfWork.SaveChangesAsync(); + _logger.LogInformation($"List creation is successful for user {userId}"); - ListDto listDtos = new ListDto // should we use shortlistDto? - { - Id = createList.Id, - Name = createList.Name, - CreatedDate = createList.CreatedDate, - CreatedUserId = createList.CreatedUserId, + ListDto listDtos = new ListDto // should we use shortlistDto? + { + Id = createList.Id, + Name = createList.Name, + CreatedDate = createList.CreatedDate, + CreatedUserId = createList.CreatedUserId, - TaskItems = [] + TaskItems = [] - }; - _logger.LogInformation($"Returning the newly created list for user {userId}"); - return Ok(listDtos); + }; + _logger.LogInformation($"Returning the newly created list for user {userId}"); + return Ok(listDtos); + } + } + catch (Exception ex) + { + _logger.LogError($"Create list process failed: {ex.Message}"); + return StatusCode(500); } } @@ -65,71 +74,87 @@ public async Task> CreateList([FromHeader] Guid userId, Li [HttpGet("{listId}", Name = "GetListById")] public async Task> GetListById([FromHeader] Guid userId, Guid listId) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Innitiating GetListById"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"UserId {userId} not authorized"); - return StatusCode(403); - } + _logger.LogInformation("Innitiating GetListById"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"UserId {userId} not authorized"); + return StatusCode(403); + } - List? list = await _unitOfWork.List.GetListByIdAsync(listId, userId); - if (list is null) - { - _logger.LogWarning($"ListId {listId} not found for UserId {userId}"); - return NotFound(listId); - } + List? list = await _unitOfWork.List.GetListByIdAsync(listId, userId); + if (list is null) + { + _logger.LogWarning($"ListId {listId} not found for UserId {userId}"); + return NotFound(listId); + } - ListDto listDtos = new ListDto - { - Id = list.Id, - Name = list.Name, - CreatedDate = list.CreatedDate, - CreatedUserId = list.CreatedUserId, - - TaskItems = list.TaskWithinLists.Select(twl => new TaskDto - { - Id = twl.TaskItem.Id, - Title = twl.TaskItem.Title, - DueDate = twl.TaskItem.DueDate, - Priority = twl.TaskItem.Priority, - CreatedDate = twl.TaskItem.CreatedDate, - CreatedUserId = twl.TaskItem.CreatedUserId, - }).ToArray() - - }; - _logger.LogInformation($"GetListById method successful for ListId {listId} and UserId {userId}"); - _logger.LogInformation("Returning get list by Id result"); - return Ok(listDtos); + ListDto listDtos = new ListDto + { + Id = list.Id, + Name = list.Name, + CreatedDate = list.CreatedDate, + CreatedUserId = list.CreatedUserId, + + TaskItems = list.TaskWithinLists.Select(twl => new TaskDto + { + Id = twl.TaskItem.Id, + Title = twl.TaskItem.Title, + DueDate = twl.TaskItem.DueDate, + Priority = twl.TaskItem.Priority, + CreatedDate = twl.TaskItem.CreatedDate, + CreatedUserId = twl.TaskItem.CreatedUserId, + }).ToArray() + + }; + _logger.LogInformation($"GetListById method successful for ListId {listId} and UserId {userId}"); + _logger.LogInformation("Returning get list by Id result"); + return Ok(listDtos); + } + } + catch (Exception ex) + { + _logger.LogError($"Get list by id process failed: {ex.Message}"); + return StatusCode(500); } } [HttpGet(Name = "GetAllList")] public async Task>> GetAllList([FromHeader] Guid userId) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) - { - _logger.LogInformation("Innitiating GetAllList"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + try + { + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"UserId {userId} not authorized"); - return StatusCode(403); - } + _logger.LogInformation("Innitiating GetAllList"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"UserId {userId} not authorized"); + return StatusCode(403); + } - List userLists = await _unitOfWork.List.GetAllListAsync(userId); + List userLists = await _unitOfWork.List.GetAllListAsync(userId); - List getListDetail = userLists.Select(sl => new ShortListDto - { - Id = sl.Id, - Name = sl.Name, - CreatedDate = sl.CreatedDate, - CreatedUserId = sl.CreatedUserId, - }).ToList(); - - _logger.LogInformation($"GetAllList method successful for UserId {userId}"); - _logger.LogInformation("Returning get all lists result"); - return Ok(getListDetail); + List getListDetail = userLists.Select(sl => new ShortListDto + { + Id = sl.Id, + Name = sl.Name, + CreatedDate = sl.CreatedDate, + CreatedUserId = sl.CreatedUserId, + }).ToList(); + + _logger.LogInformation($"GetAllList method successful for UserId {userId}"); + _logger.LogInformation("Returning get all lists result"); + return Ok(getListDetail); + } + } + catch (Exception ex) + { + _logger.LogError($"Get all lists process failed: {ex.Message}"); + return StatusCode(500); } } @@ -143,35 +168,44 @@ public Task> MoveTaskToList([FromHeader] Guid userId, Guid [HttpPut("{listId}/edit-list", Name = "Edit List")] public async Task> EditList([FromHeader] Guid userId, Guid listId, EditListDto editListDto) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating Edit List Method"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) { - _logger.LogInformation($"UserId {userId} not authorized"); - return StatusCode(403); - } - - List? userList = await _unitOfWork.List.GetListByIdAsync(listId, userId); - if (userList != null) - { - userList.Name = editListDto.Title; - await _unitOfWork.SaveChangesAsync(); - } - else + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"List Id {listId} not found for user {userId}"); - return NotFound(listId); - } - _logger.LogInformation($"Edit list is successful for user {userId}"); + _logger.LogInformation("Initiating Edit List Method"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogInformation($"UserId {userId} not authorized"); + return StatusCode(403); + } - EditListResDto editListResDto = new EditListResDto - { - Id = listId, - Name = userList.Name, - CreatedUserId = userId, - }; - _logger.LogInformation($"Returning the newly edited list for user {userId}"); - return Ok(editListResDto); + List? userList = await _unitOfWork.List.GetListByIdAsync(listId, userId); + if (userList != null) + { + userList.Name = editListDto.Title; + await _unitOfWork.SaveChangesAsync(); + } + else + { + _logger.LogWarning($"List Id {listId} not found for user {userId}"); + return NotFound(listId); + } + _logger.LogInformation($"Edit list is successful for user {userId}"); + + EditListResDto editListResDto = new EditListResDto + { + Id = listId, + Name = userList.Name, + CreatedUserId = userId, + }; + _logger.LogInformation($"Returning the newly edited list for user {userId}"); + return Ok(editListResDto); + } + } + catch (Exception ex) + { + _logger.LogError($"Edit list process failed: {ex.Message}"); + return StatusCode(500); } } @@ -179,35 +213,45 @@ public async Task> EditList([FromHeader] Guid userId, Guid [HttpDelete("{listId}", Name = "DeleteList")] public async Task> DeleteList([FromHeader] Guid userId, Guid listId) { - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + try { - return StatusCode(403); - } + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + { + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + return StatusCode(403); + } - List? list = await _unitOfWork.List.GetListByIdAsync(listId, userId); - if (list is null) - { - return NotFound(listId); - } - //checks if there is any items within the list being deleted. - if (list.TaskWithinLists.Any()) - { - return BadRequest(); - } + List? list = await _unitOfWork.List.GetListByIdAsync(listId, userId); + if (list is null) + { + return NotFound(listId); + } + //checks if there is any items within the list being deleted. + if (list.TaskWithinLists.Any()) + { + return BadRequest(); + } - _unitOfWork.List.DeleteList(list); - await _unitOfWork.SaveChangesAsync(); + _unitOfWork.List.DeleteList(list); + await _unitOfWork.SaveChangesAsync(); - ListDto deletelist = new ListDto + ListDto deletelist = new ListDto + { + Id = list.Id, + Name = list.Name, + CreatedDate = list.CreatedDate, + CreatedUserId = list.CreatedUserId, + TaskItems = [] + }; + return Ok(deletelist); // fix the returnvalue + } + } + catch (Exception ex) { - Id = list.Id, - Name = list.Name, - CreatedDate = list.CreatedDate, - CreatedUserId = list.CreatedUserId, - TaskItems=[] - }; - - return Ok(deletelist); // fix the returnvalue + _logger.LogError($"Delete list process failed: {ex.Message}"); + return StatusCode(500); + } } } } diff --git a/Web.Api/Controllers/TaskController.cs b/Web.Api/Controllers/TaskController.cs index dd364fa..d87b624 100644 --- a/Web.Api/Controllers/TaskController.cs +++ b/Web.Api/Controllers/TaskController.cs @@ -26,180 +26,204 @@ public class TaskController : ControllerBase [HttpGet("{taskId}", Name = "GetTaskById")] public async Task> GetTaskById([FromHeader] Guid userId, Guid taskId) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating GetTaskById method"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"User {userId} not authorized"); - return StatusCode(403); - } + _logger.LogInformation("Initiating GetTaskById method"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"User {userId} not authorized"); + return StatusCode(403); + } - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); - if (taskItem is null) - { - _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); - return NotFound(taskId); - } + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + if (taskItem is null) + { + _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); + return NotFound(taskId); + } - TaskDto? taskDetail = new TaskDto //create a new instance of TaskDto and set their properties - { - Id = taskItem.Id, - Title = taskItem.Title, - DueDate = taskItem.DueDate, - Priority = taskItem.Priority, - CreatedDate = taskItem.CreatedDate, - CreatedUserId = taskItem.CreatedUserId, - Notes = taskItem.TaskItemNotes.Select //within the TaskDto create a new List of Notes that grabs TaskItemNotes and set their properties - (note => new NoteDto //create new instance of NoteDto - { - Id = note.Id, - TaskItemId = note.TaskItemId, - Note = note.Note, - CreatedDate = note.CreatedDate, - CreatedUser = note.CreatedUserId, - }).ToList(), //add notes to the list - - CurrentStatus = taskItem.TaskItemStatusHistories.OrderByDescending(rank => rank.CreatedDate) //within the TaskDto create a new list of CurrentStatus that grabs task histories and set their properites - .Select(history => new StatusDto //create new instance of StatusDto - { - Id = history.Status.Id, - Name = history.Status.Name, - Code = history.Status.Code, - }).First(), - }; - _logger.LogInformation($"GetTaskById method successful for TaskId {taskId} and UserId {userId}"); - _logger.LogInformation("Returning get task by Id result"); - return Ok(taskDetail); + TaskDto? taskDetail = new TaskDto //create a new instance of TaskDto and set their properties + { + Id = taskItem.Id, + Title = taskItem.Title, + DueDate = taskItem.DueDate, + Priority = taskItem.Priority, + CreatedDate = taskItem.CreatedDate, + CreatedUserId = taskItem.CreatedUserId, + Notes = taskItem.TaskItemNotes.Select //within the TaskDto create a new List of Notes that grabs TaskItemNotes and set their properties + (note => new NoteDto //create new instance of NoteDto + { + Id = note.Id, + TaskItemId = note.TaskItemId, + Note = note.Note, + CreatedDate = note.CreatedDate, + CreatedUser = note.CreatedUserId, + }).ToList(), //add notes to the list + + CurrentStatus = taskItem.TaskItemStatusHistories.OrderByDescending(rank => rank.CreatedDate) //within the TaskDto create a new list of CurrentStatus that grabs task histories and set their properites + .Select(history => new StatusDto //create new instance of StatusDto + { + Id = history.Status.Id, + Name = history.Status.Name, + Code = history.Status.Code, + }).First(), + }; + _logger.LogInformation($"GetTaskById method successful for TaskId {taskId} and UserId {userId}"); + _logger.LogInformation("Returning get task by Id result"); + return Ok(taskDetail); + } + } + catch (Exception ex) + { + _logger.LogError($"Get task by id process failed: {ex.Message}"); + return StatusCode(500); } } [HttpPost(Name = "CreateTask")] public async Task> CreateTask([FromHeader] Guid userId, TaskCreateDto taskCreatedDto) { - using (_logger.BeginScope(new Dictionary{["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating CreateTask method"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) - { - _logger.LogWarning($"UserId {userId} not authorized"); - return StatusCode(403); - } - - //calls the TaskItem prop and set the task created dto to its prop - //Request DTO - //create a new instance of TaskItem - //calls the TaskItem prop and set the task created dto to its prop - TaskItem? taskCreation = new TaskItem() + using (_logger.BeginScope(new Dictionary{["TransactionId"] = HttpContext.TraceIdentifier, })) { - Title = taskCreatedDto.Title, - Priority = taskCreatedDto.Priority, - CreatedDate = DateTime.Now, - CreatedUserId = userId, //set the UserId which is given by the user from the header - TaskItemStatusHistories = [ - new TaskItemStatusHistory() { - StatusId = _statusChange.PendingId, - CreatedDate = DateTime.Now, - CreatedUserId = userId - } - ] - }; - - if (taskCreatedDto.DueDate == null) - { - taskCreation.DueDate = new DateTime(1900, 1, 1); //Default if null - } - { - taskCreation.DueDate = taskCreatedDto.DueDate.Value; //enetered value - } + _logger.LogInformation("Initiating CreateTask method"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"UserId {userId} not authorized"); + return StatusCode(403); + } - await _unitOfWork.TaskItem.CreateTaskAsync(taskCreation); //UofW takes the TaskItem class and calls the CreateTask method from the TaskItemRepo - await _unitOfWork.SaveChangesAsync(); //UofW calls the SaveChanges method - taskCreation = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskCreation.Id, userId); + //calls the TaskItem prop and set the task created dto to its prop + //Request DTO + //create a new instance of TaskItem + //calls the TaskItem prop and set the task created dto to its prop + TaskItem? taskCreation = new TaskItem() + { + Title = taskCreatedDto.Title, + Priority = taskCreatedDto.Priority, + CreatedDate = DateTime.Now, + CreatedUserId = userId, //set the UserId which is given by the user from the header + TaskItemStatusHistories = [ + new TaskItemStatusHistory() { + StatusId = _statusChange.PendingId, + CreatedDate = DateTime.Now, + CreatedUserId = userId + } + ] + }; + + if (taskCreatedDto.DueDate == null) + { + taskCreation.DueDate = new DateTime(1900, 1, 1); //Default if null + } + { + taskCreation.DueDate = taskCreatedDto.DueDate.Value; //enetered value + } - _logger.LogInformation($"Task Creation is Successfull for userId {userId}"); + await _unitOfWork.TaskItem.CreateTaskAsync(taskCreation); //UofW takes the TaskItem class and calls the CreateTask method from the TaskItemRepo + await _unitOfWork.SaveChangesAsync(); //UofW calls the SaveChanges method + taskCreation = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskCreation.Id, userId); - //Response DTO - //create a new instance of TaskDto - //calls the TaskDto prop and call the taskCreation and set the prop for user view - //return the result of the tasks created - TaskDto creationResult = new TaskDto() - { - Id = taskCreation.Id, - Title = taskCreation.Title, - DueDate = taskCreation.DueDate, - Priority = taskCreation.Priority, + _logger.LogInformation($"Task Creation is Successfull for userId {userId}"); - Notes = taskCreation.TaskItemNotes.Select - (note => new NoteDto - { - Id = note.Id, - TaskItemId = note.TaskItemId, - Note = note.Note, - CreatedDate = note.CreatedDate, - CreatedUser = note.CreatedUserId, - }).ToList(), - - CurrentStatus = taskCreation.TaskItemStatusHistories.OrderByDescending(rank => rank.CreatedDate) - .Select(history => new StatusDto + //Response DTO + //create a new instance of TaskDto + //calls the TaskDto prop and call the taskCreation and set the prop for user view + //return the result of the tasks created + TaskDto creationResult = new TaskDto() { - Id = history.Status.Id, - Name = history.Status.Name, - Code = history.Status.Code, - }).First(), - - CreatedDate = taskCreation.CreatedDate, - CreatedUserId = taskCreation.CreatedUserId - }; - _logger.LogInformation($"Created task result for TaskId {taskCreation.Id} and UserId {userId}"); - _logger.LogInformation("Returning the created task result"); - return CreatedAtAction(nameof(CreateTask), new { taskId = taskCreation.Id }, creationResult); + Id = taskCreation.Id, + Title = taskCreation.Title, + DueDate = taskCreation.DueDate, + Priority = taskCreation.Priority, + + Notes = taskCreation.TaskItemNotes.Select + (note => new NoteDto + { + Id = note.Id, + TaskItemId = note.TaskItemId, + Note = note.Note, + CreatedDate = note.CreatedDate, + CreatedUser = note.CreatedUserId, + }).ToList(), + + CurrentStatus = taskCreation.TaskItemStatusHistories.OrderByDescending(rank => rank.CreatedDate) + .Select(history => new StatusDto + { + Id = history.Status.Id, + Name = history.Status.Name, + Code = history.Status.Code, + }).First(), + + CreatedDate = taskCreation.CreatedDate, + CreatedUserId = taskCreation.CreatedUserId + }; + _logger.LogInformation($"Created task result for TaskId {taskCreation.Id} and UserId {userId}"); + _logger.LogInformation("Returning the created task result"); + return CreatedAtAction(nameof(CreateTask), new { taskId = taskCreation.Id }, creationResult); + } + } + catch (Exception ex) + { + _logger.LogError($"Create task process failed: {ex.Message}"); + return StatusCode(500); } } [HttpPost("{taskId}/notes", Name = "CreateNote")] public async Task> CreateNote([FromHeader] Guid userId, Guid taskId, NoteCreateDto noteCreateDto) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating CreateNote method"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"UserId {userId} not authorized"); - return StatusCode(403); - } + _logger.LogInformation("Initiating CreateNote method"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"UserId {userId} not authorized"); + return StatusCode(403); + } - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); - if (taskItem is null) - { - _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); - return NotFound(taskId); - } + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + if (taskItem is null) + { + _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); + return NotFound(taskId); + } - TaskItemNote noteCreation = new TaskItemNote - { - TaskItemId = taskId, - Note = noteCreateDto.NoteText, - CreatedDate = DateTime.Now, - CreatedUserId = userId - }; - - await _unitOfWork.TaskItem.CreateNoteAsync(noteCreation); - await _unitOfWork.SaveChangesAsync(); - _logger.LogInformation($"Note Creation is Successfull for userId {userId}"); - - //Response DTO - var noteResult = new NoteDto - { - Id = noteCreation.Id, - TaskItemId = noteCreation.TaskItemId, - Note = noteCreation.Note, - CreatedDate = noteCreation.CreatedDate, - CreatedUser = noteCreation.CreatedUserId - }; - _logger.LogInformation($"Created note result for NoteId {noteCreation.Id} and UserId {userId}"); - _logger.LogInformation("Returning the created note result"); - return CreatedAtAction(nameof(CreateNote), new { id = noteCreation.Id }, noteResult); + TaskItemNote noteCreation = new TaskItemNote + { + TaskItemId = taskId, + Note = noteCreateDto.NoteText, + CreatedDate = DateTime.Now, + CreatedUserId = userId + }; + + await _unitOfWork.TaskItem.CreateNoteAsync(noteCreation); + await _unitOfWork.SaveChangesAsync(); + _logger.LogInformation($"Note Creation is Successfull for userId {userId}"); + + //Response DTO + var noteResult = new NoteDto + { + Id = noteCreation.Id, + TaskItemId = noteCreation.TaskItemId, + Note = noteCreation.Note, + CreatedDate = noteCreation.CreatedDate, + CreatedUser = noteCreation.CreatedUserId + }; + _logger.LogInformation($"Created note result for NoteId {noteCreation.Id} and UserId {userId}"); + _logger.LogInformation("Returning the created note result"); + return CreatedAtAction(nameof(CreateNote), new { id = noteCreation.Id }, noteResult); + } + } + catch (Exception ex) + { + _logger.LogError($"Create note process failed: {ex.Message}"); + return StatusCode(500); } } @@ -212,161 +236,185 @@ public Task>> GetAllNotes([FromHeader] Guid userId, G [HttpDelete("{taskId}/notes/{noteId}", Name = "DeleteNote")] public async Task> DeleteNote([FromHeader] Guid userId, Guid taskId, Guid noteId) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating DeleteNote method"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"UserId {userId} not authorized"); - return StatusCode(403); - } + _logger.LogInformation("Initiating DeleteNote method"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"UserId {userId} not authorized"); + return StatusCode(403); + } - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); - if (taskItem is null) - { - _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); - return NotFound(taskId); - } + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + if (taskItem is null) + { + _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); + return NotFound(taskId); + } - TaskItemNote? note = taskItem.TaskItemNotes.SingleOrDefault(n => n.Id == noteId); - if (note is null) - { - _logger.LogWarning($"NoteId {noteId} not found for TaskId {taskId} and UserId {userId}"); - return NotFound(noteId); - } + TaskItemNote? note = taskItem.TaskItemNotes.SingleOrDefault(n => n.Id == noteId); + if (note is null) + { + _logger.LogWarning($"NoteId {noteId} not found for TaskId {taskId} and UserId {userId}"); + return NotFound(noteId); + } - _unitOfWork.TaskItem.DeleteNote(note); - await _unitOfWork.SaveChangesAsync(); - _logger.LogInformation($"Note Deletion is Successfull for userId {userId}"); + _unitOfWork.TaskItem.DeleteNote(note); + await _unitOfWork.SaveChangesAsync(); + _logger.LogInformation($"Note Deletion is Successfull for userId {userId}"); - NoteDto deleteNote = new NoteDto - { - Id = note.Id, - TaskItemId = taskId, - Note = note.Note, - CreatedDate = note.CreatedDate, - CreatedUser = note.CreatedUserId, - }; - _logger.LogInformation($"Deleted note result for NoteId {note.Id}, TaskId {taskId} and UserId {userId}"); - _logger.LogInformation("Returning the deleted note result"); - return Ok(deleteNote); + NoteDto deleteNote = new NoteDto + { + Id = note.Id, + TaskItemId = taskId, + Note = note.Note, + CreatedDate = note.CreatedDate, + CreatedUser = note.CreatedUserId, + }; + _logger.LogInformation($"Deleted note result for NoteId {note.Id}, TaskId {taskId} and UserId {userId}"); + _logger.LogInformation("Returning the deleted note result"); + return Ok(deleteNote); + } + } + catch (Exception ex) + { + _logger.LogError($"Delete note process failed: {ex.Message}"); + return StatusCode(500); } } [HttpDelete("{taskId}", Name = "DeleteTaskById")] public async Task> DeleteTaskById([FromHeader] Guid userId, Guid taskId) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating Delete Task By Id Method"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) - { - _logger.LogWarning ($"User id {userId} not authorized"); - return StatusCode(403); - } - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); - if (taskItem is null) - { - _logger.LogWarning($"Task item {taskId} not found for user {userId}"); - return NotFound(taskId); - } - - await _unitOfWork.TaskItem.DeleteTask(taskItem); - await _unitOfWork.SaveChangesAsync(); - _logger.LogInformation($"Successfully deleted task item {taskId} for user {userId}"); - - TaskDto deleteTask = new TaskDto + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - Id = taskItem.Id, - Title = taskItem.Title, - DueDate = taskItem.DueDate, - Priority = taskItem.Priority, - Notes = taskItem.TaskItemNotes.Select(n => new NoteDto + _logger.LogInformation("Initiating Delete Task By Id Method"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"User id {userId} not authorized"); + return StatusCode(403); + } + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + if (taskItem is null) { - Id = n.Id, - TaskItemId = n.TaskItemId, - Note = n.Note, - CreatedDate = n.CreatedDate, - CreatedUser = n.CreatedUserId, + _logger.LogWarning($"Task item {taskId} not found for user {userId}"); + return NotFound(taskId); } - ).ToList(), - CurrentStatus = taskItem.TaskItemStatusHistories.OrderByDescending(x => x.CreatedUserId) - .Select(n => new StatusDto + + await _unitOfWork.TaskItem.DeleteTask(taskItem); + await _unitOfWork.SaveChangesAsync(); + _logger.LogInformation($"Successfully deleted task item {taskId} for user {userId}"); + + TaskDto deleteTask = new TaskDto { - Id = n.Status.Id, - Name = n.Status.Name, - Code = n.Status.Code, - - }).FirstOrDefault(), - CreatedDate = taskItem.CreatedDate, - CreatedUserId = taskItem.CreatedUserId, - }; - _logger.LogInformation($"Returning the newly deleted task {taskId} for user {userId}"); - return Ok(deleteTask); + Id = taskItem.Id, + Title = taskItem.Title, + DueDate = taskItem.DueDate, + Priority = taskItem.Priority, + Notes = taskItem.TaskItemNotes.Select(n => new NoteDto + { + Id = n.Id, + TaskItemId = n.TaskItemId, + Note = n.Note, + CreatedDate = n.CreatedDate, + CreatedUser = n.CreatedUserId, + } + ).ToList(), + CurrentStatus = taskItem.TaskItemStatusHistories.OrderByDescending(x => x.CreatedUserId) + .Select(n => new StatusDto + { + Id = n.Status.Id, + Name = n.Status.Name, + Code = n.Status.Code, + + }).FirstOrDefault(), + CreatedDate = taskItem.CreatedDate, + CreatedUserId = taskItem.CreatedUserId, + }; + _logger.LogInformation($"Returning the newly deleted task {taskId} for user {userId}"); + return Ok(deleteTask); + } + } + catch (Exception ex) + { + _logger.LogError($"Delete task by id process failed: {ex.Message}"); + return StatusCode(500); } } [HttpPost("{taskId}/status-change/complete", Name = "StatusChangeComplete")] public async Task> StatusChangeComplete([FromHeader] Guid userId, Guid taskId) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating StatusChangeComplete method"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"UserId {userId} not authorized"); - return StatusCode(403); - } - - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); - if (taskItem is null) - { - _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); - return NotFound(taskId); - } + _logger.LogInformation("Initiating StatusChangeComplete method"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"UserId {userId} not authorized"); + return StatusCode(403); + } - TaskItemStatusHistory newTaskStatus = new TaskItemStatusHistory - { - TaskItemId = taskItem.Id, - StatusId = _statusChange.CompleteId, - CreatedDate = DateTime.Now, - CreatedUserId = userId, - }; + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + if (taskItem is null) + { + _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); + return NotFound(taskId); + } + TaskItemStatusHistory newTaskStatus = new TaskItemStatusHistory + { + TaskItemId = taskItem.Id, + StatusId = _statusChange.CompleteId, + CreatedDate = DateTime.Now, + CreatedUserId = userId, + }; - taskItem.TaskItemStatusHistories.Add(newTaskStatus); - await _unitOfWork.SaveChangesAsync(); - _logger.LogInformation($"Status Change to Complete is Successfull for userId {userId}"); - TaskDto statusResult = new TaskDto - { - Id = taskItem.Id, - Title = taskItem.Title, - DueDate = taskItem.DueDate, - Priority = taskItem.Priority, + taskItem.TaskItemStatusHistories.Add(newTaskStatus); + await _unitOfWork.SaveChangesAsync(); + _logger.LogInformation($"Status Change to Complete is Successfull for userId {userId}"); - Notes = taskItem.TaskItemNotes.Select(n => new NoteDto + TaskDto statusResult = new TaskDto { - Id = n.Id, - TaskItemId = n.TaskItemId, - Note = n.Note, - CreatedDate = n.CreatedDate, - CreatedUser = n.CreatedUserId - }).ToList(), - - CurrentStatus = new StatusDto - { - Id = _statusChange.CompleteId, - Name = _statusChange.Complete, - Code = _statusChange.Code2 - }, - - CreatedDate = taskItem.CreatedDate, - CreatedUserId = taskItem.CreatedUserId, - }; - _logger.LogInformation($"Status changed to Complete result for TaskId {taskItem.Id} and UserId {userId}"); - _logger.LogInformation("Returning the status changed to complete result"); - return CreatedAtAction(nameof(StatusChangeComplete), new { taskId = newTaskStatus.Id }, statusResult); + Id = taskItem.Id, + Title = taskItem.Title, + DueDate = taskItem.DueDate, + Priority = taskItem.Priority, + + Notes = taskItem.TaskItemNotes.Select(n => new NoteDto + { + Id = n.Id, + TaskItemId = n.TaskItemId, + Note = n.Note, + CreatedDate = n.CreatedDate, + CreatedUser = n.CreatedUserId + }).ToList(), + + CurrentStatus = new StatusDto + { + Id = _statusChange.CompleteId, + Name = _statusChange.Complete, + Code = _statusChange.Code2 + }, + + CreatedDate = taskItem.CreatedDate, + CreatedUserId = taskItem.CreatedUserId, + }; + _logger.LogInformation($"Status changed to Complete result for TaskId {taskItem.Id} and UserId {userId}"); + _logger.LogInformation("Returning the status changed to complete result"); + return CreatedAtAction(nameof(StatusChangeComplete), new { taskId = newTaskStatus.Id }, statusResult); + } + } + catch (Exception ex) + { + _logger.LogError($"Change status to complete process failed: {ex.Message}"); + return StatusCode(500); } } @@ -380,64 +428,72 @@ public async Task> StatusChangePending([FromHeader] Guid u [HttpPut("{taskId}", Name = "EditTask")] public async Task> EditTask([FromHeader] Guid userId, Guid taskId, TaskDto updateTaskDto) { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating EditTask method"); - if (!await _unitOfWork.User.IsUserInDbAsync(userId)) - { - _logger.LogWarning($"UserId {userId} not authorized"); - return StatusCode(403); - } - - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); - if (taskItem is null) + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); - return NotFound(taskId); - } - - if (updateTaskDto.Title != null && - updateTaskDto.DueDate.HasValue && - updateTaskDto.Priority != 0) - { - taskItem.Title = updateTaskDto.Title; - taskItem.DueDate = updateTaskDto.DueDate.Value; - taskItem.Priority = updateTaskDto.Priority; - } - await _unitOfWork.SaveChangesAsync(); - _logger.LogInformation($"Task Edit is Successfull for userId {userId}"); + _logger.LogInformation("Initiating EditTask method"); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + _logger.LogWarning($"UserId {userId} not authorized"); + return StatusCode(403); + } - TaskDto editTaskResult = new TaskDto - { - Id = taskItem.Id, - Title = taskItem.Title, - DueDate = taskItem.DueDate, - Priority = taskItem.Priority, + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + if (taskItem is null) + { + _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); + return NotFound(taskId); + } - Notes = taskItem.TaskItemNotes.Select(n => new NoteDto + if (updateTaskDto.Title != null && + updateTaskDto.DueDate.HasValue && + updateTaskDto.Priority != 0) { - Id = n.Id, - TaskItemId = n.TaskItemId, - Note = n.Note, - CreatedDate = n.CreatedDate, - CreatedUser = n.CreatedUserId - }).ToList(), - - CurrentStatus = taskItem.TaskItemStatusHistories.OrderByDescending(rank => rank.CreatedDate) - .Select(history => new StatusDto + taskItem.Title = updateTaskDto.Title; + taskItem.DueDate = updateTaskDto.DueDate.Value; + taskItem.Priority = updateTaskDto.Priority; + } + await _unitOfWork.SaveChangesAsync(); + _logger.LogInformation($"Task Edit is Successfull for userId {userId}"); + + TaskDto editTaskResult = new TaskDto { - Id = history.Status.Id, - Name = history.Status.Name, - Code = history.Status.Code, - CreatedDate = history.CreatedDate - }).First(), - - CreatedDate = taskItem.CreatedDate, - CreatedUserId = taskItem.CreatedUserId - }; - _logger.LogInformation($"Edited task result for TaskId {taskItem.Id} and UserId {userId}"); - _logger.LogInformation("Returning the edited task result"); - return Ok(editTaskResult); + Id = taskItem.Id, + Title = taskItem.Title, + DueDate = taskItem.DueDate, + Priority = taskItem.Priority, + + Notes = taskItem.TaskItemNotes.Select(n => new NoteDto + { + Id = n.Id, + TaskItemId = n.TaskItemId, + Note = n.Note, + CreatedDate = n.CreatedDate, + CreatedUser = n.CreatedUserId + }).ToList(), + + CurrentStatus = taskItem.TaskItemStatusHistories.OrderByDescending(rank => rank.CreatedDate) + .Select(history => new StatusDto + { + Id = history.Status.Id, + Name = history.Status.Name, + Code = history.Status.Code, + CreatedDate = history.CreatedDate + }).First(), + + CreatedDate = taskItem.CreatedDate, + CreatedUserId = taskItem.CreatedUserId + }; + _logger.LogInformation($"Edited task result for TaskId {taskItem.Id} and UserId {userId}"); + _logger.LogInformation("Returning the edited task result"); + return Ok(editTaskResult); + } + } + catch (Exception ex) + { + _logger.LogError($"Edit task process failed: {ex.Message}"); + return StatusCode(500); } } } diff --git a/Web.Api/Controllers/UserController.cs b/Web.Api/Controllers/UserController.cs index 0d63260..9b866ec 100644 --- a/Web.Api/Controllers/UserController.cs +++ b/Web.Api/Controllers/UserController.cs @@ -24,33 +24,41 @@ public class UserController : ControllerBase [HttpPost(Name = "RegisterUser")] //Http post request public async Task> RegisterUser(RegisterUserDto registerUserDto) //resgister User method user creation { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating Register User method"); - User? user = await _unitOfWork.User.GetUserByEmailAsync(registerUserDto.Email); - if (user is not null) + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"Attempting to register with an email that is already in use: {registerUserDto.Email}"); - return BadRequest("Email already in use."); - } - _logger.LogInformation($"Registering with email {registerUserDto.Email}"); - //RequestDTO - //create a new instance of User thats not existing - //call the User props and set the registerDto to its assign props - User newUser = new User - { - FirstName = registerUserDto.FirstName, - LastName = registerUserDto.LastName, - Email = registerUserDto.Email, - Password = registerUserDto.Password, - CreatedDate = DateTime.Now, - }; - _logger.LogInformation("New user successfully created"); + _logger.LogInformation("Initiating Register User method"); + User? user = await _unitOfWork.User.GetUserByEmailAsync(registerUserDto.Email); + if (user is not null) + { + _logger.LogWarning($"Attempting to register with an email that is already in use: {registerUserDto.Email}"); + return BadRequest("Email already in use."); + } + _logger.LogInformation($"Registering with email {registerUserDto.Email}"); + //RequestDTO + //create a new instance of User thats not existing + //call the User props and set the registerDto to its assign props + User newUser = new User + { + FirstName = registerUserDto.FirstName, + LastName = registerUserDto.LastName, + Email = registerUserDto.Email, + Password = registerUserDto.Password, + CreatedDate = DateTime.Now, + }; + _logger.LogInformation("New user successfully created"); - await _unitOfWork.User.CreateUserAsync(newUser); //UofW takes the User class and calls the CreateUser method from the UserRepo - await _unitOfWork.SaveChangesAsync(); //UofW calls the SaveChanges method - _logger.LogInformation($"Returning newly created user with id {newUser.Id}"); - return Ok(newUser.Id); //a new Id Guid is return once user is registered + await _unitOfWork.User.CreateUserAsync(newUser); //UofW takes the User class and calls the CreateUser method from the UserRepo + await _unitOfWork.SaveChangesAsync(); //UofW calls the SaveChanges method + _logger.LogInformation($"Returning newly created user with id {newUser.Id}"); + return Ok(newUser.Id); //a new Id Guid is return once user is registered + } //a new Id Guid is return once user is registered + } + catch (Exception ex) + { + _logger.LogError($"Register user process failed: {ex.Message}"); + return StatusCode(500); } } @@ -58,18 +66,26 @@ public async Task> RegisterUser(RegisterUserDto registerUserD [HttpPost("login", Name = "Login")] public async Task> Login(LoginDto userLoginDto) //login user method creation { - using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) + try { - _logger.LogInformation("Initiating Login method"); - User? userLogin = await _unitOfWork.User.GetUserByEmailAsync(userLoginDto.Email); //get user from UofW and user email from UserRepo - if (userLogin is null || userLogin.Password != userLoginDto.Password) + using (_logger.BeginScope(new Dictionary { ["TransactionId"] = HttpContext.TraceIdentifier, })) { - _logger.LogWarning($"Invalid user login: {userLoginDto.Email} or Password: {userLoginDto.Password}"); - return BadRequest("Invalid email or password."); + _logger.LogInformation("Initiating Login method"); + User? userLogin = await _unitOfWork.User.GetUserByEmailAsync(userLoginDto.Email); //get user from UofW and user email from UserRepo + if (userLogin is null || userLogin.Password != userLoginDto.Password) + { + _logger.LogWarning($"Invalid user login: {userLoginDto.Email} or Password: {userLoginDto.Password}"); + return BadRequest("Invalid email or password."); + } + _logger.LogInformation($"User has logged in successfully: {userLoginDto.Email}"); + _logger.LogInformation($"Returning user login id {userLogin.Id}"); + return Ok(userLogin.Id); // return the registered GUID Id of that user } - _logger.LogInformation($"User has logged in successfully: {userLoginDto.Email}"); - _logger.LogInformation($"Returning user login id {userLogin.Id}"); - return Ok(userLogin.Id); // return the registered GUID Id of that user + } + catch (Exception ex) + { + _logger.LogError($"Login user process failed: {ex.Message}"); + return StatusCode(500); } } }