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.

4 changes: 1 addition & 3 deletions Models/TodoItemDTO.cs → TodoApi.BLL/Dto/TodoItemDTO.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
namespace TodoApi.Models
namespace TodoApi.BLL.Dto
{
#region snippet
public class TodoItemDTO
{
public long Id { get; set; }
public string Name { get; set; }
public bool IsComplete { get; set; }
}
#endregion
}
51 changes: 51 additions & 0 deletions TodoApi.BLL/Implementation/Repository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using TodoApi.BLL.Interfaces;
using TodoApi.DAL;

namespace TodoApi.BLL.Implementation
{
public class Repository<T> : IRepository<T> where T : class
{
private readonly DbContext _dbContext;

public Repository(TodoContext dbContext)
{
_dbContext = dbContext;
}

public T GetById(long id)
{
return _dbContext.Set<T>().Find(id);
}

public async Task<T> GetByIdAsync(long id)
{
return await _dbContext.Set<T>().FindAsync(id);
}

public async Task<IEnumerable<T>> GetAllAsync()
{
return await _dbContext.Set<T>().ToListAsync();
}

public async Task AddAsync(T entity)
{
await _dbContext.Set<T>().AddAsync(entity);
await _dbContext.SaveChangesAsync();
}

public async Task UpdateAsync(T entity)
{
_dbContext.Set<T>().Update(entity);
await _dbContext.SaveChangesAsync();
}

public async Task DeleteAsync(T entity)
{
_dbContext.Set<T>().Remove(entity);
await _dbContext.SaveChangesAsync();
}
}
}
103 changes: 103 additions & 0 deletions TodoApi.BLL/Implementation/TodoItemsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TodoApi.BLL.Dto;
using TodoApi.BLL.Interfaces;
using TodoApi.DAL.Models;

namespace TodoApi.BLL.Implementation
{
public class TodoItemsService : ITodoItemsService
{
private readonly IRepository<TodoItem> _repository;

public TodoItemsService(IRepository<TodoItem> repository)
{
_repository = repository;
}


public async Task<IEnumerable<TodoItemDTO>> GetTodoItemsAsync()
{
var items = await _repository.GetAllAsync();
return items.Select(i => ItemToDTO(i));
}

public async Task<bool> UpdateTodoItemAsync(long id, TodoItemDTO todoItemDTO)
{
if (id != todoItemDTO.Id)
{
throw new Exception("id != todoItemDTO.Id");
}

var todoItem = await _repository.GetByIdAsync(id);
if (todoItem == null)
{
return false;
}

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

try
{
await _repository.UpdateAsync(todoItem);
}
catch (DbUpdateConcurrencyException) when (_repository.GetById(id) == null)
{
return false;
}

return true;
}

private static TodoItemDTO ItemToDTO(TodoItem todoItem) =>
new TodoItemDTO
{
Id = todoItem.Id,
Name = todoItem.Name,
IsComplete = todoItem.IsComplete
};

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

if (todoItem == null)
{
return null;
}

return ItemToDTO(todoItem);
}

public async Task<TodoItemDTO> CreateTodoItemAsync(TodoItemDTO todoItemDTO)
{
var todoItem = new TodoItem
{
IsComplete = todoItemDTO.IsComplete,
Name = todoItemDTO.Name
};

await _repository.AddAsync(todoItem);

return ItemToDTO(todoItem);
}

public async Task<bool> DeleteTodoItemAsync(long id)
{
var todoItem = await _repository.GetByIdAsync(id);

if (todoItem == null)
{
return false;
}

await _repository.DeleteAsync(todoItem);

return true;
}
}
}
15 changes: 15 additions & 0 deletions TodoApi.BLL/Interfaces/IRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace TodoApi.BLL.Interfaces
{
public interface IRepository<T>
{
T GetById(long id);
Task<T> GetByIdAsync(long id);
Task<IEnumerable<T>> GetAllAsync();
Task AddAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(T entity);
}
}
15 changes: 15 additions & 0 deletions TodoApi.BLL/Interfaces/ITodoItemsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using TodoApi.BLL.Dto;

namespace TodoApi.BLL.Interfaces
{
public interface ITodoItemsService
{
Task<IEnumerable<TodoItemDTO>> GetTodoItemsAsync();
Task<TodoItemDTO> GetTodoItemAsync(long id);
Task<bool> UpdateTodoItemAsync(long id, TodoItemDTO todoItemDTO);
Task<TodoItemDTO> CreateTodoItemAsync(TodoItemDTO todoItemDTO);
Task<bool> DeleteTodoItemAsync(long id);
}
}
12 changes: 12 additions & 0 deletions TodoApi.BLL/TodoApi.BLL.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TodoApi.DAL\TodoApi.DAL.csproj" />
</ItemGroup>
</Project>
46 changes: 46 additions & 0 deletions TodoApi.DAL/Migrations/20230617212119_Initial.Designer.cs

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

Loading