feat(subsonic): add internet radio support
* Initial commit of internet radio support. * Added first test for internet radio. * Refactor to prepare for more test cases. * Added a few more tests. Realized that I was not calling as admin so added ability to mock admin. * Added more internet radio tests. Added proper JSON unmarshaling for ID. * More test cases. Fixed some accidental tabs in files. * Fixed some more tabs. * lint fixes * Changed placeholder for homepage URL to fit into box. * Finished out internet radio test cases. Found a few bad error codes in internet radio AND podcasts (mea culpa). * Realized that delete via website was not checking properly if id existed. Fixed. gofmt
This commit is contained in:
@@ -123,6 +123,7 @@ type templateData struct {
|
||||
SelectedUser *db.User
|
||||
|
||||
Podcasts []*db.Podcast
|
||||
InternetRadioStations []*db.InternetRadioStation
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -73,6 +74,9 @@ func (c *Controller) ServeHome(r *http.Request) *Response {
|
||||
// podcasts box
|
||||
c.DB.Find(&data.Podcasts)
|
||||
|
||||
// internet radio box
|
||||
c.DB.Find(&data.InternetRadioStations)
|
||||
|
||||
return &Response{
|
||||
template: "home.tmpl",
|
||||
data: data,
|
||||
@@ -400,7 +404,7 @@ func (c *Controller) ServePodcastAddDo(r *http.Request) *Response {
|
||||
flashW: []string{fmt.Sprintf("could not create feed: %v", err)},
|
||||
}
|
||||
}
|
||||
if _, err = c.Podcasts.AddNewPodcast(rssURL, feed); err != nil {
|
||||
if _, err := c.Podcasts.AddNewPodcast(rssURL, feed); err != nil {
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
flashW: []string{fmt.Sprintf("could not create feed: %v", err)},
|
||||
@@ -464,3 +468,105 @@ func (c *Controller) ServePodcastDeleteDo(r *http.Request) *Response {
|
||||
redirect: "/admin/home",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) ServeInternetRadioStationAddDo(r *http.Request) *Response {
|
||||
streamURL := r.FormValue("streamURL")
|
||||
name := r.FormValue("name")
|
||||
homepageURL := r.FormValue("homepageURL")
|
||||
|
||||
if name == "" {
|
||||
return &Response{redirect: "/admin/home", flashW: []string{"no name provided"}}
|
||||
}
|
||||
|
||||
if _, err := url.ParseRequestURI(streamURL); err != nil {
|
||||
return &Response{redirect: "/admin/home", flashW: []string{fmt.Sprintf("bad stream URL provided: %v", err)}}
|
||||
}
|
||||
|
||||
if homepageURL != "" {
|
||||
if _, err := url.ParseRequestURI(homepageURL); err != nil {
|
||||
return &Response{redirect: "/admin/home", flashW: []string{fmt.Sprintf("bad homepage URL provided: %v", err)}}
|
||||
}
|
||||
}
|
||||
|
||||
var station db.InternetRadioStation
|
||||
station.StreamURL = streamURL
|
||||
station.Name = name
|
||||
station.HomepageURL = homepageURL
|
||||
if err := c.DB.Save(&station).Error; err != nil {
|
||||
return &Response{code: 500, err: fmt.Sprintf("error saving station: %v", err)}
|
||||
}
|
||||
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) ServeInternetRadioStationUpdateDo(r *http.Request) *Response {
|
||||
stationID, err := strconv.Atoi(r.URL.Query().Get("id"))
|
||||
if err != nil {
|
||||
return &Response{code: 400, err: "please provide a valid internet radio station id"}
|
||||
}
|
||||
|
||||
streamURL := r.FormValue("streamURL")
|
||||
name := r.FormValue("name")
|
||||
homepageURL := r.FormValue("homepageURL")
|
||||
|
||||
if name == "" {
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
flashW: []string{"no name provided"},
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := url.ParseRequestURI(streamURL); err != nil {
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
flashW: []string{fmt.Sprintf("bad stream URL provided: %v", err)},
|
||||
}
|
||||
}
|
||||
|
||||
if homepageURL != "" {
|
||||
if _, err := url.ParseRequestURI(homepageURL); err != nil {
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
flashW: []string{fmt.Sprintf("bad homepage URL provided: %v", err)},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var station db.InternetRadioStation
|
||||
if err := c.DB.Where("id=?", stationID).First(&station).Error; err != nil {
|
||||
return &Response{code: 404, err: fmt.Sprintf("find station by id: %v", err)}
|
||||
}
|
||||
|
||||
station.StreamURL = streamURL
|
||||
station.Name = name
|
||||
station.HomepageURL = homepageURL
|
||||
if err := c.DB.Save(&station).Error; err != nil {
|
||||
return &Response{code: 500, err: "please provide a valid internet radio station id"}
|
||||
}
|
||||
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) ServeInternetRadioStationDeleteDo(r *http.Request) *Response {
|
||||
stationID, err := strconv.Atoi(r.URL.Query().Get("id"))
|
||||
if err != nil {
|
||||
return &Response{code: 400, err: "please provide a valid internet radio station id"}
|
||||
}
|
||||
|
||||
var station db.InternetRadioStation
|
||||
if err := c.DB.Where("id=?", stationID).First(&station).Error; err != nil {
|
||||
return &Response{code: 404, err: fmt.Sprintf("find station by id: %v", err)}
|
||||
}
|
||||
|
||||
if err := c.DB.Where("id=?", stationID).Delete(&db.InternetRadioStation{}).Error; err != nil {
|
||||
return &Response{code: 500, err: fmt.Sprintf("deleting radio station: %v", err)}
|
||||
}
|
||||
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user