C
C#2y ago
Azi

Help me again!

I got this error where it says
Error: response status is 500 Here's my code Repository:
public async Task<int> CreateCategory(Category category)
{
var sql = "INSERT INTO Category (Name, WorkspaceId) VALUES (@Name, @WorkspaceId)" +
"SELECT SCOPE_IDENTITY()";
using (var connection = _context.CreateConnection())
{
return await connection.ExecuteScalarAsync<int>(sql, new { category.Name, WorkspaceId = category.Workspace?.Id });
}
}
public async Task<int> CreateCategory(Category category)
{
var sql = "INSERT INTO Category (Name, WorkspaceId) VALUES (@Name, @WorkspaceId)" +
"SELECT SCOPE_IDENTITY()";
using (var connection = _context.CreateConnection())
{
return await connection.ExecuteScalarAsync<int>(sql, new { category.Name, WorkspaceId = category.Workspace?.Id });
}
}
Controller:
public async Task<IActionResult> CreateCategory([FromBody] CategoryCreationDto categoryDto)
{
try
{
var newCategory = await _categoryService.CreateCategory(categoryDto);
return CreatedAtRoute("GetWorkspace", new { id = newCategory.Id }, newCategory);
}
catch (Exception)
{
return StatusCode(500, "Something went wrong");
}
}
public async Task<IActionResult> CreateCategory([FromBody] CategoryCreationDto categoryDto)
{
try
{
var newCategory = await _categoryService.CreateCategory(categoryDto);
return CreatedAtRoute("GetWorkspace", new { id = newCategory.Id }, newCategory);
}
catch (Exception)
{
return StatusCode(500, "Something went wrong");
}
}
Service:
public async Task<Category> CreateCategory(CategoryCreationDto categoryToCreate)
{
var categoryModel = new Category
{
Name = categoryToCreate.Name,
};
categoryModel.Id = await _repository.CreateCategory(categoryModel);
return categoryModel;
}
public async Task<Category> CreateCategory(CategoryCreationDto categoryToCreate)
{
var categoryModel = new Category
{
Name = categoryToCreate.Name,
};
categoryModel.Id = await _repository.CreateCategory(categoryModel);
return categoryModel;
}
2 Replies
Pobiega
Pobiega2y ago
Use the debugger to inspect the exception being caught in the controller
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View