Skip to content

Commit

Permalink
Merge pull request #20 from justinfarrelldev/delete-account-endpoint
Browse files Browse the repository at this point in the history
Implemented the handler for deleting accounts
  • Loading branch information
justinfarrelldev authored Dec 9, 2024
2 parents 1683229 + 7e17e95 commit bd91d4f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
8 changes: 8 additions & 0 deletions internal/account/account_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ func UpdateAccountHandler(w http.ResponseWriter, r *http.Request, db *sqlx.DB, s
return
}
}

func DeleteAccountHandler(w http.ResponseWriter, r *http.Request, db *sqlx.DB, store *auth.SessionStore) {
if err := DeleteAccount(w, r, db, store); err != nil {
// Handle the error, e.g., log it and send an appropriate response to the client
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
10 changes: 6 additions & 4 deletions internal/account/delete_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// @Description Structure for the account deletion request payload.
type DeleteAccountArgs struct {
// The account ID for the account that will be deleted.
AccountId int64 `json:"account_id"`
AccountId *int64 `json:"account_id,omitempty"`
// A valid session ID for the account (so we know they are signed in)
SessionId *int64 `json:"session_id,omitempty"`
}
Expand Down Expand Up @@ -51,7 +51,7 @@ func DeleteAccount(w http.ResponseWriter, r *http.Request, db *sqlx.DB, store *a
return errors.New("an error occurred while decoding the request body: " + err.Error())
}

if args.AccountId == 0 {
if args.AccountId == nil {
w.WriteHeader(http.StatusBadRequest)
return errors.New("account_id must be specified")
}
Expand All @@ -61,6 +61,8 @@ func DeleteAccount(w http.ResponseWriter, r *http.Request, db *sqlx.DB, store *a
return errors.New("a valid session_id must be specified")
}

fmt.Println("args: ", *args.AccountId)

session, err := store.GetSession(strconv.FormatInt(*args.SessionId, 10))

if err != nil {
Expand All @@ -69,7 +71,7 @@ func DeleteAccount(w http.ResponseWriter, r *http.Request, db *sqlx.DB, store *a
}

if session == nil {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
return errors.New("session not found")
}

Expand All @@ -90,7 +92,7 @@ func DeleteAccount(w http.ResponseWriter, r *http.Request, db *sqlx.DB, store *a
}

if rowsAffected == 0 {
return fmt.Errorf("no account exists with the ID %d", args.AccountId)
return fmt.Errorf("no rows were affected when the DELETE query ran for the account with ID %d", *args.AccountId)
}

w.WriteHeader(http.StatusOK)
Expand Down
27 changes: 19 additions & 8 deletions internal/account/delete_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ func TestDeleteAccount_MissingSessionID(t *testing.T) {
}
defer db.Close()

var acctId int64 = 1

deleteArgs := DeleteAccountArgs{
AccountId: 1,
AccountId: &acctId,
}

body, _ := json.Marshal(deleteArgs)
Expand Down Expand Up @@ -188,9 +190,11 @@ func TestDeleteAccount_SessionNotFound(t *testing.T) {
defer db.Close()

accountID := int64(1)
var acctId int64 = accountID

sessionID := int64(1)
deleteArgs := DeleteAccountArgs{
AccountId: accountID,
AccountId: &acctId,
SessionId: &sessionID,
}

Expand Down Expand Up @@ -220,8 +224,8 @@ func TestDeleteAccount_SessionNotFound(t *testing.T) {

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusInternalServerError {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}

expectedError := "session not found"
Expand All @@ -238,9 +242,11 @@ func TestDeleteAccount_SessionExpired(t *testing.T) {
defer db.Close()

accountID := int64(1)
var acctId int64 = accountID

sessionID := int64(1)
deleteArgs := DeleteAccountArgs{
AccountId: accountID,
AccountId: &acctId,
SessionId: &sessionID,
}

Expand Down Expand Up @@ -292,9 +298,11 @@ func TestDeleteAccount_Success(t *testing.T) {
defer db.Close()

accountID := int64(1)
var acctId int64 = accountID

sessionID := int64(1)
deleteArgs := DeleteAccountArgs{
AccountId: accountID,
AccountId: &acctId,
SessionId: &sessionID,
}

Expand Down Expand Up @@ -354,9 +362,12 @@ func TestDeleteAccount_NoAccountExists(t *testing.T) {
defer db.Close()

accountID := int64(1)
var acctId int64 = accountID

fmt.Println("Account id: ", acctId)
sessionID := int64(1)
deleteArgs := DeleteAccountArgs{
AccountId: accountID,
AccountId: &acctId,
SessionId: &sessionID,
}

Expand Down Expand Up @@ -398,7 +409,7 @@ func TestDeleteAccount_NoAccountExists(t *testing.T) {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError)
}

expectedError := "no account exists with the ID 1"
expectedError := "no rows were affected when the DELETE query ran for the account with ID 1"
if strings.TrimSpace(rr.Body.String()) != expectedError {
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError)
}
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func main() {
account.UpdateAccountHandler(w, r, db, sessionStore)
}))

mux.Handle("/account/delete_account", tollbooth.LimitFuncHandler(tollboothLimiter, func(w http.ResponseWriter, r *http.Request) {
account.DeleteAccountHandler(w, r, db, sessionStore)
}))

mux.Handle("/lobby/create_lobby", tollbooth.LimitFuncHandler(tollboothLimiterMinute, func(w http.ResponseWriter, r *http.Request) {
lobby.CreateLobbyHandler(w, r, db, sessionStore)
}))
Expand Down

0 comments on commit bd91d4f

Please sign in to comment.