func (r *Registration) HandleRequest(w http.ResponseWriter, req *http.Request) { result := response.NewProfileResponse(0, "") username := req.FormValue("login") password := req.FormValue("password") if username == "" || password == "" { result.Result = 400 result.ResultMessage = "Login and Password are required" } else { profile := models.NewProfile(config.GetConnection()) if profile.FindByUsername(username) == nil { profile.Username = username profile.SetPassword(password) profile.Save() result.Data = profile } else { result.Result = 403 result.ResultMessage = "Account exists" } } jsonResult, err := json.Marshal(result) if err != nil { w.WriteHeader(500) } else { w.Write(jsonResult) } }
func (m *Messaging) notifyChannels(id int) { chanels := models.GetMessageChannelWrapper().GetChannels() profileO := models.NewProfile(config.GetConnection()) profile := profileO.GetById(id) fmt.Println(profile.Id) if profile != nil && profile.Id > 0 { newOnlineFriendResponse := response.NewOnlineFriendResponse(0, "") newOnlineFriendResponse.Data = profile jsonResult, err := json.Marshal(newOnlineFriendResponse) fmt.Println(string(jsonResult)) if err == nil { for key, channel := range chanels { if key != id { channel <- string(jsonResult) } } } } }
func (m *Messaging) sendMessage(w http.ResponseWriter, to, from int, message string, chTo chan string) { if chTo != nil { fmt.Println("Undelivered") messageResponse := response.NewMessageResponse(0, "") mess := models.NewMessage(config.GetConnection()) messagePull := mess.GetUndeliveredMessages(to) var newMessage models.Message newMessage.From = from newMessage.To = to newMessage.Body = message messagePull = append(messagePull, newMessage) mess.RemoveUndeliveredMessages(to) messageResponse.Data = messagePull fmt.Println("chan" + string(to) + " exists") fmt.Println("message: " + message) jsonResult, err := json.Marshal(messageResponse) if err == nil { chTo <- string(jsonResult) } } else { messageDB := models.NewMessage(config.GetConnection()) messageDB.Body = message messageDB.From = from messageDB.To = to messageDB.CreateDate = time.Now().Local().Unix() messageDB.IsRead = false messageDB.Save() } }
/** * Get online friends */ func (f *Friends) GetOnlineUsers(w http.ResponseWriter, req *http.Request) { result := response.NewFriendsReponse(0, "") onlineKeys := models.GetMessageChannelWrapper().GetChannelKeys() result.Data = models.NewProfile(config.GetConnection()).GetUsersByIds(onlineKeys) jsonResult, err := json.Marshal(result) if err != nil { w.WriteHeader(500) } else { w.Write(jsonResult) } }
/** * Find profiles */ func (f *Friends) FindUsers(w http.ResponseWriter, req *http.Request) { result := response.NewFriendsReponse(0, "") searchStr := req.FormValue("searchStr") if searchStr == "" { result.Result = 400 result.ResultMessage = "searchStr param is required" w.WriteHeader(400) } else { result.Data = models.NewProfile(config.GetConnection()).Find(searchStr) } jsonResult, err := json.Marshal(result) if err != nil { w.WriteHeader(500) } else { w.Write(jsonResult) } }
func (r Login) HandleRequest(w http.ResponseWriter, req *http.Request) { result := response.NewProfileResponse(0, "") login := req.FormValue("login") password := req.FormValue("password") if login == "" || password == "" { w.WriteHeader(400) result.Result = 400 result.ResultMessage = "Login and Password are required" } else { profile := models.NewProfile(config.GetConnection()).FindByCredentials(login, helpers.GetMD5(password)) result.Data = profile } jsonResult, err := json.Marshal(result) if err != nil { w.WriteHeader(500) } else { w.Write(jsonResult) } }