Example #1
13
func (pub *Publication) getProps(sess *dbr.Session, id string) ([]*pubProp, error) {
	var props []*pubProp
	_, err := sess.Select(
		"pubprop.value",
		"cvterm.name as term",
	).From("pubprop").
		Join("pub", dbr.Eq("pubprop.pub_id", "pub.pub_id")).
		Join("cvterm", dbr.Eq("pubprop.type_id", "cvterm.type_id")).
		Join("cv", dbr.Eq("cvterm.cv_id", "cv.cv_id")).
		Where(
			dbr.And(
				dbr.Eq("pub.uniquename", id),
				dbr.Eq("pub.is_obsolete", "0"),
				dbr.Eq("cv.name", "pub_type"),
			),
		).LoadStructs(&props)
	return props, err
}
Example #2
1
func (pub *Publication) Get(w http.ResponseWriter, r *http.Request) {
	id := router.Params(r).ByName("id")
	sess := pub.GetDbh().NewSession(nil)
	var pubRow pubData
	err := sess.Select(
		"pub.title",
		"pub.volume",
		"pub.series_name",
		"pub.issue",
		"pub.pages",
		"pub.uniquename",
		"cvterm.name",
		"pub.pubplace",
		"pub.pyear",
	).From("pub").Join("cvterm", dbr.Eq("pub.type_id", "cvterm.cvterm_id")).
		Where(
			dbr.And(
				dbr.Eq("pub.uniquename", id),
				dbr.Eq("pub.is_obsolete", "0"),
			),
		).LoadStruct(&pubRow)
	if err != nil {
		render.DatabaseError(w, err)
		return
	}
	props, err := pub.getProps(sess, id)
	if err != nil {
		render.DatabaseError(w, err)
		return
	}
	// Now check if authors needs to be included
	var authors []*publication.Author
	params := r.URL.Query()
	if val, ok := params["include"]; ok {
		if val[0] == "authors" {
			_, err := sess.Select(
				"pubauthor.pubauthor_id",
				"pubauthor.rank",
				"pubauthor.surname",
				"pubauthor.givennames",
			).From("pubauthor").
				Join("pub", dbr.Eq("pubauthor.pub_id", "pub.pub_id")).
				Where(
					dbr.And(
						dbr.Eq("pub.uniquename", id),
						dbr.Eq("pub.is_obsolete", "0"),
					),
				).LoadStructs(&authors)
			if err != nil {
				render.DatabaseError(w, err)
				return
			}
		}
	}

	// publication type struct that will be converted to json
	pubj := &publication.Publication{
		ID:      pubRow.PubId,
		Title:   pubRow.Title,
		Journal: pubRow.Journal,
		Year:    pubRow.Year,
		Volume:  pubRow.Volume.String,
		Pages:   pubRow.Pages.String,
		PubType: pubRow.PubType,
		Source:  pubRow.Source,
		Issue:   pubRow.Issue.String,
	}
	for _, p := range props {
		v := p.Value
		switch p.Term {
		case "doi":
			pubj.Doi = v
		case "status":
			pubj.Status = v
		case "month":
			pubj.Month = v
		case "issn":
			pubj.Issn = v
		case "abstract":
			pubj.Abstract = v
		}
	}
	if len(authors) > 0 {
		pubj.Authors = authors
	}
	render.Resource(pubj, resources.GetAPIServerInfo(r, pub.PathPrefix), w)
}