16th Commit
This commit is contained in:
		
							parent
							
								
									83aa2b543d
								
							
						
					
					
						commit
						ae020c1968
					
				
							
								
								
									
										
											BIN
										
									
								
								db5.sqlite3
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								db5.sqlite3
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										35
									
								
								src/a3.nim
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								src/a3.nim
									
									
									
									
									
								
							| @ -3,6 +3,7 @@ import | ||||
|   segfaults, | ||||
|   os, | ||||
|   nimja/parser, | ||||
|   strutils, | ||||
|   ./a3pkg/[models, mics], | ||||
|   ./a3c/[products, users, cart] | ||||
| 
 | ||||
| @ -102,11 +103,43 @@ import | ||||
| 
 | ||||
|     cart.userId = db.getUserId(email, password) | ||||
|     cart.productId = db.getProductByName(ctx.queryParams["prod"]).id | ||||
|        | ||||
|     cart.quantity = parseInt(ctx.queryParams["quantity"]) | ||||
| 
 | ||||
|     if cart.quantity == 0: | ||||
|       cart.quantity = 1 | ||||
|          | ||||
|     db.addToCart(cart) | ||||
| 
 | ||||
|     ctx.redirect("/cart") | ||||
| 
 | ||||
| "/remove-from-cart" -> get: | ||||
|    | ||||
|   var | ||||
|     email: string | ||||
|     password: string | ||||
|     db = newDatabase() | ||||
| 
 | ||||
|   try: | ||||
|     email = ctx.cookies["email"] | ||||
|     password = ctx.cookies["password"] | ||||
|   except: | ||||
|     email = "" | ||||
|     password = "" | ||||
| 
 | ||||
|   if email == "": | ||||
|     ctx.redirect("/login") | ||||
| 
 | ||||
|   else: | ||||
|     var | ||||
|       cart: Cart | ||||
| 
 | ||||
|     cart.userId = db.getUserId(email, password) | ||||
|     cart.productId = db.getProductByName(ctx.queryParams["prod"]).id | ||||
|        | ||||
|     db.removeFromCart(cart) | ||||
| 
 | ||||
|     ctx.redirect("/cart") | ||||
| 
 | ||||
