diff --git a/Controllers/TarefaController.cs b/Controllers/TarefaController.cs index ba96b30f..85af9b27 100644 --- a/Controllers/TarefaController.cs +++ b/Controllers/TarefaController.cs @@ -21,14 +21,20 @@ 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")] @@ -36,7 +42,8 @@ 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")] @@ -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); } @@ -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(); } @@ -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(); } } diff --git a/Migrations/20240920210559_CriacaoTabelaTarefa.Designer.cs b/Migrations/20240920210559_CriacaoTabelaTarefa.Designer.cs new file mode 100644 index 00000000..a0533c30 --- /dev/null +++ b/Migrations/20240920210559_CriacaoTabelaTarefa.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("20240920210559_CriacaoTabelaTarefa")] + partial class CriacaoTabelaTarefa + { + 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/20240920210559_CriacaoTabelaTarefa.cs b/Migrations/20240920210559_CriacaoTabelaTarefa.cs new file mode 100644 index 00000000..5cd4a6bd --- /dev/null +++ b/Migrations/20240920210559_CriacaoTabelaTarefa.cs @@ -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(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..5266015d --- /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/TrilhaApiDesafio.csproj b/TrilhaApiDesafio.csproj index ab338f7c..9f06437b 100644 --- a/TrilhaApiDesafio.csproj +++ b/TrilhaApiDesafio.csproj @@ -1,18 +1,22 @@ - - - - net6.0 - disable - enable - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - + + + + net6.0 + disable + enable + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + diff --git a/appsettings.Development.json b/appsettings.Development.json index 9978cbfd..34bd1559 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -6,6 +6,6 @@ } }, "ConnectionStrings": { - "ConexaoPadrao": "COLOCAR SUA CONNECTION STRING AQUI" + "ConexaoPadrao": "Server=localhost\\SQLEXPRESS;Database=Organizador;Trusted_Connection=True" } }