示例#1
0
func CreateGoal(enc encoder.Encoder, goal Goal) (int, []byte) {
	goalId, err := uuid.NewV4()
	if err != nil {
		return 500, encoder.Must(enc.Encode(""))
	}
	goal.Id = goalId.String()
	if goal.TargetType == "user" {
		user, ok := users[goal.Target]
		if !ok {
			return 400, encoder.Must(enc.Encode("Target does not exist"))
		}
		user.Goals[goal.Id] = &goal
	} else if goal.TargetType == "team" {
		team, ok := teams[goal.Target]
		if !ok {
			return 400, encoder.Must(enc.Encode("Target does not exist"))
		}
		team.Goals[goal.Id] = &goal
	} else {
		return 400, encoder.Must(enc.Encode("Unknown target type"))
	}

	goals[goal.Id] = &goal
	return 201, encoder.Must(enc.Encode(goal.Id))
}
示例#2
0
func GetGoal(enc encoder.Encoder, params martini.Params) (int, []byte) {
	goal, ok := goals[params["id"]]
	if ok {
		return 200, encoder.Must(enc.Encode(goal))
	}
	return 404, encoder.Must(enc.Encode())
}
示例#3
0
func GetUsers(dataStore DataStore, enc encoder.Encoder) (int, []byte) {
	users, err := dataStore.GetAllUsers()
	if err != nil {
		return http.StatusNoContent, encoder.Must(enc.Encode(NewError(ErrInternal, "Can't get users try again later")))
	}
	return http.StatusOK, encoder.Must(enc.Encode(users))
}
示例#4
0
func FetchStatus(s *Status, ms *mgo.Session, enc encoder.Encoder, r *http.Request) (int, []byte) {
	s.Log["MongoDB"] = NewLog(true, "")
	if err := ms.Ping(); err != nil {
		s.Log["MongoDB"] = NewLog(false, err.Error())
	}
	return http.StatusOK, encoder.Must(enc.Encode(s))
}
示例#5
0
func GetMac(res http.ResponseWriter, req *http.Request, enc encoder.Encoder) (int, []byte) {
	ip := strings.Split(req.RemoteAddr, ":")[0]
	mac, err := GetMacAddress("/proc/net/arp", ip)
	if err != nil {
		return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrMacNotFound, err.Error())))
	}
	return http.StatusOK, encoder.Must(enc.Encode(map[string]string{"mac": mac}))
}
示例#6
0
func listLocations(enc encoder.Encoder, r *http.Request, db *mgo.Database) (int, []byte) {
	var locations []Location

	c := db.C("locations")
	err := c.Find(nil).All(&locations)
	if err != nil {
		return http.StatusInternalServerError, []byte("Impossible to retrieve the locations: " + err.Error())
	}

	return http.StatusOK, encoder.Must(enc.Encode(locations))
}
示例#7
0
文件: server.go 项目: yc850k/haha
func GetServer(enc encoder.Encoder, db database.DB, parms martini.Params) (int, []byte) {
	fmt.Printf("begin get server\n")
	id, err := strconv.ParseInt(parms["id"], 10, 64)
	al := db.Get(id)
	if err != nil || al == nil {
		msg := fmt.Sprintf("the album with id %s does not exist", parms["id"])
		return http.StatusNotFound, encoder.Must(enc.Encode(
			NewError{errcode: 404, errmsg: msg}))
	}
	return http.StatusOK, encoder.Must(enc.Encode(al))
}
示例#8
0
文件: server.go 项目: yc850k/haha
func FindServer(enc encoder.Encoder, db database.DB, r *http.Request) (int, []byte) {
	fmt.Printf("begin find server\n")
	params := r.URL.Query()
	roomId, err := strconv.ParseInt(params.Get("room_id"), 10, 64)
	al := db.Find(roomId)
	if err != nil || al == nil {
		msg := fmt.Sprintf("server witch room_id %s does not exist", params.Get("id"))
		return http.StatusNotFound, encoder.Must(enc.Encode(
			NewError{errcode: 404, errmsg: msg}))
	}
	return http.StatusOK, encoder.Must(enc.Encode(al))
}
示例#9
0
func getLocation(params martini.Params, enc encoder.Encoder, db *mgo.Database) (int, []byte) {
	var location Location

	if !bson.IsObjectIdHex(params["id"]) {
		return http.StatusNotFound, []byte("Location with id \"" + params["id"] + "\" not found")
	}

	c := db.C("locations")
	err := c.FindId(bson.ObjectIdHex(params["id"])).One(&location)
	if err != nil {
		return http.StatusNotFound, []byte("Location with id \"" + params["id"] + "\" not found")
	}

	return http.StatusOK, encoder.Must(enc.Encode(location))
}
示例#10
0
func CompleteGoal(enc encoder.Encoder, params martini.Params, jsonParams GoalCompleteParams) (int, []byte) {
	goal, ok := goals[params["id"]]
	if !ok {
		return 404, encoder.Must(enc.Encode())
	}
	if _, ok := users[jsonParams.UserId]; !ok {
		return 400, encoder.Must(enc.Encode("User does not exist"))
	}
	goal.NumberOfCompletions--
	if goal.NumberOfCompletions > 0 {
		goal.State = "progress"
		return 200, encoder.Must(enc.Encode(goal.State)) //what should be returned here?
	}

	goal.State = "completed"
	return 200, encoder.Must(enc.Encode(goal.Reward))
}
示例#11
0
func createLocation(newLocation NewLocationForm, enc encoder.Encoder, db *mgo.Database) (int, []byte) {
	// transform the form struct into a real Location
	location := Location{
		ID: bson.NewObjectId(),
		Coordinates: Coordinates{
			Lat:  newLocation.Lat,
			Long: newLocation.Long,
		},
		Name:        newLocation.Name,
		Description: newLocation.Description,
		Visited:     newLocation.Visited,
		CreatedAt:   time.Now(),
	}

	// and try to insert it
	c := db.C("locations")
	err := c.Insert(&location)

	if err != nil {
		return http.StatusInternalServerError, []byte("Impossible to insert the location: " + err.Error())
	}

	return http.StatusCreated, encoder.Must(enc.Encode(location))
}
示例#12
0
func GetUser(dataStore DataStore, enc encoder.Encoder, params martini.Params) (int, []byte) {
	id, err := strconv.Atoi(params["id"])
	if err != nil {
		return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrInternal, "Invalid id")))
	}

	user, dbErr := dataStore.GetUser(id)
	if dbErr != nil {
		return http.StatusNotFound, encoder.Must(enc.Encode(NewError(ErrUserNotFound, dbErr.Error())))
	}
	return http.StatusOK, encoder.Must(enc.Encode(user))
}
示例#13
0
func CreatePreset(
	params martini.Params, w http.ResponseWriter,
	r *http.Request, enc encoder.Encoder) (int, []byte) {
	bytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return http.StatusBadRequest,
			encoder.Must(enc.Encode(&Error{err.Error()}))
	}
	preset := &Preset{}
	err = json.Unmarshal(bytes, &preset)
	if err != nil {
		return http.StatusBadRequest,
			encoder.Must(enc.Encode(&Error{err.Error()}))
	}
	presets.Add(preset)
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	return http.StatusCreated, encoder.Must(enc.Encode(preset))
}
示例#14
0
func GetTeams(enc encoder.Encoder) (int, []byte) {
	return 200, encoder.Must(enc.Encode(teams))
}
示例#15
0
func GetUsers(enc encoder.Encoder) (int, []byte) {
	return 200, encoder.Must(enc.Encode(users))
}
示例#16
0
func CreateTeam(enc encoder.Encoder, team Team) (int, []byte) {
	team.Users = make(map[string]string)
	team.Goals = make(map[string]*Goal)
	teams[team.Id] = &team
	return 201, encoder.Must(enc.Encode(team.Id))
}
示例#17
0
func CreateUser(enc encoder.Encoder, user User) (int, []byte) {
	user.Teams = make(map[string]string)
	user.Goals = make(map[string]*Goal)
	users[user.Id] = &user
	return 201, encoder.Must(enc.Encode(user.Id))
}
示例#18
0
func GetTeam(enc encoder.Encoder, params martini.Params) (int, []byte) {
	if team, ok := teams[params["id"]]; ok {
		return 200, encoder.Must(enc.Encode(team))
	}
	return 404, encoder.Must(enc.Encode())
}
示例#19
0
func GetUserGoals(enc encoder.Encoder, params martini.Params) (int, []byte) {
	if user, ok := users[params["id"]]; ok {
		return 200, encoder.Must(enc.Encode(user.Goals))
	}
	return 404, encoder.Must(enc.Encode())
}
示例#20
0
func GetPreset(enc encoder.Encoder, w http.ResponseWriter) (int, []byte) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	return http.StatusOK, encoder.Must(enc.Encode(&presets))
}