public async Task <IActionResult> PostJobAsync([FromBody] PostJobRequest request) { var response = new SingleResponse <Job>(); try { var existingEntity = await DbContext .GetJobsByJobTitleAsync(new Job { JobTitle = request.JobTitle }); if (existingEntity != null) { ModelState.AddModelError("JobTitle", "Job already exists"); } if (!ModelState.IsValid) { return(BadRequest()); } // Create entity from request model var entity = request.ToEntity(); // Add entity to repository DbContext.Add(entity); // Save entity in database await DbContext.SaveChangesAsync(); // Set the entity to response model response.Model = entity; } catch (Exception ex) { response.DidError = true; response.ErrorMessage = "There was an internal error"; Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(PostJobAsync), ex); } return(response.ToHttpCreatedResponse()); }