##Usage
Create a new empty ServiceStack v4 console application. Paste in program.cs. Copy index.html to the bin directory and run. Then simply navigate to http://localhost:8081/index.html.
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Create Recipe</title> | |
| <style> | |
| body { padding: 30px; margin: 0; font-family: helvetica, arial, verdana; font-size: 16px; } | |
| h1,h2,h3 { padding: 0; margin: 0; line-height: normal;} | |
| label { display: inline-block; width: 140px; text-align: right; margin-right: 5px; } | |
| form { margin-top: 20px; } | |
| #RecipeName {font-size: 22px; } | |
| #Ingredients { padding: 10px; background-color: #f0f0f0; } | |
| #Steps { background-color: #f8f8f8; padding: 10px 10px 0 10px; margin-bottom: 20px; } | |
| .step { padding: 10px; border-bottom: 1px solid #dfdfdf; } | |
| .step:last-child { border-bottom: none; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>Create Recipe</h1> | |
| <form action="/Recipes" method="POST" enctype="multipart/form-data"> | |
| <!-- Recipe Name --> | |
| <input type="text" name="Name" value="Pancakes" id="RecipeName" /><br/> | |
| <!-- Ingredients --> | |
| <div id="Ingredients"> | |
| <h2>Ingredients</h2> | |
| <div class="ingredient"> | |
| <label>Name</label><input type="text" name="IngredientName" value="Flour" /> | |
| <label>Quantity</label><input type="text" name="IngredientQuantityName" value="Grams" /> | |
| <label>Value</label><input type="text" name="IngredientQuantityValue" value="120" /> | |
| </div> | |
| <div class="ingredient"> | |
| <label>Name</label><input type="text" name="IngredientName" value="Water" /> | |
| <label>Quantity</label><input type="text" name="IngredientQuantityName" value="Milliletres" /> | |
| <label>Value</label><input type="text" name="IngredientQuantityValue" value="500" /> | |
| </div> | |
| </div> | |
| <!-- Steps --> | |
| <div id="Steps"> | |
| <h2>Steps</h2> | |
| <div class="step"> | |
| <label>Name</label><input type="text" name="StepName" value="1 - Prepare" /><br/> | |
| <label>Description</label><input type="text" name="StepDescription" value="Measure 120 grams of flour ..." /><br/> | |
| <label>Duration</label><input type="text" name="StepDuration" value="15" /><br/> | |
| </div> | |
| <div class="step"> | |
| <label>Name</label><input type="text" name="StepName" value="2 - Mix" /><br/> | |
| <label>Description</label><input type="text" name="StepDescription" value="Add the ingredients to a large bowl and mix" /><br/> | |
| <label>Duration</label><input type="text" name="StepDuration" value="5" /><br/> | |
| </div> | |
| <div class="step"> | |
| <label>Name</label><input type="text" name="StepName" value="3 - Bake" /><br/> | |
| <label>Description</label><input type="text" name="StepDescription" value="Place the tray into an oven for about 25 minutes" /><br/> | |
| <label>Duration</label><input type="text" name="StepDuration" value="25" /><br/> | |
| </div> | |
| </div> | |
| <input type="submit" value="Create Recipe"/> | |
| </form> | |
| </body> | |
| </html> |
| using System; | |
| using ServiceStack; | |
| namespace Tests.Recipe | |
| { | |
| class MainClass | |
| { | |
| public static void Main() | |
| { | |
| // Very basic console host | |
| var appHost = new AppHost(500); | |
| appHost.Init(); | |
| appHost.Start("http://*:8081/"); | |
| Console.ReadKey(); | |
| } | |
| } | |
| public class AppHost : AppHostHttpListenerPoolBase | |
| { | |
| public AppHost(int poolSize) : base("Recipe Service", poolSize, typeof(RecipeService).Assembly) | |
| { | |
| } | |
| public override void Configure(Funq.Container container) | |
| { | |
| Config = new HostConfig { | |
| DebugMode = true | |
| }; | |
| } | |
| } | |
| [Route("/Recipes", "POST")] | |
| public class CreateRecipeRequest : IReturn<Recipe> | |
| { | |
| public string Name { get; set; } | |
| public string[] StepName { get; set; } | |
| public string[] StepDescription { get; set; } | |
| public int[] StepDuration { get; set; } | |
| public string[] IngredientName { get; set; } | |
| public string[] IngredientQuantityName { get; set; } | |
| public decimal[] IngredientQuantityValue { get; set; } | |
| } | |
| public class Recipe | |
| { | |
| public string Name { get; set; } | |
| public Ingredient[] Ingredients { get; set; } | |
| public Step[] Steps { get; set; } | |
| } | |
| public class Ingredient | |
| { | |
| public string Name { get; set; } | |
| public string QuantityName { get; set; } | |
| public decimal Quantity { get; set; } | |
| } | |
| public class Step | |
| { | |
| public string Name { get; set; } | |
| public string Description { get; set; } | |
| public int Duration { get; set; } | |
| } | |
| public class RecipeService : Service | |
| { | |
| public Recipe Post(CreateRecipeRequest request) | |
| { | |
| // Convert the flat CreateRecipeRequest object to a structured Recipe object | |
| // Steps | |
| int numberOfSteps = request.StepName.Length; | |
| // Ingredients | |
| int numberOfIngredients = request.IngredientName.Length; | |
| // Check there is a description and duration for all steps | |
| if(request.StepDescription == null || request.StepDescription.Length != numberOfSteps || request.StepDuration == null || request.StepDuration.Length != numberOfSteps) | |
| throw new Exception("There must be a duration and description for all steps"); | |
| // Create the Recipe object | |
| var recipe = new Recipe { Name = request.Name, Steps = new Step[numberOfSteps], Ingredients = new Ingredient[numberOfIngredients] }; | |
| for(int s = 0; s < numberOfSteps; s++) | |
| recipe.Steps[s] = new Step { Name = request.StepName[s], Description = request.StepDescription[s], Duration = request.StepDuration[s] }; | |
| // Check there is a quantity type and quantity value for all ingredients | |
| if(request.IngredientQuantityName == null || request.IngredientQuantityName.Length != numberOfIngredients || request.IngredientQuantityValue == null || request.IngredientQuantityValue.Length != numberOfIngredients) | |
| throw new Exception("The quantity must be provided for each ingredient"); | |
| for(int i = 0; i < numberOfIngredients; i++) | |
| recipe.Ingredients[i] = new Ingredient { Name = request.IngredientName[i], QuantityName = request.IngredientQuantityName[i], Quantity = request.IngredientQuantityValue[i] }; | |
| /* | |
| * Recipe can now be accessed through a logical collection: | |
| * | |
| * recipe.Name | |
| * recipe.Steps[0].Name | |
| * recipe.Ingredients[1].Quantity | |
| * | |
| */ | |
| return recipe; | |
| } | |
| } | |
| } |