/ presentation.md
presentation.md
  1  ---
  2  theme: default
  3  paginate: true
  4  marp: true
  5  ---
  6  
  7  ![bg left](./.assets/splash.jpg)
  8  
  9  # Mini Project
 10  
 11  BILLY Maxime - CHATELAIN Clément
 12  
 13  ---
 14  
 15  ## Table of Contents
 16  
 17  1. Database Model and Schema
 18  2. XML Database
 19  3. Simple Scenarios
 20      1. List Instructions from a recipe
 21      2. List available menus
 22      3. List oreders for a given customer
 23  4. Complex Scenarios
 24      1. List recipes by cuisine type
 25      2. Export Statistics to JSON
 26  5. Python Scenario
 27  5. Conclusion
 28  
 29  ---
 30  
 31  ## Database Model
 32  
 33  ![h:500](./.assets/database_model.png)
 34  
 35  ---
 36  
 37  ## XML Schema
 38  
 39  #### Table Structure
 40  
 41  ```xml
 42  <xs:element name="comments">
 43      <xs:complexType>
 44          <xs:sequence>
 45              <xs:element name="comment" minOccurs="1" maxOccurs="unbounded">
 46                  <xs:complexType>
 47                      <xs:sequence>
 48                          <xs:element name="text" type="stringNotEmpty"/>
 49                          <xs:element name="rating" type="xs:integer"/>
 50                      </xs:sequence>
 51                      <xs:attribute name="id" type="xs:ID" use="required"/>
 52                      <xs:attribute name="recipeId" type="xs:IDREF" use="required"/>
 53                      <xs:attribute name="customerId" type="xs:IDREF" use="required"/>
 54                  </xs:complexType>
 55              </xs:element>
 56          </xs:sequence>
 57      </xs:complexType>
 58  </xs:element>
 59  ```
 60  
 61  ---
 62  
 63  ## XML Schema
 64  
 65  #### Keys and KeyRefs
 66  
 67  ```xml
 68  <xs:keyref name="recipeIdRef2" refer="recipeIdKey">
 69      <xs:selector xpath="comments/comment"/>
 70      <xs:field xpath="@recipeId"/>
 71  </xs:keyref>
 72  
 73  <xs:keyref name="customerIdRef2" refer="customerIdKey">
 74      <xs:selector xpath="comments/comment"/>
 75      <xs:field xpath="@customerId"/>
 76  </xs:keyref>
 77  ```
 78  
 79  ---
 80  
 81  ## XML Database
 82  
 83  #### Comments
 84  
 85  ```xml
 86  <comments>
 87      <comment id="com101" recipeId="r101" customerId="c101">
 88          <text>Loved the flavors! Perfect balance of spices.</text>
 89          <rating>5</rating>
 90      </comment>
 91      <comment id="com102" recipeId="r101" customerId="c102">
 92          <text>Delicious! I will definitely make this again.</text>
 93          <rating>4</rating>
 94      </comment>
 95  </comments>
 96  ```
 97  
 98  ---
 99  
