C#C
C#4y 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 });
            }
        }


Controller:
[HttpPost]
        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;
        }
Was this page helpful?