public virtual async Task <ActionResult <TResource> > CreateOrUpdate( string recipeName, TResource entity, ApiVersion version) { string decodedRecipeName = Recipe.FromUrlSafeNameToOrdinaryName(recipeName); TResourceKey key = GetKey(entity); if (await _repo.ExistsAsync(decodedRecipeName, key)) { TResource?updatedEntity = await _repo.CreateOrUpdateAsync(decodedRecipeName, entity); if (updatedEntity is null) { return(BadRequest()); } return(Ok(updatedEntity)); } TResource?createdEntity = await _repo.CreateOrUpdateAsync(decodedRecipeName, entity); if (createdEntity is null) { return(BadRequest()); } return(CreatedAtRoute( routeName: $"{nameof(Get)}{typeof(TResource).Name}", routeValues: new { id = GetKey(createdEntity), recipeName = decodedRecipeName }, value: createdEntity)); }
public override async Task <Recipe?> CreateOrUpdateAsync(string recipeName, Recipe toStore) { await using var db = new NpgsqlConnection(ConnectionString); int recipeId = await GetRecipeId(recipeName, db); try { string idQuery = EntityKeyIsNull(toStore) ? "default" : ":Id"; AddTypeHandlers(); Recipe?result = await CreateOrUpdateSendQueryAsync(toStore, db, idQuery, recipeId); foreach (Ingredient ingredient in toStore.Ingredients) { Ingredient?storedIngredient = await _ingredientRepository.CreateOrUpdateAsync(toStore.Name, ingredient); if (storedIngredient is null) { continue; } result?.Ingredients.Add(storedIngredient); } foreach (Step step in toStore.Steps) { Step?storedStep = await _stepRepository.CreateOrUpdateAsync(toStore.Name, step); if (storedStep is null) { continue; } result?.Steps.Add(storedStep); } return(result); } catch (NpgsqlException e) { if (e.SqlState != "23505") { throw; } Logger.LogWarning("Could not create or update resource because of column conflict"); return(null); } }