Created
September 12, 2019 10:29
-
-
Save amitastreait/67ea705997c9c292efc420524a213d19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class BeerController { | |
public BeerController() { | |
} | |
public static String generateCardId(){ | |
String CharList = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'; | |
Integer position; | |
Integer LengthRequired = 30; | |
String Res = ''; | |
for(Integer i = 0; i <= LengthRequired; i++) { | |
position = Integer.valueof(String.valueof(Math.roundToLong(CharList.length()*Math.random()))) -1; | |
Res += CharList.substring(position,position+1); | |
} | |
return Res; | |
} | |
public static String getCouponInformation(String name){ | |
Coupon__c couponRec = [Select Id, Name, | |
Price__c | |
FROM Coupon__c | |
Where Expired__c = false | |
AND Name =: name]; | |
return couponRec.Id; | |
} | |
@AuraEnabled | |
public static string createCartItems(String CartId, String BeerId, Decimal Amount){ | |
Cart_Item__c item = new Cart_Item__c(); | |
Map<Id, Cart_Item__c> cartItemsMap = getCartItems(CartId); | |
if ( cartItemsMap.containsKey(BeerId) ) { | |
item = cartItemsMap.get(BeerId); | |
item.Item_Amount__c = item.Item_Amount__c + Amount; | |
item.Item_Quantity__c = item.Item_Quantity__c + 1; | |
update item; | |
} else { | |
item.Beer__c = BeerId; | |
item.Cart__c = CartId; | |
item.Item_Quantity__c = 1; | |
item.Item_Amount__c = Amount; | |
insert item; | |
} | |
return item.Id; | |
} | |
public Static Map<Id, Cart_Item__c> getCartItems(String cartId){ | |
/* beerId, Cart_Iems*/ | |
Map<Id, Cart_Item__c> cartItemsMap = new Map<Id, Cart_Item__c>(); | |
for ( Cart_Item__c cart : [Select Id, Name, Beer__c, Cart__c, | |
Item_Quantity__c, Item_Amount__c | |
FROM Cart_Item__c | |
Where Cart__c =: cartId]) { | |
cartItemsMap.put(cart.Beer__c, cart); | |
} | |
return cartItemsMap; | |
} | |
@AuraEnabled(cacheable=false) | |
public static string getCartId(){ | |
List<Cart__c> cartInfo = [ Select Id, Name | |
FROM Cart__c | |
Where User__c =: UserInfo.getUserId() | |
AND Cart_Status__c = 'Open' | |
AND Is_Active__c = true]; | |
if (cartInfo != null && cartInfo.size() > 0) { | |
List<Cart_Item__c> cartItems = new list<Cart_Item__c>(); | |
cartItems = [Select Id, Name, Beer__c, Cart__c, Item_Quantity__c, Item_Amount__c | |
FROM Cart_Item__c | |
Where Cart__c =: cartInfo.get(0).Id]; | |
Decimal itemQnty = 0; | |
for (Cart_Item__c item : cartItems) { | |
itemQnty = itemQnty + item.Item_Quantity__c; | |
} | |
CartWrapper wrapper = new CartWrapper(); | |
wrapper.CartId = cartInfo.get(0).Id; | |
wrapper.items = cartItems; | |
wrapper.Count = itemQnty; | |
return JSON.serialize(wrapper); | |
} else { | |
Cart__c defaultCart = new Cart__c(); | |
String couponId = getCouponInformation('Default'); | |
defaultCart.Cart_Id__c = String.valueOf(Math.random()); | |
defaultCart.Cart_Status__c = 'Open'; | |
defaultCart.Is_Active__c = true; | |
defaultCart.Coupon__c = couponId; | |
defaultCart.User__c = UserInfo.getUserId(); | |
insert defaultCart; | |
CartWrapper wrapper = new CartWrapper(); | |
wrapper.CartId = defaultCart.Id; | |
wrapper.items = null; | |
wrapper.Count = 0; | |
return JSON.serialize(wrapper); | |
} | |
} | |
@AuraEnabled(cacheable=true) | |
public static List<sObject> searchBeer(String searchParam){ | |
String likeParam = '%' +searchParam+'%'; | |
String Query = ''; | |
/* | |
Select Id, Name From beer__c Where Name Like '%aa%' | |
*/ | |
if(searchParam != null){ | |
Query = 'Select Id, Name, Alcohol__c,brewery_id__c, brewery_Name__c, Id__c, Price__c,Image__c,Remaining_Quantity__c, Tags__c, Total_Quantity__c From Beer__c Where Name Like :likeParam LIMIT 50 '; | |
}else{ | |
Query = 'Select Id, Name, Alcohol__c,brewery_id__c, brewery_Name__c, Id__c, Price__c,Image__c,Remaining_Quantity__c, Tags__c, Total_Quantity__c From Beer__c LIMIT 50 '; | |
} | |
List<SObject> sObjectList = Database.query(Query); | |
return sObjectList; | |
} | |
public class CartWrapper { | |
@AuraEnabled | |
public String CartId { get; set; } | |
@AuraEnabled | |
public Decimal Count { get; set; } | |
@AuraEnabled | |
public List<Cart_Item__c> items { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div style="background: white; border-radius: 10px;"> | |
<div style="border: 2px solid red; width: 30px; height: 30px; border-radius: 50%; | |
margin-left: 1313px; | |
margin-bottom: -30px; | |
text-align: center; | |
font-size: 19px;" | |
> | |
<strong class="slds-m-top_small" style="text-align:center; vertical-align:center;">{itemsinCart}</strong> | |
</div> | |
<div style="margin-left: 1300px;" > | |
<img src={cart} height="70" width="80" /> | |
</div> | |
<div class="slds-m-left_small slds-m-right_small slds-m-bottom_small"> | |
<c-beer-search onsearch={handleEvent} ></c-beer-search> | |
</div> | |
<div class="slds-m-left_small slds-m-right_small slds-m-bottom_small"> | |
<template if:true={beerRecords} > | |
<div class="slds-grid slds-wrap"> | |
<template for:each={beerRecords} for:item="item"> | |
<div class="slds-col slds-size_1-of-3 " key={item.Id}> | |
<c-beer-tile beer-record={item} oncart={addToCart}></c-beer-tile> | |
</div> | |
</template> | |
</div> | |
</template> | |
</div> | |
</div> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable radix */ | |
/* eslint-disable no-alert */ | |
/* eslint-disable no-console */ | |
import { LightningElement, track, wire } from 'lwc'; | |
import searchBeer from '@salesforce/apex/BeerController.searchBeer'; | |
import cartIco from '@salesforce/resourceUrl/cart'; | |
import getCartId from '@salesforce/apex/BeerController.getCartId'; | |
import createCartItems from '@salesforce/apex/BeerController.createCartItems'; | |
import { ShowToastEvent } from 'lightning/platformShowToastEvent'; | |
export default class BeerList extends LightningElement { | |
@track beerRecords; | |
@track errros; | |
@track cart = cartIco; | |
@track cartId; | |
@track itemsinCart = 0; | |
connectedCallback(){ | |
this.defaultCartId(); | |
} | |
defaultCartId(){ | |
getCartId() | |
.then(data => { | |
const wrapper = JSON.parse(data); | |
if ( wrapper ){ | |
this.itemsinCart = wrapper.Count; | |
this.cartId = wrapper.CartId; | |
} | |
}) | |
.catch(error => { | |
this.cartId = undefined; | |
console.log(error); | |
}); | |
} | |
addToCart(event){ | |
const selectBeerId = event.detail; | |
const selectBeerRecord = this.beerRecords.find( | |
record => record.Id === selectBeerId | |
); | |
/* | |
for(Beer__c beer : beerRecords ){ | |
if(beer.Id == selectBeerId ){ | |
return beer; | |
} | |
} | |
*/ | |
createCartItems({ | |
CartId : this.cartId, | |
BeerId : selectBeerId, | |
Amount : selectBeerRecord.Price__c | |
}) | |
.then(data => { | |
console.log(' Cart Item Id ', data); | |
this.itemsinCart = this.itemsinCart + 1; | |
const toast = new ShowToastEvent({ | |
'title' : 'Success!!', | |
"message" : selectBeerRecord.Name +' Added into Cart!', | |
"variant" : "success", | |
}); | |
this.dispatchEvent(toast); | |
}) | |
.catch(error => { | |
console.log(error); | |
const toast = new ShowToastEvent({ | |
'title' : 'Error!!', | |
"message" : JSON.stringify(error), | |
"variant" : "error", | |
}); | |
this.dispatchEvent(toast); | |
}); | |
} | |
@wire(searchBeer) | |
wiredRecords({error, data}){ | |
if ( data ) { | |
this.beerRecords = data; | |
this.errors = undefined; | |
} | |
if( error ) { | |
this.beerRecords = undefined; | |
this.errors = error; | |
} | |
} | |
handleEvent(event){ | |
const eventVal = event.detail; | |
searchBeer({ | |
searchParam : eventVal | |
}) | |
.then(result => { | |
this.beerRecords = result; | |
this.errros = undefined; | |
}) | |
.catch(error => { | |
this.errors = error; | |
this.beerRecords = undefined; | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment