diff --git a/Controllers/TarefaController.cs b/Controllers/TarefaController.cs index ba96b30f..06994a84 100644 --- a/Controllers/TarefaController.cs +++ b/Controllers/TarefaController.cs @@ -18,25 +18,43 @@ 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(); + // Implementado + 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(); + // Implementado + var tarefa = _context.Tarefas.ToList(); + + if (tarefa == null || tarefa.Count == 0) + { + return NotFound("Nenhuma tarefa encontrada"); + } + + 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(); + // Implementado + var tarefa = _context.Tarefas.Where(t => t.Titulo.Contains(titulo)).ToList(); + + if (tarefa == null || tarefa.Count == 0) + { + return NotFound("Pesquisa por titulo não encontrada"); + } + + return Ok(tarefa); } [HttpGet("ObterPorData")] @@ -49,9 +67,13 @@ 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); + // Implementado + var tarefa = _context.Tarefas.Where(x => x.Status == status).ToList(); + + if (tarefa == null || tarefa.Count == 0) + { + return NotFound("Pesquisa por status não encontrada"); + } return Ok(tarefa); } @@ -61,7 +83,10 @@ 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) + // Implementado + _context.Tarefas.Add(tarefa); + _context.SaveChanges(); + return CreatedAtAction(nameof(ObterPorId), new { id = tarefa.Id }, tarefa); } @@ -76,9 +101,16 @@ 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(); + // Implementado + tarefaBanco.Titulo = tarefa.Titulo; + tarefaBanco.Status = tarefa.Status; + tarefaBanco.Descricao = tarefa.Descricao; + tarefaBanco.Data = tarefa.Data; + + _context.Tarefas.Update(tarefaBanco); + _context.SaveChanges(); + + return Ok(tarefaBanco); } [HttpDelete("{id}")] @@ -89,7 +121,10 @@ 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) + // Implementado + _context.Tarefas.Remove(tarefaBanco); + _context.SaveChanges(); + return NoContent(); } } diff --git a/TrilhaApiDesafio.csproj b/TrilhaApiDesafio.csproj index ab338f7c..d3a0e026 100644 --- a/TrilhaApiDesafio.csproj +++ b/TrilhaApiDesafio.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 disable enable diff --git a/TrilhaApiDesafio.sln b/TrilhaApiDesafio.sln new file mode 100644 index 00000000..b66931a1 --- /dev/null +++ b/TrilhaApiDesafio.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrilhaApiDesafio", "TrilhaApiDesafio.csproj", "{0CFC3774-11B6-48BA-8F21-333FCE752975}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0CFC3774-11B6-48BA-8F21-333FCE752975}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0CFC3774-11B6-48BA-8F21-333FCE752975}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0CFC3774-11B6-48BA-8F21-333FCE752975}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0CFC3774-11B6-48BA-8F21-333FCE752975}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {359C325E-15BB-4B55-9E2F-1BCF6EAB4D7D} + EndGlobalSection +EndGlobal diff --git a/appsettings.Development.json b/appsettings.Development.json index 9978cbfd..69c0bf51 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=Organizador; Integrated Security=True" } } diff --git a/appsettings.json b/appsettings.json index 10f68b8c..ec04bc12 100644 --- a/appsettings.json +++ b/appsettings.json @@ -6,4 +6,4 @@ } }, "AllowedHosts": "*" -} +} \ No newline at end of file