Skip to content

Commit 178e8a8

Browse files
committed
Major change in delete and migrated to go mod
1 parent e87bbca commit 178e8a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+122
-17216
lines changed

Godeps/Godeps.json

Lines changed: 0 additions & 56 deletions
This file was deleted.

Godeps/Readme

Lines changed: 0 additions & 5 deletions
This file was deleted.

api/handler/gists.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,26 +128,19 @@ func deleteGist(s gists.Service) http.Handler {
128128

129129
deleteReq := &gists.DeleteGist{}
130130
_ = json.NewDecoder(r.Body).Decode(deleteReq)
131-
isDeleted, err := s.DeleteGist(claims["id"].(float64), deleteReq.GistID)
131+
files, err := s.DeleteGist(claims["id"].(float64), deleteReq)
132132
if err != nil {
133133
view.Wrap(err, w)
134134
return
135135
}
136136

137-
if isDeleted == true {
138-
w.Header().Add("Content-Type", "application/json; charset=utf-8")
139-
_ = json.NewEncoder(w).Encode(map[string]interface{}{
140-
"status": http.StatusOK,
141-
"message": "Gist Deleted",
142-
})
143-
} else {
144-
w.Header().Add("Content-Type", "application/json; charset=utf-8")
145-
w.WriteHeader(http.StatusBadRequest)
146-
_ = json.NewEncoder(w).Encode(map[string]interface{}{
147-
"status": http.StatusBadRequest,
148-
"message": "Error Deleting Gist",
149-
})
150-
}
137+
w.Header().Add("Content-Type", "application/json; charset=utf-8")
138+
139+
_ = json.NewEncoder(w).Encode(map[string]interface{}{
140+
"message": "File Updated",
141+
"status": http.StatusOK,
142+
"files": files,
143+
})
151144
})
152145
}
153146

go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/rithikjain/GistsBackend
2+
3+
go 1.13
4+
5+
require (
6+
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b
7+
github.com/dgrijalva/jwt-go v3.2.0+incompatible
8+
github.com/gorilla/mux v1.7.4 // indirect
9+
github.com/jinzhu/gorm v1.9.12
10+
github.com/joho/godotenv v1.3.0
11+
github.com/smartystreets/goconvey v1.6.4 // indirect
12+
github.com/urfave/negroni v1.0.0 // indirect
13+
)

go.sum

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b h1:CvoEHGmxWl5kONC5icxwqV899dkf4VjOScbxLpllEnw=
2+
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b/go.mod h1:LWMyo4iOLWXHGdBki7NIht1kHru/0wM179h+d3g8ATM=
3+
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM=
4+
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
5+
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
6+
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
7+
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
8+
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
9+
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
10+
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
11+
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
12+
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
13+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
14+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
15+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
16+
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
17+
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
18+
github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q=
19+
github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs=
20+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
21+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
22+
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
23+
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
24+
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
25+
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
26+
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
27+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
28+
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
29+
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
30+
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
31+
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
32+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
33+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
34+
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
35+
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
36+
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
37+
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
38+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
39+
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
40+
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM=
41+
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
42+
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
43+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
44+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
45+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
46+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
47+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
48+
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
49+
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
50+
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=

pkg/gists/entities.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ type UpdateFileRequest struct {
4545
}
4646

4747
type DeleteGist struct {
48-
GistID string `json:"gist_id"`
48+
GistID string `json:"gist_id"`
49+
Filename string `json:"filename"`
4950
}

pkg/gists/service.go

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Service interface {
1818

1919
UpdateGist(userID float64, gistFile *GistFile) (*[]File, error)
2020

21-
DeleteGist(userID float64, gistID string) (bool, error)
21+
DeleteGist(userID float64, deleteGist *DeleteGist) (*[]File, error)
2222
}
2323

2424
type service struct {
@@ -214,35 +214,67 @@ func (s *service) UpdateGist(userID float64, gistFile *GistFile) (*[]File, error
214214
return &files, nil
215215
}
216216

217-
func (s *service) DeleteGist(userID float64, gistID string) (bool, error) {
217+
func (s *service) DeleteGist(userID float64, deleteGist *DeleteGist) (*[]File, error) {
218+
// Transforming the file into correct format to send to the gist api
219+
file := make(map[string]interface{})
220+
file[deleteGist.Filename] = nil
221+
222+
request := make(map[string]interface{})
223+
request["files"] = file
224+
225+
requestJson, _ := json.Marshal(request)
226+
218227
user := &user.User{}
219228
err := s.db.Where("id=?", userID).First(user).Error
220229
if err != nil {
221-
return false, pkg.ErrDatabase
230+
return nil, pkg.ErrDatabase
222231
}
223232

224233
// Deleting file from github
225234
token := user.OAuthToken
226-
req, err := http.NewRequest("DELETE", "https://api.github.com/gists/"+gistID, nil)
227-
if err != nil {
228-
return false, err
235+
req, er := http.NewRequest("PATCH", "https://api.github.com/gists/"+deleteGist.GistID, bytes.NewBuffer(requestJson))
236+
if er != nil {
237+
return nil, er
229238
}
230239
req.Header.Set("Authorization", "token "+token)
231-
resp, err := client.Do(req)
232-
if err != nil {
233-
return false, err
240+
req.Header.Set("Content-Type", "application/json")
241+
242+
resp, er := client.Do(req)
243+
if er != nil {
244+
return nil, er
234245
}
235246

236-
defer resp.Body.Close()
247+
if resp.Header.Get("Status") == "404 Not Found" {
248+
return nil, pkg.ErrNotFound
249+
}
237250

238-
body, err := ioutil.ReadAll(resp.Body)
239-
if err != nil {
240-
return false, err
251+
var gist Gist
252+
er = json.NewDecoder(resp.Body).Decode(&gist)
253+
if er != nil {
254+
return nil, er
241255
}
256+
var files []File
257+
for _, file := range gist.Files {
258+
file.GistID = gist.ID
259+
file.GistUrl = gist.Url
260+
file.IsPublic = gist.IsPublic
261+
file.UpdatedAt = gist.UpdatedAt
262+
file.Description = gist.Description
263+
264+
// Get content of the file
265+
res, err := http.Get(file.RawUrl)
266+
if err != nil {
267+
return nil, err
268+
}
269+
resBody, err := ioutil.ReadAll(res.Body)
270+
if err != nil {
271+
return nil, err
272+
}
273+
content := string(resBody)
274+
file.Content = content
242275

243-
if string(body) == "" {
244-
return true, nil
245-
} else {
246-
return false, nil
276+
files = append(files, file)
247277
}
278+
defer resp.Body.Close()
279+
return &files, nil
248280
}

vendor/github.com/auth0/go-jwt-middleware/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)