Skip to content

Commit

Permalink
Closes #60
Browse files Browse the repository at this point in the history
Introduce Item - List mapping
  • Loading branch information
mtracewicz committed Sep 27, 2024
1 parent bd3598f commit 9e11db1
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 25 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ dev:
no-reload:
docker compose --profile without-hot-reload up --build

.PHONY: dev no-reload
db:
docker compose up db -d

.PHONY: dev no-reload db
.DEFAULT_GOAL := dev
3 changes: 2 additions & 1 deletion backend/bruno/KSummarized/ToDo/Items/Create.bru
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ body:json {
"tags": [],
"subtasks": []
}
]
],
"listId": {{listId}}
}
}

Expand Down
6 changes: 4 additions & 2 deletions backend/src/api/Responses/TodoList.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using core;

namespace api.Resonses;

public record TodoList(int Id, string Name);
public record TodoList(int Id, string Name, IEnumerable<TodoItem> Items);

public static class MapExtensions
{
public static TodoList ToResponse(this core.TodoList list) => new TodoList(list.Id, list.Name);
public static TodoList ToResponse(this core.TodoList list) => new TodoList(list.Id, list.Name, list.Items);
}
7 changes: 4 additions & 3 deletions backend/src/core/TodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ public record TodoItem
{
public int? Id { get; set; }
public required string Name { get; set; }
public bool Compleated {get; set;}
public DateTime Deadline {get; set;}
public bool Compleated { get; set; }
public DateTime Deadline { get; set; }
public string Notes { get; set; } = null!;
public required IEnumerable<Tag> Tags { get; set; }
public required IEnumerable<TodoItem> Subtasks { get; set; }
}
public int ListId { get; set; }
}
3 changes: 1 addition & 2 deletions backend/src/core/TodoList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ public class TodoList
public int Id { get; set; }
public required string Name { get; set; }
public required Guid Owner { get; set; }

public bool IsAuthorized(Guid person) => Owner.Equals(person);
public required IEnumerable<TodoItem> Items { get; set; }
}
2 changes: 1 addition & 1 deletion backend/src/infrastructure/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options
{
public required DbSet<TodoListModel> TodoLists { get; set; }
public required DbSet<TodoItemModel> Todos { get; set; }
public required DbSet<Tag> Tags { get; set; }
public required DbSet<TagModel> Tags { get; set; }
}
2 changes: 1 addition & 1 deletion backend/src/infrastructure/Data/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace infrastructure.Data;

[Table("Tags")]
public class Tag
public class TagModel
{
[Key]
public int Id { get; set; }
Expand Down
4 changes: 3 additions & 1 deletion backend/src/infrastructure/Data/TodoItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ public class TodoItemModel
public DateTime Deadline { get; set; }
[MaxLength(4096)]
public string Notes { get; set; } = null!;
public required ICollection<Tag> Tags { get; set; }
public required ICollection<TagModel> Tags { get; set; }
public required ICollection<TodoItemModel> Subtasks { get; set; }
public int? MainTaskId { get; set; }
public TodoItemModel? MainTask { get; set; }
public int ListId { get; set; }
public TodoListModel? List { get; set; }
}
1 change: 1 addition & 0 deletions backend/src/infrastructure/Data/TodoListModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class TodoListModel
[MaxLength(512)]
public required string Name { get; set; }
public required Guid Owner { get; set; }
public required ICollection<TodoItemModel> Items { get; set; }
}
33 changes: 25 additions & 8 deletions backend/src/infrastructure/Data/TodoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,40 @@ public IEnumerable<TodoList> GetLists(Guid user)
{
return _context.TodoLists.AsNoTracking()
.Where(list => list.Owner.Equals(user))
.Select(list => new TodoList { Id = list.Id, Name = list.Name, Owner = list.Owner })
.Select(list => new TodoList
{
Id = list.Id,
Name = list.Name,
Owner = list.Owner,
Items = Enumerable.Empty<TodoItem>()
})
.AsEnumerable();
}

public TodoList? GetList(Guid user, int id)
{
var list = _context.TodoLists.AsNoTracking()
.Include(l => l.Items)
.SingleOrDefault(l => l.Owner.Equals(user) && l.Id == id);
//TODO: Consider creating a mapper instead of this manual new
if (list is not null) { return new() { Id = list.Id, Name = list.Name, Owner = list.Owner }; }
if (list is not null)
{
return new()
{
Id = list.Id,
Name = list.Name,
Owner = list.Owner,
Items = list.Items.Select(i => MapTodoItem(i)).ToList()
};
};
return null;
}

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

public bool DeleteList(Guid user, int id)
Expand Down Expand Up @@ -73,7 +88,8 @@ public async Task<TodoItem> CreateItem(Guid user, TodoItem item)
Deadline = item.Deadline,
Notes = item.Notes,
Subtasks = [],
Tags = []
Tags = [],
ListId = item.ListId
};
foreach (var st in item.Subtasks)
{
Expand All @@ -85,7 +101,8 @@ public async Task<TodoItem> CreateItem(Guid user, TodoItem item)
Deadline = st.Deadline,
Notes = st.Notes,
Tags = [],
Subtasks = []
Subtasks = [],
ListId = item.ListId
};
newItem.Subtasks.Add(newSubtask);
}
Expand Down Expand Up @@ -204,7 +221,7 @@ public async Task<bool> UpdateItem(Guid user, TodoItem item)

private static TodoItem MapTodoItem(infrastructure.Data.TodoItemModel item)
{
Log.Information("Mapped {item}", JsonSerializer.Serialize(item, new JsonSerializerOptions() { ReferenceHandler = ReferenceHandler.Preserve }));
Log.Debug("Mapped {item}", JsonSerializer.Serialize(item, new JsonSerializerOptions() { ReferenceHandler = ReferenceHandler.Preserve }));
return new TodoItem()
{
Id = item.Id,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9e11db1

Please sign in to comment.