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

Resolução Desafio #74

Open
wants to merge 10 commits 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
53 changes: 37 additions & 16 deletions Controllers/TarefaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,55 @@ public TarefaController(OrganizadorContext context)
[HttpGet("{id}")]
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();
if (tarefa == null)
{
return NotFound();
}
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 == titulo);
if (tarefa == null)
{
return NotFound();
}
return Ok(tarefa);
}

[HttpGet("ObterPorData")]
public IActionResult ObterPorData(DateTime data)
{
var tarefa = _context.Tarefas.Where(x => x.Data.Date == data.Date);
if (tarefa == null)
{
return NotFound();
}
return Ok(tarefa);
}

[HttpGet("ObterPorStatus")]
public IActionResult ObterPorStatus(EnumStatusTarefa status)
{
// TODO: Buscar as tarefas no banco utilizando o EF, que contenha o status recebido por parâmetro
// Dica: Usar como exemplo o endpoint ObterPorData
var tarefa = _context.Tarefas.Where(x => x.Status == status);
if (tarefa == null)
{
return NotFound();
}
return Ok(tarefa);
}

Expand All @@ -61,7 +76,8 @@ public IActionResult Criar(Tarefa tarefa)
if (tarefa.Data == DateTime.MinValue)
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.Add(tarefa);
_context.SaveChanges();
return CreatedAtAction(nameof(ObterPorId), new { id = tarefa.Id }, tarefa);
}

Expand All @@ -76,9 +92,13 @@ public IActionResult Atualizar(int id, Tarefa tarefa)
if (tarefa.Data == DateTime.MinValue)
return BadRequest(new { Erro = "A data da tarefa não pode ser vazia" });

// 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)
return Ok();
tarefaBanco.Titulo = tarefa.Titulo;
tarefaBanco.Descricao = tarefa.Descricao;
tarefaBanco.Data = tarefa.Data;
tarefaBanco.Status = tarefa.Status;

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

[HttpDelete("{id}")]
Expand All @@ -89,7 +109,8 @@ public IActionResult Deletar(int id)
if (tarefaBanco == null)
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
55 changes: 55 additions & 0 deletions Migrations/20241009030422_CriacaoTabelaTarefa.Designer.cs

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

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

#nullable disable

namespace TrilhaApiDesafio.Migrations
{
/// <inheritdoc />
public partial class CriacaoTabelaTarefa : Migration
{
/// <inheritdoc />
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);
});
}

/// <inheritdoc />
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", "8.0.8")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

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

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

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
}
}
}
8 changes: 4 additions & 4 deletions TrilhaApiDesafio.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.5">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<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" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</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; Initial Catalog=Tarefa; Integrated Security=True; TrustServerCertificate=True"
}
}