本文整理匯總了Golang中github.com/coduno/api/model.Result.SaveWithParent方法的典型用法代碼示例。如果您正苦於以下問題:Golang Result.SaveWithParent方法的具體用法?Golang Result.SaveWithParent怎麽用?Golang Result.SaveWithParent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/coduno/api/model.Result
的用法示例。
在下文中一共展示了Result.SaveWithParent方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CreateResult
// CreateResult saves a new result when a coder starts a challenge.
func CreateResult(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) {
if !util.CheckMethod(r, "POST") {
return http.StatusMethodNotAllowed, nil
}
var body = struct {
ChallengeKey string
}{}
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
var profiles model.Profiles
keys, err := model.NewQueryForProfile().
Ancestor(p.UserKey).
GetAll(ctx, &profiles)
if len(keys) != 1 {
return http.StatusInternalServerError, errors.New("Profile not found")
}
err = json.NewDecoder(r.Body).Decode(&body)
if err != nil {
return http.StatusBadRequest, err
}
key, err := datastore.DecodeKey(body.ChallengeKey)
if err != nil {
return http.StatusBadRequest, err
}
var results []model.Result
resultKeys, err := model.NewQueryForResult().
Ancestor(keys[0]).
Filter("Challenge = ", key).
Limit(1).
GetAll(ctx, &results)
if err != nil {
return http.StatusInternalServerError, err
}
if len(resultKeys) == 1 {
json.NewEncoder(w).Encode(results[0].Key(resultKeys[0]))
return http.StatusOK, nil
}
var challenge model.Challenge
if err = datastore.Get(ctx, key, &challenge); err != nil {
return http.StatusInternalServerError, err
}
result := model.Result{
Challenge: key,
StartTimes: make([]time.Time, len(challenge.Tasks)),
FinalSubmissions: make([]*datastore.Key, len(challenge.Tasks)),
}
key, err = result.SaveWithParent(ctx, keys[0])
if err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(result.Key(key))
return http.StatusOK, nil
}