100  ## Simple Scenarios
101  
102  #### List Instructions from a recipe
103  
104  Find the name of a recipe by its id.
105  ```xml
106  <xsl:value-of select="recipeBox/recipes/recipe[@id='r101']/title"/>
107  ```
108  
109  List the instructions for a recipe by its id.
110  ```xml
111  <xsl:for-each select="recipeBox/recipes/recipe[@id='r101']/instructions/step">
112      <xsl:value-of select="."/>
113  </xsl:for-each>
114  ```
115  
116  ---
117  
118  ## Simple Scenarios
119  
120  #### List available menus
121  
122  List all the menus and retrieve specific fields.
123  ```xml
124  <xsl:for-each select="recipeBox/menus/menu">
125      <h3>Menu: <xsl:value-of select="@name"/></h3>
126      <xsl:for-each select="meal">
127          <xsl:sort select="substring-before('MondayTuesdayWednesdayThursdayFridaySaturdaySunday', @day)" data-type="number"/>
128          <xsl:for-each select="/recipeBox/recipes/recipe[@id=current()/@recipeId]">
129              <xsl:value-of select="title"/>
130          </xsl:for-each>
131      </xsl:for-each>
132  </xsl:for-each>
133  ```
134  
135  ---
136  
137  ## Simple Scenarios
138  
139  #### List orders for a given customer
140  
141  Get a customer's name by its id.
142  ```xml
143  <xsl:value-of select="recipeBox/customers/customer[@id='c101']/name"/>
144  ```
145  
146  List all the orders for a customer by its id.
147  ```xml
148  <xsl:for-each select="recipeBox/orders/order[@customerId='c101']">
149      <xsl:value-of select="@date"/>
150      <xsl:value-of select="deliveryAddress"/>
151      <xsl:value-of select="status"/>
152  </xsl:for-each>
153  ```
154  
155  ---
156  
157  ## Complex Scenarios
158  
159  #### List recipes by cuisine type
160  
161  Setting the recipe count variable.
162  ```xml
163  <xsl:key name="recipes-by-cuisine" match="recipe" use="cuisine"/>
164  ```
165  
166  Listing all cuisine types.
167  ```xml
168  <cuisines>
169      <xsl:for-each select="recipes/recipe[count(. | key('recipes-by-cuisine', cuisine)[1]) = 1]">
170  ```
171  
172  ---
173  
174  ## Complex Scenarios
175  
176  #### List recipes by cuisine type
177  
178  Creating the cuisine element.
179  ```xml
180  <cuisine name="{cuisine}">
181  ```
182  
183  Listing all recipes for a cuisine.
184  ```xml
185  <xsl:for-each select="key('recipes-by-cuisine', cuisine)">
186  ```
187  
188  ---
189  
190  ## Complex Scenarios
191  
192  #### List recipes by cuisine type
193  
194  Creating the recipe element.
195  ```xml
196  <recipe id="{@id}">
197      <title><xsl:value-of select="title"/></title>
198      ...
199      <instructions>
200          <xsl:for-each select="instructions/step">
201              <step><xsl:value-of select="."/></step>
202          </xsl:for-each>
203      </instructions>
204  </recipe>
205  ```
206  
207  ---
208  
209  ## Complex Scenarios
210  
211  #### Export Statistics to JSON
212  
213  Defining variables for the Statistics.
214  ```xml
215  <xsl:variable name="recipeCount" select="count(//recipe)"/>
216  ...
217  <xsl:variable name="commentCount" select="count(//comment)"/>
218  ```
219  
220  ---
221  
222  ## Complex Scenarios
223  
224  #### Export Statistics to JSON
225  
226  Exporting the Statistics to JSON.
227  ```xml
228  <xsl:text>{</xsl:text>
229      <xsl:text>"statistics": {</xsl:text>
230          <xsl:text>"recipeCount": </xsl:text><xsl:value-of select="$recipeCount"/>,
231          ...
232          <xsl:text>"commentCount": </xsl:text><xsl:value-of select="$commentCount"/>
233      <xsl:text>},</xsl:text>
234  ```
235  
236  Renders like this:
237  ```json
238  {
239      "statistics": {
240          "recipeCount": 3,
241          ...
242          "commentCount": 2
243      },
244  ```
245  
246  ---
247  
248  ## Complex Scenarios
249  
250  #### Export Statistics to JSON
251  
252  Adding data to the JSON.
253  ```xml
254  <xsl:text>"data": {</xsl:text>
255      <xsl:text>"recipes": [</xsl:text>
256          <xsl:apply-templates select="//recipe"/>
257      <xsl:text>],</xsl:text>
258      ...
259  ```
260  
261  Using a template to render the elemnts.
262  ```xml
263  <xsl:template match="recipe">
264      <xsl:text>{"title": "</xsl:text><xsl:value-of select="title"/> ... }</xsl:text>
265      <xsl:if test="position() != last()">,</xsl:if>
266  </xsl:template>
267  ```
268  
269  ---
270  
271  ## Python Scenario
272  
273  #### Order System
274  
275  1. Login user by it's email.
276  2. List all the available menus.
277  3. Add one menu to the cart.
278  4. Choose a delivery person.
279  5. Validate the order.
280  
281  ---
282  
283  ## Python Scenario
284  
285  #### Manipulating the XML
286  
287  ```python
288  import xml.etree.ElementTree as ET
289  
290  tree = ET.parse('recipeBox.xml')
291  root = tree.getroot()
292  ```
293  
294  ---
295  
296  ## Python Scenario
297  
298  #### Login user by it's email.
299  
300  ```python
301  email = input("Enter your email: ")
302  
303  user = None
304  
305  for customer in root.findall('customers/customer'):
306      if customer.find('email').text == email:
307          user = customer
308          break
309  
310  if user is None:
311      raise ValueError("User not found")
312  
313  print(f"\nWelcome {user.find('name').text}!")
314  ```
315  
316  ---
317  
318  ## Python Scenario
319  
320  #### List all the available menus.
321  
322  ```python
323  for i, menu in enumerate(root.findall('menus/menu')):
324      print(f"{i+1}. {menu.get('name')}")
325      # for each menu, print the recipes
326      for meal in menu.findall('meal'):
327          recipe = root.find(f"recipes/recipe[@id='{meal.get('recipeId')}']")
328          print(f"  - {recipe.find('title').text}")
329  
330      print()
331  ```
332  
333  ---
334  
335  ## Python Scenario
336  
337  #### Add one menu to the cart.
338  
339  ```python
340  print("Which menu would you like to order?\n")
341  
342  menu = int(input("Enter the number of the menu: "))
343  
344  if menu < 1 or menu > len(root.findall('menus/menu')):
345      raise ValueError("Invalid menu number")
346  
347  menu = root.findall('menus/menu')[menu-1]
348  
349  print(f"\nYou have selected the {menu.get('name')} menu. Here are the recipes:\n")
350  
351  for meal in menu.findall('meal'):
352      recipe = root.find(f"recipes/recipe[@id='{meal.get('recipeId')}']")
353      print(f"  - {recipe.find('title').text}")
354  ```
355  
356  --- 
357  
358  ## Python Scenario
359  
360  #### Choose a delivery person.
361  
362  ```python
363  print("\nWhich deliveryPerson would you like to deliver your order?\n")
364  
365  for i, deliveryPerson in enumerate(root.findall('deliveryPeople/deliveryPerson')):
366      print(f"{i+1}. {deliveryPerson.find('name').text}")
367  
368  print()
369  deliveryPerson = int(input("Enter the number of the deliveryPerson: "))
370  
371  if deliveryPerson < 1 or deliveryPerson > len(root.findall('deliveryPeople/deliveryPerson')):
372      raise ValueError("Invalid deliveryPerson number")
373  
374  deliveryPerson = root.findall('deliveryPeople/deliveryPerson')[deliveryPerson-1]
375  
376  print(f"\nYou have selected {deliveryPerson.find('name').text} to deliver your order.")
377  ```
378  
379  ---
380  
381  ## Python Scenario
382  
383  #### Validate the order.
384  
385  ```python
386  print("\nOrder Confirmation:\n")
387  print(f"Menu: {menu.get('name')}")
388  for meal in menu.findall('meal'):
389      recipe = root.find(f"recipes/recipe[@id='{meal.get('recipeId')}']")
390      print(f"  - {recipe.find('title').text}")
391  
392  print(f"Delivery Person: {deliveryPerson.find('name').text}")
393  print(f"Delivery Address: {user.find('address').text}")
394  
395  confirmation = input("\nDo you confirm your order? (yes/no): ")
396  
397  if confirmation.lower() in ['yes', 'y']:
398      print("\nYour order has been confirmed. Thank you!")
399  else:
400      print("\nYour order has been cancelled. Thank you!")
401  
402  print(f"\nYou can contact {deliveryPerson.find('name').text} at {deliveryPerson.find('phone').text} for any questions.")
403  ```
404  
405  ---
406  
407  ![bg right](./.assets/splash.jpg)
408  
409  # Thank you for your attention !