daily pastebin goal
18%
SHARE
TWEET

POST decoding issue

a guest Sep 28th, 2018 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package router
  2.  
  3. import (
  4.     "encoding/json"
  5.     "log"
  6.     "net/http"
  7.  
  8.     "golang.org/x/crypto/bcrypt"
  9.     "gopkg.in/mgo.v2"
  10.  
  11.     // "math/big"      TODO change balances type from float64 to BigFloat
  12.     "time"
  13. )
  14.  
  15. // Wallet represent a bank account not assigned to anyone, it's created to let move wallets between customers
  16. type Wallet struct {
  17.     currency    string
  18.     bankName    string
  19.     bankCountry string
  20.     balance     float64
  21.     IBAN        uint32
  22. }
  23.  
  24. // Account is a representation of a person who own a wallet and it's personal data
  25. type Account struct {
  26.     name    string
  27.     surname string
  28.     mail    string
  29.  
  30.     loginID      uint32
  31.     passwordHash string
  32.  
  33.     registrationTime time.Time
  34.  
  35.     wallets []Wallet
  36. }
  37.  
  38. // RegistrationData this data is passed in the POST requets
  39. type RegistrationData struct {
  40.     name         string
  41.     surname      string
  42.     mail         string
  43.     passwordHash string
  44.  
  45.     // First bank Account Data
  46.  
  47.     bankName    string
  48.     bankCountry string
  49.     currency    string
  50. }
  51.  
  52. func createNewUser(w http.ResponseWriter, req *http.Request) {
  53.  
  54.     decoder := json.NewDecoder(req.Body)
  55.  
  56.     var registrationData RegistrationData
  57.     decoderErr := decoder.Decode(&registrationData)
  58.     if decoderErr != nil {
  59.         panic(decoderErr)
  60.     }
  61.  
  62.     name := registrationData.name
  63.     surname := registrationData.surname
  64.     mail := registrationData.mail
  65.     passwordHash := registrationData.passwordHash
  66.  
  67.     // Registration account data
  68.  
  69.     bankName := registrationData.bankName
  70.     bankCountry := registrationData.bankCountry
  71.     currency := registrationData.currency
  72.  
  73.     s := session.Copy()
  74.     defer s.Close()
  75.     c := s.DB("bank_mockup").C("accounts")
  76.  
  77.     var newUser Account
  78.  
  79.     newUser.name = name
  80.     newUser.surname = surname
  81.     newUser.mail = mail
  82.     newUser.loginID = generateLoginID(name, surname, mail, time.Now())
  83.     newUser.passwordHash = passwordHash
  84.     newUser.registrationTime = time.Now()
  85.     newUser.wallets = make([]Wallet, 0)
  86.     newUser.wallets = append(newUser.wallets, generateWallet(currency, bankName, bankCountry))
  87.  
  88.     dbErr := c.Insert(&newUser)
  89.     if dbErr != nil {
  90.         if mgo.IsDup(dbErr) {
  91.             log.Println("User already exists")
  92.             return
  93.         }
  94.     }
  95. }
  96.  
  97. func InsertIntoDatabase() {
  98.  
  99. }
  100.  
  101. // TODO
  102. func generateLoginID(name string, surname string, email string, birthdate time.Time) uint32 {
  103.     return 0
  104. }
  105.  
  106. // TODO
  107. func generateWallet(currency string, bankName string, bankCountry string) Wallet {
  108.     return Wallet{currency: currency, balance: 0, IBAN: generateIBAN(currency, bankName, bankCountry)}
  109. }
  110.  
  111. // TODO
  112. func generateIBAN(currency string, bankName string, bankCountry string) uint32 {
  113.     return 1000000000
  114. }
  115.  
  116. // TODO
  117. func getIBANCountryCode(country string) string {
  118.     return ""
  119. }
  120.  
  121. // TODO
  122. func getIBANByCurrency(currency string) string {
  123.     return ""
  124. }
  125.  
  126. // TODO
  127. func getIBANByBankName(bankName string) string {
  128.     return ""
  129. }
  130.  
  131. func hashPassword(password string) string {
  132.     passBytes := []byte(password)
  133.     hash, err := bcrypt.GenerateFromPassword(passBytes, bcrypt.MinCost)
  134.  
  135.     if err != nil {
  136.         log.Println(err)
  137.     }
  138.  
  139.     return string(hash)
  140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Pastebin PRO 'AUTUMN SPECIAL'!
Get 60% OFF Pastebin PRO accounts!
 
Top