Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 0 additions & 116 deletions Controllers/TodoItemsController.cs

This file was deleted.

26 changes: 0 additions & 26 deletions Program.cs

This file was deleted.

12 changes: 12 additions & 0 deletions TodoApi.BLL/Infrastructure/ValidationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace TodoApi.BLL.Infrastructure
{
public class ValidationException : Exception
{
public string Property { get; protected set; }
public ValidationException(string message, string prop) : base(message)
{
Property = prop;
}
}
}
14 changes: 14 additions & 0 deletions TodoApi.BLL/Interfaces/ITodoItemService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using TodoApi.DTO;

namespace TodoApi.BLL.Interfaces
{
public interface ITodoItemService
{
Task<TodoItemDTO> GetTodoItemAsync(long id);
Task<IEnumerable<TodoItemDTO>> GetTodoItemsAsync();
Task UpdateTodoItemAsync(long id, TodoItemDTO todoItemDTO);
Task<TodoItemDTO> CreateTodoItemAsync(TodoItemDTO todoItemDTO);
Task DeleteTodoItemAsync(long id);
void Dispose();
}
}
85 changes: 85 additions & 0 deletions TodoApi.BLL/Services/TodoItemService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using TodoApi.BLL.Interfaces;
using TodoApi.DAL.Entities;
using TodoApi.DAL.Extensions;
using TodoApi.DAL.Interfaces;
using TodoApi.DTO;

namespace TodoApi.BLL.Services
{
public class TodoItemService : ITodoItemService
{
IUnitOfWork Database { get; set; }

public TodoItemService(IUnitOfWork uow)
{
Database = uow;
}

public async Task<TodoItemDTO> GetTodoItemAsync(long id)
{
var todoItem = await Database.TodoItems.GetTodoItemAsync(id);

if (todoItem == null)
{
throw new KeyNotFoundException("Item not found!");
}
return todoItem.ItemToDTO();
}

public async Task<IEnumerable<TodoItemDTO>> GetTodoItemsAsync()
{
var todoItems = await Database.TodoItems.GetTodoItemsAsync();

return todoItems.Select(x => x.ItemToDTO()).ToList();
}

public async Task UpdateTodoItemAsync(long id, TodoItemDTO todoItemDTO)
{
if (id != todoItemDTO.Id)
{
throw new Exception("Different object IDs!");
}

var todoItem = await Database.TodoItems.GetTodoItemAsync(id);
if (todoItem == null)
{
throw new KeyNotFoundException("Item not found!");
}

todoItem.Name = todoItemDTO.Name;
todoItem.IsComplete = todoItemDTO.IsComplete;

await Database.TodoItems.UpdateTodoItemAsync(todoItem);
}

public async Task<TodoItemDTO> CreateTodoItemAsync(TodoItemDTO todoItemDTO)
{

var todoItem = new TodoItem
{
Name = todoItemDTO.Name,
IsComplete = todoItemDTO.IsComplete,
Secret = string.Empty
};

return (await Database.TodoItems.CreateTodoItemAsync(todoItem)).ItemToDTO();
}

public async Task DeleteTodoItemAsync(long id)
{
var todoItem = await Database.TodoItems.GetTodoItemAsync(id);

if (todoItem == null)
{
throw new KeyNotFoundException("Item not found!");
}

await Database.TodoItems.DeleteTodoItemAsync(id);
}

public void Dispose()
{
Database.Dispose();
}
}
}
14 changes: 14 additions & 0 deletions TodoApi.BLL/TodoApi - Backup.BLL.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\TodoApi.BLL\TodoApi.DAL.csproj" />
<ProjectReference Include="..\TodoApi.DTO\TodoApi.DTO.csproj" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions TodoApi.BLL/TodoApi.BLL.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NLog.Extensions.Hosting" Version="5.3.3" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.3" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TodoApi.DAL\TodoApi.DAL.csproj" />
<ProjectReference Include="..\TodoApi.DTO\TodoApi.DTO.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using TodoApi.DAL.Entities;

namespace TodoApi.Models
namespace TodoApi.DAL.DBContext
{
public class TodoContext : DbContext
{
Expand Down
5 changes: 2 additions & 3 deletions Models/TodoItem.cs → TodoApi.DAL/Entities/TodoItem.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace TodoApi.Models

namespace TodoApi.DAL.Entities
{
#region snippet
public class TodoItem
{
public long Id { get; set; }
public string Name { get; set; }
public bool IsComplete { get; set; }
public string Secret { get; set; }
}
#endregion
}
16 changes: 16 additions & 0 deletions TodoApi.DAL/Extensions/TodoItemExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using TodoApi.DTO;
using TodoApi.DAL.Entities;

namespace TodoApi.DAL.Extensions
{
public static class TodoItemExtension
{
public static TodoItemDTO ItemToDTO(this TodoItem todoItem) =>
new TodoItemDTO
{
Id = todoItem.Id,
Name = todoItem.Name,
IsComplete = todoItem.IsComplete
};
}
}
12 changes: 12 additions & 0 deletions TodoApi.DAL/Interfaces/IRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace TodoApi.DAL.Interfaces
{
public interface IRepository<T> where T : class
{
Task<T> GetTodoItemAsync(long id);
Task<IEnumerable<T>> GetTodoItemsAsync();
Task UpdateTodoItemAsync(T todoItemDTO);
Task<T> CreateTodoItemAsync(T todoItemDTO);
Task DeleteTodoItemAsync(long id);
}
}
10 changes: 10 additions & 0 deletions TodoApi.DAL/Interfaces/IUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

using TodoApi.DAL.Entities;

namespace TodoApi.DAL.Interfaces
{
public interface IUnitOfWork : IDisposable
{
IRepository<TodoItem> TodoItems { get; }
}
}
Loading