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
4 changes: 2 additions & 2 deletions Models/TodoItemDTO.cs → BLL/DTO/TodoItemDTO.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace TodoApi.Models
namespace TodoApiDTO.BLL.DTO
{
#region snippet
public class TodoItemDTO
public class TodoItemDto
{
public long Id { get; set; }
public string Name { get; set; }
Expand Down
15 changes: 15 additions & 0 deletions BLL/Interfaces/ITodoItemService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using TodoApiDTO.BLL.DTO;

namespace TodoApiDTO.BLL.Interfaces
{
public interface ITodoItemService
{
Task<IEnumerable<TodoItemDto>> GetTodoItems();
Task<TodoItemDto> GetTodoItem(long id);
Task<bool> UpdateTodoItem(long id, TodoItemDto todoItemDto);
Task<TodoItemDto> CreateTodoItem(TodoItemDto todoItemDto);
Task<bool> DeleteTodoItem(long id);
}
}
111 changes: 111 additions & 0 deletions BLL/Services/TodoItemService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using TodoApiDTO.BLL.DTO;
using TodoApiDTO.BLL.Interfaces;
using TodoApiDTO.DAL.Models;

namespace TodoApiDTO.BLL.Services
{
public class TodoItemService : ITodoItemService
{
private readonly TodoContext _context;

public TodoItemService(TodoContext context)
{
_context = context;
}

public async Task<IEnumerable<TodoItemDto>> GetTodoItems()
{
return await _context.TodoItems
.Select(x => ItemToDto(x))
.ToListAsync();
}

public async Task<TodoItemDto> GetTodoItem(long id)
{
var todoItem = await _context.TodoItems.FindAsync(id);

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

return ItemToDto(todoItem);
}

public async Task<bool> UpdateTodoItem(long id, TodoItemDto todoItemDto)
{
if (id != todoItemDto.Id)
{
return false;
}

var todoItem = await _context.TodoItems.FindAsync(id);
if (todoItem == null)
{
return false;
}

todoItem.Name = todoItemDto.Name;
todoItem.IsComplete = todoItemDto.IsComplete;

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TodoItemExists(id))
{
return false;
}
throw;
}

return true;
}

public async Task<TodoItemDto> CreateTodoItem(TodoItemDto todoItemDto)
{
var todoItem = new TodoItem
{
IsComplete = todoItemDto.IsComplete,
Name = todoItemDto.Name
};

_context.TodoItems.Add(todoItem);
await _context.SaveChangesAsync();

return ItemToDto(todoItem);
}

public async Task<bool> DeleteTodoItem(long id)
{
var todoItem = await _context.TodoItems.FindAsync(id);

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

_context.TodoItems.Remove(todoItem);
await _context.SaveChangesAsync();

return true;
}

private bool TodoItemExists(long id) =>
_context.TodoItems.Any(e => e.Id == id);

private static TodoItemDto ItemToDto(TodoItem todoItem) =>
new TodoItemDto
{
Id = todoItem.Id,
Name = todoItem.Name,
IsComplete = todoItem.IsComplete
};
}
}
83 changes: 22 additions & 61 deletions Controllers/TodoItemsController.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,48 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TodoApi.Models;
using Microsoft.AspNetCore.Mvc;
using TodoApiDTO.BLL.DTO;
using TodoApiDTO.BLL.Interfaces;

namespace TodoApi.Controllers
namespace TodoApiDTO.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TodoItemsController : ControllerBase
{
private readonly TodoContext _context;
private readonly ITodoItemService _todoItemService;

public TodoItemsController(TodoContext context)
public TodoItemsController(ITodoItemService todoItemService)
{
_context = context;
_todoItemService = todoItemService;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<TodoItemDTO>>> GetTodoItems()
public async Task<ActionResult<IEnumerable<TodoItemDto>>> GetTodoItems()
{
return await _context.TodoItems
.Select(x => ItemToDTO(x))
.ToListAsync();
var todoItems = await _todoItemService.GetTodoItems();
return Ok(todoItems);
}

[HttpGet("{id}")]
public async Task<ActionResult<TodoItemDTO>> GetTodoItem(long id)
public async Task<ActionResult<TodoItemDto>> GetTodoItem(long id)
{
var todoItem = await _context.TodoItems.FindAsync(id);
var todoItem = await _todoItemService.GetTodoItem(id);

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

return ItemToDTO(todoItem);
return Ok(todoItem);
}

[HttpPut("{id}")]
public async Task<IActionResult> UpdateTodoItem(long id, TodoItemDTO todoItemDTO)
public async Task<IActionResult> UpdateTodoItem(long id, TodoItemDto todoItemDto)
{
if (id != todoItemDTO.Id)
{
return BadRequest();
}
var result = await _todoItemService.UpdateTodoItem(id, todoItemDto);

var todoItem = await _context.TodoItems.FindAsync(id);
if (todoItem == null)
{
return NotFound();
}

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

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException) when (!TodoItemExists(id))
if (!result)
{
return NotFound();
}
Expand All @@ -69,48 +51,27 @@ public async Task<IActionResult> UpdateTodoItem(long id, TodoItemDTO todoItemDTO
}

[HttpPost]
public async Task<ActionResult<TodoItemDTO>> CreateTodoItem(TodoItemDTO todoItemDTO)
public async Task<ActionResult<TodoItemDto>> CreateTodoItem(TodoItemDto todoItemDto)
{
var todoItem = new TodoItem
{
IsComplete = todoItemDTO.IsComplete,
Name = todoItemDTO.Name
};

_context.TodoItems.Add(todoItem);
await _context.SaveChangesAsync();
var createdTodoItem = await _todoItemService.CreateTodoItem(todoItemDto);

return CreatedAtAction(
nameof(GetTodoItem),
new { id = todoItem.Id },
ItemToDTO(todoItem));
new { id = createdTodoItem.Id },
createdTodoItem);
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteTodoItem(long id)
{
var todoItem = await _context.TodoItems.FindAsync(id);
var result = await _todoItemService.DeleteTodoItem(id);

if (todoItem == null)
if (!result)
{
return NotFound();
}

_context.TodoItems.Remove(todoItem);
await _context.SaveChangesAsync();

return NoContent();
}

private bool TodoItemExists(long id) =>
_context.TodoItems.Any(e => e.Id == id);

private static TodoItemDTO ItemToDTO(TodoItem todoItem) =>
new TodoItemDTO
{
Id = todoItem.Id,
Name = todoItem.Name,
IsComplete = todoItem.IsComplete
};
}
}
2 changes: 1 addition & 1 deletion Models/TodoContext.cs → DAL/Models/TodoContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore;

namespace TodoApi.Models
namespace TodoApiDTO.DAL.Models
{
public class TodoContext : DbContext
{
Expand Down
2 changes: 1 addition & 1 deletion Models/TodoItem.cs → DAL/Models/TodoItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TodoApi.Models
namespace TodoApiDTO.DAL.Models
{
#region snippet
public class TodoItem
Expand Down
46 changes: 46 additions & 0 deletions Migrations/20230522140042_InitialMigration.Designer.cs

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

31 changes: 31 additions & 0 deletions Migrations/20230522140042_InitialMigration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace TodoApiDTO.Migrations
{
public partial class InitialMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TodoItems",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(nullable: true),
IsComplete = table.Column<bool>(nullable: false),
Secret = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_TodoItems", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "TodoItems");
}
}
}
Loading