-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of getting all of users Todo Lists.
- Loading branch information
1 parent
d3b156a
commit 936dc10
Showing
10 changed files
with
176 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using api.Data.DTO; | ||
using api.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace api.Controllers; | ||
|
||
[Authorize] | ||
[Route("/api/todo")] | ||
[ApiController] | ||
public class TodoController : ControllerBase | ||
{ | ||
private readonly ITodoService _service; | ||
|
||
public TodoController(ITodoService service) => _service = service; | ||
|
||
[HttpGet("lists")] | ||
public IActionResult List() | ||
{ | ||
return Request.UserId() switch | ||
{ | ||
null => Unauthorized(), | ||
var user => Ok(_service.GetLists(user)), | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace api.Data.DAO; | ||
|
||
[Table("todo_lists")] | ||
public class TodoListModel | ||
{ | ||
[Key] | ||
public int Id { get; set; } | ||
[MaxLength(512)] | ||
public required string Name { get; set; } | ||
public required Guid Owner { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace api.Data.DTO; | ||
|
||
public record TodoListDTO(int Id, string Name); |
51 changes: 51 additions & 0 deletions
51
backend/src/api/Migrations/20240330212015_TodoList.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; | ||
|
||
#nullable disable | ||
|
||
namespace api.Migrations | ||
{ | ||
/// <inheritdoc /> | ||
public partial class TodoList : Migration | ||
{ | ||
/// <inheritdoc /> | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "todo_lists", | ||
columns: table => new | ||
{ | ||
Id = table.Column<int>(type: "integer", nullable: false) | ||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), | ||
Name = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false), | ||
Owner = table.Column<Guid>(type: "uuid", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_todo_lists", x => x.Id); | ||
}); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "todo_lists"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using api.Data.DTO; | ||
|
||
namespace api.Services; | ||
|
||
public interface ITodoService | ||
{ | ||
public IEnumerable<TodoListDTO> GetLists(string userId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using api.Data; | ||
using api.Data.DTO; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace api.Services; | ||
|
||
public class TodoService : ITodoService | ||
{ | ||
|
||
private readonly ApplicationDbContext _context; | ||
|
||
public TodoService(ApplicationDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public IEnumerable<TodoListDTO> GetLists(string userId) | ||
{ | ||
return _context.TodoLists.AsNoTracking() | ||
.Where(list => list.Owner.Equals(Guid.Parse(userId))) | ||
.Select(list => new TodoListDTO(list.Id, list.Name)) | ||
.AsEnumerable(); | ||
} | ||
} |