func QueryUserCommunities(w http.ResponseWriter, r *http.Request) { var loggedUser *model.User var ok bool if ok, loggedUser = authentication.RequireTokenAuthentication(w, r); !ok { return } vars := mux.Vars(r) status := vars["status"] communityQuery := &model.CommunityQuery{} if err := util.DecodeJson(r.Body, communityQuery); err != nil { packError(w, err, ERROR_CODE, "query communitied, struct error") return } statusCode := 0 if status == "joined" { statusCode = model.COMMUNITY_USER_STATUS_JOINED } else if status == "apply" { statusCode = model.COMMUNITY_USER_STATUS_APPLY } if statusCode == 0 { packResult(w, nil) return } communities := communityQuery.QueryByUserAndStatus(loggedUser, statusCode) packResult(w, communities) }
func Login(w http.ResponseWriter, r *http.Request) { login := &model.Login{} if err := util.DecodeJson(r.Body, login); err != nil { packError(w, err, ERROR_CODE, "") return } responseStatus, token := service.Login(login) w.Header().Set("Content-Type", "application/json") w.WriteHeader(responseStatus) w.Write(token) }
func RefreshToken(w http.ResponseWriter, r *http.Request) { if ok, _ := authentication.RequireTokenAuthentication(w, r); !ok { return } login := &model.Login{} if err := util.DecodeJson(r.Body, login); err != nil { packError(w, err, ERROR_CODE, "") return } w.Header().Set("Content-Type", "application/json") w.Write(service.RefreshToken(login)) }
func QueryCommunities(w http.ResponseWriter, r *http.Request) { if ok, _ := authentication.RequireTokenAuthentication(w, r); !ok { return } communityQuery := &model.CommunityQuery{} if err := util.DecodeJson(r.Body, communityQuery); err != nil { packError(w, err, ERROR_CODE, "query communitied, struct error") return } communities := communityQuery.Query() packResult(w, communities) }
func Register(w http.ResponseWriter, r *http.Request) { account := &model.Account{} if err := util.DecodeJson(r.Body, account); err != nil { packError(w, err, ERROR_CODE, "register, user structure decode error") return } if err := account.Register(); err != nil { packError(w, err, ERROR_CODE, "register, register error") return } packResult(w, "ok") }
func UpdateCommunity(w http.ResponseWriter, r *http.Request) { var loggedUser *model.User var ok bool if ok, loggedUser = authentication.RequireTokenAuthentication(w, r); !ok { return } community := &model.Community{} if err := util.DecodeJson(r.Body, community); err != nil { packError(w, err, ERROR_CODE, "update community, struct error") return } community.UpdatedUserId = loggedUser.UserId if err := community.Update(); err != nil { packError(w, err, ERROR_CODE, "") return } packResult(w, "ok") }