// PUT /developers/{token}/reset, Edit password func PasswordEditHandler(rw http.ResponseWriter, req *http.Request) { if err := req.ParseForm(); err != nil { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } id := req.FormValue("id") u, err := db.GetDeveloperById(id) if err != nil { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } query := map[string]interface{}{"token": mux.Vars(req)["token"]} update := map[string]interface{}{"password": util.HashPassword(req.FormValue("new"), u.Salt)} if err := db.UpdateDeveloper(query, update); err != nil { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } renderer.JSON(rw, http.StatusOK, map[string]interface{}{ "status": requests.StatusSuccess, "user": u, }) }
// POST /developer/token, logs in a user by creating a new token func CreateTokenHandler(rw http.ResponseWriter, req *http.Request) { var body requests.LoginReq decoder := json.NewDecoder(req.Body) err := decoder.Decode(&body) if err != nil { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } email := body.Email password := body.Password if email == "" || password == "" { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": "Email and Password Required.", }) return } query := map[string]interface{}{"email": email} u, err := db.GetDeveloper(query) if err != nil { renderer.JSON(rw, http.StatusInternalServerError, map[string]string{ "status": requests.StatusFailed, "error": "No such developer with email " + email + ".", }) return } if util.HashPassword(password, u.Salt) != u.Password { renderer.JSON(rw, http.StatusInternalServerError, map[string]string{ "status": requests.StatusFailed, "error": "Incorrect Password", }) return } token := util.HashToken() update := map[string]interface{}{"token": token} if err := db.UpdateDeveloper(query, update); err != nil { renderer.JSON(rw, http.StatusInternalServerError, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } renderer.JSON(rw, http.StatusOK, map[string]interface{}{ "status": requests.StatusCreated, "token": token, }) }
// POST /developers/{token}/pay payments func PaymentHandler(rw http.ResponseWriter, req *http.Request) { var body requests.PaymentReq decoder := json.NewDecoder(req.Body) err := decoder.Decode(&body) if err != nil { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } d, err := db.GetDeveloper(map[string]interface{}{"token": mux.Vars(req)["token"]}) if err != nil { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } // Create Stripe Customer customerParams := stripe.CustomerParams{ Email: d.Email, Desc: d.Name, Token: body.StripeToken, } customer, err := stripe.Customers.Create(&customerParams) if err != nil { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } // Charge Stripe Customer chargeParams := stripe.ChargeParams{ Desc: "Bowery 3", Amount: 2900, Currency: "usd", Customer: customer.Id, } _, err = stripe.Charges.Create(&chargeParams) if err != nil { RenderTemplate(rw, "error", map[string]string{"Error": err.Error()}) return } if err := db.UpdateDeveloper(map[string]interface{}{"token": d.Token}, map[string]interface{}{"isPaid": true}); err != nil { renderer.JSON(rw, http.StatusInternalServerError, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } renderer.JSON(rw, http.StatusOK, map[string]interface{}{ "status": requests.StatusSuccess, "developer": d, }) }
// PUT /developers/{token}, edits a developer func UpdateDeveloperHandler(rw http.ResponseWriter, req *http.Request) { token := mux.Vars(req)["token"] if token == "" { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": "missing token", }) return } if err := req.ParseForm(); err != nil { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } query := map[string]interface{}{"token": token} update := map[string]interface{}{} u, err := db.GetDeveloper(query) if err != nil { renderer.JSON(rw, http.StatusInternalServerError, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } if password := req.FormValue("password"); password != "" { oldpass := req.FormValue("oldpassword") if oldpass == "" || util.HashPassword(oldpass, u.Salt) != u.Password { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": "Old password is incorrect.", }) return } update["password"] = util.HashPassword(password, u.Salt) } if nextPaymentTime := req.FormValue("nextPaymentTime"); nextPaymentTime != "" { update["nextPaymentTime"], err = time.Parse(time.RFC3339, nextPaymentTime) } if isAdmin := req.FormValue("isAdmin"); isAdmin != "" { update["isAdmin"] = isAdmin == "on" || isAdmin == "true" } if isPaid := req.FormValue("isPaid"); isPaid != "" { update["isPaid"] = isPaid == "on" || isPaid == "true" } // TODO add datetime parsing for _, field := range []string{"name", "email", "integrationEngineer"} { val := req.FormValue(field) if val != "" { update[field] = val } } if err := db.UpdateDeveloper(query, update); err != nil { renderer.JSON(rw, http.StatusInternalServerError, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } renderer.JSON(rw, http.StatusOK, map[string]interface{}{ "status": requests.StatusUpdated, "update": update, }) }