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

Fazer solução do desafio #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
25 changes: 22 additions & 3 deletions Controllers/TarefaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,29 @@ public IActionResult ObterPorId(int id)
// TODO: Buscar o Id no banco utilizando o EF
// TODO: Validar o tipo de retorno. Se não encontrar a tarefa, retornar NotFound,
// caso contrário retornar OK com a tarefa encontrada
return Ok();
var tarefa = _context.Tarefas.Find(id);

if(tarefa == null)
return NotFound();

return Ok(tarefa);
}

[HttpGet("ObterTodos")]
public IActionResult ObterTodos()
{
// TODO: Buscar todas as tarefas no banco utilizando o EF
return Ok();
var tarefa = _context.Tarefas.ToList();
return Ok(tarefa);
}

[HttpGet("ObterPorTitulo")]
public IActionResult ObterPorTitulo(string titulo)
{
// TODO: Buscar as tarefas no banco utilizando o EF, que contenha o titulo recebido por parâmetro
// Dica: Usar como exemplo o endpoint ObterPorData
return Ok();
var tarefa = _context.Tarefas.Where(x => x.Titulo.ToUpper().Contains(titulo.ToUpper())).ToList();
return Ok(tarefa);
}

[HttpGet("ObterPorData")]
Expand All @@ -62,6 +69,9 @@ public IActionResult Criar(Tarefa tarefa)
return BadRequest(new { Erro = "A data da tarefa não pode ser vazia" });

// TODO: Adicionar a tarefa recebida no EF e salvar as mudanças (save changes)
_context.Tarefas.Add(tarefa);
_context.SaveChanges();

return CreatedAtAction(nameof(ObterPorId), new { id = tarefa.Id }, tarefa);
}

Expand All @@ -78,6 +88,13 @@ public IActionResult Atualizar(int id, Tarefa tarefa)

// TODO: Atualizar as informações da variável tarefaBanco com a tarefa recebida via parâmetro
// TODO: Atualizar a variável tarefaBanco no EF e salvar as mudanças (save changes)
tarefaBanco.Titulo = tarefa.Titulo;
tarefaBanco.Descricao = tarefa.Descricao;
tarefaBanco.Data = tarefa.Data;
tarefaBanco.Status = tarefa.Status;

_context.Tarefas.Update(tarefaBanco);
_context.SaveChanges();
return Ok();
}

Expand All @@ -90,6 +107,8 @@ public IActionResult Deletar(int id)
return NotFound();

// TODO: Remover a tarefa encontrada através do EF e salvar as mudanças (save changes)
_context.Tarefas.Remove(tarefaBanco);
_context.SaveChanges();
return NoContent();
}
}
Expand Down
54 changes: 54 additions & 0 deletions Migrations/20240920210559_CriacaoTabelaTarefa.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Migrations/20240920210559_CriacaoTabelaTarefa.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TrilhaApiDesafio.Migrations
{
public partial class CriacaoTabelaTarefa : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Tarefas",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Titulo = table.Column<string>(type: "nvarchar(max)", nullable: true),
Descricao = table.Column<string>(type: "nvarchar(max)", nullable: true),
Data = table.Column<DateTime>(type: "datetime2", nullable: false),
Status = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Tarefas", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Tarefas");
}
}
}
52 changes: 52 additions & 0 deletions Migrations/OrganizadorContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using TrilhaApiDesafio.Context;

#nullable disable

namespace TrilhaApiDesafio.Migrations
{
[DbContext(typeof(OrganizadorContext))]
partial class OrganizadorContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.5")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);

modelBuilder.Entity("TrilhaApiDesafio.Models.Tarefa", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);

b.Property<DateTime>("Data")
.HasColumnType("datetime2");

b.Property<string>("Descricao")
.HasColumnType("nvarchar(max)");

b.Property<int>("Status")
.HasColumnType("int");

b.Property<string>("Titulo")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("Tarefas");
});
#pragma warning restore 612, 618
}
}
}
40 changes: 22 additions & 18 deletions TrilhaApiDesafio.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
}
},
"ConnectionStrings": {
"ConexaoPadrao": "COLOCAR SUA CONNECTION STRING AQUI"
"ConexaoPadrao": "Server=localhost\\SQLEXPRESS;Database=Organizador;Trusted_Connection=True"
}
}