Skip to content

Commit

Permalink
better response for deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Apr 18, 2024
1 parent d211910 commit 82ab3d6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
7 changes: 5 additions & 2 deletions internal/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ func GetCertificateRequest(env *Environment) http.HandlerFunc {
func DeleteCertificateRequest(env *Environment) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
err := env.DB.Delete(id)
insertId, err := env.DB.Delete(id)
if err != nil {
logError(err.Error(), http.StatusInternalServerError, w)
return
}
w.WriteHeader(http.StatusNoContent)
w.WriteHeader(http.StatusAccepted)
if _, err := w.Write([]byte(strconv.FormatInt(insertId, 10))); err != nil {
logError(err.Error(), http.StatusInternalServerError, w)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func TestGoCertRouter(t *testing.T) {
method: "DELETE",
path: "/api/v1/certificate_requests/1",
data: "",
response: "",
status: http.StatusNoContent,
response: "1",
status: http.StatusAccepted,
},
{
desc: "get csr1 fail",
Expand Down
15 changes: 11 additions & 4 deletions internal/certdb/certdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,19 @@ func (db *CertificateRequestsRepository) Update(id string, cert string) (int64,
}

// Delete removes a CSR from the database alongside the certificate that may have been generated for it.
func (db *CertificateRequestsRepository) Delete(id string) error {
_, err := db.conn.Exec(fmt.Sprintf(queryDeleteCSR, db.table), id)
func (db *CertificateRequestsRepository) Delete(id string) (int64, error) {
result, err := db.conn.Exec(fmt.Sprintf(queryDeleteCSR, db.table), id)
if err != nil {
return err
return 0, err
}
return nil
deleteId, err := result.RowsAffected()
if err != nil {
return 0, err
}
if deleteId == 0 {
return 0, errors.New("csr id not found")
}
return deleteId, nil
}

// Close closes the connection to the repository cleanly.
Expand Down
2 changes: 1 addition & 1 deletion internal/certdb/certdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestEndToEnd(t *testing.T) {
t.Fatalf("The CSR from the database doesn't match the CSR that was given")
}

if err = db.Delete(strconv.FormatInt(id1, 10)); err != nil {
if _, err = db.Delete(strconv.FormatInt(id1, 10)); err != nil {
t.Fatalf("Couldn't complete Delete: %s", err)
}
res, _ = db.RetrieveAll()
Expand Down

0 comments on commit 82ab3d6

Please sign in to comment.