Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lekce 06 #5

Merged
merged 5 commits into from
Nov 6, 2024
Merged
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
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,16 +3,18 @@
using Microsoft.AspNetCore.Mvc;
using ToDoList.Domain.Models;
using ToDoList.Persistence;
using ToDoList.Persistence.Repositories;
using ToDoList.WebApi.Controllers;

public class DeleteTests
{
[Fact]
public void Delete_ValidId_ReturnsNoContent()

Check warning on line 12 in ToDoList/tests/ToDoList.Test/IntegrationTests/DeleteTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.DeleteTests.Delete_ValidId_ReturnsNoContent() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)

Check warning on line 12 in ToDoList/tests/ToDoList.Test/IntegrationTests/DeleteTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.DeleteTests.Delete_ValidId_ReturnsNoContent() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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 @@ -30,11 +32,12 @@
}

[Fact]
public void Delete_InvalidId_ReturnsNotFound()

Check warning on line 35 in ToDoList/tests/ToDoList.Test/IntegrationTests/DeleteTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.DeleteTests.Delete_InvalidId_ReturnsNotFound() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)

Check warning on line 35 in ToDoList/tests/ToDoList.Test/IntegrationTests/DeleteTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.DeleteTests.Delete_InvalidId_ReturnsNotFound() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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,16 +3,18 @@
using Microsoft.AspNetCore.Mvc;
using ToDoList.Domain.Models;
using ToDoList.Persistence;
using ToDoList.Persistence.Repositories;
using ToDoList.WebApi.Controllers;

public class GetByIdTests
{
[Fact]
public void GetById_ValidId_ReturnsItem()

Check warning on line 12 in ToDoList/tests/ToDoList.Test/IntegrationTests/GetByIdTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.GetByIdTests.GetById_ValidId_ReturnsItem() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)

Check warning on line 12 in ToDoList/tests/ToDoList.Test/IntegrationTests/GetByIdTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.GetByIdTests.GetById_ValidId_ReturnsItem() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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 All @@ -38,11 +40,12 @@
}

[Fact]
public void GetById_InvalidId_ReturnsNotFound()

Check warning on line 43 in ToDoList/tests/ToDoList.Test/IntegrationTests/GetByIdTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.GetByIdTests.GetById_InvalidId_ReturnsNotFound() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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 @@ -9,12 +9,12 @@
public class PostTests
{
[Fact]
public void Post_ValidRequest_ReturnsNewItem()

Check warning on line 12 in ToDoList/tests/ToDoList.Test/IntegrationTests/PostTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.PostTests.Post_ValidRequest_ReturnsNewItem() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)

Check warning on line 12 in ToDoList/tests/ToDoList.Test/IntegrationTests/PostTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.PostTests.Post_ValidRequest_ReturnsNewItem() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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,16 +4,18 @@
using ToDoList.Domain.DTOs;
using ToDoList.Domain.Models;
using ToDoList.Persistence;
using ToDoList.Persistence.Repositories;
using ToDoList.WebApi.Controllers;

public class PutTests
{
[Fact]
public void Put_ValidId_ReturnsNoContent()

Check warning on line 13 in ToDoList/tests/ToDoList.Test/IntegrationTests/PutTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.PutTests.Put_ValidId_ReturnsNoContent() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)

Check warning on line 13 in ToDoList/tests/ToDoList.Test/IntegrationTests/PutTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.PutTests.Put_ValidId_ReturnsNoContent() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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 All @@ -37,11 +39,12 @@
}

[Fact]
public void Put_InvalidId_ReturnsNotFound()

Check warning on line 42 in ToDoList/tests/ToDoList.Test/IntegrationTests/PutTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.PutTests.Put_InvalidId_ReturnsNotFound() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)

Check warning on line 42 in ToDoList/tests/ToDoList.Test/IntegrationTests/PutTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.IntegrationTests.PutTests.Put_InvalidId_ReturnsNotFound() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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 @@ -11,11 +11,11 @@
public class PostUnitTests
{
[Fact]
public void Post_ValidRequest_ReturnsNewItem()

Check warning on line 14 in ToDoList/tests/ToDoList.Test/UnitTests/PostTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.PostUnitTests.Post_ValidRequest_ReturnsNewItem() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)

Check warning on line 14 in ToDoList/tests/ToDoList.Test/UnitTests/PostTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.PostUnitTests.Post_ValidRequest_ReturnsNewItem() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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 @@

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

Assert.Equal(request.Description, value.Description);
Expand All @@ -37,11 +38,11 @@
}

[Fact]
public void Post_UnhandledException_Returns500()

Check warning on line 41 in ToDoList/tests/ToDoList.Test/UnitTests/PostTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.PostUnitTests.Post_UnhandledException_Returns500() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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
Loading