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) } } } } }
/** * 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) } }