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