Skip to content

Commit

Permalink
Merge pull request #10 from uclaacm/1.14
Browse files Browse the repository at this point in the history
Updates codebase to Go 1.14, preps for Heroku deploy
  • Loading branch information
krashanoff authored Apr 7, 2020
2 parents eb4e500 + 3a77d42 commit 7388432
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 134 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ jobs:
run: go build
- name: Passes tests
run: go test ./...
- name: Passes go vet, fmt
run: |
go vet
gofmt -l .
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: bin/teach-la-go-backend
18 changes: 8 additions & 10 deletions db/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ package db
// It provides functions for converting the struct
// to firebase-digestible types.
type Class struct {
Thumbnail int64 `firestore:"thumbnail" json:"thumbnail"`
Name string `firestore:"name" json:"name"`
Creator string `firestore:"creator" json:"creator"`
Instructors []string `firestore:"instructors" json:"instructors"`
Members []string `firestore:"members" json:"members"`
Programs []string `json:"programs"`
CID string `json:"cid"`
WID string `json:"wid"`
Thumbnail int64 `firestore:"thumbnail" json:"thumbnail"`
Name string `firestore:"name" json:"name"`
Creator string `firestore:"creator" json:"creator"`
Instructors []string `firestore:"instructors" json:"instructors"`
Members []string `firestore:"members" json:"members"`
Programs []string `json:"programs"`
CID string `json:"cid"`
WID string `json:"wid"`
}


67 changes: 31 additions & 36 deletions db/classManagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import (
"encoding/json"
"net/http"

"../tools/requests"
"github.com/uclaacm/teach-la-go-backend/tools/requests"
)

