Skip to content

Commit

Permalink
Merge pull request #146 from Strayker-Software/feature/113-provide-dt…
Browse files Browse the repository at this point in the history
…o-for-transfer-between-api-and-application-layers

Provide DTO for transfer between API and Application layers
  • Loading branch information
StraykerPL authored Jan 30, 2024
2 parents 409428c + fa95b0d commit c80e6ee
Show file tree
Hide file tree
Showing 32 changed files with 511 additions and 166 deletions.
23 changes: 14 additions & 9 deletions binder-web-backend/Binder.Api/Controllers/TablesController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Binder.Application.Models.Interfaces;
using Binder.Core.Models;
using AutoMapper;
using Binder.Api.Models;
using Binder.Application.Models.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace Binder.Api.Controllers
Expand All @@ -9,29 +10,33 @@ namespace Binder.Api.Controllers
public sealed class TablesController : ControllerBase
{
private readonly IDefaultTableService _service;
private readonly IMapper _mapper;

public TablesController(IDefaultTableService service)
public TablesController(IDefaultTableService service, IMapper mapper)
{
_service = service;
_mapper = mapper;
}

[Route("{tableId}")]
[HttpGet]
public ActionResult<DefaultTable> Get(int tableId)
public ActionResult<DefaultTableDTO> Get(int tableId)
{
return _service.GetTable(tableId);
return _mapper.Map<DefaultTableDTO>(_service.GetTable(tableId));
}

[HttpGet]
public ActionResult<DefaultTable[]> Get()
public ActionResult<DefaultTableDTO[]> Get()
{
return Ok(_service.GetAllTables());
return Ok(_mapper.Map<DefaultTableDTO[]>(_service.GetAllTables()));
}

[HttpPost]
public ActionResult<DefaultTable> Post(string tableName)
public ActionResult Post(string tableName)
{
return _service.CreateTable(tableName);
_service.CreateTable(tableName);

return Ok();
}
}
}
24 changes: 16 additions & 8 deletions binder-web-backend/Binder.Api/Controllers/ToDoTasksController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Binder.Api.Models;
using AutoMapper;
using Binder.Api.Models;
using Binder.Application.Models.Interfaces;
using Binder.Core.Models;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -10,28 +11,35 @@ namespace Binder.Api.Controllers
public sealed class ToDoTasksController : ControllerBase
{
private readonly IToDoTasksService _service;
private readonly IMapper _mapper;

public ToDoTasksController(IToDoTasksService service)
public ToDoTasksController(IToDoTasksService service, IMapper mapper)
{
_service = service;
_mapper = mapper;
}

[HttpGet]
public ActionResult<ToDoTask[]> Get(int tableId, TaskShow showFiltering)
public ActionResult<ToDoTaskDTO[]> Get(int tableId, TaskShow showFiltering)
{
return showFiltering switch
{
TaskShow.ShowCompleted => Ok(_service.GetTasksForTable(tableId).Where(x => x.IsCompleted == true)),
TaskShow.HideCompleted => Ok(_service.GetTasksForTable(tableId).Where(x => x.IsCompleted == false)),
TaskShow.ShowAll => Ok(_service.GetTasksForTable(tableId)),
TaskShow.ShowCompleted => Ok(
_mapper.Map<ToDoTaskDTO[]>(_service.GetTasksForTable(tableId).Where(x => x.IsCompleted == true))),
TaskShow.HideCompleted => Ok(
_mapper.Map<ToDoTaskDTO[]>(_service.GetTasksForTable(tableId).Where(x => x.IsCompleted == false))),
TaskShow.ShowAll => Ok(
_mapper.Map<ToDoTaskDTO[]>(_service.GetTasksForTable(tableId))),
_ => NotFound(),
};
}

[HttpPost]
public ActionResult<ToDoTask> Post(ToDoTask task)
public ActionResult Post(ToDoTaskDTO task)
{
return _service.AddTaskToTable(task);
_service.AddTaskToTable(_mapper.Map<ToDoTask>(task));

return Ok();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using AutoMapper;
using Binder.Api.Models;
using Binder.Core.Models;

namespace Binder.Api.Mappings
{
public class DtoToCoreModelsMappingsProfile : Profile
{
public DtoToCoreModelsMappingsProfile()
{
CreateMap<ToDoTask, ToDoTaskDTO>()
.ReverseMap();
CreateMap<DefaultTable, DefaultTableDTO>();
}
}
}
13 changes: 13 additions & 0 deletions binder-web-backend/Binder.Api/Models/DefaultTableDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Binder.Api.Models
{
public class DefaultTableDTO
{
public int Id { get; set; }
public string Name { get; set; }

public DefaultTableDTO()
{
Name = string.Empty;
}
}
}
18 changes: 18 additions & 0 deletions binder-web-backend/Binder.Api/Models/ToDoTaskDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Binder.Api.Models
{
public class ToDoTaskDTO
{
public int Id { get; private set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsCompleted { get; set; }

public int TableId { get; set; }

public ToDoTaskDTO()
{
Name = string.Empty;
Description = string.Empty;
}
}
}
1 change: 0 additions & 1 deletion binder-web-backend/Binder.Application/Models/Dtos/.gitkeep

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,49 @@ namespace Binder.Application.Services
{
public sealed class DefaultTableService : IDefaultTableService
{
private readonly IDefaultTableRepository _repository;
private readonly IDefaultTableRepository _defaultTableRepository;
private readonly IToDoTasksRepository _toDoTasksRepository;

public DefaultTableService(IDefaultTableRepository repository)
public DefaultTableService(
IDefaultTableRepository defaultTableRepository,
IToDoTasksRepository toDoTasksRepository)
{
_repository = repository;
_defaultTableRepository = defaultTableRepository;
_toDoTasksRepository = toDoTasksRepository;
}

public DefaultTable GetTable(int tableId)
{
return _repository.GetById(tableId) ??
var table = _defaultTableRepository.GetById(tableId) ??
throw new NotFoundException(ExceptionConstants.ResourceNotFoundMessage);
table.Tasks = _toDoTasksRepository.GetTasksByTable(tableId) ??
throw new NotFoundException(ExceptionConstants.ResourceNotFoundMessage);

return table;
}

public ICollection<DefaultTable> GetAllTables()
{
return _repository.GetAll() ??
var tables = _defaultTableRepository.GetAll() ??
throw new NotFoundException(ExceptionConstants.ResourceNotFoundMessage);

foreach (var table in tables)
{
table.Tasks = _toDoTasksRepository.GetTasksByTable(table.Id) ??
throw new NotFoundException(ExceptionConstants.ResourceNotFoundMessage);
}

return tables;
}

public DefaultTable CreateTable(string tableName)
{
{
var table = new DefaultTable
{
Name = tableName
};
return _repository.Add(table) ??

return _defaultTableRepository.Add(table) ??
throw new InvalidOperationException(ExceptionConstants.InvalidOperationMessage);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public async Task InvokeAsync(HttpContext context)
{
ProblemDetails problemDetails = GetProblemDetailsByExceptionFactory.GetProblemDetails(e);
context.Response.StatusCode = (int)problemDetails.Status!;

context.Response.ContentType = problemJsonType;

var json = JsonSerializer.Serialize(problemDetails);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@ namespace Binder.Application.Services
{
public sealed class ToDoTasksService : IToDoTasksService
{
private readonly IToDoTasksRepository _repository;
private readonly IToDoTasksRepository _toDoTaskRepository;
private readonly IDefaultTableRepository _defaultTableRepository;

public ToDoTasksService(IToDoTasksRepository repository)
public ToDoTasksService(IToDoTasksRepository toDoTaskRepository, IDefaultTableRepository defaultTableRepository)
{
_repository = repository;
_toDoTaskRepository = toDoTaskRepository;
_defaultTableRepository = defaultTableRepository;
}

public ICollection<ToDoTask> GetTasksForTable(int tableId)
{
return _repository.GetTasksByTable(tableId) ??
return _toDoTaskRepository.GetTasksByTable(tableId) ??
throw new NotFoundException(ExceptionConstants.ResourceNotFoundMessage);
}

public ToDoTask AddTaskToTable(ToDoTask task)
{
var newTask = new ToDoTask(task.Name, task.Description, task.IsCompleted, task.TableId);
task.Table = _defaultTableRepository.GetById(task.TableId) ??
throw new NotFoundException(ExceptionConstants.ResourceNotFoundMessage);

return _repository.InsertTaskIntoTable(newTask) ??
return _toDoTaskRepository.InsertTaskIntoTable(task) ??
throw new InvalidOperationException(ExceptionConstants.InvalidOperationMessage);
}
}
Expand Down
4 changes: 2 additions & 2 deletions binder-web-backend/Binder.Core/Models/DefaultTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Binder.Core.Models
{
public record DefaultTable : IBaseEntity
public class DefaultTable : IBaseEntity
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ToDoTask> Tasks { get; }
public ICollection<ToDoTask> Tasks { get; set; }

public DefaultTable()
{
Expand Down
11 changes: 2 additions & 9 deletions binder-web-backend/Binder.Core/Models/ToDoTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

namespace Binder.Core.Models
{
public record ToDoTask : IBaseEntity
public class ToDoTask : IBaseEntity
{
public int Id { get; private set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsCompleted { get; set; }

public int TableId { get; set; }

[JsonIgnore]
public DefaultTable Table { get; set; }

Expand All @@ -20,13 +21,5 @@ public ToDoTask()
Description = string.Empty;
Table = new DefaultTable();
}

public ToDoTask(string name, string description, bool isCompleted, int tableId)
{
Name = name;
Description = description;
IsCompleted = isCompleted;
TableId = tableId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public DefaultTableRepository(BinderDbContext context)

public ICollection<DefaultTable>? GetAll()
{
return _context.Tables.ToList();
return _context.Tables.ToList().Count != 0 ? _context.Tables.ToList() : null;
}

public DefaultTable Add(DefaultTable table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Binder.Api\Binder.Api.csproj" />
<ProjectReference Include="..\Binder.Application\Binder.Application.csproj" />
<ProjectReference Include="..\Binder.Core\Binder.Core.csproj" />
<ProjectReference Include="..\Binder.Persistence\Binder.Persistence.csproj" />
Expand Down
Loading

0 comments on commit c80e6ee

Please sign in to comment.