diff --git a/Controllers/TarefaController.cs b/Controllers/TarefaController.cs
index ba96b30f..85f8ebd2 100644
--- a/Controllers/TarefaController.cs
+++ b/Controllers/TarefaController.cs
@@ -18,24 +18,28 @@ 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
+ var tarefa = _context.Tarefas.Find(id);
+
+ if (tarefa == null)
+ {
+ return NotFound();
+ }
+
return Ok();
}
[HttpGet("ObterTodos")]
public IActionResult ObterTodos()
{
- // TODO: Buscar todas as tarefas no banco utilizando o EF
- return Ok();
+ var tarefa = _context.Tarefas.Find();
+ 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
+ var tarefass = _context.Tarefas.Where(x => x.Titulo.Contains(titulo));
+
return Ok();
}
@@ -49,8 +53,6 @@ public IActionResult ObterPorData(DateTime data)
[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);
return Ok(tarefa);
}
@@ -59,9 +61,13 @@ public IActionResult ObterPorStatus(EnumStatusTarefa status)
public IActionResult Criar(Tarefa tarefa)
{
if (tarefa.Data == DateTime.MinValue)
+ {
return BadRequest(new { Erro = "A data da tarefa não pode ser vazia" });
+ }
+
+ _context.Add(tarefa);
+ _context.SaveChanges();
- // TODO: Adicionar a tarefa recebida no EF e salvar as mudanças (save changes)
return CreatedAtAction(nameof(ObterPorId), new { id = tarefa.Id }, tarefa);
}
@@ -71,14 +77,23 @@ public IActionResult Atualizar(int id, Tarefa tarefa)
var tarefaBanco = _context.Tarefas.Find(id);
if (tarefaBanco == null)
+ {
return NotFound();
+ }
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.Status = tarefa.Status;
+
+ _context.Tarefas.Update(tarefaBanco);
+ _context.SaveChanges();
+
+ return Ok(tarefaBanco);
}
[HttpDelete("{id}")]
@@ -87,9 +102,13 @@ public IActionResult Deletar(int id)
var tarefaBanco = _context.Tarefas.Find(id);
if (tarefaBanco == null)
+ {
return NotFound();
+ }
+
+ _context.Tarefas.Remove(tarefaBanco);
+ _context.SaveChanges();
- // TODO: Remover a tarefa encontrada através do EF e salvar as mudanças (save changes)
return NoContent();
}
}
diff --git a/Migrations/20240924114015_CriacaoTabelaTarefas.Designer.cs b/Migrations/20240924114015_CriacaoTabelaTarefas.Designer.cs
new file mode 100644
index 00000000..5b066e9f
--- /dev/null
+++ b/Migrations/20240924114015_CriacaoTabelaTarefas.Designer.cs
@@ -0,0 +1,54 @@
+//
+using System;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using TrilhaApiDesafio.Context;
+
+#nullable disable
+
+namespace TrilhaApiDesafio.Migrations
+{
+ [DbContext(typeof(OrganizadorContext))]
+ [Migration("20240924114015_CriacaoTabelaTarefas")]
+ partial class CriacaoTabelaTarefas
+ {
+ protected override void BuildTargetModel(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("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1);
+
+ b.Property("Data")
+ .HasColumnType("datetime2");
+
+ b.Property("Descricao")
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Status")
+ .HasColumnType("int");
+
+ b.Property("Titulo")
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("Tarefas");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/Migrations/20240924114015_CriacaoTabelaTarefas.cs b/Migrations/20240924114015_CriacaoTabelaTarefas.cs
new file mode 100644
index 00000000..5af09bbb
--- /dev/null
+++ b/Migrations/20240924114015_CriacaoTabelaTarefas.cs
@@ -0,0 +1,35 @@
+using System;
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace TrilhaApiDesafio.Migrations
+{
+ public partial class CriacaoTabelaTarefas : Migration
+ {
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "Tarefas",
+ columns: table => new
+ {
+ Id = table.Column(type: "int", nullable: false)
+ .Annotation("SqlServer:Identity", "1, 1"),
+ Titulo = table.Column(type: "nvarchar(max)", nullable: true),
+ Descricao = table.Column(type: "nvarchar(max)", nullable: true),
+ Data = table.Column(type: "datetime2", nullable: false),
+ Status = table.Column(type: "int", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Tarefas", x => x.Id);
+ });
+ }
+
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "Tarefas");
+ }
+ }
+}
diff --git a/Migrations/OrganizadorContextModelSnapshot.cs b/Migrations/OrganizadorContextModelSnapshot.cs
new file mode 100644
index 00000000..a03c0a7e
--- /dev/null
+++ b/Migrations/OrganizadorContextModelSnapshot.cs
@@ -0,0 +1,52 @@
+//
+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("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1);
+
+ b.Property("Data")
+ .HasColumnType("datetime2");
+
+ b.Property("Descricao")
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Status")
+ .HasColumnType("int");
+
+ b.Property("Titulo")
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("Tarefas");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/appsettings.Development.json b/appsettings.Development.json
index 9978cbfd..3b1c8279 100644
--- a/appsettings.Development.json
+++ b/appsettings.Development.json
@@ -6,6 +6,6 @@
}
},
"ConnectionStrings": {
- "ConexaoPadrao": "COLOCAR SUA CONNECTION STRING AQUI"
+ "ConexaoPadrao": "Server=localhost\\sqlexpress; Initial Catalog=Agenda;Integrated Security=True; TrustServerCertificate=True"
}
}
diff --git a/trilha-net-api-desafio.sln b/trilha-net-api-desafio.sln
new file mode 100644
index 00000000..8751533b
--- /dev/null
+++ b/trilha-net-api-desafio.sln
@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.5.002.0
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrilhaApiDesafio", "TrilhaApiDesafio.csproj", "{9D04E7BF-1241-408D-8643-EB42003F6F66}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {9D04E7BF-1241-408D-8643-EB42003F6F66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9D04E7BF-1241-408D-8643-EB42003F6F66}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9D04E7BF-1241-408D-8643-EB42003F6F66}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9D04E7BF-1241-408D-8643-EB42003F6F66}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {868517B6-BE2C-4A64-A459-C0A6BD200260}
+ EndGlobalSection
+EndGlobal