Skip to content

Commit

Permalink
Lekce 06 (#5)
Browse files Browse the repository at this point in the history
Lekce 06
  • Loading branch information
Hijtec authored Nov 6, 2024
1 parent ec372d9 commit 759f39b
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace ToDoList.Domain.DTOs;

using ToDoList.Domain.Models;

public record ToDoItemCreateRequestDto(string Name, string Description, bool IsCompleted) //id is generated
Expand Down
4 changes: 4 additions & 0 deletions ToDoList/src/ToDoList.Persistence/Repositories/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ namespace ToDoList.Persistence.Repositories;
public interface IRepository<T> where T : class
{
public void Create(T item);
public IEnumerable<T> ReadAll();
public T? ReadById(int id);
public void Update(T item);
public void DeleteById(int id);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace ToDoList.Persistence.Repositories;

using System.Collections.Generic;
using ToDoList.Domain.Models;

public class ToDoItemsRepository : IRepository<ToDoItem>
Expand All @@ -16,4 +17,19 @@ public void Create(ToDoItem item)
context.ToDoItems.Add(item);
context.SaveChanges();
}
public IEnumerable<ToDoItem> ReadAll() => context.ToDoItems.ToList();
public ToDoItem? ReadById(int id) => context.ToDoItems.Find(id);
public void Update(ToDoItem item)
{
var foundItem = context.ToDoItems.Find(item.ToDoItemId) ?? throw new ArgumentOutOfRangeException($"ToDo item with ID {item.ToDoItemId} not found.");
context.Entry(foundItem).CurrentValues.SetValues(item);
context.SaveChanges();
}

public void DeleteById(int id)
{
var item = context.ToDoItems.Find(id) ?? throw new ArgumentOutOfRangeException($"ToDo item with ID {id} not found.");
context.ToDoItems.Remove(item);
context.SaveChanges();
}
}
23 changes: 9 additions & 14 deletions ToDoList/src/ToDoList.WebApi/Controllers/ToDoItemsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ namespace ToDoList.WebApi.Controllers;
using Microsoft.AspNetCore.Mvc;
using ToDoList.Domain.DTOs;
using ToDoList.Domain.Models;
using ToDoList.Persistence;
using ToDoList.Persistence.Repositories;

