Beispiel #1
0
func GetEndpoints(c *middleware.Context, query m.GetEndpointsQuery) Response {
	query.OrgId = c.OrgId

	if err := bus.Dispatch(&query); err != nil {
		return ApiError(500, "Failed to query endpoints", err)
	}
	return Json(200, query.Result)
}
Beispiel #2
0
func GetEndpoints(query *m.GetEndpointsQuery) error {
	sess := x.Table("endpoint")
	rawParams := make([]interface{}, 0)
	rawSql := `SELECT
		GROUP_CONCAT(DISTINCT(endpoint_tag.tag)) as tags,
		endpoint.*
	FROM endpoint
	LEFT JOIN endpoint_tag ON endpoint.id = endpoint_tag.endpoint_id
	`

	whereSql := make([]string, 0)
	whereSql = append(whereSql, "endpoint.org_id=?")
	rawParams = append(rawParams, query.OrgId)

	if len(query.Tag) > 0 {
		// this is a bit complicated because we want to
		// match only endpoints that have one of the specified tags
		// but we still need to return all of the tags that
		// the endpoint has.
		rawSql += "LEFT JOIN endpoint_tag AS et ON et.endpoint_id = endpoint.id\n"
		p := make([]string, len(query.Tag))
		for i, t := range query.Tag {
			p[i] = "?"
			rawParams = append(rawParams, t)
		}
		whereSql = append(whereSql, fmt.Sprintf("et.tag IN (%s)", strings.Join(p, ",")))
	}

	rawSql += "WHERE " + strings.Join(whereSql, " AND ")
	rawSql += " GROUP BY endpoint.id"

	result := make([]*EndpointWithTag, 0)
	err := sess.Sql(rawSql, rawParams...).Find(&result)
	if err != nil {
		return err
	}

	endpoints := make([]*m.EndpointDTO, 0)
	//iterate through all of the results and build out our model.
	for _, row := range result {
		tags := make([]string, 0)
		if row.Tags != "" {
			tags = strings.Split(row.Tags, ",")
		}
		endpoints = append(endpoints, &m.EndpointDTO{
			Id:    row.Id,
			OrgId: row.OrgId,
			Name:  row.Name,
			Slug:  row.Slug,
			Tags:  tags,
		})
	}

	query.Result = endpoints

	return nil
}