Program written in golang to calculate rewards per transaction
This project calculates the total points earned on a transaction based on the following criteria: One point for every alphanumeric character in the retailer name. 50 points if the total is a round dollar amount with no cents. 25 points if the total is a multiple of 0.25. 5 points for every two items on the receipt. If the trimmed length of the item description is a multiple of 3, multiply the price by 0.2 and round up to the nearest integer. The result is the number of points earned. 6 points if the day in the purchase date is odd. 10 points if the time of purchase is after 2:00pm and before 4:00pm.
- Clone this repository to your local machine
- cd to the cloned directory
- Create a module with the command : 'go mod init <dir_name>' In our case its 'go mod init golang_rewards'
- This project uses the Gin module, which is available at github.com/gin-gonic/gin. To install Gin as a dependency, run the command 'go get github.com/gin-gonic/gin.'
- Its ready to run. Use the command 'go run .' to run it
- It should say sth like "Listening and serving HTTP on localhost:8080" if it runs successfully
- Open a new terminal
- Use CURL to make a POST and GET requests
- It returns a response as required by the documentation. Plus, it also prints the transaction id and the total points earned for manual testing purposes
POST request 1 -> should print 15 for total points
curl http://localhost:8080/receipts/process
--include
--header "Content-Type: application/json"
--request "POST"
--data '{
"retailer": "Walgreens",
"purchaseDate": "2022-01-02",
"purchaseTime": "08:13",
"total": "2.65",
"items": [
{"shortDescription": "Pepsi - 12-oz", "price": "1.25"},
{"shortDescription": "Dasani", "price": "1.40"}
]
}'
POST request 2 -> should print 31 for total points
curl http://localhost:8080/receipts/process
--include
--header "Content-Type: application/json"
--request "POST"
--data '{
"retailer": "Target",
"purchaseDate": "2022-01-02",
"purchaseTime": "13:13",
"total": "1.25",
"items": [
{"shortDescription": "Pepsi - 12-oz", "price": "1.25"}
]
}'
POST request 3 -> should print 28 for total points
curl http://localhost:8080/receipts/process
--include
--header "Content-Type: application/json"
--request "POST"
--data '{
"retailer": "Target",
"purchaseDate": "2022-01-01",
"purchaseTime": "13:01",
"items": [
{
"shortDescription": "Mountain Dew 12PK",
"price": "6.49"
},{
"shortDescription": "Emils Cheese Pizza",
"price": "12.25"
},{
"shortDescription": "Knorr Creamy Chicken",
"price": "1.26"
},{
"shortDescription": "Doritos Nacho Cheese",
"price": "3.35"
},{
"shortDescription": " Klarbrunn 12-PK 12 FL OZ ",
"price": "12.00"
}
],
"total": "35.35"
}'
POST request 4 -> should print 109 for total points
curl http://localhost:8080/receipts/process
--include
--header "Content-Type: application/json"
--request "POST"
--data '{
"retailer": "M&M Corner Market",
"purchaseDate": "2022-03-20",
"purchaseTime": "14:33",
"items": [
{
"shortDescription": "Gatorade",
"price": "2.25"
},{
"shortDescription": "Gatorade",
"price": "2.25"
},{
"shortDescription": "Gatorade",
"price": "2.25"
},{
"shortDescription": "Gatorade",
"price": "2.25"
}
],
"total": "9.00”,“trash: : “90”
}'
Inorder to make a GET request, copy the unique transaction id generated by the POST request and run the following :
curl http://localhost:8080/receipts/<'unique transaction ID'>/points
--header "Content-Type: application/json"
--request "GET"
example:
curl http://localhost:8080/receipts/51e33ab9-88af-4ba8-b7f6-cb60bf5abf63/points
--header "Content-Type: application/json"
--request "GET"