[ApiController]
[Route("api/[controller]")]
public class ToDoItemsController : ControllerBase
{
private readonly ToDoItemsContext context;
private readonly IRepository<ToDoItem> repository;

public ToDoItemsController(ToDoItemsContext context, IRepository<ToDoItem> repository) // Az domigrujeme, zbyde nam tu jen repository :)
public ToDoItemsController(IRepository<ToDoItem> repository)
{
this.context = context;
this.repository = repository;
}

Expand Down Expand Up @@ -44,18 +41,18 @@ public ActionResult<ToDoItemGetResponseDto> Create(ToDoItemCreateRequestDto requ
[HttpGet]
public ActionResult<IEnumerable<ToDoItemGetResponseDto>> Read()
{
List<ToDoItem> itemsToGet;
IEnumerable<ToDoItem> itemsToGet;
try
{
itemsToGet = context.ToDoItems.ToList();
itemsToGet = repository.ReadAll();
}
catch (Exception ex)
{
return Problem(ex.Message, null, StatusCodes.Status500InternalServerError); //500
}

//respond to client
return (itemsToGet is null || itemsToGet.Count is 0)
return (itemsToGet is null || !itemsToGet.Any())
? NotFound() //404
: Ok(itemsToGet.Select(ToDoItemGetResponseDto.FromDomain)); //200
}
Expand All @@ -67,7 +64,7 @@ public ActionResult<ToDoItemGetResponseDto> ReadById(int toDoItemId)
ToDoItem? itemToGet;
try
{
itemToGet = context.ToDoItems.Find(toDoItemId);
itemToGet = repository.ReadById(toDoItemId);
}
catch (Exception ex)
{
Expand All @@ -91,14 +88,13 @@ public IActionResult UpdateById(int toDoItemId, [FromBody] ToDoItemUpdateRequest
try
{
//retrieve the item
var itemToUpdate = context.ToDoItems.Find(toDoItemId);
var itemToUpdate = repository.ReadById(toDoItemId);
if (itemToUpdate is null)
{
return NotFound(); //404
}

context.Entry(itemToUpdate).CurrentValues.SetValues(updatedItem);
context.SaveChanges();
repository.Update(updatedItem);
}
catch (Exception ex)
{
Expand All @@ -115,14 +111,13 @@ public IActionResult DeleteById(int toDoItemId)
//try to delete the item
try
{
var itemToDelete = context.ToDoItems.Find(toDoItemId);
var itemToDelete = repository.ReadById(toDoItemId);
if (itemToDelete is null)
{
return NotFound(); //404
}

context.ToDoItems.Remove(itemToDelete);
context.SaveChanges();
repository.DeleteById(toDoItemId);
}
catch (Exception ex)
{
Expand Down
7 changes: 5 additions & 2 deletions ToDoList/tests/ToDoList.Test/IntegrationTests/DeleteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ToDoList.Test.IntegrationTests;
using Microsoft.AspNetCore.Mvc;
using ToDoList.Domain.Models;
using ToDoList.Persistence;
using ToDoList.Persistence.Repositories;
using ToDoList.WebApi.Controllers;

public class DeleteTests
Expand All @@ -12,7 +13,8 @@ public void Delete_ValidId_ReturnsNoContent()
{
// Arrange
var context = new ToDoItemsContext("Data Source=../../../../../data/localdb.db");
var controller = new ToDoItemsController(context: context, repository: null); // Docasny hack, nez z controlleru odstranime context.
var repository = new ToDoItemsRepository(context);
var controller = new ToDoItemsController(repository);
var toDoItem = new ToDoItem
{
Name = "Jmeno",
Expand All @@ -34,7 +36,8 @@ public void Delete_InvalidId_ReturnsNotFound()
{
// Arrange
var context = new ToDoItemsContext("Data Source=../../../../../data/localdb.db");
var controller = new ToDoItemsController(context, null); // Docasny hack, nez z controlleru odstranime context.
var repository = new ToDoItemsRepository(context);
var controller = new ToDoItemsController(repository);
var toDoItem = new ToDoItem
{
Name = "Jmeno",
Expand Down
7 changes: 5 additions & 2 deletions ToDoList/tests/ToDoList.Test/IntegrationTests/GetByIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ToDoList.Test.IntegrationTests;
using Microsoft.AspNetCore.Mvc;
using ToDoList.Domain.Models;
using ToDoList.Persistence;
using ToDoList.Persistence.Repositories;
using ToDoList.WebApi.Controllers;

public class GetByIdTests
Expand All @@ -12,7 +13,8 @@ public void GetById_ValidId_ReturnsItem()
{
// Arrange
var context = new ToDoItemsContext("Data Source=../../../../../data/localdb.db");
var controller = new ToDoItemsController(context, null); // Docasny hack, nez z controlleru odstranime context.
var repository = new ToDoItemsRepository(context);
var controller = new ToDoItemsController(repository);
var toDoItem = new ToDoItem
{
Name = "Jmeno",
Expand Down Expand Up @@ -42,7 +44,8 @@ public void GetById_InvalidId_ReturnsNotFound()
{
// Arrange
var context = new ToDoItemsContext("Data Source=../../../../../data/localdb.db");
var controller = new ToDoItemsController(context, null); // Docasny hack, nez z controlleru odstranime context.
var repository = new ToDoItemsRepository(context);
var controller = new ToDoItemsController(repository);

var toDoItem = new ToDoItem
{
Expand Down
4 changes: 3 additions & 1 deletion ToDoList/tests/ToDoList.Test/IntegrationTests/GetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ToDoList.Test.IntegrationTests;
using Microsoft.AspNetCore.Mvc;
using ToDoList.Domain.Models;
using ToDoList.Persistence;
using ToDoList.Persistence.Repositories;
using ToDoList.WebApi.Controllers;

public class GetTests
Expand All @@ -12,7 +13,8 @@ public void Get_AllItems_ReturnsAllItems()
{
// Arrange
var context = new ToDoItemsContext("Data Source=../../../../../data/localdb.db");
var controller = new ToDoItemsController(context, null); // Docasny hack, nez z controlleru odstranime context.
var repository = new ToDoItemsRepository(context);
var controller = new ToDoItemsController(repository);

var toDoItem = new ToDoItem
{
Expand Down
2 changes: 1 addition & 1 deletion ToDoList/tests/ToDoList.Test/IntegrationTests/PostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Post_ValidRequest_ReturnsNewItem()
// Arrange
var context = new ToDoItemsContext("Data Source=../../../../../data/localdb.db");
var repository = new ToDoItemsRepository(context);
var controller = new ToDoItemsController(null, repository); // Docasny hack, nez z controlleru odstranime context.
var controller = new ToDoItemsController(repository);
var request = new ToDoItemCreateRequestDto(
Name: "Jmeno",
Description: "Popis",
Expand Down
7 changes: 5 additions & 2 deletions ToDoList/tests/ToDoList.Test/IntegrationTests/PutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ToDoList.Test.IntegrationTests;
using ToDoList.Domain.DTOs;
using ToDoList.Domain.Models;
using ToDoList.Persistence;
using ToDoList.Persistence.Repositories;
using ToDoList.WebApi.Controllers;

public class PutTests
Expand All @@ -13,7 +14,8 @@ public void Put_ValidId_ReturnsNoContent()
{
// Arrange
var context = new ToDoItemsContext("Data Source=../../../../../data/localdb.db");
var controller = new ToDoItemsController(context, null); // Docasny hack, nez z controlleru odstranime context.
var repository = new ToDoItemsRepository(context);
var controller = new ToDoItemsController(repository);
var toDoItem = new ToDoItem
{
Name = "Jmeno",
Expand Down Expand Up @@ -41,7 +43,8 @@ public void Put_InvalidId_ReturnsNotFound()
{
// Arrange
var context = new ToDoItemsContext("Data Source=../../../../../data/localdb.db");
var controller = new ToDoItemsController(context, null); // Docasny hack, nez z controlleru odstranime context.
var repository = new ToDoItemsRepository(context);
var controller = new ToDoItemsController(repository);
var toDoItem = new ToDoItem
{
Name = "Jmeno",
Expand Down
5 changes: 3 additions & 2 deletions ToDoList/tests/ToDoList.Test/UnitTests/PostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Post_ValidRequest_ReturnsNewItem()
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
var controller = new ToDoItemsController(null, repositoryMock); // Docasny hack, nez z controlleru odstranime context.
var controller = new ToDoItemsController(repositoryMock);
var request = new ToDoItemCreateRequestDto(
Name: "Jmeno",
Description: "Popis",
Expand All @@ -29,6 +29,7 @@ public void Post_ValidRequest_ReturnsNewItem()

// Assert
Assert.IsType<CreatedAtActionResult>(resultResult);
repositoryMock.Received(1).Create(Arg.Any<ToDoItem>());
Assert.NotNull(value);

Assert.Equal(request.Description, value.Description);
Expand All @@ -41,7 +42,7 @@ public void Post_UnhandledException_Returns500()
{
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
var controller = new ToDoItemsController(null, repositoryMock); // Docasny hack, nez z controlleru odstranime context.
var controller = new ToDoItemsController(repositoryMock);
var request = new ToDoItemCreateRequestDto(
Name: "Jmeno",
Description: "Popis",
Expand Down

0 comments on commit 759f39b

Please sign in to comment.