Skip to content

Commit 4c0c794

Browse files
Fix: resolvendo bug na criação de tarefa.
(retornava 500 mesmo inserindo a tarefa sem nenhum erro, com o código anterior)
1 parent bbfcfec commit 4c0c794

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

Services/TaskManagerServices.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,38 @@ public IActionResult GetById(int id)
111111

112112
public IActionResult InsertTask (TaskEntity model, int userId)
113113
{
114-
var category = context.CategorieTasks.FirstOrDefault(c => c.Id == model.CategorieTaskId);
114+
try
115+
{
116+
var category = context.CategorieTasks.FirstOrDefault(c => c.Id == model.CategorieTaskId);
115117

116-
if(category == null || model == null) return new BadRequestResult();
118+
if (category == null || model == null)
119+
{
120+
return BadRequest("Categoria ou modelo inválido.");
121+
}
117122

118-
TaskEntity updateTask = new ()
123+
TaskEntity updateTask = new TaskEntity
124+
{
125+
Title = model.Title,
126+
Description = model.Description,
127+
Done = false,
128+
CreatedAt = DateTime.Now,
129+
CategorieTaskId = model.CategorieTaskId,
130+
Category = category,
131+
UserId = userId
132+
};
133+
134+
context.Tasks.Add(updateTask);
135+
context.SaveChanges();
136+
137+
return Ok(new { taskId = updateTask.Id });
138+
}
139+
catch (Exception ex)
119140
{
120-
Title = model.Title,
121-
Description = model.Description,
122-
Done = false,
123-
CreatedAt = DateTime.Now,
124-
CategorieTaskId = model.CategorieTaskId,
125-
Category = context.CategorieTasks.FirstOrDefault(c => c.Id == model.CategorieTaskId),
126-
UserId = userId
127-
};
128-
129-
context.Tasks.Add(updateTask);
130-
context.SaveChanges();
141+
142+
Console.WriteLine($"Erro ao inserir tarefa: {ex}");
131143

132-
return Ok(updateTask);
144+
return StatusCode(500, "Ocorreu um erro ao criar a tarefa.");
145+
}
133146
}
134147

135148
public IActionResult EditTask (TaskEntity model, int id)

0 commit comments

Comments
 (0)