Skip to content

Commit

Permalink
fixed linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ArshMalik02 committed May 25, 2022
1 parent c25b1ec commit 90712f6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 94 deletions.
88 changes: 0 additions & 88 deletions db/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,94 +108,6 @@ func (d *DB) UpdateProgram(c echo.Context) error {
return c.String(http.StatusOK, "")
}

// DeleteProgram deletes a program entry from a user.
//
// Request Body:
// {
// uid: string
// pid: string
// }
//
// Returns status 200 OK on deletion.
func (d *DB) DeleteProgram(c echo.Context) error {
// acquire parameters via anonymous struct.
var req struct {
UID string `json:"uid"`
PID string `json:"pid"`
}
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.PID == "" {
return c.String(http.StatusBadRequest, "uid and idx fields are both required")
}

err := d.RunTransaction(c.Request().Context(), func(ctx context.Context, tx *firestore.Transaction) error {
// remove program from user list
uref := d.Collection(usersPath).Doc(req.UID)
uSnap, err := tx.Get(uref)
if err != nil {
return err
}
userDoc := User{}
if err := uSnap.DataTo(&userDoc); err != nil {
return err
}

// get pid to delete then remove the entry
idx := 0
for i, p := range userDoc.Programs {
if p == req.PID {
idx = i
break
}
}
if idx >= len(userDoc.Programs) {
return errors.New("invalid PID")
}
toDelete := userDoc.Programs[idx]
userDoc.Programs = append(userDoc.Programs[:idx], userDoc.Programs[idx+1:]...)

pref := d.Collection(programsPath).Doc(toDelete)

// remove program from class if is in class
pSnap, err := tx.Get(pref)
if err != nil {
return err
}
programDoc := Program{}
if err := pSnap.DataTo(&programDoc); err != nil {
return err
}
if programDoc.WID != "" {
cid, err := d.GetUIDFromWID(c.Request().Context(), programDoc.WID, ClassesAliasPath)
if err != nil {
return err
}
classRef := d.Collection(classesPath).Doc(cid)
if err := tx.Update(classRef, []firestore.Update{
{Path: "programs", Value: firestore.ArrayRemove(toDelete)},
}); err != nil {
return err
}
}

// attempt to delete program doc
if err := tx.Set(uref, &userDoc); err != nil {
return err
}
return tx.Delete(pref)
})
if err != nil {
if status.Code(err) == codes.NotFound {
return c.String(http.StatusNotFound, "user or program does not exist")
}
return c.String(http.StatusInternalServerError, errors.Wrap(err, "failed to commit transaction to database").Error())
}

return c.String(http.StatusOK, "")
}

// ForkProgram forks a program `pid` to the user `uid`.
//
// Request Body:
Expand Down
15 changes: 10 additions & 5 deletions handler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func DeleteProgram(cc echo.Context) error {
}

u, err := c.LoadUser(c.Request().Context(), req.UID)
if err != nil{
if err != nil {
return err
}

Expand All @@ -151,12 +151,14 @@ func DeleteProgram(cc echo.Context) error {
toDelete := u.Programs[idx]
u.Programs = append(u.Programs[:idx], u.Programs[idx+1:]...)

// pref := d.Collection(programsPath).Doc(toDelete)
c.StoreUser(c.Request().Context(), u)
err = c.StoreUser(c.Request().Context(), u)
if err != nil {
return err
}

// remove program from class if is in class
p, err := c.LoadProgram(c.Request().Context(), toDelete)

if err != nil {
return err
}
Expand Down Expand Up @@ -185,7 +187,10 @@ func DeleteProgram(cc echo.Context) error {
}
cls.Programs = append(cls.Programs[:idx], cls.Programs[idx+1:]...)

c.StoreClass(c.Request().Context(), cls)
err = c.StoreClass(c.Request().Context(), cls)
if err != nil {
return err
}
}

err = c.RemoveProgram(c.Request().Context(), req.PID)
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func serve(c *cli.Context) error {
e.GET("/program/get", handler.GetProgram)
e.PUT("/program/update", d.UpdateProgram)
e.POST("/program/create", handler.CreateProgram)
e.DELETE("/program/delete", d.DeleteProgram)
e.DELETE("/program/delete", handler.DeleteProgram)

// class management
e.POST("/class/get", handler.GetClass)
Expand Down

0 comments on commit 90712f6

Please sign in to comment.