forked from sheetalgithub30/9podcasts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcasts.go
175 lines (142 loc) · 4.4 KB
/
podcasts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/labstack/echo"
)
type podcast struct {
ID int64 `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
WebsiteAddress string `json:"website_address"`
CategoryID int64 `json:"category_id,omitempty"`
Category string `json:"category,omitempty"`
Language string `json:"language"`
IsExplicit bool `json:"is_explicit"`
CoverArtID int64 `json:"cover_art_id,omitempty"`
CoverArt Media `json:"cover_art,omitempty"`
AuthorName string `json:"author_name"`
AuthorEmail string `json:"author_email"`
Copyright string `json:"copyright"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Episodes []episode `json:"episodes,omitempty"`
}
func createPodcast(c echo.Context) (err error) {
pd := &podcast{}
if err = c.Bind(pd); err != nil {
return
}
pd.CreatedAt = time.Now()
pd.UpdatedAt = time.Now()
q := `
INSERT INTO podcasts(
title, description, website_address, category_id, language,
is_explicit, cover_art_id, author_name, author_email, copyright,
created_at, updated_at
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) RETURNING id
`
err = db.QueryRow(q, pd.Title, pd.Description, pd.WebsiteAddress, pd.CategoryID,
pd.Language, pd.IsExplicit, pd.CoverArtID, pd.AuthorName, pd.AuthorEmail, pd.Copyright,
pd.CreatedAt, pd.UpdatedAt).Scan(&pd.ID)
if err != nil {
fmt.Println(err)
return
}
err = pd.GenerateRSS()
if err != nil {
fmt.Println(err)
}
return c.JSON(http.StatusOK, pd)
}
func getPodcast(c echo.Context) (err error) {
var pds []podcast
q := `
SELECT p.id, p.title, description, website_address, category_id, c.title, language, is_explicit,
cover_art_id, author_name, author_email,copyright, created_at, updated_at FROM podcasts p
JOIN categories c on c.id = p.category_id
`
rows, err := db.Query(q)
if err != nil {
fmt.Println(err)
return
}
for rows.Next() {
var pd podcast
err = rows.Scan(&pd.ID, &pd.Title, &pd.Description, &pd.WebsiteAddress, &pd.CategoryID, &pd.Category,
&pd.Language, &pd.IsExplicit, &pd.CoverArtID, &pd.AuthorName, &pd.AuthorEmail, &pd.Copyright,
&pd.CreatedAt, &pd.UpdatedAt)
if err != nil {
fmt.Println(err)
}
pd.CoverArt, _ = getMediaByID(pd.CoverArtID)
pds = append(pds, pd)
}
return c.JSON(http.StatusOK, pds)
}
func getPodcastByID(c echo.Context) (err error) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
fmt.Println(err)
return
}
pd, err := _getPodcastByID(id)
if err != nil {
fmt.Println(err)
return
}
return c.JSON(http.StatusOK, pd)
}
func _getPodcastByID(podcastID int64) (pd podcast, err error) {
q := `
SELECT p.id, p.title, description, website_address, category_id, c.title, language, is_explicit,
cover_art_id, author_name, author_email,copyright, created_at,updated_at FROM podcasts p
JOIN categories c on c.id = p.category_id
WHERE p.id = $1
`
err = db.QueryRow(q, podcastID).Scan(&pd.ID, &pd.Title, &pd.Description, &pd.WebsiteAddress,
&pd.CategoryID, &pd.Category, &pd.Language, &pd.IsExplicit, &pd.CoverArtID, &pd.AuthorName, &pd.AuthorEmail,
&pd.Copyright, &pd.CreatedAt, &pd.UpdatedAt)
if err != nil {
return
}
pd.Episodes, err = _getPodcastEpisodes(pd.ID)
if err != nil {
return
}
pd.CoverArt, _ = getMediaByID(pd.CoverArtID)
return
}
func deletePodcast(c echo.Context) (err error) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
fmt.Println(err)
return
}
us, err := db.Exec(`Delete from podcasts where id = $1 `, id)
if err != nil {
fmt.Println(err)
return
}
return c.JSON(http.StatusOK, us)
}
func updatePodcast(c echo.Context) (err error) {
pd := &podcast{}
pd.UpdatedAt = time.Now()
if err = c.Bind(pd); err != nil {
return
}
q := `UPDATE podcasts SET title = $1,
description= $2, website_address =$3, category_id = $4,language =$5,
is_explicit =$6,cover_art_id = $7 , author_name =$8,
author_email =$9 ,copyright =$10 ,updated_at=$11 where id =$12`
_, err = db.Exec(q, pd.Title, pd.Description, pd.WebsiteAddress, pd.CategoryID, pd.Language, pd.IsExplicit,
pd.CoverArtID, pd.AuthorName, pd.AuthorEmail, pd.Copyright, pd.UpdatedAt, pd.ID)
if err != nil {
fmt.Println(err)
return
}
return c.String(http.StatusOK, "podcast updated successfully")
}