Example #1
0
func (this *eventService) Events() (*[]Event, error) {
	var err error
	collection := make([]Event, 0)
	if conn, ok := db.Connection(); ok {
		query := &conn
		limit := 20 // Defaults to 20 items per page
		page := 1
		if v, ok := this.params["conference"]; ok {
			query = query.Where("conference_slug = ?", v)
		}
		if v, ok := this.params["query"]; ok {
			// FIXME: CHECK injection possibility
			query = query.Where("title ILIKE ?", fmt.Sprintf("%%%v%%", v))
		}
		if _, ok := this.params["shuffle"]; ok {
			query = query.Where("random() < 0.1")
		}
		if v, ok := this.params["limit"]; ok {
			if v.(int) <= 50 {
				limit = v.(int)
			}
		}
		if v, ok := this.params["page"]; ok {
			page = v.(int)
		}
		err = query.Scopes(Active, Paginate(limit, page)).Preload("Conference").Find(&collection).Error
	}
	return &collection, err
}
Example #2
0
func (this *scopeService) Scopes() (*[]Scope, error) {
	var (
		collection []Scope = make([]Scope, 0)
		err        error
	)
	if conn, ok := db.Connection(); ok {
		err = conn.Find(&collection).Error
	}
	return &collection, err
}
Example #3
0
func (this *userService) User(id string) (*User, error) {
	var (
		resource User
		err      error
	)
	if conn, ok := db.Connection(); ok {
		err = conn.Where("id = ?", id).First(&resource).Error
	}
	return &resource, err
}
Example #4
0
func DbDrop() {
	if d, ok := db.Connection(); ok {
		d.DropTable(&models.Conference{})
		d.DropTable(&models.Event{})
		d.DropTable(&models.Video{})
		d.DropTable(&models.Tag{})
		d.DropTable(&models.User{})
		d.DropTable(&models.Speaker{})
		d.DropTable(&models.TagAlias{})
	}
}
Example #5
0
func (this *SpeakerService) Speaker() (*Speaker, error) {
	var (
		resource Speaker
		err      error
	)
	if conn, ok := db.Connection(); ok {
		if v, ok := this.params["id"]; ok {
			err = conn.Where("id = ?", v).First(&resource).Error
		}
	}
	return &resource, err
}
Example #6
0
func (this *scopeService) Scope() (*Scope, error) {
	var (
		resource Scope
		err      error
	)
	if conn, ok := db.Connection(); ok {
		if v, ok := this.params["scope"]; ok {
			err = conn.Where("slug = ?", v).First(&resource).Error
		}
	}
	return &resource, err
}
Example #7
0
func (this *tagService) Tag() (*Tag, error) {
	var (
		resource Tag
		err      error
	)
	if conn, ok := db.Connection(); ok {
		if v, ok := this.params["tag"]; ok {
			err = conn.Where("slug = ?", v).First(&resource).Error
		}
	}
	return &resource, err
}
Example #8
0
func (this *eventService) Event() (*Event, error) {
	var (
		resource Event
		err      error
	)
	if conn, ok := db.Connection(); ok {
		if v, ok := this.params["event"]; ok {
			err = conn.Scopes(Active).Where("id = ?", v).First(&resource).Error
		}
	}
	return &resource, err
}
Example #9
0
func (this *conferenceService) Conference() (*Conference, error) {
	var (
		resource Conference
		err      error
	)
	if conn, ok := db.Connection(); ok {
		if v, ok := this.params["conference"]; ok {
			err = conn.Scopes(Active).Where("slug = ?", v).First(&resource).Error
		}
	}
	return &resource, err
}
Example #10
0
func (this *tagAliasService) TagAlias() (*TagAlias, error) {
	var (
		resource TagAlias
		err      error
	)
	if conn, ok := db.Connection(); ok {
		if v, ok := this.params["tag_alias_id"]; ok {
			err = conn.Where("id = ?", v).First(&resource).Error
		}
	}
	return &resource, err
}
Example #11
0
func DbMigrate() {
	if d, ok := db.Connection(); ok {
		d.AutoMigrate(
			&models.Conference{},
			&models.Event{},
			&models.Video{},
			&models.Tag{},
			&models.User{},
			&models.Speaker{},
			&models.TagAlias{},
		)
	}
}
Example #12
0
func Seed() {
	if d, ok := db.Connection(); ok {
		tags := []models.Tag{{
			Slug:  "go",
			Title: "Go",
		}, {
			Slug:  "ruby",
			Title: "Ruby",
		}}

		for _, tag := range tags {
			d.Create(&tag)
		}

		conferences := []models.Conference{{
			Slug:        "moscowjs",
			Title:       "MoscowJS",
			Url:         "http://moscowjs.ru",
			Type:        "meetup",
			Events:      []models.Event{{Address: "Russian Federation, Moscow, Red square street."}},
			Description: "Cum illum voluptas ducimus",
			Videos: []models.Video{{
				Title:   "Videooo",
				Url:     "http://www.youtube.com/watch?v=oHg5SJYRHA0",
				Length:  180,
				Service: "youtube",
				Tags:    tags,
			}, {
				Title:   "Videooo 2",
				Url:     "http://www.youtube.com/watch?v=oHg5SJYRHA0",
				Length:  120,
				Service: "youtube",
			}},
		},
			{
				Slug:   "railsclub",
				Title:  "Rails Club",
				Url:    "http://gay.org",
				Type:   "conference",
				Events: []models.Event{{Address: "Russian Federation, Siberia."}},
			}}

		for _, conference := range conferences {
			d.Create(&conference)
		}
	}

}
Example #13
0
func (this *tagAliasService) TagAliases() (*[]TagAlias, error) {
	var err error
	collection := make([]TagAlias, 0)
	if conn, ok := db.Connection(); ok {
		query := &conn
		if v, ok := this.params["query"]; ok {
			// FIXME: CHECK injection possibility
			query = query.Where("title ILIKE ?", fmt.Sprintf("%%%v%%", v))
		}
		if v, ok := this.params["tag"]; ok {
			var tag Tag
			conn.Find(&tag, "slug = ?", v)
			if tag.Slug != "" {
				err = query.Model(&tag).Related(&collection, "TagAliases").Error
			}
		} else {
			err = query.Find(&collection).Error
		}
	}
	return &collection, err
}
Example #14
0
func (this *tagService) Tags() (*[]Tag, error) {
	var err error
	collection := make([]Tag, 0)
	if conn, ok := db.Connection(); ok {
		query := &conn
		limit := 20 // Defaults to 20 items per page
		page := 1
		if v, ok := this.params["query"]; ok {
			// FIXME: CHECK injection possibility
			query = query.Where("slug ILIKE ?", fmt.Sprintf("%%%v%%", v))
			// TODO: find in tag_alias, merge results here
		}
		if v, ok := this.params["limit"]; ok {
			if v.(int) <= 50 {
				limit = v.(int)
			}
		}
		if v, ok := this.params["page"]; ok {
			page = v.(int)
		}
		err = query.Scopes(Paginate(limit, page)).Limit(limit).Find(&collection).Error
	}
	return &collection, err
}
Example #15
0
func (this *videoService) Video() (*Video, error) {
	var (
		resource Video
		err      error
	)
	if conn, ok := db.Connection(); ok {
		if v, ok := this.params["video"]; ok {
			err = conn.Where("id = ?", v).Preload("Conference").Preload("Event").First(&resource).Error
			if err == nil {
				var tagResources []Tag
				speakers := make([]Speaker, 0)
				tags := make([]string, 0)

				conn.Model(&resource).Related(&tagResources, "TagResources").Related(&speakers, "Speakers")
				for _, v := range tagResources {
					tags = append(tags, v.Slug)
				}
				resource.Tags = tags
				resource.Speakers = &speakers
			}
		}
	}
	return &resource, err
}
Example #16
0
func (this *videoService) Videos() (*[]Video, int, int, int, error) {
	var (
		err        error
		count      int
		offset     int
		collection []Video = make([]Video, 0)
		limit      int     = 20
	)
	if conn, ok := db.Connection(); ok {
		query := &conn
		if v, ok := this.params["conference"]; ok {
			query = query.Where("conference_slug = ?", v)
		}
		if v, ok := this.params["event"]; ok {
			query = query.Where("event_id = ?", v)
		}
		if v, ok := this.params["query"]; ok {
			// FIXME: CHECK sql injection possibility
			query = query.
				Where("title ILIKE ?", fmt.Sprintf("%%%v%%", v))
		}
		if _, ok := this.params["shuffle"]; ok {
			query = query.Where("random() < 0.01")
		}
		if v, ok := this.params["limit"]; ok {
			if v.(int) > 0 && v.(int) <= 50 {
				limit = v.(int)
			}
		}
		if v, ok := this.params["offset"]; ok {
			offset = v.(int)
		}
		if v, ok := this.params["tag"]; ok {
			query = query.Joins("INNER JOIN videos_tags ON videos_tags.video_id = videos.id").Where("videos_tags.tag_slug = ?", v)
		}

		err = query.
			Preload("Conference").
			Preload("Event").
			Scopes(GetRange(limit, offset)).
			Find(&collection).
			Scopes(Unpaginate).
			Count(&count).
			Error

		if err == nil {
			tags := make(map[uint][]string)
			videoIds := make([]uint, 0)
			for _, video := range collection {
				videoIds = append(videoIds, video.ID)
			}
			if len(videoIds) > 0 {
				if rows, err1 := conn.Table("videos_tags").Select("video_id, tag_slug").Where("video_id IN (?)", videoIds).Rows(); err1 == nil {
					var (
						videoId uint
						tag     string
					)
					for rows.Next() {
						rows.Scan(&videoId, &tag)
						tags[videoId] = append(tags[videoId], tag)
					}
					for i, video := range collection {
						video.Tags = tags[video.ID]
						collection[i] = video
					}
				}
			}
		}
	}
	return &collection, count, limit, offset, err
}