func dbVerifyRecover(magic string) (int, int64) { dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile")) if err != nil { log.Fatal("%v", err) } defer dbHandler.Close() var getSql = "SELECT u.id FROM user u, user_recover_pass urp WHERE u.email=urp.email AND urp.magic=?" var value sql.NullInt64 err = dbHandler.QueryRow(getSql, magic).Scan(&value) if err != nil { if err == sql.ErrNoRows { log.Info("No magic found for %s", magic) return http.StatusNotFound, 0 } else { log.Error("DB query failed: %v", err) return http.StatusInternalServerError, 0 } } var delSql = "DELETE from user_recover_pass WHERE magic=?" _, err = dbHandler.Exec(delSql, magic) if err != nil { log.Error("SQL: %s", delSql) return http.StatusInternalServerError, 0 } return http.StatusOK, value.Int64 }
func Register() { ws := new(restful.WebService) ws. Path("/users"). Consumes(restful.MIME_JSON, restful.MIME_XML). Produces(restful.MIME_JSON, restful.MIME_XML) // you can specify this per route as well ws.Route(ws.GET("/").To(currUser)) ws.Route(ws.GET("/id/{user-id}").To(findUser)) ws.Route(ws.GET("/shippings").To(getShippingOptions).Filter(auth.AuthFilter)) ws.Route(ws.POST("/").To(createUser)) ws.Route(ws.POST("/signin").To(signinUser)) ws.Route(ws.POST("/signout").To(signoutUser)) ws.Route(ws.POST("/address").To(setShipping).Filter(auth.AuthFilter)) ws.Route(ws.PUT("/").To(updateUser)) ws.Route(ws.PUT("/password").To(updateUserPassword)) ws.Route(ws.DELETE("/{user-id}").To(removeUser)) restful.Add(ws) ws_recover := new(restful.WebService) ws_recover. Path("/recover"). Consumes(restful.MIME_JSON, restful.MIME_XML). Produces(restful.MIME_JSON, restful.MIME_XML) ws_recover.Route(ws_recover.GET("/{recover_magic}").To(verifyRecover)) ws_recover.Route(ws_recover.POST("/").To(sendRecover)) restful.Add(ws_recover) ws_customer := new(restful.WebService) ws_customer.Path("/customers"). Consumes(restful.MIME_XML, restful.MIME_JSON). Produces(restful.MIME_JSON, restful.MIME_XML) ws_customer.Route(ws_customer.GET("/search/{searchtext}/page/{pagenumber}").To(searchCustomers).Filter(auth.AuthEmployeeFilter)) ws_customer.Route(ws_customer.GET("/search/{searchtext}/count").To(searchCustomersCount).Filter(auth.AuthEmployeeFilter)) ws_customer.Route(ws_customer.GET("/{cond}").To(findCustomersByCond).Filter(auth.AuthEmployeeFilter)) ws_customer.Route(ws_customer.GET("/id/{id}").To(findCustomer).Filter(auth.AuthEmployeeFilter)) ws_customer.Route(ws_customer.POST("").To(addCustomer).Filter(auth.AuthEmployeeFilter)) ws_customer.Route(ws_customer.POST("/id").To(saveCustomer).Filter(auth.AuthEmployeeFilter)) ws_customer.Route(ws_customer.POST("/log").To(addCustomerLog).Filter(auth.AuthEmployeeFilter)) restful.Add(ws_customer) log.Info("user registered! ") }
func dbFindCustomer(c *Customer) int { log.Debug("get customer detail for %d", c.Id) dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile")) if err != nil { log.Fatal("%v", err) } defer dbHandler.Close() querySql := "SELECT id, name, cover_photo, desc, phone, email FROM user WHERE type in (1,2) AND id=? " var id sql.NullInt64 var name, coverPhoto, desc, phone, email sql.NullString err = dbHandler.QueryRow(querySql, c.Id).Scan(&id, &name, &coverPhoto, &desc, &phone, &email) if err != nil { if err == sql.ErrNoRows { log.Info("No customer found for %d", c.Id) return http.StatusNotFound } else { log.Debug("sql : %s", querySql) log.Error("DB query failed: %v", err) return http.StatusInternalServerError } } c.Name = name.String c.CoverPhoto = coverPhoto.String c.Desc = desc.String c.Phone = phone.String c.Email = email.String queryLogSql := "SELECT operation_type, operation_detail, operation_time FROM user_log WHERE user_id=? ORDER BY id DESC LIMIT 100" rows, err := dbHandler.Query(queryLogSql, c.Id) defer rows.Close() logs := make([]CustomerLog, 0, 100) for rows.Next() { var operation_type, operation_detail sql.NullString var operation_time time.Time rows.Scan(&operation_type, &operation_detail, &operation_time) logs = append(logs, CustomerLog{c.Id, operation_type.String, operation_detail.String, operation_time.Format(timeLayout)}) } c.Logs = logs return http.StatusOK }
func dbEmailExist(email string) bool { dbHandler, err := sql.Open("sqlite3", config.GetValue("DbFile")) if err != nil { log.Fatal("%v", err) } defer dbHandler.Close() if len(email) == 0 { return false } var getSql = "SELECT id FROM user WHERE email=?" var value sql.NullInt64 err = dbHandler.QueryRow(getSql, email).Scan(&value) if err != nil { if err == sql.ErrNoRows { log.Info("No email found for %s", email) return false } else { log.Error("DB query failed: %v", err) return false } } return true }