Golang Context.Render Examples

Golang Context.Render - 13 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.Render extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func (r *StatesResource) Index(c *gin.Context) {
	var s []*models.State
	search := bson.M{}

	err := r.DB.Find(search, GetLimit(c.Request), 1, "-updatedat", &s)

	if err != nil {
		c.JSON(500, gin.H{"error": "500", "message": err.Error()})
	} else {
		if len(s) == 0 {
			c.JSON(200, gin.H{"states": []string{}})
			return
		}

		for i, _ := range s {
			s[i].Capital = r.getCapital(s[i].CapitalId)
			s[i].Links = r.getStateLinks(*s[i])
		}

		data := gin.H{
			"states": s,
		}

		render := &DataRender{c.Request, data}
		c.Render(200, render)
	}
}
Example #2
0
func (self TrainsSpResource) Lines(c *gin.Context) {
	var l []*models.Line

	err := self.DB.Find(bson.M{}, GetLimit(c.Request), 1, "linenumber", &l)

	if err != nil {
		c.JSON(500, gin.H{"error": "500", "message": err.Error()})
	} else {
		done := make(chan bool)
		go func() {
			self.setLinks(l)
			self.setStatus(l)
			done <- true
		}()
		<-done

		data := gin.H{
			"lines": l,
			"paging": pagination(
				"v1/sp/metro/lines",
				self.DB,
				GetLimit(c.Request),
				1,
				models.Line{},
				bson.M{},
			),
		}

		render := &DataRender{c.Request, data}
		c.Render(200, render)
	}
}
Example #3
0
func (r *ParliamentarianResource) Get(c *gin.Context) {
	uri := c.Params.ByName("uri")

	var p models.Parliamentarian

	err := r.DB.FindOne(bson.M{"id": uri}, &p)

	done := make(chan bool)

	go func() {
		p.Links = r.getLink(p.Id)
		p.Memberships = r.getMemberships(p.Id)
		done <- true
	}()

	<-done

	if err != nil {
		c.JSON(404, gin.H{"error": "404", "message": err.Error()})
	} else {

		render := &DataRender{c.Request, gin.H{"parliamentarian": p}}
		c.Render(200, render)
	}
}
Example #4
0
func (r *ParliamentarianResource) Index(c *gin.Context) {
	var p []*models.Parliamentarian
	search := bson.M{}
	query := c.Request.URL.Query()
	identifier := query.Get("identifier")

	if identifier != "" {
		search["identifiers"] = bson.M{
			"$elemMatch": bson.M{
				"identifier": identifier,
			},
		}
	}

	pageS := query.Get("page")
	if pageS == "" {
		pageS = "1"
	}
	page, _ := strconv.Atoi(pageS)

	err := r.DB.Find(search, GetLimit(c.Request), page, "-updatedat", &p)

	if err != nil {
		c.JSON(500, gin.H{"error": "500", "message": err.Error()})
	} else {
		if len(p) == 0 {
			c.JSON(200, gin.H{"parliamentarians": []string{}})
			return
		}

		done := make(chan bool)

		go func() {
			r.setLinks(p)
			r.setMemberships(p)
			done <- true
		}()

		<-done
		data := gin.H{
			"parliamentarians": p,
			"paging": pagination(
				"v1/parliamentarians",
				r.DB,
				GetLimit(c.Request),
				page,
				models.Parliamentarian{},
				search,
			),
		}

		render := &DataRender{c.Request, data}
		c.Render(200, render)
	}
}
Example #5
0
func (r *PartyResource) Get(c *gin.Context) {
	uri := c.Params.ByName("uri")

	var p models.Party

	err := r.DB.FindOne(bson.M{"id": uri}, &p)

	if err != nil {
		c.JSON(404, gin.H{"error": "404", "message": err.Error()})
	} else {
		c.Render(200, DataRender{c.Request}, gin.H{"party": p})
	}
}
Example #6
0
func (r *StatesResource) Get(c *gin.Context) {
	uri := c.Params.ByName("uri")

	var s models.State

	err := r.DB.FindOne(bson.M{"id": uri}, &s)

	if err != nil {
		c.JSON(404, gin.H{"error": "404", "message": err.Error()})
	} else {
		s.Capital = r.getCapital(s.CapitalId)
		s.Links = r.getStateLinks(s)
		c.Render(200, DataRender{c.Request}, gin.H{"state": s})
	}
}