func GetResult(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) { if !util.CheckMethod(r, "GET") { return http.StatusMethodNotAllowed, nil } resultKey, err := datastore.DecodeKey(mux.Vars(r)["resultKey"]) if err != nil { return http.StatusBadRequest, err } var result model.Result if err := datastore.Get(ctx, resultKey, &result); err != nil { return http.StatusInternalServerError, nil } p, ok := passenger.FromContext(ctx) if !ok { return http.StatusUnauthorized, nil } if p.UserKey.Parent() != nil { json.NewEncoder(w).Encode(result.Key(resultKey)) return http.StatusOK, nil } if !util.HasParent(resultKey, p.UserKey) { return http.StatusUnauthorized, nil } return createFinalResult(ctx, w, resultKey, result) }
func DeleteProfile(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) { if !util.CheckMethod(r, "DELETE") { return http.StatusMethodNotAllowed, nil } key, err := datastore.DecodeKey(mux.Vars(r)["key"]) if err != nil { return http.StatusBadRequest, err } if err = datastore.Delete(ctx, key); err != nil { return http.StatusInternalServerError, err } return http.StatusOK, nil }
// GetUsersByCompany queries the user accounts belonging to a company. func GetUsersByCompany(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) { if !util.CheckMethod(r, "GET") { return http.StatusMethodNotAllowed, nil } key, err := datastore.DecodeKey(r.URL.Query()["result"][0]) if err != nil { return http.StatusBadRequest, err } var users model.Users keys, err := model.NewQueryForUser(). Ancestor(key). GetAll(ctx, &users) if err != nil { return http.StatusInternalServerError, err } json.NewEncoder(w).Encode(users.Key(keys)) return http.StatusOK, nil }
func GetProfileByKey(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) { if !util.CheckMethod(r, "GET") { return http.StatusMethodNotAllowed, nil } key, err := datastore.DecodeKey(mux.Vars(r)["key"]) if err != nil { return http.StatusBadRequest, err } var profile model.Profile if err := datastore.Get(ctx, key, &profile); err != nil { return http.StatusInternalServerError, err } json.NewEncoder(w).Encode(profile.Key(key)) return http.StatusOK, nil }
func GetCompanyByUser(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) { if !util.CheckMethod(r, "GET") { return http.StatusMethodNotAllowed, nil } p, ok := passenger.FromContext(ctx) if !ok { return http.StatusUnauthorized, nil } key := p.UserKey.Parent() if key == nil { return http.StatusUnauthorized, nil } // The account is associated with a company, so we return it. var company model.Company if err := datastore.Get(ctx, key, &company); err != nil { return http.StatusInternalServerError, err } json.NewEncoder(w).Encode(company.Key(key)) return http.StatusOK, nil }
// GetResultsByChallenge queries the results for a certain challenge to be reviewed by a company. func GetResultsByChallenge(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) { if !util.CheckMethod(r, "GET") { return http.StatusMethodNotAllowed, nil } key, err := datastore.DecodeKey(mux.Vars(r)["key"]) if err != nil { return http.StatusBadRequest, err } var results model.Results keys, err := model.NewQueryForResult(). Filter("Challenge=", key). GetAll(ctx, &results) if err != nil { return http.StatusInternalServerError, err } json.NewEncoder(w).Encode(results.Key(keys)) return http.StatusOK, nil }
// 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 }