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
Binary file added .DS_Store
Binary file not shown.
116 changes: 0 additions & 116 deletions Controllers/TodoItemsController.cs

This file was deleted.

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.

11 changes: 0 additions & 11 deletions Models/TodoItemDTO.cs

This file was deleted.

121 changes: 121 additions & 0 deletions Todo.API/Controllers/TodoItemsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Todo.Core.Exceptions;
using Todo.Core.Interfaces;
using Todo.Core.Models.TodoItem;
using Todo.Infrastructure.Entities;

namespace Todo.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TodoItemsController : ControllerBase
{
private readonly IItemService _itemService;
private readonly IMapper _mapper;

public TodoItemsController(IItemService itemService, IMapper mapper)
{
_itemService = itemService;
_mapper = mapper;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<TodoItemDTO>>> GetTodoItems()
{
try
{
var result = await _itemService.GetAll();

return Ok(_mapper.Map<IEnumerable<TodoItemDTO>>(result));
}
catch (Exception ex)
{
var errorDetails = new ErrorDetails(500, ex.Message);
return StatusCode(500, errorDetails);
}
}

[HttpGet("{id}")]
public async Task<ActionResult<TodoItemDTO>> GetTodoItem(long id)
{
try
{
var item = await _itemService.Read(id);

return Ok(_mapper.Map<TodoItemDTO>(item));
}
catch (Exception e)
{
Console.WriteLine(e);
return BadRequest();
}
}

[HttpPut("{id}")]
public async Task<IActionResult> UpdateTodoItem(long id, [FromBody] TodoItemDTOUpdate todoItemDTO)
{
try
{
var item = await TodoItemExists(id);
_mapper.Map(todoItemDTO, item);

await _itemService.Update(item);

return NoContent();
}
catch (Exception e)
{
Console.WriteLine(e);
return BadRequest();
}
}

[HttpPost]
public async Task<ActionResult<TodoItemDTO>> CreateTodoItem([FromBody] TodoItemDTOCreate todoItemDTO)
{
try
{
var toEntity = _mapper.Map<TodoItem>(todoItemDTO);
await _itemService.Create(toEntity);

var createdItem = _mapper.Map<TodoItemDTO>(toEntity);

return CreatedAtRoute("TodoItemById", new { todoItemId = createdItem.Id }, createdItem);
}
catch (Exception e)
{
Console.WriteLine(e);
return BadRequest();
}
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteTodoItem(long id)
{
try
{
var item = await TodoItemExists(id);

await _itemService.Delete(item);

return NoContent();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return BadRequest();
}
}

private async Task<TodoItem> TodoItemExists(long id)
{
var item = await _itemService.Read(id);

return item;
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"TodoApiDTO": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
Expand Down
File renamed without changes.
39 changes: 28 additions & 11 deletions Startup.cs → Todo.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using TodoApi.Models;
using Todo.Core.Interfaces;
using Todo.Core.Mappings;
using Todo.Core.Services;
using Todo.Infrastructure.DbContexts;
using Todo.Infrastructure.Entities;
using Todo.Infrastructure.Interfaces;
using Todo.Infrastructure.Repositories;

namespace TodoApi
{
Expand All @@ -27,8 +27,14 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TodoContext>(opt =>
opt.UseInMemoryDatabase("TodoList"));
services.AddTransient<IRepository<TodoItem>, Repository<TodoItem>>();
services.AddTransient<IItemService, ItemService>();
services.AddSwaggerGen();
services.AddAutoMapper(typeof(MappingProfile));

services.AddDbContext<TodoDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Sql")));

services.AddControllers();
}

Expand All @@ -49,7 +55,18 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

endpoints.MapControllerRoute(
name: "TodoItemById",
pattern: "api/todo/{todoItemId}",
defaults: new { controller = "TodoItems", action = "GetTodoItem" });
});

app.UseSwagger();
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Test1 Api v1"); });
}
}
}
}
22 changes: 22 additions & 0 deletions Todo.API/Todo.API.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Todo.Core\Todo.Core.csproj" />
</ItemGroup>


</Project>
File renamed without changes.
13 changes: 13 additions & 0 deletions Todo.API/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Sql": "Server=localhost;Database=Account;User Id=sa;Password=Password.1;ApplicationIntent=ReadWrite;MultiSubnetFailover=False; TrustServerCertificate=True"
}
}
Loading