Skip to content

Commit

Permalink
refactor get all task to only loginned user
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben0189 committed Nov 5, 2024
1 parent 2d7d6e6 commit 88df315
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 8 additions & 1 deletion blotztask-api/Controllers/TaskController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ public TaskController(ITaskService taskService)
[HttpGet("alltask")]
public async Task<IActionResult> GetAllTask()
{
return Ok(await _taskService.GetTodoItems());
var userId = HttpContext.Items["UserId"] as string;

if (userId == null)
{
throw new UnauthorizedAccessException("Could not find user id from Http Context");
}

return Ok(await _taskService.GetTodoItemsByUser(userId));
}

[HttpGet("{id}")]
Expand Down
8 changes: 4 additions & 4 deletions blotztask-api/Services/TaskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
using Microsoft.EntityFrameworkCore;
using BlotzTask.Data.Entities;
using BlotzTask.Models.CustomError;
using System.Threading.Tasks;

namespace BlotzTask.Services;

public interface ITaskService
{
public Task<List<TaskItemDTO>> GetTodoItems();
public Task<List<TaskItemDTO>> GetTodoItemsByUser(string userId);
public Task<TaskItemDTO> GetTaskByID(int Id);
public Task<int> EditTask(int Id, EditTaskItemDTO editTaskItem);
public Task<string> AddTask(AddTaskItemDTO addtaskItem);
Expand All @@ -26,11 +25,12 @@ public TaskService(BlotzTaskDbContext dbContext)
_dbContext = dbContext;
}

public async Task<List<TaskItemDTO>> GetTodoItems()
public async Task<List<TaskItemDTO>> GetTodoItemsByUser(string userId)
{
try
{
return await _dbContext.TaskItems
.Where(x => x.UserId == userId)
.Select(x => new TaskItemDTO
{
Id = x.Id,
Expand All @@ -40,7 +40,7 @@ public async Task<List<TaskItemDTO>> GetTodoItems()
}
catch (Exception ex)

Check warning on line 41 in blotztask-api/Services/TaskService.cs

View workflow job for this annotation

GitHub Actions / build-and-test-backend

The variable 'ex' is declared but never used
{
//TODO: Add some error log throw (havent create PBI)
//TODO: Add some error log throw
throw;
}
}
Expand Down

0 comments on commit 88df315

Please sign in to comment.