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
15 changes: 15 additions & 0 deletions BL/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.Models;

namespace TodoApiDTO.BL.Interfaces
{
public interface ITodoItemService
{
Task<IEnumerable<TodoItemDTO>> GetTodoItemsAsync();
Task<TodoItemDTO> GetTodoItemAsync( long id );
Task<TodoItemDTO> CreateTodoItemAsync( TodoItemDTO todoItemDto );
Task UpdateTodoItemAsync( long id, TodoItemDTO todoItemDto );
Task DeleteTodoItemAsync( long id );
}
}
92 changes: 92 additions & 0 deletions BL/Service/TodoItemService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TodoApiDTO.BL.Interfaces;
using TodoApiDTO.DAL.Data;
using TodoApiDTO.DAL.Models;
using TodoApiDTO.Models;

namespace TodoApiDTO.BL.Service
{
public class TodoItemService : ITodoItemService
{
private readonly TodoContext _context;

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

public async Task<IEnumerable<TodoItemDTO>> GetTodoItemsAsync()
{
return await _context.TodoItems
.Select( x => ItemToDTO( x ) )
.ToListAsync();
}

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

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

return ItemToDTO( todoItem );
}

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

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

return ItemToDTO( todoItem );
}

public async Task UpdateTodoItemAsync( long id, TodoItemDTO todoItemDto )
{
if( id != todoItemDto.Id ) {
throw new ArgumentException( "Id mismatch" );
}

var todoItem = await _context.TodoItems.FindAsync( id );
if( todoItem == null ) {
throw new Exception( "Todo item not found" );
}

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

await _context.SaveChangesAsync();
}

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

if( todoItem == null ) {
throw new Exception( "Todo item not found" );
}

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

private static TodoItemDTO ItemToDTO( TodoItem todoItem ) =>
new TodoItemDTO
{
Id = todoItem.Id,
Name = todoItem.Name,
IsComplete = todoItem.IsComplete
};
}
}
102 changes: 36 additions & 66 deletions Controllers/TodoItemsController.cs
Original file line number Diff line number Diff line change
@@ -1,116 +1,86 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TodoApi.Models;
using TodoApiDTO.BL.Interfaces;
using TodoApiDTO.Models;

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

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

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

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

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

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

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

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

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

try
{
await _context.SaveChangesAsync();
try {
await _todoItemService.UpdateTodoItemAsync( id, todoItemDTO );
}
catch (DbUpdateConcurrencyException) when (!TodoItemExists(id))
{
catch( Exception ) {
return NotFound();
}

return NoContent();
}

[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();
if( !ModelState.IsValid ) {
return BadRequest( ModelState );
}

return CreatedAtAction(
nameof(GetTodoItem),
new { id = todoItem.Id },
ItemToDTO(todoItem));
var createdTodoItem = await _todoItemService.CreateTodoItemAsync( todoItemDTO );
return CreatedAtAction( nameof( GetTodoItem ), new
{
id = createdTodoItem.Id
}, createdTodoItem );
}

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

if (todoItem == null)
{
try {
await _todoItemService.DeleteTodoItemAsync( id );
}
catch( Exception ) {
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
};
}
}
16 changes: 16 additions & 0 deletions DAL/Data/TodoContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using TodoApiDTO.DAL.Models;

namespace TodoApiDTO.DAL.Data
{
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options): base(options) { }

public DbSet<TodoItem> TodoItems {
get;set;}
}
}
28 changes: 28 additions & 0 deletions DAL/Models/TodoItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace TodoApiDTO.DAL.Models
{
public class TodoItem
{
public long Id {
get; set;
}
public string Name {
get; set;
}
public bool IsComplete {
get; set;
}
public string Secret {
get; set;
}

[Timestamp]
public byte[] Timestamp {
get; set;
}
}
}
14 changes: 0 additions & 14 deletions Models/TodoContext.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Models/TodoItem.cs

This file was deleted.

14 changes: 10 additions & 4 deletions Models/TodoItemDTO.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
namespace TodoApi.Models
namespace TodoApiDTO.Models
{
#region snippet
public class TodoItemDTO
{
public long Id { get; set; }
public string Name { get; set; }
public bool IsComplete { get; set; }
public long Id {
get; set;
}
public string Name {
get; set;
}
public bool IsComplete {
get; set;
}
}
#endregion
}
25 changes: 17 additions & 8 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;

namespace TodoApi
namespace TodoApiDTO
{
public class Program
{
public static void Main(string[] args)
public static void Main( string[] args )
{
CreateHostBuilder(args).Build().Run();
CreateHostBuilder( args ).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
public static IHostBuilder CreateHostBuilder( string[] args ) =>
Host.CreateDefaultBuilder( args )
.ConfigureWebHostDefaults( webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
webBuilder.UseStartup<Startup>()
.ConfigureLogging( ( hostingContext, logging ) =>
{
logging.ClearProviders();
logging.AddSerilog( new LoggerConfiguration()
.MinimumLevel.Error()
.WriteTo.File( "logs\\log-.txt", rollingInterval: RollingInterval.Day )
.CreateLogger() );
} );
} );
}
}
Loading