23rd Commit
This commit is contained in:
		
							parent
							
								
									ee0d6ba78d
								
							
						
					
					
						commit
						c4354da63e
					
				
							
								
								
									
										16
									
								
								src/a3.nim
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								src/a3.nim
									
									
									
									
									
								
							| @ -181,6 +181,11 @@ import | |||||||
|     email: string |     email: string | ||||||
|     password: string |     password: string | ||||||
|     db = newDatabase() |     db = newDatabase() | ||||||
|  |     productName= "" | ||||||
|  |     quantity = 0 | ||||||
|  |     cart: seq[Cart] | ||||||
|  |     products: seq[Products] | ||||||
|  |     price = 0.0 | ||||||
| 
 | 
 | ||||||
|   try: |   try: | ||||||
|     email = ctx.cookies["email"] |     email = ctx.cookies["email"] | ||||||
| @ -190,13 +195,22 @@ import | |||||||
|     password = "" |     password = "" | ||||||
| 
 | 
 | ||||||
|   if email == "": |   if email == "": | ||||||
|  |     try: | ||||||
|  |       productName = ctx.queryParams["prod"] | ||||||
|  |       quantity = parseInt(ctx.queryParams["quantity"]) | ||||||
|  |     except: | ||||||
|  |       productName = "" | ||||||
|  |       quantity = 0 | ||||||
|  | 
 | ||||||
|  |     if productName == "": | ||||||
|       ctx.redirect("/login") |       ctx.redirect("/login") | ||||||
| 
 | 
 | ||||||
|  |     price = db.getPriceByProductName(productName) | ||||||
|  | 
 | ||||||
|   else: |   else: | ||||||
|     var |     var | ||||||
|       userId = db.getUserId(email, password) |       userId = db.getUserId(email, password) | ||||||
|     cart = db.getUserCart(userId) |     cart = db.getUserCart(userId) | ||||||
|       products: seq[Products] |  | ||||||
|        |        | ||||||
|     for c, d in cart: |     for c, d in cart: | ||||||
|       var product = db.getProductById(d.productId) |       var product = db.getProductById(d.productId) | ||||||
|  | |||||||
| @ -68,7 +68,7 @@ | |||||||
|       <div class="row mb-5"> |       <div class="row mb-5"> | ||||||
|         <div class="col-md-12"> |         <div class="col-md-12"> | ||||||
|           <div class="border p-4 rounded" role="alert"> |           <div class="border p-4 rounded" role="alert"> | ||||||
|             Returning customer? <a href="#">Click here</a> to login |             Returning customer? <a href="/login">Click here</a> to login | ||||||
|           </div> |           </div> | ||||||
|         </div> |         </div> | ||||||
|       </div> |       </div> | ||||||
| @ -88,6 +88,7 @@ | |||||||
|                 <option value="7">Bahrain</option>     |                 <option value="7">Bahrain</option>     | ||||||
|                 <option value="8">Colombia</option>     |                 <option value="8">Colombia</option>     | ||||||
|                 <option value="9">Dominican Republic</option>     |                 <option value="9">Dominican Republic</option>     | ||||||
|  |                 <option value="10">India</option> | ||||||
|               </select> |               </select> | ||||||
|             </div> |             </div> | ||||||
|             <div class="form-group row"> |             <div class="form-group row"> | ||||||
| @ -172,6 +173,7 @@ | |||||||
|                       <option value="7">Bahrain</option>     |                       <option value="7">Bahrain</option>     | ||||||
|                       <option value="8">Colombia</option>     |                       <option value="8">Colombia</option>     | ||||||
|                       <option value="9">Dominican Republic</option> |                       <option value="9">Dominican Republic</option> | ||||||
|  |                       <option value="10">India</option>     | ||||||
|                     </select> |                     </select> | ||||||
|                   </div> |                   </div> | ||||||
| 
 | 
 | ||||||
