Example #1
0
func (self *Server) getResource(w http.ResponseWriter, r *http.Request) {
	project, err := getProject(self.core.Db, r)

	if err != nil {
		self.writeError(w, err, http.StatusBadRequest)
		return
	}

	vars := mux.Vars(r)
	resource_id := vars["resource_id"]

	var resource *database.Resource
	err = self.core.Db.Get(database.ResourcesCol, resource_id, &resource)

	if err != nil {

		self.writeError(w, err, http.StatusBadRequest)
		return
	}

	if project.Id.Hex() != resource.ProjectId.Hex() {
		self.writeError(w, fmt.Errorf("project %s does not have a resource with id: %s", project.Id.Hex(), resource_id), http.StatusBadRequest)
		return
	}

	self.respond(w, &resource)

}
Example #2
0
func (self *Server) getProject(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["project_id"]

	var project database.Project
	err := self.core.Db.Get(database.ProjectsCol, id, &project)

	if err != nil {
		self.writeError(w, err, 300)
		return
	}

	self.respond(w, &project)

}
Example #3
0
func (self *Server) removeProject(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["project_id"]

	err := self.core.Db.Remove(database.ProjectsCol, id)

	if err != nil {
		self.writeError(w, err, 500)
	}

	self.render.JSON(w, http.StatusOK, &Query{
		"code":    200,
		"message": "ok",
	})

}
Example #4
0
func getProject(db database.Datastore, r *http.Request) (*database.Project, error) {
	vars := mux.Vars(r)
	projectId := vars["project_id"]

	if projectId == "" {
		return nil, fmt.Errorf("no project_id specified")
	}

	var project database.Project
	err := db.Get(database.ProjectsCol, projectId, &project)
	if err != nil {
		return nil, fmt.Errorf("could not find project: %s, error: %v", projectId, err)
	} else if !project.Id.Valid() {
		return nil, fmt.Errorf("project with id: %s, not found", projectId)
	}

	return &project, nil
}
Example #5
0
func (self *Server) updateProject(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["project_id"]
	var project database.Project
	err := self.read(r, &project)

	if err != nil {
		self.writeError(w, err, http.StatusBadRequest)
		return
	}

	err = self.core.Db.Update(database.ProjectsCol, id, project)

	if err != nil {
		self.writeError(w, err, http.StatusInternalServerError)
	}

	self.respond(w, &project)

}
Example #6
0
func (self *Server) infoProject(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["project_id"]

	query := database.Query{
		"project_id": bson.ObjectIdHex(id),
	}

	count, err := self.core.Db.Count(database.ResourcesCol, query)

	if err != nil {
		self.writeError(w, err, http.StatusInternalServerError)
		return
	}

	out := types.Message{
		"resources": count,
	}

	self.respond(w, &out)

}