Skip to content

Commit

Permalink
release 1.2
Browse files Browse the repository at this point in the history
Some minor bug fix, updated readme and forgot-password addded
  • Loading branch information
1-Harshit committed Jul 24, 2021
1 parent f5c291f commit 63bd090
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 90 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@ jobs:
go-version: 1.16

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
run: go build -v ./...
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
config/email_cred.go
iitk-coin
data.db
commands*
temp*
.*
.*
150 changes: 111 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# iitk-coin
**Summer Project 2021**
---
**SnT Project 2021, Programming Club**
---

## **Summer Project 2021**

## **SnT Project 2021, Programming Club**

This repository contains the backend code for the IITK Coin which is a reward based psuedo currency of IIT Kanpur.

Expand All @@ -12,6 +12,7 @@ This repository contains the backend code for the IITK Coin which is a reward ba
- [Midterm Documentation](https://docs.google.com/document/d/1bvOWH4k0U-l2pQ1jLWIDzOkJ2wbHNW4jJw7tMWkUV6o/edit?usp=sharing)

## Table Of Content

- [Development Environment](#development-environment)
- [Directory Structure](#directory-structure)
- [Usage](#usage)
Expand All @@ -22,15 +23,18 @@ This repository contains the backend code for the IITK Coin which is a reward ba
## Development Environment

```bash
- go version: go1.16.6 linux/amd64 # https://golang.org/dl/
- text editor: VSCode # https://code.visualstudio.com/download
- terminal: Zsh # https://ohmyz.sh/
- OS: Ubuntu 20.04.2 LTS x86-64 # https://ubuntu.com/download
- Kernel: Linux 5.8.0-59-generic # https://kernel.ubuntu.com/
- go version: go1.16.6 linux/amd64 # https://golang.org/dl/
- text editor: VSCode # https://code.visualstudio.com/download
- terminal: Zsh # https://ohmyz.sh/
```

## Directory Structure

```
.
~/go/src/github.com/1-Harshit/iitk-coin
~go/src/github.com/1-Harshit/iitk-coin
├── auth
│   └── auth.go
├── config
Expand All @@ -39,6 +43,7 @@ This repository contains the backend code for the IITK Coin which is a reward ba
│   ├── otp.go
│   ├── settings.go
│   └── validate.go
├── data.db
├── db
│   ├── db.go
│   ├── otp.go
Expand All @@ -47,6 +52,7 @@ This repository contains the backend code for the IITK Coin which is a reward ba
├── Dockerfile
├── go.mod
├── go.sum
├── iitk-coin
├── LICENSE
├── main.go
├── Models.png
Expand All @@ -57,133 +63,199 @@ This repository contains the backend code for the IITK Coin which is a reward ba
├── user.go
└── utility.go
4 directories, 21 files
4 directories, 23 files
```

## Usage

### Use this Repo

```bash
mkdir $GOPATH/src/github.com/1-Harshit
cd $GOPATH/src/github.com/1-Harshit
git clone https://github.com/1-Harshit/iitk-coin.git
cd iitk-coin
```
```bash
go run main.go

go run main.go
# or build the program and run the executable
go build
./iitk-coin
```

### Use Docker Image

```bash
docker run --rm -p 8080:8080 1harshit/iitk-coin
```
Output should look like

Expected Output:

```
2021/07/18 01:03:46 Starting server. Listening on http://localhost:8080
```

## Endpoints

POST requests take place via `JSON` requests. A typical usage would look like

```bash
curl -H "Content-Type: application/json" [-H "Authorization: Bearer JWT"] --request POST -d '<json-request>' http://localhost:8080/<endpoint>
# or if using get
curl [-H "Authorization: Bearer JWT"] --request GET http://localhost:8080/<endpoint>
```

#### Signup

- `/signup/otp` : `POST`

```json
{"name":"<name>", "roll":"<rollno>", "email":"<emailid>"}
{ "name": "<name>", "roll": "<rollno>", "email": "<emailid>" }
```

- `/signup` : `POST`

```json
{"name":"<name>", "roll":"<rollno>", "email":"<emailid>", "password":"<password>", "otp":"<otp>", "batch":"<batch>"}
{
"name": "<name>",
"roll": "<rollno>",
"email": "<emailid>",
"password": "<password>",
"otp": "<otp>",
"batch": "<batch>"
}
```

#### Login

- `/login` : `POST`

```json
{"roll":"<rollno>", "password":"<password>"}
{ "roll": "<rollno>", "password": "<password>" }
```

- `/secretpage` : `GET`

```bash
curl -H "Authorization: Bearer $JWT" --request GET http://localhost:8080/secretpage
```

Auth Header needed in all endpoints below this

#### User's Info

- `/user/info` : `GET`

```bash
curl -H "Authorization: Bearer $JWT" --request GET http://localhost:8080/user/info
```

- `/user/reward` : `GET`

```bash
curl -H "Authorization: Bearer $JWT" --request GET http://localhost:8080/user/reward
```

- `/user/transaction` : `GET`

```bash
curl -H "Authorization: Bearer $JWT" --request GET http://localhost:8080/user/transaction
```

- `/user/redeem` : `GET`

```bash
curl -H "Authorization: Bearer $JWT" --request GET http://localhost:8080/user/redeem
```

#### Reset Password

- `/forgotpass/otp` : `POST`

```json
{ "roll": "<rollno>", "email": "<emailid>" }
```

- `/forgotpass` : `POST`

```json
{ "roll": "<rollno>", "password": "<password>", "otp": "<otp>" }
```

#### Rewards

- `/reward` : `POST` Only GenSec and AH

```json
{"roll":"<rollno>", "coins":"<coins>"}
{ "roll": "<rollno>", "coins": "<coins>" }
```

#### Transfer

- `/transfer/otp` : `GET`

```bash
curl -H "Authorization: Bearer $JWT" --request GET http://localhost:8080/transfer/otp
```
- `/transfer` : `POST`

- `/transfer` : `POST`

```json
{"roll":"<rollno>", "coins":"<coins>", "otp":"<otp>"}
{ "roll": "<rollno>", "coins": "<coins>", "otp": "<otp>" }
```

#### Store

- `/store/list` : `GET`

```bash
curl --request GET http://localhost:8080/store/list
```

- `/store/add` : `POST` Only GenSec and AH

```json
{"name":"<itemname>", "value":"<itemvalue>"}
{ "name": "<itemname>", "value": "<itemvalue>" }
```

- `/store/remove` : `POST` Only GenSec and AH

```json
{"itemNo":"<itemno>"}
{ "itemNo": "<itemno>" }
```

#### Redeem

- `/redeem/request/otp` : `GET`

```bash
curl -H "Authorization: Bearer $JWT" --request GET http://localhost:8080/redeem/request/otp
```

- `/redeem/request` : `POST`

```json
{"itemNo":"<itemno>", "otp":"<otp>"}
{ "itemNo": "<itemno>", "otp": "<otp>" }
```

- `/redeem/list` : `GET` Only GenSec and AH

```bash
curl -H "Authorization: Bearer $JWT" --request GET http://localhost:8080//redeem/list
```

- `/redeem/approve` : `POST` Only GenSec and AH

```json
{"redeemid":"<redeemid>"}
{ "redeemid": "<redeemid>" }
```

- `/redeem/reject` : `POST` Only GenSec and AH

```json
{"redeemid":"<redeemid>"}
{ "redeemid": "<redeemid>" }
```
### How to use

### How to use

1. Run the `main.go` file this will Listen and Serve on `localhost:8080`
2. Use `curl` to use endpoints as shown above
Expand All @@ -192,20 +264,20 @@ curl -H "Authorization: Bearer $JWT" --request GET http://localhost:8080//redeem

![Models](Models.png "Database Struct")


## Settings

### about `config/settings.go`
Some unkown variables are stored there.
- Path of DB
- Max coins one can have
- Minimum Events needed for transfer
- tax
- const IntraBatchTax float64 = 0.02
- const InterBatchTax float64 = 0.33
- const IsStoreOpen bool = true


Footnotes
---

Some unkown variables are stored there.

- Path of DB
- Max coins one can have
- Minimum Events needed for transfer
- tax
- const IntraBatchTax float64 = 0.02
- const InterBatchTax float64 = 0.33
- const IsStoreOpen bool = true

## Footnotes

I'd like to name it **Koins**
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Redeem struct {

type Reward struct {
Time string `json:"time"`
Coins int `json:"coins"`
Coins float64 `json:"coins"`
Remarks string `json:"remarks"`
}

Expand Down
22 changes: 18 additions & 4 deletions config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ func ValidateUserforOTP(t User) string {
if t.Roll <= 0 {
return "Roll Number not found in request"
}
if t.Name == "" {
return "Name not found in request"
}
// if t.Name == "" {
// return "Name not found in request"
// }
if t.Email == "" {
return "Email not found in request"
}
Expand Down Expand Up @@ -48,6 +48,20 @@ func ValidateUser(t User) string {
return ""
}

// Check non empty request and email
func ValidatePass(t User) string {
if t.Roll <= 0 {
return "Roll Number not found in request"
}
if t.Password == "" {
return "Password not found in request"
}
if t.OTP == "" {
return "OTP not found in the request"
}
return ""
}

func ValidateCredentials(t User) string {
if t.Roll == 0 {
return "Roll Number not found in request"
Expand All @@ -65,7 +79,7 @@ func ValidateReward(t Wallet) string{
if t.Coins < 0 {
return "Positive coins needed in request"
}
if x := 100*t.Coins; x == float64(int(x)){
if x := 100*t.Coins; x != float64(int(x)){
return "Coins only allowed till two decimal places"
}
return ""
Expand Down
2 changes: 1 addition & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func TransferCoins(t c.Wallet, f *c.Claims) error {
return errors.New("max limit of coins for user exeeded")
}

txn_stm := `INSERT INTO "main"."Transaction"
txn_stm := `INSERT INTO "main"."Transactions"
("from", "to", "sent", "tax", "remarks")
VALUES (?, ?, ?, ?, ?);`

Expand Down
Loading

0 comments on commit 63bd090

Please sign in to comment.