| @ -268,24 +270,14 @@ | |||||||
|                     <th>Total</th> |                     <th>Total</th> | ||||||
|                   </thead> |                   </thead> | ||||||
|                   <tbody> |                   <tbody> | ||||||
|                     {# <tr> |  | ||||||
|                       <td>Top Up T-Shirt <strong class="mx-2">x</strong> 1</td> |  | ||||||
|                       <td>$250.00</td> |  | ||||||
|                     </tr> |  | ||||||
|                     <tr> |  | ||||||
|                       <td>Polo Shirt <strong class="mx-2">x</strong>   1</td> |  | ||||||
|                       <td>$100.00</td> |  | ||||||
|                     </tr> |  | ||||||
|                     <tr> |  | ||||||
|                       <td class="text-black font-weight-bold"><strong>Cart Subtotal</strong></td> |  | ||||||
|                       <td class="text-black">$350.00</td> |  | ||||||
|                     </tr> |  | ||||||
|                     <tr> |  | ||||||
|                       <td class="text-black font-weight-bold"><strong>Order Total</strong></td> |  | ||||||
|                       <td class="text-black font-weight-bold"><strong>$350.00</strong></td> |  | ||||||
|                     </tr> #} |  | ||||||
| 
 |  | ||||||
|                     {% var total = 0.0 %} |                     {% var total = 0.0 %} | ||||||
|  |                     {% if email == "" %} | ||||||
|  |                     <tr> | ||||||
|  |                       <td>{{productName}} <strong class="mx-2">x</strong> {{quantity}}</td> | ||||||
|  |                       <td>₹{{toFloat(quantity)*price}}</td> | ||||||
|  |                       {% total = total + toFloat(quantity)*price %} | ||||||
|  |                     </tr> | ||||||
|  |                     {% else %} | ||||||
|                     {% for (id, product) in products.pairs() %} |                     {% for (id, product) in products.pairs() %} | ||||||
|                     <tr> |                     <tr> | ||||||
|                       <td>{{product.name}} <strong class="mx-2">x</strong> {{toFloat(cart[id].quantity)}}</td> |                       <td>{{product.name}} <strong class="mx-2">x</strong> {{toFloat(cart[id].quantity)}}</td> | ||||||
| @ -293,6 +285,7 @@ | |||||||
|                     </tr> |                     </tr> | ||||||
|                     {% total = total + toFloat(cart[id].quantity)*product.price %} |                     {% total = total + toFloat(cart[id].quantity)*product.price %} | ||||||
|                     {% endfor %} |                     {% endfor %} | ||||||
|  |                     {% endif %} | ||||||
|                     <tr> |                     <tr> | ||||||
|                       <td class="text-black font-weight-bold"><strong>Cart Subtotal</strong></td> |                       <td class="text-black font-weight-bold"><strong>Cart Subtotal</strong></td> | ||||||
|                       <td class="text-black">₹{{total}}</td> |                       <td class="text-black">₹{{total}}</td> | ||||||
|  | |||||||
| @ -106,7 +106,11 @@ | |||||||
|           </div> |           </div> | ||||||
| 
 | 
 | ||||||
|           </div> |           </div> | ||||||
|  |           {% if email != "" %} | ||||||
|           <p><a href="#" class="buy-now btn btn-sm btn-primary" onclick="addToCart()">Add To Cart</a></p> |           <p><a href="#" class="buy-now btn btn-sm btn-primary" onclick="addToCart()">Add To Cart</a></p> | ||||||
|  |           {% else %} | ||||||
|  |           <p><a href="#" class="buy-now btn btn-sm btn-primary" onclick="checkOut()">Check Out</a></p> | ||||||
|  |           {% endif %} | ||||||
| 
 | 
 | ||||||
|         </div> |         </div> | ||||||
|       </div> |       </div> | ||||||
| @ -195,5 +199,12 @@ | |||||||
|         + '?prod={{productName}}&quantity=' |         + '?prod={{productName}}&quantity=' | ||||||
|         + quantity; |         + quantity; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     function checkOut() { | ||||||
|  |       var quantity = document.getElementById("quantity").value; | ||||||
|  |       window.location.href = '/checkout' | ||||||
|  |         + '?prod={{productName}}&quantity=' | ||||||
|  |         + quantity; | ||||||
|  |     } | ||||||
|   </script> |   </script> | ||||||
| {% endblock %} | {% endblock %} | ||||||
|  | |||||||
| @ -79,3 +79,7 @@ proc getProductById*(db: DbConn, id: int): Products = | |||||||
| 
 | 
 | ||||||
| proc drop*(db: DbConn) = | proc drop*(db: DbConn) = | ||||||
|   db.exec(sql"DROP TABLE IF EXISTS products") |   db.exec(sql"DROP TABLE IF EXISTS products") | ||||||
|  | 
 | ||||||
|  | proc getPriceByProductName*(db: DbConn, name: string): float = | ||||||
|  |   var row = db.getRow(sql"SELECT price FROM products WHERE name = ?", name) | ||||||
|  |   return parseFloat(row[0]) | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user