Пример #1
0
func Index(c mp.Context, w http.ResponseWriter, r *http.Request) error {
	songs, err := data.GetSongs(c)
	if err != nil {
		return err
	}

	vm := &SongListViewModel{
		Includes: core.NewIncludes(c),
		Songs:    songs,
	}
	return core.Render(w, "songs.index.tmpl.html", vm)
}
Пример #2
0
func Search(c mp.Context, w http.ResponseWriter, r *http.Request) error {
	query := r.URL.Query()
	q := query.Get("q")
	if q == "" {
		return core.Render(w, "songs.search.tmpl.html", &SongSearchResult{Includes: core.NewIncludes(c)})
	}

	searchQuery := m.Wordify(q)
	docList, docIDs, err := data.SearchSongs(c, searchQuery)
	if err != nil {
		return err
	}

	if len(docList) == 0 {
		c.Infof("AND query [%s] return nothing", searchQuery)
		orQuery := strings.Join(strings.Fields(searchQuery), " OR ")
		c.Infof("Searching with OR query [%s]", orQuery)
		docList, docIDs, err = data.SearchSongs(c, orQuery)
		if err != nil {
			return err
		}
	}

	items := make([]*SongSearchResultItem, 0)
	for i, _ := range docList {
		items = append(items, createSongSearchResultItem(q, docIDs[i], docList[i]))
	}

	result := &SongSearchResult{
		Includes: core.NewIncludes(c),
		Query:    q,
		Items:    items,
	}

	return core.Render(w, "songs.search.tmpl.html", result)
}
Пример #3
0
func getSongViewModel(c mp.Context, id int64) (*SongViewModel, error) {
	n := goon.FromContext(c)
	s := &data.Song{Id: id}
	if err := n.Get(s); err != nil {
		return nil, err
	}

	songs, _, err := chordpro.Parse(strings.NewReader(s.ChordPro))
	if err != nil {
		return nil, err
	}

	vm := &SongViewModel{
		Includes: core.NewIncludes(c),
		Id:       s.Id,
		Song:     songs[0],
		ChordPro: s.ChordPro,
	}

	return vm, nil
}
Пример #4
0
func New(c mp.Context, w http.ResponseWriter, r *http.Request) error {
	return core.Render(w, "songs.new.tmpl.html", core.NewIncludes(c))
}