Skip to content

Commit

Permalink
Closes #60
Browse files Browse the repository at this point in the history
Implement get for a single list
  • Loading branch information
mtracewicz committed Sep 10, 2024
1 parent 7a5f829 commit 87de18e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
15 changes: 15 additions & 0 deletions backend/bruno/KSummarized/ToDo/List/Get.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
meta {
name: Get
type: http
seq: 5
}

get {
url: https://localhost:5000/api/todo/lists/9
body: none
auth: bearer
}

auth:bearer {
token: {{token}}
}
24 changes: 20 additions & 4 deletions backend/src/api/Controllers/TodoController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using api.Services;
using api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -12,7 +12,8 @@ public class TodoController : ControllerBase
private readonly ITodoService _service;
private readonly ILogger<TodoController> _logger;

public TodoController(ITodoService service, ILogger<TodoController> logger){
public TodoController(ITodoService service, ILogger<TodoController> logger)
{
_service = service;
_logger = logger;
}
Expand All @@ -29,6 +30,20 @@ public IActionResult GetLists()
};
}

[HttpGet("lists/{id}")]
public IActionResult GetList([FromRoute] int id)
{
var userId = Request.UserId();
_logger.LogDebug("User: {user} requested his lists", userId);
if (userId is null) { return Unauthorized(); }
var list = _service.GetList(userId, id);
return list switch
{
null => NotFound(),
var user => Ok(list),
};
}

[HttpPost("lists")]
public async Task<IActionResult> CreateLists([FromQuery] string name)
{
Expand All @@ -39,9 +54,10 @@ public async Task<IActionResult> CreateLists([FromQuery] string name)
null => Unauthorized(),
var user => await Create(user, name),
};
async Task<IActionResult> Create(string user, string name){
async Task<IActionResult> Create(string user, string name)
{
var list = await _service.CreateList(user, name);
return Created(HttpContext.Request.Path.Add(new PathString($"/{list.Id}")), list);
}
}
}
}
3 changes: 2 additions & 1 deletion backend/src/api/Services/Todo/ITodoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface ITodoService
{
Task<TodoListModel> CreateList(string user, string name);
public IEnumerable<TodoListDTO> GetLists(string userId);
}
public TodoListDTO? GetList(string userId, int id);
}
17 changes: 13 additions & 4 deletions backend/src/api/Services/Todo/TodoService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using api.Data;
using api.Data;
using api.Data.DAO;
using api.Data.DTO;
using Microsoft.EntityFrameworkCore;
Expand All @@ -23,10 +23,19 @@ public IEnumerable<TodoListDTO> GetLists(string userId)
.AsEnumerable();
}

public async Task<TodoListModel> CreateList(string user, string name){
var newList = new TodoListModel(){Name = name, Owner = Guid.Parse(user)};
public TodoListDTO? GetList(string userId, int id)
{
var list = _context.TodoLists.AsNoTracking()
.SingleOrDefault(l => l.Owner.Equals(Guid.Parse(userId)) && l.Id == id);
if (list is not null) { return new(list.Id, list.Name); }
return null;
}

public async Task<TodoListModel> CreateList(string user, string name)
{
var newList = new TodoListModel() { Name = name, Owner = Guid.Parse(user) };
_context.TodoLists.Add(newList);
await _context.SaveChangesAsync();
return newList;
}
}
}

0 comments on commit 87de18e

Please sign in to comment.