Esempio n. 1
0
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)
}
Esempio n. 2
0
func Logout(w http.ResponseWriter, r *http.Request) {

	if ok, _ := authentication.RequireTokenAuthentication(w, r); !ok {
		return
	}

	err := service.Logout(r)
	w.Header().Set("Content-Type", "application/json")
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
	} else {
		w.WriteHeader(http.StatusOK)
	}
}
Esempio n. 3
0
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))
}
Esempio n. 4
0
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)
}
Esempio n. 5
0
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")
}
Esempio n. 6
0
func GetCommunity(w http.ResponseWriter, r *http.Request) {

	if ok, _ := authentication.RequireTokenAuthentication(w, r); !ok {
		return
	}

	vars := mux.Vars(r)
	communityId := util.StringToInt(vars["community_id"])

	community := &model.Community{
		CommunityId: communityId,
	}

	if exists, err := community.Get(); err != nil {
		packError(w, err, ERROR_CODE, "")
		return
	} else if !exists {
		packResult(w, "")
		return
	}

	packResult(w, community)
}
Esempio n. 7
0
func DeleteCommunity(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)
	communityId := util.StringToInt(vars["community_id"])

	community := &model.Community{
		CommunityId: communityId,
	}

	community.DeletedUserId = loggedUser.UserId

	if err := community.Delete(); err != nil {
		packError(w, err, ERROR_CODE, "")
		return
	}

	packResult(w, "ok")
}