public async Task <IActionResult> AddIngredient([FromBody] IngredientNew ingredient) { try { var retVal = await ContentProcesses.CreateIngredientAsync(ingredient); switch (retVal.State) { case CallReturnState.Success: return(Ok(retVal)); case CallReturnState.Warning: case CallReturnState.ValidationError: return(BadRequest(retVal.Errors)); case CallReturnState.Failure: return(StatusCode((int)HttpStatusCode.InternalServerError, retVal.Errors)); } } catch (Exception ex) { return(StatusCode((int)HttpStatusCode.InternalServerError, ex)); } return(StatusCode((int)HttpStatusCode.InternalServerError)); }
internal static async Task <Ingredient> CreateAsync(IngredientNew ingredient) { Ingredient addedIngredient = null; int id; try { using (var connection = new SqlConnection(GlobalSettings.DbConnectionString)) { using (var cmd = new SqlCommand(StoredProc.Ingredient_Create, connection)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(Parameter.Id, SqlDbType.Int).Direction = ParameterDirection.Output; cmd.Parameters.AddWithValue(Parameter.Ingredient, ingredient.Ingredient); cmd.Parameters.AddWithValue(Parameter.RecipeId, ingredient.RecipeId); await connection.OpenAsync(); await cmd.ExecuteNonQueryAsync(); id = Int32.Parse(cmd.Parameters[Parameter.Id].Value.ToString()); addedIngredient = await GetAsync(id); } } } catch (Exception) { throw; } return(addedIngredient); }
public static async Task <CallReturn <Ingredient> > CreateIngredientAsync(IngredientNew ingredient) { var retVal = new CallReturn <Ingredient>(); try { retVal.Object = await IngredientDBAsync.CreateAsync(new IngredientNew { Ingredient = ingredient.Ingredient, RecipeId = ingredient.RecipeId }); } catch (Exception ex) { Console.WriteLine(ex); retVal.SetError(ErrorType.SystemError, ex); } return(retVal); }