| "/checkout" -> get: | ||||
|    | ||||
|   var | ||||
|  | ||||
| @ -117,54 +117,6 @@ | ||||
|                 </tr> | ||||
|               </thead> | ||||
|               <tbody> | ||||
|                 {# <tr> | ||||
|                   <td class="product-thumbnail"> | ||||
|                     <img src="/a3b/images/cloth_1.jpg" alt="Image" class="img-fluid"> | ||||
|                   </td> | ||||
|                   <td class="product-name"> | ||||
|                     <h2 class="h5 text-black">Top Up T-Shirt</h2> | ||||
|                   </td> | ||||
|                   <td>$49.00</td> | ||||
|                   <td> | ||||
|                     <div class="input-group mb-3" style="max-width: 120px;"> | ||||
|                       <div class="input-group-prepend"> | ||||
|                         <button class="btn btn-outline-primary js-btn-minus" type="button">−</button> | ||||
|                       </div> | ||||
|                       <input type="text" class="form-control text-center" value="1" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1"> | ||||
|                       <div class="input-group-append"> | ||||
|                         <button class="btn btn-outline-primary js-btn-plus" type="button">+</button> | ||||
|                       </div> | ||||
|                     </div> | ||||
| 
 | ||||
|                   </td> | ||||
|                   <td>$49.00</td> | ||||
|                   <td><a href="#" class="btn btn-primary btn-sm">X</a></td> | ||||
|                 </tr> | ||||
| 
 | ||||
|                 <tr> | ||||
|                   <td class="product-thumbnail"> | ||||
|                     <img src="/a3b/images/cloth_2.jpg" alt="Image" class="img-fluid"> | ||||
|                   </td> | ||||
|                   <td class="product-name"> | ||||
|                     <h2 class="h5 text-black">Polo Shirt</h2> | ||||
|                   </td> | ||||
|                   <td>$49.00</td> | ||||
|                   <td> | ||||
|                     <div class="input-group mb-3" style="max-width: 120px;"> | ||||
|                       <div class="input-group-prepend"> | ||||
|                         <button class="btn btn-outline-primary js-btn-minus" type="button">−</button> | ||||
|                       </div> | ||||
|                       <input type="text" class="form-control text-center" value="1" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1"> | ||||
|                       <div class="input-group-append"> | ||||
|                         <button class="btn btn-outline-primary js-btn-plus" type="button">+</button> | ||||
|                       </div> | ||||
|                     </div> | ||||
| 
 | ||||
|                   </td> | ||||
|                   <td>$49.00</td> | ||||
|                   <td><a href="#" class="btn btn-primary btn-sm">X</a></td> | ||||
|                 </tr> #} | ||||
| 
 | ||||
|                 {% for (id, product) in products.pairs() %} | ||||
|                 <tr> | ||||
|                   <td class="product-thumbnail"> | ||||
| @ -179,7 +131,7 @@ | ||||
|                       <div class="input-group-prepend"> | ||||
|                         <button class="btn btn-outline-primary js-btn-minus" type="button">−</button> | ||||
|                       </div> | ||||
|                       <input type="text" class="form-control text-center" value="1" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1"> | ||||
|                       <input type="text" id="quantity" class="form-control text-center" value="{{cart[id].quantity}}" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1"> | ||||
|                       <div class="input-group-append"> | ||||
|                         <button class="btn btn-outline-primary js-btn-plus" type="button">+</button> | ||||
|                       </div> | ||||
| @ -187,7 +139,7 @@ | ||||
| 
 | ||||
|                   </td> | ||||
|                   <td>₹{{product.price}}</td> | ||||
|                   <td><a href="#" class="btn btn-primary btn-sm">X</a></td> | ||||
|                   <td><a href="/remove-from-cart?prod={{product.name}}" class="btn btn-primary btn-sm">X</a></td> | ||||
|                 </tr> | ||||
|                 {% endfor %} | ||||
|               </tbody> | ||||
| @ -255,4 +207,12 @@ | ||||
|       </div> | ||||
|     </div> | ||||
|   </div> | ||||
|   <script> | ||||
|     function removeFromCart(productName) { | ||||
|       var quantity = document.getElementById("quantity").value; | ||||
|       window.location.href = '/remove-from-cart' | ||||
|         + '?prod=' | ||||
|         + productName; | ||||
|     } | ||||
|   </script> | ||||
| {% endblock %} | ||||
| @ -40,4 +40,13 @@ proc getUserCart*(db: DbConn, userId: int): seq[Cart] = | ||||
|   return cartDetails | ||||
| 
 | ||||
| proc addToCart*(db: DbConn, cart: Cart) = | ||||
|   db.exec(sql"INSERT INTO cart (user_id, product_id, quantity) VALUES (?, ?, ?)", cart.userId, cart.productId, cart.quantity) | ||||
|   var cartDetails = getUserCart(db, cart.userId) | ||||
|   for d, f in cartDetails: | ||||
|     if f.userId == cart.userId and f.productId == cart.productId: | ||||
|       db.exec(sql"UPDATE cart SET quantity=? WHERE user_id=? AND product_id=?", f.quantity + cart.quantity, cart.userId, cart.productId) | ||||
|       return | ||||
| 
 | ||||
|   db.exec(sql"INSERT INTO cart (user_id, product_id, quantity) VALUES (?, ?, ?)", cart.userId, cart.productId, cart.quantity) | ||||
| 
 | ||||
| proc removeFromCart*(db: DbConn, cart: Cart) = | ||||
|   db.exec(sql"DELETE FROM cart WHERE user_id=? AND product_id=?", cart.userId, cart.productId) | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user