// HandleCreateClass is the handler for creating a new class.
// It takes the UID of the creator, the name of the class, and a thunmbnail id.
// It takes the UID of the creator, the name of the class, and a thunmbnail id.
func (d *DB) HandleCreateClass(w http.ResponseWriter, r *http.Request) {

var (
err error
)

//create an anonymous structure to handle requests
req := struct {
UID string `json:"uid"`
Name string `json:"name"`
Thumbnail int64 `json:"thumbnail"`
UID string `json:"uid"`
Name string `json:"name"`
Thumbnail int64 `json:"thumbnail"`
}{}
//read JSON from request body
if err = requests.BodyTo(r, &req); err != nil {
Expand All @@ -31,30 +31,26 @@ func (d *DB) HandleCreateClass(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error occurred in reading UID.", http.StatusInternalServerError)
return
}

if req.Name == "" {
http.Error(w, "Error occurred in reading Name.", http.StatusInternalServerError)
return
}

if req.Thumbnail < 0 || req.Thumbnail >= ThumbnailCount {
if req.Thumbnail < 0 || req.Thumbnail >= ThumbnailCount {
http.Error(w, "Bad thumbnail provided, Exiting", http.StatusInternalServerError)
return
}



// structure for class info
class := Class{
Thumbnail: req.Thumbnail,
Name: req.Name,
Creator: req.UID,
Thumbnail: req.Thumbnail,
Name: req.Name,
Creator: req.UID,
Instructors: []string{req.UID},
Members: []string{},
Programs: []string{},
}


Members: []string{},
Programs: []string{},
}

// create the class
cid, err := d.CreateClass(r.Context(), &class)
Expand All @@ -66,7 +62,7 @@ func (d *DB) HandleCreateClass(w http.ResponseWriter, r *http.Request) {
//create an wid for this class
wid, err := d.MakeAlias(r.Context(), cid, ClassesAliasPath)

//Update class info
//Update class info
// create the class
err = d.UpdateClassWID(r.Context(), cid, wid)

Expand All @@ -77,7 +73,7 @@ func (d *DB) HandleCreateClass(w http.ResponseWriter, r *http.Request) {
return
}

// read the class document just created
// read the class document just created
c, err := d.GetClass(r.Context(), cid)
if err != nil || c == nil {
http.Error(w, "Class does not exist", http.StatusNotFound)
Expand All @@ -91,17 +87,17 @@ func (d *DB) HandleCreateClass(w http.ResponseWriter, r *http.Request) {
}
}

// HandleGetClass takes the UID (either of a member or an instructor)
// and a CID (wid) as a JSON, and returns an object representing the class.
// HandleGetClass takes the UID (either of a member or an instructor)
// and a CID (wid) as a JSON, and returns an object representing the class.
// If the given UID is not a member or an instructor, error is returned
func (d *DB) HandleGetClass(w http.ResponseWriter, r *http.Request) {

var err error

//create an anonymous structure to handle requests
req := struct {
UID string `json:"uid"`
WID string `json:"cid"`
UID string `json:"uid"`
WID string `json:"cid"`
}{}

//read JSON from request body
Expand Down Expand Up @@ -130,7 +126,7 @@ func (d *DB) HandleGetClass(w http.ResponseWriter, r *http.Request) {
}

//check if the uid exists in the members list or instructor list
is_in := false;
is_in := false

for _, m := range c.Members {
if m == req.UID {
Expand All @@ -139,13 +135,13 @@ func (d *DB) HandleGetClass(w http.ResponseWriter, r *http.Request) {
}
}

for _, i := range c.Instructors{
for _, i := range c.Instructors {
if i == req.UID {
is_in = true
break
}
}

// if UID was not in class, return error
if !is_in {
http.Error(w, "Couldn't find user in class", http.StatusInternalServerError)
Expand All @@ -160,8 +156,8 @@ func (d *DB) HandleGetClass(w http.ResponseWriter, r *http.Request) {
}
}

// HandleJoinClass takes a UID and cid(wid) as a JSON, and attempts to
// add the UID to the class given by cid. The updated struct of the class is returned as a
// HandleJoinClass takes a UID and cid(wid) as a JSON, and attempts to
// add the UID to the class given by cid. The updated struct of the class is returned as a
// JSON

func (d *DB) HandleJoinClass(w http.ResponseWriter, r *http.Request) {
Expand All @@ -170,8 +166,8 @@ func (d *DB) HandleJoinClass(w http.ResponseWriter, r *http.Request) {

//create an anonymous structure to handle requests
req := struct {
UID string `json:"uid"`
WID string `json:"cid"`
UID string `json:"uid"`
WID string `json:"cid"`
}{}

//read JSON from request body
Expand All @@ -181,7 +177,7 @@ func (d *DB) HandleJoinClass(w http.ResponseWriter, r *http.Request) {
}
if req.UID == "" {
http.Error(w, "Error occurred in reading UID", http.StatusInternalServerError)
return
return
}
if req.WID == "" {
http.Error(w, "error occurred in reading WID", http.StatusInternalServerError)
Expand Down Expand Up @@ -236,7 +232,6 @@ func (d *DB) HandleJoinClass(w http.ResponseWriter, r *http.Request) {

}


func (d *DB) HandleLeaveClass(w http.ResponseWriter, r *http.Request) {

var (
Expand All @@ -245,8 +240,8 @@ func (d *DB) HandleLeaveClass(w http.ResponseWriter, r *http.Request) {

//create an anonymous structure to handle requests
req := struct {
UID string `json:"uid"`
CID string `json:"cid"`
UID string `json:"uid"`
CID string `json:"cid"`
}{}

//read JSON from request body
Expand Down
6 changes: 2 additions & 4 deletions db/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"time"

"../tools/log"
"github.com/uclaacm/teach-la-go-backend/tools/log"
)

const (
Expand Down Expand Up @@ -32,7 +32,7 @@ const (
// UsersPath describes the path to the user management
// endpoint
UsersPath = "users"

// ClassesPath describes the path to the classes
// management endpoint.
ClassesPath = "classes"
Expand All @@ -42,8 +42,6 @@ const (

// ClassesAliasPath describes the path to the collection with 3 word id => hash mapping for classes
ClassesAliasPath = "classes_alias"


)

// LanguageName acquires the name for the language desecribed
Expand Down
Loading

0 comments on commit 7388432

Please sign in to comment.