Skip to content

Commit 21100d4

Browse files
committed
Logic for formatting the files output correctly
1 parent 1b30fdc commit 21100d4

File tree

4 files changed

+114
-8
lines changed

4 files changed

+114
-8
lines changed

api/handler/gists.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package handler
2+
3+
import (
4+
"encoding/json"
5+
"github.com/rithikjain/GistsBackend/api/view"
6+
"github.com/rithikjain/GistsBackend/pkg/gists"
7+
"net/http"
8+
)
9+
10+
func test(s gists.Service) http.Handler {
11+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12+
g, err := s.ViewAllFiles(1)
13+
if err != nil {
14+
view.Wrap(err, w)
15+
}
16+
17+
var files []gists.File
18+
var allGists []gists.Gist
19+
mGists := *g
20+
21+
// Logic for formatting the files in right format
22+
for i := 0; i < len(mGists); i++ {
23+
allGists = append(allGists, mGists[i])
24+
for _, file := range mGists[i].Files {
25+
file.GistID = allGists[i].ID
26+
file.GistUrl = allGists[i].Url
27+
file.IsPublic = allGists[i].IsPublic
28+
file.UpdatedAt = allGists[i].UpdatedAt
29+
files = append(files, file)
30+
}
31+
}
32+
33+
w.Header().Add("Content-Type", "application/json; charset=utf-8")
34+
_ = json.NewEncoder(w).Encode(map[string]interface{}{
35+
"files": files,
36+
})
37+
})
38+
}
39+
40+
func MakGistsHandler(r *http.ServeMux, svc gists.Service) {
41+
r.Handle("/api/test", test(svc))
42+
}

api/view/errors.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package view
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"github.com/rithikjain/GistsBackend/pkg"
7+
"net/http"
8+
)
9+
10+
type ErrView struct {
11+
Message string `json:"message"`
12+
Status int `json:"status"`
13+
}
14+
15+
//noinspection ALL
16+
var (
17+
ErrMethodNotAllowed = errors.New("Error: Method is not allowed")
18+
ErrInvalidToken = errors.New("Error: Invalid Authorization token")
19+
ErrUserExists = errors.New("User already exists")
20+
)
21+
22+
var ErrHTTPStatusMap = map[string]int{
23+
pkg.ErrNotFound.Error(): http.StatusNotFound,
24+
pkg.ErrInvalidSlug.Error(): http.StatusBadRequest,
25+
pkg.ErrExists.Error(): http.StatusConflict,
26+
pkg.ErrNoContent.Error(): http.StatusNotFound,
27+
pkg.ErrDatabase.Error(): http.StatusInternalServerError,
28+
pkg.ErrUnauthorized.Error(): http.StatusUnauthorized,
29+
pkg.ErrForbidden.Error(): http.StatusForbidden,
30+
pkg.ErrEmail.Error(): http.StatusBadRequest,
31+
pkg.ErrPassword.Error(): http.StatusBadRequest,
32+
ErrMethodNotAllowed.Error(): http.StatusMethodNotAllowed,
33+
ErrInvalidToken.Error(): http.StatusBadRequest,
34+
ErrUserExists.Error(): http.StatusConflict,
35+
}
36+
37+
func Wrap(err error, w http.ResponseWriter) {
38+
msg := err.Error()
39+
code := ErrHTTPStatusMap[msg]
40+
41+
if code == 0 {
42+
code = http.StatusInternalServerError
43+
}
44+
45+
w.WriteHeader(code)
46+
w.Header().Add("Content-Type", "application/json; charset=utf-8")
47+
48+
errView := ErrView{
49+
Message: msg,
50+
Status: code,
51+
}
52+
_ = json.NewEncoder(w).Encode(errView)
53+
}

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"github.com/jinzhu/gorm"
66
_ "github.com/jinzhu/gorm/dialects/postgres"
77
"github.com/joho/godotenv"
8+
"github.com/rithikjain/GistsBackend/api/handler"
9+
"github.com/rithikjain/GistsBackend/pkg/gists"
810
"log"
911
"net/http"
1012
"os"
@@ -54,8 +56,13 @@ func main() {
5456
fmt.Println("Connected to DB...")
5557
db.LogMode(true)
5658

59+
gistsSvc := gists.NewService(db)
60+
5761
// Setting up the router
5862
r := http.NewServeMux()
63+
64+
handler.MakGistsHandler(r, gistsSvc)
65+
5966
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
6067
w.WriteHeader(http.StatusOK)
6168
_, _ = w.Write([]byte("Hello There"))

pkg/gists/entities.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package gists
22

33
type Gist struct {
4-
Url string `json:"url"`
5-
ID string `json:"id"`
6-
IsPublic bool `json:"public"`
7-
UpdatedAt string `json:"updated_at"`
8-
Files []map[string]File `json:"files"`
4+
Url string `json:"url"`
5+
ID string `json:"id"`
6+
IsPublic bool `json:"public"`
7+
UpdatedAt string `json:"updated_at"`
8+
Files map[string]File `json:"files"`
99
}
1010

1111
type File struct {
12-
Filename string `json:"filename"`
13-
Language string `json:"language"`
14-
RawUrl string `json:"raw_url"`
12+
GistID string `json:"gist_id"`
13+
GistUrl string `json:"gist_url"`
14+
IsPublic bool `json:"public"`
15+
UpdatedAt string `json:"updated_at"`
16+
Filename string `json:"filename"`
17+
Language string `json:"language"`
18+
RawUrl string `json:"raw_url"`
1519
}

0 commit comments

Comments
 (0)