Exemplo n.º 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)
}
Exemplo n.º 2
0
func Edit(c mp.Context, w http.ResponseWriter, r *http.Request) error {
	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["songId"], 10, 64)
	if err != nil {
		return err
	}

	vm, err := getSongViewModel(c, id)
	if err != nil {
		return err
	}

	return core.Render(w, "songs.edit.tmpl.html", vm)
}
Exemplo n.º 3
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)
}
Exemplo n.º 4
0
func Show(c mp.Context, w http.ResponseWriter, r *http.Request) error {
	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["songId"], 10, 64)
	if err != nil {
		return err
	}

	vm, err := getSongViewModel(c, id)
	if err != nil {
		return err
	}

	qVals := r.URL.Query()
	if qVals.Get("fmt") == "json" {
		return core.Json(w, vm.Song)
	}

	return core.Render(w, "songs.show.tmpl.html", vm)
}
Exemplo n.º 5
0
func New(c mp.Context, w http.ResponseWriter, r *http.Request) error {
	return core.Render(w, "songs.new.tmpl.html", core.NewIncludes(c))
}