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})
	}
}
Example #7
0
func (s *StatesResource) Cities(c *gin.Context) {

	var ci []*models.City
	stateUri := c.Params.ByName("uri")
	query := c.Request.URL.Query()

	search := bson.M{"stateid": stateUri}

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

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

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

		done := make(chan bool)

		go func() {
			s.setCitiesLinks(ci)
			done <- true
		}()

		<-done
		data := gin.H{
			"cities": ci,
			"paging": pagination(
				"v1/states/"+stateUri+"/cities",
				s.DB,
				GetLimit(c.Request),
				page,
				models.City{},
				search,
			),
		}

		render := &DataRender{c.Request, data}
		c.Render(200, render)
	}
}
Example #8
0
func (self TrainsSpResource) GetLineStatuses(c *gin.Context) {
	uri := c.Params.ByName("line_uri")
	var s []*models.Status

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

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

		c.Render(200, DataRender{c.Request}, gin.H{"statuses": s})
	}
}
Example #9
0
func (self TrainsSpResource) GetLineStatus(c *gin.Context) {
	uri := c.Params.ByName("line_uri")
	statusId := bson.ObjectIdHex(c.Params.ByName("status_id"))
	var status models.Status

	err := self.DB.FindOne(bson.M{"line_id": uri, "_id": statusId}, &status)
	if err != nil {
		c.JSON(404, gin.H{"error": "404", "message": err.Error()})
	} else {

		done := make(chan bool)
		go func() {
			status.Links = self.getStatusLink(uri, status.Id)
			done <- true
		}()
		<-done

		c.Render(200, DataRender{c.Request}, gin.H{"status": status})
	}
}
Example #10
0
func (s *StatesResource) GetCity(c *gin.Context) {
	var city models.City
	stateUri := c.Params.ByName("uri")
	cityUri := c.Params.ByName("city_uri")

	q := bson.M{"stateid": stateUri, "id": cityUri}
	err := s.DB.FindOne(q, &city)

	if err != nil {
		c.JSON(404, gin.H{"error": "404", "message": err.Error()})
	} else {
		done := make(chan bool)
		go func() {
			city.Links = s.setCityLinks(city)
			done <- true
		}()
		<-done

		c.Render(200, DataRender{c.Request}, gin.H{"city": city})
	}
}
Example #11
0
func (r *ReservoirSpResource) Index(c *gin.Context) {
	var rv []struct {
		Id         string             `bson:"_id" json:"granularity"`
		Reservoirs []models.Reservoir `json:"reservoirs"`
	}

	search := bson.M{}
	err := r.DB.FindAndGroupBy("granularity", search, &rv, models.Reservoir{})

	if err != nil {
		c.JSON(500, gin.H{"error": "500", "message": err.Error()})
	} else {
		data := make(map[string][]models.Reservoir, 0)

		for _, item := range rv {
			data[item.Id] = item.Reservoirs
		}

		c.Render(200, DataRender{c.Request}, data)
	}
}
Example #12
0
func (self TrainsSpResource) GetLine(c *gin.Context) {
	uri := c.Params.ByName("line_uri")
	var l models.Line

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

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

		done := make(chan bool)
		go func() {
			l.Links = self.getLink(l.Id, l.CannonicalUri)
			l.Status = self.getStatus(l.CannonicalUri)
			done <- true
		}()
		<-done

		c.Render(200, DataRender{c.Request}, gin.H{"line": l})
	}
}
Example #13
0
func (r *PartyResource) Index(c *gin.Context) {
	var p []*models.Party
	search := bson.M{}
	query := c.Request.URL.Query()

	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{"parties": []string{}})
			return
		}

		data := gin.H{
			"parties": p,
			"paging": pagination(
				"v1/parties",
				r.DB,
				GetLimit(c.Request),
				page,
				models.Party{},
				search,
			),
		}

		render := &DataRender{c.Request, data}
		c.Render(200, render)
	}
}