public async Task <ActionResult <ChartVM> > CreateChart(ChartVM chartVM) { try { if (chartVM == null) { return(BadRequest()); } // Add custom model validation error Chart chart = await chartRepository.GetChartByname(chartVM.Chart); if (chart != null) { ModelState.AddModelError("Name", $"Chart name: {chartVM.Chart.Name} already in use"); return(BadRequest(ModelState)); } await chartRepository.CreateChart(chartVM); return(CreatedAtAction(nameof(GetChart), new { id = chartVM.Chart.Id }, chartVM)); } catch (DbUpdateException Ex) { return(StatusCode(StatusCodes.Status500InternalServerError, Ex.InnerException.Message)); } }