// GetMessagesByVisitID - func GetMessagesByVisitID(e *config.Env, visitID string) ([]Message, int, error) { visitUUID, err := shortuuid.New().Decode(visitID) if err != nil { return nil, http.StatusBadRequest, err } messages := []Message{} err = e.MainDB.Select(&messages, "SELECT * FROM messages WHERE visit_id = ? ORDER BY updated_at ASC", visitUUID.Bytes()) if err != nil { return nil, http.StatusNotFound, err } // parse all messages's UUID bytes into human readable IDs for key := range messages { statusCode, err := messages[key].convertUUIDToIDs() if err != nil { return nil, statusCode, err } } return messages, http.StatusOK, nil }
func (m *Message) createFieldsBuilder() { messageUUID := uuid.NewV4() m.ID = shortuuid.New().Encode(messageUUID) m.UUID = messageUUID.Bytes() m.CreatedAt = time.Now().String() m.UpdatedAt = m.CreatedAt }
func (v *Visit) createFieldsBuilder() { visitUUID := uuid.NewV4() v.ID = shortuuid.New().Encode(visitUUID) v.UUID = visitUUID.Bytes() v.CreatedAt = time.Now().String() v.UpdatedAt = v.CreatedAt }
func (m *Message) convertUUIDToIDs() (int, error) { messageUUID, err := uuid.FromBytes(m.UUID) if err != nil { return http.StatusBadRequest, err } visitUUID, err := uuid.FromBytes(m.VisitUUID) if err != nil { return http.StatusBadRequest, err } m.ID = shortuuid.New().Encode(messageUUID) m.VisitID = shortuuid.New().Encode(visitUUID) return http.StatusOK, nil }
func (m *Message) convertMessageIDtoUUID() (int, error) { messageUUID, err := shortuuid.New().Decode(m.ID) if err != nil { return http.StatusBadRequest, err } m.UUID = messageUUID.Bytes() return http.StatusOK, nil }
// Converts VisitID into a Byte UUIDs // Returns BadRequest if error occurs func (m *Message) convertRefrencesIDtoUUID() (int, error) { visitUUID, err := shortuuid.New().Decode(m.VisitID) if err != nil { return http.StatusBadRequest, err } m.VisitUUID = visitUUID.Bytes() return http.StatusOK, nil }
// Converts VisitUUID into a Readable IDs // Returns BadRequest if error occurs func (m *Message) convertRefrencesUUIDtoID() (int, error) { visitUUID, err := uuid.FromBytes(m.VisitUUID) if err != nil { return http.StatusBadRequest, err } m.VisitID = shortuuid.New().Encode(visitUUID) return http.StatusOK, nil }
func (b *Broker) TimerCallback(schedule string) *TimerCallback { callback := &TimerCallback{ ID: fmt.Sprintf("timer:%s", shortuuid.New()), Schedule: schedule, Chan: make(chan time.Time), } if err := b.RegisterCallback(callback); err != nil { Logger.Debug("error registering callback ", callback.ID, ":: ", err) return nil } return callback }
func (v *Visit) convertVisitIDtoUUID() (int, error) { visitUUID, err := shortuuid.New().Decode(v.ID) if err != nil { fmt.Printf("convert visit err") return http.StatusBadRequest, err } v.UUID = visitUUID.Bytes() return http.StatusOK, nil }
func (b *Broker) QuestionCallback(user string, prompt string) *QuestionCallback { callback := &QuestionCallback{ ID: fmt.Sprintf("question:%s", shortuuid.New()), User: user, Question: prompt, Answer: make(chan string), } if err := b.RegisterCallback(callback); err != nil { Logger.Debug("error registering callback ", callback.ID, ":: ", err) return nil } return callback }
func (b *Broker) EventCallback(key string, val string) *EventCallback { callback := &EventCallback{ ID: fmt.Sprintf("event:%s", shortuuid.New()), Key: key, Val: val, Chan: make(chan map[string]interface{}), } if err := b.RegisterCallback(callback); err != nil { Logger.Debug("error registering callback ", callback.ID, ":: ", err) return nil } return callback }
// Delete - func (m *Message) Delete(e *config.Env) (int, error) { messageUUID, err := shortuuid.New().Decode(m.ID) if err != nil { return http.StatusBadRequest, err } _, err = e.MainDB.Exec("DELETE FROM messages WHERE id=?", messageUUID.Bytes()) if err != nil { return http.StatusBadRequest, err } return http.StatusOK, nil }
// GetByPhoneNumber - func (v *Visit) GetByPhoneNumber(e *config.Env) (int, error) { err := e.MainDB.Get(v, "SELECT * FROM visits WHERE phone_number=? ORDER BY created_at DESC LIMIT 1", v.PhoneNumber) if err != nil { return http.StatusBadRequest, err } visitUUID, err := uuid.FromBytes(v.UUID) if err != nil { return http.StatusBadRequest, err } v.ID = shortuuid.New().Encode(visitUUID) return http.StatusOK, nil }
func (b *Broker) MessageCallback(pattern string, respond bool, channel ...string) *MessageCallback { callback := &MessageCallback{ ID: fmt.Sprintf("message:%s", shortuuid.New()), Pattern: pattern, Respond: respond, Chan: make(chan PatternMatch), } if channel != nil { callback.SlackChan = channel[0] // todo: support an array of channels } if err := b.RegisterCallback(callback); err != nil { Logger.Debug("error registering callback ", callback.ID, ":: ", err) return nil } return callback }
func GetAll(e *config.Env) ([]*Visit, int, error) { var visits []*Visit err := e.MainDB.Select(&visits, "SELECT * FROM visits ORDER BY created_at DESC") if err != nil { return nil, http.StatusBadRequest, err } for _, v := range visits { visitUUID, err := uuid.FromBytes(v.UUID) if err != nil { return nil, http.StatusBadRequest, err } v.ID = shortuuid.New().Encode(visitUUID) } return visits, http.StatusOK, nil }
// Get - func (v *Visit) Get(e *config.Env) (int, error) { statusCode, err := v.convertVisitIDtoUUID() if err != nil { return statusCode, err } err = e.MainDB.Get(v, "SELECT * FROM visits WHERE id = ?", v.UUID) if err != nil { return http.StatusBadRequest, err } visitUUID, err := uuid.FromBytes(v.UUID) if err != nil { return http.StatusBadRequest, err } v.ID = shortuuid.New().Encode(visitUUID) return http.StatusOK, nil }
// Create and initialize a new application. This is a front gate for // the framework, since you should start by creating a new app struct. // Every application should have a valid name (tag) and a version. So // this function makes sure they have been passed and are all valid. // Generally, you should not be creating more than one application. func New(name, version string) *App { var room = make(map[string]interface{}) const url = "https://github.com/ts33kr/boot" const ename = "name is not of correct format" const eversion = "version is not valid semver" pattern := regexp.MustCompile("^[a-zA-Z0-9-_]+$") var parsed semver.Version = semver.MustParse(version) if !pattern.MatchString(name) { panic(ename) } if parsed.Validate() != nil { panic(eversion) } application := &App{Name: name, Version: parsed} application.Storage = Storage{Container: room} application.CronEngine = cron.New() // create CRON application.Servers = make(map[string]*http.Server) application.Reference = shortuuid.New() // V4 application.Providers = make([]*Provider, 0) application.Services = make([]*Service, 0) application.TimeLayout = time.RFC850 application.Namespace = url // set return application // prepared app }
package main import ( "fmt" "math/rand" "os" "time" "github.com/boltdb/bolt" "github.com/renstrom/shortuuid" ) var ( dbPath = shortuuid.New() + ".db" bucketName = shortuuid.New() numKeys = 10 keyLen = 3 valLen = 7 keys = make([][]byte, numKeys) vals = make([][]byte, numKeys) ) func init() { fmt.Println("Starting writing random data...") for i := range keys { keys[i] = randBytes(keyLen) vals[i] = randBytes(valLen) } fmt.Println("Done with writing random data...")