Skip to content

Instantly share code, notes, and snippets.

@nmerouze
Last active March 13, 2019 06:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmerouze/7393ea75eb48a013a4c1d45ac07b6837 to your computer and use it in GitHub Desktop.
Save nmerouze/7393ea75eb48a013a4c1d45ac07b6837 to your computer and use it in GitHub Desktop.
class MyEditor {
onPaste = (_event, paste, state) => {
if (paste.type === "text" && isUrl(paste.text) && state.isCollapsed) {
return (
state
.transform()
.insertText(paste.text)
.extend(0 - paste.text.length)
.wrapInline({
type: "link",
data: {href: paste.text},
})
.collapseToEnd()
.apply()
);
}
};
}
class MyEditor {
onPaste = (_event, paste, state) => {
if (paste.type === "text" && isUrl(paste.text) && state.isCollapsed) {
return (
state
.transform()
.insertText(paste.text)
.extend(0 - paste.text.length)
.wrapInline({
type: "link",
data: {href: paste.text},
})
.select(state.selection)
.move(paste.text.length)
.collapseToEndOfNextText()
.apply()
);
}
};
}
@yusfianG
Copy link

Please help , i have problem when try to save consume api result to mongodb, the code it's error

package main

import (
"encoding/json"
"fmt"
"bytes"
"io/ioutil"
"net/http"
//"mydb"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)

type Response struct {
StatusCode int json:"statusCode"
Headers map[string]string json:"headers"
Body string json:"body"
}

type Passanger struct {
bson.ObjectId json:"id" bson:"_id"
Title string json:"title" bson:"title"
Image string json:"image" bson:"image"
Price string json:"price" bson:"price"
Rating string json:"rating" bson:"rating"
}

//type Passanger struct {
//ID bson.ObjectId bson:"_id,omitempty"
//Title string
//Image string
//Price string
//Rating string
//}

var (
IsDrop = true
)

type Adapter func(http.Handler) http.Handler
func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
for _, adapter := range adapters {
h = adapter(h)
}
return h
}

func main() {
fmt.Println("Starting the application...")

url := "http://"
fmt.Println("URL:>", url)

jsonData := map[string]string{}
jsonValue, _ := json.Marshal(jsonData)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
req.Header.Set("Content-Type", "application/json")
// req.Header.Set("Authorization", strAuthorization)

client := &http.Client{}

response, err := client.Do(req)

var info map[string]interface{}
var infodata string

if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
} else {
data, _ := ioutil.ReadAll(response.Body)
//defer response.Body.Close()
//json.Unmarshal(data, &info)
fmt.Printf("ALTEA Response: %s\n", string(data))
}

fmt.Println("Save Data To Database...")

session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}

defer session.Close()

session.SetMode(mgo.Monotonic, true)

// Drop Database
if IsDrop {
err = session.DB("test").DropDatabase()
if err != nil {
panic(err)
}
}

// Collection Passanger
c := session.DB("flylist").C("passanger")

// Index
index := mgo.Index{
Key: []string{"title", "image", "price", "rating"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}

err = c.EnsureIndex(index)
if err != nil {
panic(err)
}

// Insert Datas
infodata = info["data"].(map[string]interface{})["id"].(string)
//konversi:= []byte(info)
err = c.Insert(&Passanger{Title: string(infodata[0]), Image: string(infodata[1]), Price: string(infodata[2]), Rating: string(infodata[3])})
if err != nil {
panic(err)
}

// Query All
var results []Passanger
err = c.Find(bson.M{"name": "Ale"}).Sort("-timestamp").All(&results)

if err != nil {
panic(err)
}
fmt.Println("Results All: ", results)
}

Please correct my code, thanks alot before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment