/Mr.Ares77
How to update mysql server data?
Hi, I'm building todo list project server.
I wanna update database but have got 500 error.
public async Task<bool> UpdateTodo(Guid userId, Guid todoId, TodoEntity todo)
{
var existingTodo = await _dbContext.todos
.Include(t => t.TodoItems)
.Where(t => t.DeletedAt == null && t.Id == todoId && t.UserId == userId)
.FirstOrDefaultAsync();
if (existingTodo != null)
{
existingTodo.UpdatedAt = DateTime.UtcNow;
existingTodo.DueDate = todo.DueDate;
existingTodo.Title = todo.Title;
existingTodo.TodoItems.Clear();
existingTodo.TodoItems = todo.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted
}).ToList();
_dbContext.todos.Update(existingTodo);
await _dbContext.SaveChangesAsync();
return true;
}
return false;
}
public async Task<bool> UpdateTodo(Guid userId, Guid todoId, TodoEntity todo)
{
var existingTodo = await _dbContext.todos
.Include(t => t.TodoItems)
.Where(t => t.DeletedAt == null && t.Id == todoId && t.UserId == userId)
.FirstOrDefaultAsync();
if (existingTodo != null)
{
existingTodo.UpdatedAt = DateTime.UtcNow;
existingTodo.DueDate = todo.DueDate;
existingTodo.Title = todo.Title;
existingTodo.TodoItems.Clear();
existingTodo.TodoItems = todo.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted
}).ToList();
_dbContext.todos.Update(existingTodo);
await _dbContext.SaveChangesAsync();
return true;
}
return false;
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateDate(Guid id, [FromBody] TodoRequestDto request)
{
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (string.IsNullOrEmpty(userIdClaim))
{
return Unauthorized("User not authenticated.");
}
var userId = Guid.Parse(userIdClaim);
var todo = new TodoEntity
{
DueDate = request.DueDate,
Title = request.Title,
TodoItems = request.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted,
}).ToList(),
};
var result = await _todoService.UpdateTodo(userId, id, todo);
return Ok(new { Success = result });
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateDate(Guid id, [FromBody] TodoRequestDto request)
{
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (string.IsNullOrEmpty(userIdClaim))
{
return Unauthorized("User not authenticated.");
}
var userId = Guid.Parse(userIdClaim);
var todo = new TodoEntity
{
DueDate = request.DueDate,
Title = request.Title,
TodoItems = request.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted,
}).ToList(),
};
var result = await _todoService.UpdateTodo(userId, id, todo);
return Ok(new { Success = result });
}
45 replies