Beispiel #1
0
func (conv *Conversation) CreateConversation() (RD.ReturnData, Conversation) {
	returnData := RD.ReturnData{}
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("conversation")
	conv.Id = bson.NewObjectId()
	conv.Created_On = time.Now()
	conv.Is_Approved = true

	err := c.Insert(&conv)
	if err != nil {
		log.Print(err.Error())
		returnData.ErrorMsg = err.Error()
		returnData.Success = false
		returnData.Status = "422"
	} else {

		returnData.Success = true
		jsonData, _ := json.Marshal(&conv)
		returnData.JsonData = jsonData
		returnData.Status = "201"
	}

	return returnData, *conv
}
Beispiel #2
0
func GetDialoguesForLocation(locationId string) RD.ReturnData {
	returnData := RD.ReturnData{}
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("Dialogue")

	res := []Dialogue{}
	err := c.Find(bson.M{"venue.fourid": locationId}).All(&res)
	if err != nil {
		log.Println("Found Nothing Or Something went wrong fetching the Dialogue")
		returnData.ErrorMsg = err.Error()
		returnData.Status = "400"
		returnData.Success = false
	} else {
		log.Println(res)
		returnData.ErrorMsg = "All is well"
		returnData.Status = "200"
		returnData.Success = true
		jsonRes, _ := json.Marshal(res)
		returnData.JsonData = jsonRes
		log.Println(string(jsonRes))
	}
	return returnData
}
Beispiel #3
0
func (msg *Message) SaveMessage(conversationId string) RD.ReturnData {
	returnData := RD.ReturnData{}
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("conversation")
	msg.CreatedOn = time.Now()

	err := c.Update(bson.M{"_id": bson.ObjectIdHex(conversationId)}, bson.M{
		"$push": bson.M{"messages": bson.M{
			"_id":         bson.NewObjectId(),
			"msg_text":    msg.MsgText,
			"user_id":     msg.UserId,
			"user_handle": msg.UserHandle,
			"created_on":  msg.CreatedOn,
		}}})

	if err != nil {
		log.Println(err.Error())
		returnData.ErrorMsg = err.Error()
		returnData.Success = false
		returnData.Status = "422"
	} else {
		jsonData := []byte("{}")
		returnData.Success = true
		returnData.JsonData = jsonData
		returnData.Status = "201"
	}
	return returnData
}
Beispiel #4
0
func GetDialogue(DialogueId string) (returnData RD.ReturnData) {
	fmt.Println(DialogueId)
	dbSession := Connection.GetDBSession()
	// dbSession.SetMode(mgo.Monotonic, true)
	defer dbSession.Close()
	log.Println(os.Getenv("MONGOHQ_URL"))
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("Dialogue")
	res := Dialogue{}
	// err := c.FindId(bson.ObjectIdHex(DialogueId)).One(&res)
	err := c.Find(bson.M{"_id": bson.ObjectIdHex(DialogueId)}).One(&res)
	if err != nil {
		log.Println(err)
		returnData.ErrorMsg = err.Error()
		returnData.Status = "400"
		returnData.Success = false
	} else {
		log.Println(res)
		returnData.ErrorMsg = "All is well"
		returnData.Status = "200"
		returnData.Success = true
		jsonRes, _ := json.Marshal(res)
		returnData.JsonData = jsonRes
	}
	return
}
Beispiel #5
0
func (u *UserMetaData) SaveUserMetaData() RD.ReturnData {
	returnData := RD.ReturnData{}
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("jove")

	u.Id = bson.NewObjectId()
	u.Created_on = time.Now()

	err := c.Insert(u)
	if err != nil {
		log.Print(err.Error())
		returnData.ErrorMsg = err.Error()
		returnData.Success = false
		returnData.Status = "422"
	} else {
		returnData.Success = true
		jsonData, _ := json.Marshal(&u)
		returnData.JsonData = jsonData
		returnData.Status = "201"
	}

	return returnData
}
Beispiel #6
0
func GetUserById(userId string) string {
	var response string
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("jove")

	result := UserMetaData{}
	err := c.Find(bson.M{"_id": bson.ObjectIdHex(userId)}).One(&result)
	if err != nil {
		log.Println(err)
		response = err.Error()
		// returnData.ErrorMsg = err.Error()
		// returnData.Status = "400"
		// returnData.Success = false
	} else {
		log.Println(result)
		// returnData.ErrorMsg = "All is well"
		// returnData.Status = "200"
		// returnData.Success = true
		response, _ := json.Marshal(result)
		log.Println(response)
		// returnData.JsonData = jsonRes
	}
	return response
}
Beispiel #7
0
func GetUserById(user_id string) (string, error) {
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("jove")

	result := User{}
	err := c.Find(bson.M{"_id": bson.ObjectIdHex(user_id)}).One(&result)
	return result.UserToJSON(), err
}
Beispiel #8
0
func GetByEmailAndUserId(email string, user_id int) (User, error) {
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("jove")

	result := User{}
	err := c.Find(bson.M{"email": email, "userid": user_id}).One(&result)
	if err != nil {
		log.Fatal(err)
	}
	return result, err
}
Beispiel #9
0
func (u *User) GetByHandle() User {
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("jove")

	result := User{}
	err := c.Find(bson.M{"handle": u.Handle}).One(&result)
	if err != nil {
		log.Fatal(err)
	}

	return result
}
Beispiel #10
0
func GetAll() string {
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("jove")

	result := []User{}
	err := c.Find(nil).Limit(10).All(&result)
	if err != nil {
		panic(err.Error())
	}

	return "hello"
}
Beispiel #11
0
func (u *User) GetUser() string {
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("jove")

	result := User{}
	err := c.Find(bson.M{"email": u.Email, "userid": u.UserId}).One(&result)
	if err != nil {
		log.Println(err.Error())
	}
	js, _ := json.Marshal(result)
	return string(js)
}
Beispiel #12
0
// func CircleExists(name string, owner User) (exists bool, msg string) {
func (circle *Circle) CircleExists() (exists bool) {
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	c := dbSession.DB("msgme").C("circle")

	result := Circle{}
	err := c.Find(bson.M{"name": circle.Name}).One(&result)
	if err == nil {
		exists = true
	} else {
		exists = false
	}

	return exists
}
Beispiel #13
0
func GetConversationsForLocation(locationId string) ([]byte, error) {
	var response []byte
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("conversation")

	conversations := []Conversation{}
	err := c.Find(bson.M{"venue.fourid": locationId}).All(&conversations)
	if err != nil {
	} else {
		response, err = json.Marshal(conversations)
	}
	log.Println(string(response))
	return response, err
}
Beispiel #14
0
func GetUserMessagesList(userId string) (string, error) {
	var response []byte
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("conversation")

	Msgs := []Messages{}
	err := c.Find(bson.M{"messages.user_id": userId}).Select(bson.M{"messages": 1}).All(&Msgs)
	if err != nil {
	} else {
		response, err = json.Marshal(Msgs)
	}
	log.Println(string(response))

	return string(response), err
}
Beispiel #15
0
func (cir *Circle) makeCircle() (bool, error) {
	status := true
	err := errors.New("")
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	c := dbSession.DB("msgme").C("circle")

	if cir.CircleExists() {
		status = false
		err = errors.New("Circle already exists with this name")
	} else {
		err = c.Insert(&cir)
		if err != nil {
			status = false
			log.Println(err.Error())
		}
	}

	return status, err
}
Beispiel #16
0
func DeleteConversation(conversationId string) RD.ReturnData {
	returnData := RD.ReturnData{}
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("conversation")

	err := c.Remove(bson.ObjectIdHex(conversationId))
	// err := c.Find(bson.M{"venue.fourid": locationId}).One(&res)
	if err != nil {
		log.Println("Found Nothing. Something went wrong fetching the Conversation document")
		log.Println(err)
		returnData.ErrorMsg = err.Error()
		returnData.Status = "400"
		returnData.Success = false
	} else {
		returnData.ErrorMsg = "All is well"
		returnData.Status = "200"
		returnData.Success = true
		returnData.JsonData = nil
	}
	return returnData
}
Beispiel #17
0
func GetMessages(conversationId string) RD.ReturnData {
	returnData := RD.ReturnData{}
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("conversation")

	Msgs := []Message{}
	m := Messages{}
	err := c.Find(bson.M{"_id": bson.ObjectIdHex(conversationId)}).Select(bson.M{"messages": 1}).One(&m)
	if err != nil {
		log.Println(err.Error())
		returnData.ErrorMsg = err.Error()
		returnData.Success = false
		returnData.Status = "422"
	} else {
		log.Println(Msgs)
		jsonData, _ := json.Marshal(&m)
		returnData.Success = true
		returnData.JsonData = jsonData
		returnData.Status = "201"
	}
	return returnData
}
Beispiel #18
0
func (D *Dialogue) CreateDialogue() (RD.ReturnData, Dialogue) {
	returnData := RD.ReturnData{}
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("Dialogue")
	D.Id = bson.NewObjectId()
	D.CreatedOn = time.Now()
	D.IsApproved = true

	err := c.Insert(&D)
	if err != nil {
		log.Print(err.Error())
		returnData.ErrorMsg = err.Error()
		returnData.Success = false
		returnData.Status = "422"
	} else {
		returnData.Success = true
		returnData.JsonData = []byte(D.DialogueToJSON())
		returnData.Status = "201"
	}

	return returnData, *D
}
Beispiel #19
0
func (D *Dialogue) DeleteDialogue() RD.ReturnData {
	returnData := RD.ReturnData{}
	dbSession := Connection.GetDBSession()
	dbSession.SetMode(mgo.Monotonic, true)
	dataBase := strings.SplitAfter(os.Getenv("MONGOHQ_URL"), "/")
	c := dbSession.DB(dataBase[3]).C("Dialogue")

	// err := c.Remove(bson.ObjectIdHex(DialogueId))
	err := c.Remove(D.Id)

	if err != nil {
		log.Println("Found Nothing. Something went wrong fetching the Dialogue document")
		log.Println(err)
		returnData.ErrorMsg = err.Error()
		returnData.Status = "400"
		returnData.Success = false
	} else {
		returnData.ErrorMsg = "All is well"
		returnData.Status = "200"
		returnData.Success = true
		returnData.JsonData = nil
	}
	return returnData
}