Skip to content

Commit 8b53da6

Browse files
committed
Fixed up file separation
1 parent 52de55f commit 8b53da6

File tree

5 files changed

+136
-117
lines changed

5 files changed

+136
-117
lines changed

server/category.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package main
33
import (
44
"context"
55
"log"
6-
"net/http"
7-
"strconv"
86

9-
"github.com/labstack/echo/v4"
107
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/mongo"
119
)
1210

13-
func GetCat(c echo.Context) error {
14-
id, _ := strconv.Atoi(c.QueryParam("id"))
11+
func GetCat(collection *mongo.Collection, id int) *Category {
12+
1513
var result *Category
1614
filter := bson.D{{"categoryID", id}}
1715

@@ -21,36 +19,27 @@ func GetCat(c echo.Context) error {
2119
log.Fatal(err)
2220
}
2321

24-
return c.JSON(http.StatusOK, H{
25-
"category": result,
26-
})
22+
return result
2723
}
2824

29-
func NewCat(c echo.Context) error {
30-
catID, _ := strconv.Atoi(c.FormValue("id"))
31-
index, _ := strconv.Atoi(c.FormValue("index"))
32-
25+
func NewCat(collection *mongo.Collection, catID int, index int, name string) {
3326
category := Category{
3427
categoryID: catID,
35-
categoryName: c.FormValue("name"),
28+
categoryName: name,
3629
index: index,
3730
}
3831

3932
_, err := collection.InsertOne(context.TODO(), category)
4033
if err != nil {
4134
log.Fatal(err)
4235
}
43-
return c.JSON(http.StatusOK, H{})
4436
}
4537

46-
func PatchCat(c echo.Context) error {
47-
categoryID, _ := strconv.Atoi(c.FormValue("id"))
48-
categoryName := c.FormValue("name")
49-
index, _ := strconv.Atoi(c.FormValue("index"))
50-
filter := bson.D{{"categoryID", categoryID}}
38+
func PatchCat(collection *mongo.Collection, catID int, name string, index int) {
39+
filter := bson.D{{"categoryID", catID}}
5140
update := bson.D{
5241
{"$set", bson.D{
53-
{"categoryName", categoryName},
42+
{"categoryName", name},
5443
{"index", index},
5544
}},
5645
}
@@ -60,19 +49,14 @@ func PatchCat(c echo.Context) error {
6049
if err != nil {
6150
log.Fatal(err)
6251
}
63-
64-
return c.JSON(http.StatusOK, H{})
6552
}
6653

67-
func DeleteCat(c echo.Context) error {
68-
id, _ := strconv.Atoi(c.FormValue("id"))
54+
func DeleteCat(collection *mongo.Collection, id int) {
6955
filter := bson.D{{"categoryID", id}}
7056

7157
// Find a category by id and delete it
7258
_, err := collection.DeleteOne(context.TODO(), filter)
7359
if err != nil {
7460
log.Fatal(err)
7561
}
76-
77-
return c.JSON(http.StatusOK, H{})
7862
}

server/login.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,25 @@ import (
44
"context"
55
"crypto/sha256"
66
"log"
7-
"net/http"
87
"time"
98

109
"github.com/dgrijalva/jwt-go"
11-
"github.com/labstack/echo/v4"
1210
"go.mongodb.org/mongo-driver/bson"
11+
"go.mongodb.org/mongo-driver/mongo"
1312
"gopkg.in/ldap.v2"
1413
)
1514

16-
func auth(c echo.Context) error {
15+
func auth(collection *mongo.Collection, zid string, password string) string {
1716
// Connect to UNSW LDAP server
1817
l, err := ldap.Dial("tcp", "ad.unsw.edu.au")
1918
if err != nil {
2019
log.Fatal(err)
2120
}
2221

2322
// Attempt to sign in using credentials
24-
zid := c.FormValue("zid")
2523
hashedZID := sha256.Sum256([]byte(zid))
2624
stringZID := string(hashedZID[:])
2725
username := zid + "ad.unsw.edu.au"
28-
password := c.FormValue("password")
2926

3027
err = l.Bind(username, password)
3128
if err != nil {
@@ -101,7 +98,5 @@ func auth(c echo.Context) error {
10198
}
10299
}
103100

104-
return c.JSON(http.StatusOK, H{
105-
"token": tokenString,
106-
})
101+
return tokenString
107102
}

server/post.go

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,27 @@ package main
33
import (
44
"context"
55
"log"
6-
"net/http"
7-
"strconv"
86
"time"
97

10-
"github.com/labstack/echo/v4"
118
"go.mongodb.org/mongo-driver/bson"
129
"go.mongodb.org/mongo-driver/mongo"
1310
"go.mongodb.org/mongo-driver/mongo/options"
1411
)
1512

16-
func GetPost(c echo.Context) error {
13+
func GetPost(collection *mongo.Collection, id int, category string) *Post {
1714
var result *Post
18-
id, _ := strconv.Atoi(c.QueryParam("id"))
19-
category := c.QueryParam("category")
2015

2116
// Search for post by id and category
2217
filter := bson.D{{"postID", id}, {"category", category}}
2318
err := collection.FindOne(context.TODO(), filter).Decode(&result)
2419
if err != nil {
2520
log.Fatal(err)
2621
}
27-
return c.JSON(http.StatusOK, H{
28-
"post": result,
29-
})
30-
}
3122

32-
func GetAllPosts(c echo.Context) error {
33-
count, _ := strconv.Atoi(c.QueryParam("id"))
34-
cat := c.QueryParam("category")
23+
return result
24+
}
3525

26+
func GetAllPosts(collection *mongo.Collection, count int, cat string) []*Post {
3627
findOptions := options.Find()
3728
if count != 10 {
3829
findOptions.SetLimit(int64(count))
@@ -66,61 +57,43 @@ func GetAllPosts(c echo.Context) error {
6657
posts = append(posts, &elem)
6758
}
6859

69-
return c.JSON(http.StatusOK, H{
70-
"posts": posts,
71-
})
60+
return posts
7261
}
7362

74-
func NewPost(c echo.Context) error {
75-
id, _ := strconv.Atoi(c.FormValue("id"))
76-
category, _ := strconv.Atoi(c.FormValue("category"))
77-
showinMenu, _ := strconv.ParseBool(c.FormValue("showInMenu"))
78-
63+
func NewPost(collection *mongo.Collection, id int, category int, showInMenu bool, title string, subtitle string, postType string, content string, github string, fb string) {
7964
post := Post{
8065
postID: id,
81-
postTitle: c.FormValue("title"),
82-
postSubtitle: c.FormValue("subtitle"),
83-
postType: c.FormValue("type"),
66+
postTitle: title,
67+
postSubtitle: subtitle,
68+
postType: postType,
8469
postCategory: category,
8570
createdOn: time.Now(),
8671
lastEditedOn: time.Now(),
87-
postContent: c.FormValue("content"),
88-
postLinkGithub: c.FormValue("linkGithub"),
89-
postLinkFacebook: c.FormValue("linkFacebook"),
90-
showInMenu: showinMenu,
72+
postContent: content,
73+
postLinkGithub: github,
74+
postLinkFacebook: fb,
75+
showInMenu: showInMenu,
9176
}
9277

9378
_, err := collection.InsertOne(context.TODO(), post)
9479
if err != nil {
9580
log.Fatal(err)
9681
}
97-
98-
return c.JSON(http.StatusOK, H{})
9982
}
10083

101-
func UpdatePost(c echo.Context) error {
102-
postID, _ := strconv.Atoi(c.FormValue("id"))
103-
postTitle := c.FormValue("title")
104-
postSubtitle := c.FormValue("subtitle")
105-
postType := c.FormValue("type")
106-
postCategory := c.FormValue("category")
107-
postContent := c.FormValue("content")
108-
postLinkGithub := c.FormValue("linkGithub")
109-
postLinkFacebook := c.FormValue("linkFacebook")
110-
showinMenu, _ := strconv.ParseBool(c.FormValue("showInMenu"))
111-
112-
filter := bson.D{{"postID", postID}}
84+
func UpdatePost(collection *mongo.Collection, id int, category int, showInMenu bool, title string, subtitle string, postType string, content string, github string, fb string) {
85+
filter := bson.D{{"postID", id}}
11386
update := bson.D{
11487
{"$set", bson.D{
115-
{"postTitle", postTitle},
116-
{"postSubtitle", postSubtitle},
88+
{"postTitle", title},
89+
{"postSubtitle", subtitle},
11790
{"postType", postType},
118-
{"postCategory", postCategory},
91+
{"postCategory", category},
11992
{"lastEditedOn", time.Now()},
120-
{"postContent", postContent},
121-
{"postLinkGithub", postLinkGithub},
122-
{"postLinkFacebook", postLinkFacebook},
123-
{"showinMenu", showinMenu},
93+
{"postContent", content},
94+
{"postLinkGithub", github},
95+
{"postLinkFacebook", fb},
96+
{"showinMenu", showInMenu},
12497
}},
12598
}
12699

@@ -129,19 +102,14 @@ func UpdatePost(c echo.Context) error {
129102
if err != nil {
130103
log.Fatal(err)
131104
}
132-
133-
return c.JSON(http.StatusOK, H{})
134105
}
135106

136-
func DeletePost(c echo.Context) error {
137-
id, _ := strconv.Atoi(c.FormValue("id"))
107+
func DeletePost(collection *mongo.Collection, id int) {
138108
filter := bson.D{{"postID", id}}
139109

140110
// Find a post by id and delete it
141111
_, err := collection.DeleteOne(context.TODO(), filter)
142112
if err != nil {
143113
log.Fatal(err)
144114
}
145-
146-
return c.JSON(http.StatusOK, H{})
147115
}

0 commit comments

Comments
 (0)