func (uc PublicationController) ListUserPublications(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["id"]
	// Verify id is ObjectId, otherwise bail
	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(404)
		return
	}

	// Grab id
	oid := bson.ObjectIdHex(id)

	// Stub publication
	results := make([]models.Publication, 0)

	// Fetch publications
	if err := uc.session.DB("prizma").C("publications").Find(bson.M{"publisherId": oid}).All(&results); err != nil {
		w.WriteHeader(404)
		return
	}

	// Marshal provided interface into JSON structure
	uj, _ := json.Marshal(results)

	// Write content-type, statuscode, payload
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)
	fmt.Fprintf(w, "%s", uj)
}
func (uc SubscriptionController) GetSubscription(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["id"]
	// Verify id is ObjectId, otherwise bail
	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(404)
		return
	}

	// Grab id
	oid := bson.ObjectIdHex(id)

	// Stub subscription
	u := models.Subscription{}

	// Fetch subscription
	if err := uc.session.DB("prizma").C("subscriptions").FindId(oid).One(&u); err != nil {
		w.WriteHeader(404)
		return
	}

	// Marshal provided interface into JSON structure
	uj, _ := json.Marshal(u)

	// Write content-type, statuscode, payload
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)
	fmt.Fprintf(w, "%s", uj)
}
// RemovePublication removes an existing publication resource
func (uc PublicationController) UpdatePublication(w http.ResponseWriter, r *http.Request) {
	// Grab id
	vars := mux.Vars(r)
	id := vars["pid"]
	// Stub an publication to be populated from the body
	u := models.Publication{}

	// Populate the publication data
	json.NewDecoder(r.Body).Decode(&u)

	// Verify id is ObjectId, otherwise bail
	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(404)
		return
	}

	// Grab id
	oid := bson.ObjectIdHex(id)

	// update publication
	if err := uc.session.DB("prizma").C("publications").UpdateId(oid, u); err != nil {
		w.WriteHeader(404)
		return
	}

	// Write status
	w.WriteHeader(200)
}
// RemoveSubscription removes an existing subscription resource
func (uc SubscriptionController) RemoveSubscription(w http.ResponseWriter, r *http.Request) {
	// Grab id
	vars := mux.Vars(r)
	id := vars["id"]

	// Verify id is ObjectId, otherwise bail
	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(404)
		return
	}

	// Grab id
	oid := bson.ObjectIdHex(id)

	// Remove subscription
	if err := uc.session.DB("prizma").C("subscriptions").RemoveId(oid); err != nil {
		w.WriteHeader(404)
		return
	}

	// Write status
	w.WriteHeader(200)
}
Exemple #5
0
func GetById(s *mgo.Session, e models.Entity, collection string, w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["id"]
	// Verify id is ObjectId, otherwise bail
	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(404)
		return
	}
	// Grab id
	oid := bson.ObjectIdHex(id)

	// Fetch user
	if err := s.DB("prizma").C(collection).FindId(oid).One(&e); err != nil {
		w.WriteHeader(404)
		return
	}
	// Marshal provided interface into JSON structure
	uj, _ := json.Marshal(e)

	// Write content-type, statuscode, payload
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)
	fmt.Fprintf(w, "%s", uj)
}