Skip to content
This repository was archived by the owner on Mar 17, 2026. It is now read-only.
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
9 changes: 9 additions & 0 deletions Backend/TodoList.Api/Models/Models.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
15 changes: 15 additions & 0 deletions Backend/TodoList.Api/Models/TodoItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
public class TodoItem
{
public Guid Id { get; set; }
public string Description { get; set; }
public bool IsCompleted { get; set; }
}
}
19 changes: 19 additions & 0 deletions Backend/TodoList.Api/Repository/ITodoRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repository
{
public interface ITodoRepository
{
Task<List<TodoItem>> GetIncompleteItemsAsync();
Task<TodoItem> GetItemByIdAsync(Guid id);
Task AddItemAsync(TodoItem item);
Task UpdateItemAsync(TodoItem item);
Task<bool> ItemExistsAsync(Guid id);
Task<bool> DescriptionExistsAsync(string description);
}s
}
9 changes: 9 additions & 0 deletions Backend/TodoList.Api/Repository/Repository.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
56 changes: 55 additions & 1 deletion Backend/TodoList.Api/TodoList.Api.UnitTests/DummyTestShould.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TodoList.Api.Controllers;
using TodoList.Models;
using TodoList.RepositoryService.Interface;
using Xunit;

namespace TodoList.Api.UnitTests
{
public class DummyTestShould
{
private readonly Mock<ITodoRepository> _mockRepo;
private readonly Mock<ILogger<TodoItemsController>> _mockLogger;
private readonly TodoItemsController _controller;

public DummyTestShould()
{
_mockRepo = new Mock<ITodoRepository>();
_mockLogger = new Mock<ILogger<TodoItemsController>>();
_controller = new TodoItemsController(_mockRepo.Object, _mockLogger.Object);
}

[Fact]
public void Test_Should_TestSomething()
public async Task GetTodoItems_ReturnsOkResult_WithAListOfTodoItems()
{
// Arrange
_mockRepo.Setup(repo => repo.GetIncompleteItemsAsync()).ReturnsAsync(new List<TodoItem> { new TodoItem() });

// Act
var result = await _controller.GetTodoItems();

// Assert
var okResult = Assert.IsType<OkObjectResult>(result);
var items = Assert.IsType<List<TodoItem>>(okResult.Value);
Assert.Single(items);
}

[Fact]
public async Task GetTodoItem_ReturnsNotFound_WhenItemDoesNotExist()
{
// Arrange
_mockRepo.Setup(repo => repo.GetItemByIdAsync(It.IsAny<Guid>())).ReturnsAsync((TodoItem)null);

// Act
var result = await _controller.GetTodoItem(Guid.NewGuid());

// Assert
Assert.IsType<NotFoundResult>(result);
}

[Fact]
public async Task PostTodoItem_ReturnsBadRequest_WhenDescriptionIsEmpty()
{
// Act
var result = await _controller.PostTodoItem(new TodoItem { Description = "" });

// Assert
var badRequestResult = Assert.IsType<BadRequestObjectResult>(result);
Assert.Equal("Description is required", badRequestResult.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
18 changes: 15 additions & 3 deletions Backend/TodoList.Api/TodoList.Api.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31613.86
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TodoList.Api", "TodoList.Api\TodoList.Api.csproj", "{065CE954-2618-4A24-A613-9C336C2154C6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.Api.UnitTests", "TodoList.Api.UnitTests\TodoList.Api.UnitTests.csproj", "{2BBBFEED-85B6-4361-85B2-F9E08C86C55B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TodoList.Api.UnitTests", "TodoList.Api.UnitTests\TodoList.Api.UnitTests.csproj", "{2BBBFEED-85B6-4361-85B2-F9E08C86C55B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.RepositoryService", "TodoList.RepositoryService\TodoList.RepositoryService.csproj", "{7EA324A5-B570-4A8A-8D09-1E752C66DFCA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TodoList.Models", "TodoList.Models\TodoList.Models.csproj", "{8058D951-13DA-4736-90FF-BA3025F390AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +25,14 @@ Global
{2BBBFEED-85B6-4361-85B2-F9E08C86C55B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BBBFEED-85B6-4361-85B2-F9E08C86C55B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BBBFEED-85B6-4361-85B2-F9E08C86C55B}.Release|Any CPU.Build.0 = Release|Any CPU
{7EA324A5-B570-4A8A-8D09-1E752C66DFCA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EA324A5-B570-4A8A-8D09-1E752C66DFCA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EA324A5-B570-4A8A-8D09-1E752C66DFCA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EA324A5-B570-4A8A-8D09-1E752C66DFCA}.Release|Any CPU.Build.0 = Release|Any CPU
{8058D951-13DA-4736-90FF-BA3025F390AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8058D951-13DA-4736-90FF-BA3025F390AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8058D951-13DA-4736-90FF-BA3025F390AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8058D951-13DA-4736-90FF-BA3025F390AD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
143 changes: 93 additions & 50 deletions Backend/TodoList.Api/TodoList.Api/Controllers/TodoItemsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,145 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using TodoList.Models;
using TodoList.RepositoryService.Interface;

namespace TodoList.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TodoItemsController : ControllerBase
{
private readonly TodoContext _context;
private readonly ITodoRepository _todoRepository;
private readonly ILogger<TodoItemsController> _logger;

public TodoItemsController(TodoContext context, ILogger<TodoItemsController> logger)
public TodoItemsController(ITodoRepository todoRepository, ILogger<TodoItemsController> logger)
{
_context = context;
_todoRepository = todoRepository;
_logger = logger;
}

// GET: api/TodoItems

/// <summary>
/// Get Todo Items
/// </summary>
/// <returns>Todo Items</returns>
[HttpGet]
public async Task<IActionResult> GetTodoItems()
{
var results = await _context.TodoItems.Where(x => !x.IsCompleted).ToListAsync();
return Ok(results);
try
{
var results = await _todoRepository.GetIncompleteItemsAsync();
return Ok(results);
}
catch (Exception ex)
{
_logger.LogError(ex, "TodoItemsController => GetTodoItems");
throw;
}

}

// GET: api/TodoItems/...
/// <summary>
/// Get Todo Item
/// </summary>
/// <param name="id"></param>
/// <returns>Todo Item</returns>
[HttpGet("{id}")]
public async Task<IActionResult> GetTodoItem(Guid id)
{
var result = await _context.TodoItems.FindAsync(id);

if (result == null)
try
{
var result = await _todoRepository.GetItemByIdAsync(id);
if (result == null)
{
return NotFound();
}
return Ok(result);
}
catch (Exception ex)
{
return NotFound();
_logger.LogError(ex, "TodoItemsController => GetTodoItem");
throw;
}

return Ok(result);
}

// PUT: api/TodoItems/...
/// <summary>
/// Put Todo Item
/// </summary>
/// <param name="id"></param>
/// <param name="todoItem"></param>
/// <returns>Error or Success Message</returns>
[HttpPut("{id}")]
public async Task<IActionResult> PutTodoItem(Guid id, TodoItem todoItem)
{
if (id != todoItem.Id)
try
{
return BadRequest();
}

_context.Entry(todoItem).State = EntityState.Modified;
if (id != todoItem.Id)
{
return BadRequest();
}

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TodoItemIdExists(id))
try
{
return NotFound();
await _todoRepository.UpdateItemAsync(todoItem);
}
else
catch (DbUpdateConcurrencyException)
{
throw;
if (!await _todoRepository.ItemExistsAsync(id))
{
return NotFound();
}
else
{
throw;
}
}

return Ok(new { message = "Item has been updated successfully" });
}
catch (Exception ex)
{

_logger.LogError(ex, "TodoItemsController => PutTodoItem");
throw;
}
}

return NoContent();
}

// POST: api/TodoItems
/// <summary>
/// Post Todo Item
/// </summary>
/// <param name="todoItem"></param>
/// <returns>Error or Success Message</returns>
[HttpPost]
public async Task<IActionResult> PostTodoItem(TodoItem todoItem)
{
if (string.IsNullOrEmpty(todoItem?.Description))
{
return BadRequest("Description is required");
}
else if (TodoItemDescriptionExists(todoItem.Description))
try
{
return BadRequest("Description already exists");
}

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

return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id }, todoItem);
}
if (string.IsNullOrEmpty(todoItem?.Description))
{
return BadRequest("Description is required");
}
else if (await _todoRepository.DescriptionExistsAsync(todoItem.Description))
{
return BadRequest("Description already exists");
}

private bool TodoItemIdExists(Guid id)
{
return _context.TodoItems.Any(x => x.Id == id);
}
await _todoRepository.AddItemAsync(todoItem);
return Ok(new { message = "Item has been added successfully" });

private bool TodoItemDescriptionExists(string description)
{
return _context.TodoItems
.Any(x => x.Description.ToLowerInvariant() == description.ToLowerInvariant() && !x.IsCompleted);

}
catch (Exception ex)
{

_logger.LogError(ex, "TodoItemsController => PostTodoItem");
throw;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
//"launchUrl": "swagger",
"launchUrl": "api/todoitems",
"launchUrl": "swagger",
// "launchUrl": "api/todoitems",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand All @@ -22,8 +22,8 @@
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
//"launchUrl": "swagger",
"launchUrl": "api/todoitems",
"launchUrl": "swagger",
//"launchUrl": "api/todoitems",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
Loading