Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joycechen721 committed Nov 7, 2022
1 parent ba1333c commit 477e622
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 61 deletions.
60 changes: 1 addition & 59 deletions db/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
"context"
"fmt"
"net/http"

"cloud.google.com/go/firestore"
Expand Down Expand Up @@ -225,61 +224,4 @@ func (d *DB) LeaveClass(c echo.Context) error {

// return the latest state of the user
return c.String(http.StatusOK, "")
}

// GetClassMembers returns the user IDs and display names of each member in the requested class.
// It takes a class id and checks that the given uid is in the class first
func (d *DB) GetClassMembers(c echo.Context) error {
var req struct {
CID string `json:"cid"`
UID string `json:"uid"`
}

if err := httpext.RequestBodyTo(c.Request(), &req); err != nil {
return c.String(http.StatusInternalServerError, errors.Wrap(err, "failed to read request body").Error())
}
if req.UID == "" || req.CID == "" {
return c.String(http.StatusBadRequest, "uid and cid fields are both required")
}
uid := req.UID
cid := req.CID

// get the class as a struct (pointer)
class, err := d.loadClass(c.Request().Context(), cid)
if err != nil || class == nil {
return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to get class: %s", err))
}

// Check if user is in class
isIn := false
for _, m := range class.Members {
if m == uid {
isIn = true
break
}
}

if !isIn {
return c.String(http.StatusNotFound, "given user not in class")
}

res := make(map[string]User)

for _, uid := range class.Members {
userSnap, err := d.Collection(usersPath).Doc(uid).Get(c.Request().Context())
if err != nil {
continue
}

tmpUser := User{}
err = userSnap.DataTo(&tmpUser)
if err != nil {
continue
}

res[uid] = tmpUser
}

// convert to JSON and return
return c.JSON(http.StatusOK, res)
}
}
57 changes: 56 additions & 1 deletion handler/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package handler

import (
"net/http"

"fmt"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/uclaacm/teach-la-go-backend/db"
Expand All @@ -11,6 +11,61 @@ import (
"google.golang.org/grpc/status"
)

// GetClassMembers returns the user IDs and display names of each member in the requested class.
// It takes a class id and checks that the given uid is in the class first
func GetClassMembers(cc echo.Context) error {
var req struct {
CID string `json:"cid"`
UID string `json:"uid"`
UserData db.User `json:"userData"`
}

c := cc.(*db.DBContext)

if err := httpext.RequestBodyTo(c.Request(), &req); err != nil {
return c.String(http.StatusInternalServerError, errors.Wrap(err, "failed to read request body").Error())
}

uid := req.UID
cid := req.CID

if uid == "" || cid == "" {
return c.String(http.StatusBadRequest, "uid and cid fields are both required")
}

// get the class as a struct (pointer)
class, err := c.LoadClass(c.Request().Context(), cid)
if err != nil {
return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to get class: %s", err))
}

// Check if user is in class
isIn := false
for _, m := range class.Members {
if m == uid {
isIn = true
break
}
}

if !isIn {
return c.String(http.StatusNotFound, "given user not in class")
}

res := make(map[string]db.User)

for _, uid := range class.Members {
userSnap, err := c.LoadUser(c.Request().Context(), c.QueryParam(uid))
if err != nil {
continue
}
req.UserData = userSnap
}

// convert to JSON and return
return c.JSON(http.StatusOK, res)
}

// GetClass 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, an error is returned.
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func serve(c *cli.Context) error {
e.POST("/class/create", d.CreateClass)
e.PUT("/class/join", handler.JoinClass)
e.PUT("/class/leave", d.LeaveClass)
e.POST("/class/members", d.GetClassMembers)
e.POST("/class/members", handler.GetClassMembers)

// collaborative coding management
e.POST("/collab/create", d.CreateCollab)
Expand Down

0 comments on commit 477e622

Please sign in to comment.