mostly functionnal, poorly tested
This commit is contained in:
@@ -2,9 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/beego/beego/logs"
|
||||
@@ -12,20 +10,34 @@ import (
|
||||
|
||||
var (
|
||||
Peers []Peer
|
||||
Store Storage
|
||||
)
|
||||
|
||||
type Peer struct {
|
||||
PeerId string `json:"peer_id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
PeerId string `json:"peer_id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
EntityName string `json:"entity_name,omitempty"`
|
||||
EntityType string `json:"entity_type,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Website string `json:"website,omitempty"`
|
||||
Address string `json:"address,omitempty"`
|
||||
Postcode string `json:"postcode,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
Phone string `json:"phone,omitempty"`
|
||||
Email string `json:"email,omitempty"`
|
||||
Activity string `json:"activity,omitempty"`
|
||||
Keywords []string `json:"keywords,omitempty"`
|
||||
ApiUrl string `json:"url,omitempty"`
|
||||
PublicKey string `json:"public_key,omitempty"`
|
||||
// internal use
|
||||
Score int64 `json:"score,omitempty"`
|
||||
Keywords []string `json:"keywords,omitempty"`
|
||||
LastSeenOnline time.Time `json:"last_seen_online,omitempty"`
|
||||
ApiVersion string `json:"api_version,omitempty"`
|
||||
Url string `json:"url,omitempty"`
|
||||
PublicKey string `json:"public_key,omitempty"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
Store = Storage{"http://localhost:4080", "admin", "admin", "localhost:6379", ""}
|
||||
//p := Peer{uuid.New().String(), 0, []string{"car", "highway", "images", "video"}, time.Now(), "1", "asf", ""}
|
||||
// pa := []Peer{p}
|
||||
// byteArray, err := json.Marshal(pa)
|
||||
@@ -41,29 +53,34 @@ func init() {
|
||||
if err != nil {
|
||||
logs.Error("Error during Unmarshal(): ", err)
|
||||
}
|
||||
|
||||
Store.ImportData(LoadPeersJson("./peers.json"))
|
||||
}
|
||||
|
||||
func AddOne(peer Peer) (PeerId string) {
|
||||
peer.PeerId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||
Peers = append(Peers, peer)
|
||||
return peer.PeerId
|
||||
func AddPeers(peers []Peer) (status string) {
|
||||
err := Store.ImportData(peers)
|
||||
if err != nil {
|
||||
logs.Error("Error during Unmarshal(): ", err)
|
||||
return "error"
|
||||
}
|
||||
return "ok"
|
||||
}
|
||||
|
||||
func GetOne(PeerId string) (peer *Peer, err error) {
|
||||
|
||||
return nil, errors.New("PeerId Not Exist")
|
||||
func FindPeers(query string) (peers []Peer, err error) {
|
||||
result, err := Store.FindPeers(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetAll() []Peer {
|
||||
return Peers
|
||||
func GetPeer(uid string) (*Peer, error) {
|
||||
return Store.GetPeer(uid)
|
||||
}
|
||||
|
||||
func Update(PeerId string, Score int64) (err error) {
|
||||
|
||||
return errors.New("PeerId Not Exist")
|
||||
}
|
||||
|
||||
func Delete(PeerId string) {
|
||||
|
||||
func Delete(PeerId string) error {
|
||||
err := Store.DeletePeer(PeerId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user