Skip to content

Commit

Permalink
Closes #60
Browse files Browse the repository at this point in the history
Initial implementation of getting all of users Todo Lists.
  • Loading branch information
mtracewicz committed Mar 30, 2024
1 parent d3b156a commit 936dc10
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 10 deletions.
27 changes: 27 additions & 0 deletions backend/src/api/Controllers/TodoController.cs
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)),
};
}

}
3 changes: 1 addition & 2 deletions backend/src/api/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using api.Data.DAO;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace api.Data;
Expand All @@ -9,5 +8,5 @@ public class ApplicationDbContext : DbContext
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<UserModel> Users { get; set; }
public DbSet<TodoListModel> TodoLists { get; set; }
}
14 changes: 14 additions & 0 deletions backend/src/api/Data/DAO/TodoListModel.cs
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; }
}
3 changes: 3 additions & 0 deletions backend/src/api/Data/DTO/TodoListDTO.cs
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 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.

37 changes: 37 additions & 0 deletions backend/src/api/Migrations/20240330212015_TodoList.cs
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");
}
}
}
17 changes: 9 additions & 8 deletions backend/src/api/Migrations/ApplicationDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
Expand All @@ -16,30 +17,30 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.2")
.HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("api.Data.DAO.UserModel", b =>
modelBuilder.Entity("api.Data.DAO.TodoListModel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");

NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));

b.Property<string>("Email")
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
.HasMaxLength(512)
.HasColumnType("character varying(512)");

b.Property<string>("KeycloakUuid")
.IsRequired()
.HasColumnType("text");
b.Property<Guid>("Owner")
.HasColumnType("uuid");

b.HasKey("Id");

b.ToTable("Users");
b.ToTable("todo_lists");
});
#pragma warning restore 612, 618
}
Expand Down
2 changes: 2 additions & 0 deletions backend/src/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Serilog;
using api;
using System.Security.Cryptography;
using api.Services;

const string logFormat = "[{Timestamp:HH:mm:ss} {Level:u3}] {CorelationId} | {Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration().Enrich.WithCorrelationId()
Expand Down Expand Up @@ -57,6 +58,7 @@
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "api", Version = "v1" });
});
builder.Services.AddScoped<ITodoService, TodoService>();

builder.Services.AddCors(options =>
{
Expand Down
8 changes: 8 additions & 0 deletions backend/src/api/Services/Todo/ITodoService.cs
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);
}
24 changes: 24 additions & 0 deletions backend/src/api/Services/Todo/TodoService.cs
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();
}
}

0 comments on commit 936dc10

Please sign in to comment.