-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add delete method to user model * Create users' delete controller * Add delete method to story model * Create stories' delete controller * Add TODOs * Update router
- Loading branch information
1 parent
4d395e9
commit 972695d
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package stories | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/sirupsen/logrus" | ||
"github.com/source-academy/stories-backend/controller" | ||
"github.com/source-academy/stories-backend/internal/database" | ||
apierrors "github.com/source-academy/stories-backend/internal/errors" | ||
"github.com/source-academy/stories-backend/model" | ||
storyviews "github.com/source-academy/stories-backend/view/stories" | ||
) | ||
|
||
func HandleDelete(w http.ResponseWriter, r *http.Request) error { | ||
storyIDStr := chi.URLParam(r, "storyID") | ||
storyID, err := strconv.Atoi(storyIDStr) | ||
if err != nil { | ||
return apierrors.ClientBadRequestError{ | ||
Message: fmt.Sprintf("Invalid storyID: %v", err), | ||
} | ||
} | ||
|
||
// Get DB instance | ||
db, err := database.GetDBFrom(r) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
|
||
story, err := model.DeleteStory(db, storyID) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
controller.EncodeJSONResponse(w, storyviews.SingleFrom(story)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package users | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/sirupsen/logrus" | ||
"github.com/source-academy/stories-backend/controller" | ||
"github.com/source-academy/stories-backend/internal/database" | ||
apierrors "github.com/source-academy/stories-backend/internal/errors" | ||
"github.com/source-academy/stories-backend/model" | ||
userviews "github.com/source-academy/stories-backend/view/users" | ||
) | ||
|
||
func HandleDelete(w http.ResponseWriter, r *http.Request) error { | ||
userIDStr := chi.URLParam(r, "userID") | ||
userID, err := strconv.Atoi(userIDStr) | ||
if err != nil { | ||
return apierrors.ClientBadRequestError{ | ||
Message: fmt.Sprintf("Invalid userID: %v", err), | ||
} | ||
} | ||
|
||
// Get DB instance | ||
db, err := database.GetDBFrom(r) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
|
||
user, err := model.DeleteUser(db, userID) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
controller.EncodeJSONResponse(w, userviews.SingleFrom(user)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters