本文整理匯總了Golang中github.com/mshockwave/nthuaplus-backend/public.ResponseOkAsJson函數的典型用法代碼示例。如果您正苦於以下問題:Golang ResponseOkAsJson函數的具體用法?Golang ResponseOkAsJson怎麽用?Golang ResponseOkAsJson使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ResponseOkAsJson函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: handleReviewLogin
func handleReviewLogin(resp http.ResponseWriter, req *http.Request) {
email := public.EmailFilter(req.FormValue("email"))
password := req.FormValue("password")
if len(email) <= 0 || len(password) <= 0 {
r := public.SimpleResult{
Message: "Error",
Description: "Incorrect email or password",
}
public.ResponseStatusAsJson(resp, 403, &r)
return
}
//Check login status
if _, err := public.GetSessionReviewerId(req); err == nil {
r := public.SimpleResult{
Message: "Already Login",
Description: email,
}
public.ResponseOkAsJson(resp, &r)
return
}
reviewerDb := public.GetNewReviewerDatabase()
defer reviewerDb.Session.Close()
profiles := reviewerDb.C(REVIEWER_DB_PROFILE_COLLECTION)
q := profiles.Find(bson.M{"baseprofile.email": email})
reviewer := db.Reviewer{}
if q.One(&reviewer) == nil {
//Check password
if bcrypt.CompareHashAndPassword([]byte(reviewer.BaseProfile.AuthInfo.BcyptHash), []byte(password)) != nil {
r := public.SimpleResult{
Message: "Error",
Description: "Incorrect email or password",
}
public.ResponseStatusAsJson(resp, 403, &r)
return
}
if err := public.SetReviewerSessionValue(req, resp, public.REVIEWER_ID_SESSION_KEY, reviewer.Id.Hex()); err != nil {
public.LogE.Printf("Error setting session user id: %s\n", err.Error())
}
r := public.SimpleResult{
Message: "Login Successed",
Description: email,
}
public.ResponseOkAsJson(resp, &r)
} else {
r := public.SimpleResult{
Message: "Error",
Description: "Incorrect email or password",
}
public.ResponseStatusAsJson(resp, 403, &r)
return
}
}
示例2: handleViewStagingRecomms
func handleViewStagingRecomms(resp http.ResponseWriter, req *http.Request) {
user_id, _ := public.GetSessionUserId(req)
stagingDb := public.GetNewStagingDatabase()
defer stagingDb.Session.Close()
recomms := stagingDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
q := recomms.Find(bson.M{
"recommender": user_id,
})
var result []public.RecommView
recomm_item := db.RecommEntity{}
it := q.Iter()
for it.Next(&recomm_item) {
signed_url := ""
if client, err := storage.GetNewStorageClient(); err == nil {
expireTime := time.Now().Add(time.Duration(1) * time.Hour) //an hour
signed_url, _ = client.GetNewSignedURL(string(recomm_item.Attachment), expireTime)
}
result = append(result, public.RecommView{
Hash: recomm_item.Hash,
ApplyUser: recomm_item.ApplyUser,
Topic: recomm_item.Topic,
LastModified: recomm_item.LastModified,
Content: recomm_item.Content,
Attachment: signed_url,
})
}
public.ResponseOkAsJson(resp, &result)
}
示例3: removeStagingRecomm
func removeStagingRecomm(resp http.ResponseWriter, req *http.Request) {
user_id, _ := public.GetSessionUserId(req)
vars := mux.Vars(req)
hash := vars["hash"]
stagingDb := public.GetNewStagingDatabase()
defer stagingDb.Session.Close()
recomm := stagingDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
q := recomm.Find(bson.M{
"hash": hash,
"recommender": user_id,
})
recomm_result := db.RecommEntity{}
if e := q.One(&recomm_result); e != nil {
public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
Message: "No such recomm entry",
})
return
}
if e := recomm.RemoveId(recomm_result.Id); e != nil {
public.LogE.Printf("Remove recomm entity failed: %s\n", e.Error())
public.ResponseStatusAsJson(resp, 500, &public.SimpleResult{
Message: "Remove recomm failed",
})
} else {
public.ResponseOkAsJson(resp, nil)
}
}
示例4: handleGMRecommsResend
func handleGMRecommsResend(resp http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
hash := vars["recommHash"]
appDb := public.GetNewApplicationDatabase()
defer appDb.Session.Close()
recomm := appDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
q := recomm.Find(bson.M{
"hash": hash,
})
recommObj := db.Recomm{}
if err := q.One(&recommObj); err != nil || len(hash) <= 0 {
public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
Message: "Error",
Description: "No Such page",
})
return
}
url := "https://application.nthuaplus.org/recomm.html?hash=" + hash
if e := public.SendMail(recommObj.Recommender.Email, recommObj.ApplyUser, url); e != nil {
public.LogE.Printf("Error sending email: %s\n", e.Error())
public.ResponseStatusAsJson(resp, 500, &public.SimpleResult{
Message: "Error",
})
} else {
public.ResponseOkAsJson(resp, nil)
}
}
示例5: handleBulletinNotes
func handleBulletinNotes(resp http.ResponseWriter, req *http.Request) {
miscDb := public.GetNewMiscDatabase()
defer miscDb.Session.Close()
bulletin := miscDb.C(MISC_DB_BULLETIN_COLLECTION)
q := bulletin.Find(bson.M{})
if _, e := q.Count(); e != nil {
public.ResponseStatusAsJson(resp, 500, &public.SimpleResult{
Message: "Error",
Description: "Failed fetching bulletin notes",
})
return
}
var results []bulletinNoteResult
it := q.Iter()
result := db.BulletinNote{}
for it.Next(&result) {
results = append(results, bulletinNoteResult{
Title: result.Title,
Content: result.Content,
TimeStamp: result.TimeStamp,
})
}
public.ResponseOkAsJson(resp, &results)
}
示例6: handleReviewerLogout
func handleReviewerLogout(resp http.ResponseWriter, req *http.Request) {
if err := public.SetReviewerSessionValue(req, resp, public.REVIEWER_ID_SESSION_KEY, nil); err != nil {
public.LogE.Printf("Logout Failed: %s\n", err.Error())
public.ResponseStatusAsJson(resp, 500, &public.SimpleResult{
Message: "Error",
Description: "Logout Failed",
})
} else {
public.ResponseOkAsJson(resp, &public.SimpleResult{
Message: "Logout Success",
})
}
}
示例7: getUserRecomms
func getUserRecomms(resp http.ResponseWriter, req *http.Request) {
user_id, _ := public.GetSessionUserId(req)
appDb := public.GetNewApplicationDatabase()
defer appDb.Session.Close()
forms := appDb.C(public.APPLICATION_DB_FORM_COLLECTION)
q := forms.Find(bson.M{"ownerid": user_id}).Sort("-timestamp")
it := q.Iter()
form := db.ApplicationForm{}
var recommList []public.RecommResult
topicMap := make(map[public.TopicId]bool)
for it.Next(&form) {
if _, exist := topicMap[form.Topic]; exist {
//Skip
continue
}
recomm := appDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
for _, h := range form.Recommendations {
//Transform recommendation from hash to structures
q := recomm.Find(bson.M{
"hash": h,
})
if n, e := q.Count(); e == nil && n > 0 {
r := db.Recomm{}
if e := q.One(&r); e == nil {
r := public.RecommResult{
Recommender: r.Recommender,
ApplyUser: r.ApplyUser,
Done: r.Submitted,
Hash: h,
}
recommList = append(recommList, r)
}
}
}
topicMap[form.Topic] = true
}
public.ResponseOkAsJson(resp, &recommList)
}
示例8: handleLogout
func handleLogout(resp http.ResponseWriter, req *http.Request) {
if e := public.SetUserSessionValue(req, resp, public.USER_PERMISSION_SESSION_KEY, nil); e != nil {
public.LogE.Printf("Error cleaning session user permission: %s\n", e.Error())
}
if err := public.SetUserSessionValue(req, resp, public.USER_ID_SESSION_KEY, nil); err != nil {
public.LogE.Printf("Logout Failed: %s\n", err.Error())
public.ResponseStatusAsJson(resp, 500, &public.SimpleResult{
Message: "Error",
Description: "Logout Failed",
})
} else {
public.ResponseOkAsJson(resp, &public.SimpleResult{
Message: "Logout Success",
})
}
}
示例9: handleViewRecomm
func handleViewRecomm(resp http.ResponseWriter, req *http.Request) {
user_id, _ := public.GetSessionUserId(req)
user_perm, _ := public.GetSessionUserPermission(req)
vars := mux.Vars(req)
hash_str := vars["hash"]
appDb := public.GetNewApplicationDatabase()
defer appDb.Session.Close()
recomm := appDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
q := recomm.Find(bson.M{
"hash": hash_str,
})
recomm_result := db.RecommEntity{}
if e := q.One(&recomm_result); e != nil {
public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
Message: "No such recomm entity",
})
return
}
// Permission check
if recomm_result.Recommender != user_id && user_perm != public.USER_PERMISSION_GM {
public.ResponseStatusAsJson(resp, 403, nil)
return
}
signed_url := ""
if client, err := storage.GetNewStorageClient(); err == nil {
expireTime := time.Now().Add(time.Duration(1) * time.Hour) //an hour
signed_url, _ = client.GetNewSignedURL(string(recomm_result.Attachment), expireTime)
}
public.ResponseOkAsJson(resp, &public.RecommView{
Hash: recomm_result.Hash,
ApplyUser: recomm_result.ApplyUser,
Topic: recomm_result.Topic,
LastModified: recomm_result.LastModified,
Content: recomm_result.Content,
Attachment: signed_url,
})
}
示例10: editStagingRecomm
func editStagingRecomm(resp http.ResponseWriter, req *http.Request) {
user_id, _ := public.GetSessionUserId(req)
vars := mux.Vars(req)
hash := vars["hash"]
stagingDb := public.GetNewStagingDatabase()
defer stagingDb.Session.Close()
recomm := stagingDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
q := recomm.Find(bson.M{
"hash": hash,
"recommender": user_id,
})
recomm_result := db.RecommEntity{}
if e := q.One(&recomm_result); e != nil {
public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
Message: "No such recomm entry",
})
return
}
content := req.FormValue("content")
attachment := req.FormValue("attachment")
if len(content) > 0 {
recomm_result.Content = content
recomm_result.LastModified = time.Now()
}
if len(attachment) > 0 {
recomm_result.Attachment = public.FileStoragePath(attachment)
recomm_result.LastModified = time.Now()
}
if e := recomm.UpdateId(recomm_result.Id, &recomm_result); e != nil {
public.LogE.Printf("Update recomm entity failed: %s\n", e.Error())
public.ResponseStatusAsJson(resp, 500, &public.SimpleResult{
Message: "Update recomm failed",
})
} else {
public.ResponseOkAsJson(resp, nil)
}
}
示例11: handleQueryAccount
func handleQueryAccount(resp http.ResponseWriter, req *http.Request) {
email := req.URL.Query().Get("email")
if len(email) > 0 {
userDb := public.GetNewUserDatabase()
defer userDb.Session.Close()
profiles := userDb.C(USER_DB_PROFILE_COLLECTION)
q := profiles.Find(bson.M{"email": email})
user := db.User{}
if e := q.One(&user); e == nil {
//GM can get more information
public.ResponseOkAsJson(resp, &user)
} else {
public.ResponseStatusAsJson(resp, 404, nil)
}
} else {
public.ResponseStatusAsJson(resp, 404, nil)
}
}
示例12: handleFormFileUpload
func handleFormFileUpload(resp http.ResponseWriter, req *http.Request) {
if f, h, e := req.FormFile("file"); e == nil && f != nil && h != nil {
if objName, err := saveFile(h, f); err == nil {
public.ResponseOkAsJson(resp, &public.SimpleResult{
Message: "Success",
Description: objName,
})
} else {
public.LogE.Printf("Error storing file: %s\n", err.Error())
public.ResponseStatusAsJson(resp, 400, &public.SimpleResult{
Message: "Error",
})
}
} else {
public.ResponseStatusAsJson(resp, 400, &public.SimpleResult{
Message: "Error",
})
}
}
示例13: handleRecommDoorBell
func handleRecommDoorBell(resp http.ResponseWriter, req *http.Request) {
_, user_err := public.GetSessionUserId(req)
if user_err != nil {
public.ResponseStatusAsJson(resp, 403, nil)
return
}
user_perm, _ := public.GetSessionUserPermission(req)
if !user_perm.ContainsPermission(public.USER_PERMISSION_RECOMM) {
public.ResponseStatusAsJson(resp, 403, &public.SimpleResult{
Message: "Not Recommender",
})
return
}
public.ResponseOkAsJson(resp, &public.SimpleResult{
Message: "Ok",
})
}
示例14: handleReviewerProfile
func handleReviewerProfile(resp http.ResponseWriter, req *http.Request) {
userId, _ := public.GetSessionReviewerId(req)
reviewerDb := public.GetNewReviewerDatabase()
defer reviewerDb.Session.Close()
profile := reviewerDb.C(REVIEWER_DB_PROFILE_COLLECTION)
q := profile.FindId(userId)
if c, err := q.Count(); c == 0 || err != nil {
r := public.SimpleResult{
Message: "Error",
Description: "User Not Found",
}
public.ResponseStatusAsJson(resp, 500, &r)
} else {
reviewer := db.Reviewer{}
q.One(&reviewer)
r := public.ReviewerProfile{
Email: reviewer.BaseProfile.Email,
Username: reviewer.BaseProfile.Username,
FormalId: reviewer.BaseProfile.FormalId,
Topics: reviewer.Topics,
Permissions: reviewer.Permissions,
}
if client, err := storage.GetNewStorageClient(); err == nil && len(reviewer.BaseProfile.Thumbnail) > 0 {
defer client.Close()
expire := time.Now().Add(time.Duration(12) * time.Hour)
if r.Thumbnail, err = client.GetNewSignedURL(reviewer.BaseProfile.Thumbnail, expire); err != nil {
r.Thumbnail = ""
}
}
public.ResponseOkAsJson(resp, &r)
}
}
示例15: handleReviewerRecommView
func handleReviewerRecommView(resp http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
hashStr := vars["recommHash"]
appDb := public.GetNewApplicationDatabase()
defer appDb.Session.Close()
recomm := appDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
q := recomm.Find(bson.M{
"hash": hashStr,
})
recommInstance := db.Recomm{}
if err := q.One(&recommInstance); err != nil {
public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
Message: "Error",
Description: "Hash not found",
})
return
}
recommResult := reviewerRecommResult{
Content: recommInstance.Content,
}
if len(recommInstance.Attachment) > 0 {
//Create temp url
if client, err := storage.GetNewStorageClient(); err == nil {
expireTime := time.Now().Add(time.Duration(1) * time.Hour) //an hour
if obj, e := client.GetNewSignedURL(recommInstance.Attachment, expireTime); e == nil {
recommResult.AttachmentUrl = obj
} else {
public.LogE.Println("Get object error: " + e.Error())
}
}
}
public.ResponseOkAsJson(resp, &recommResult)
}