@@ -713,7 +713,7 @@ func (api *API) taskGet(rw http.ResponseWriter, r *http.Request) {
713713// @Router /api/experimental/tasks/{user}/{id} [delete]
714714//
715715// EXPERIMENTAL: This endpoint is experimental and not guaranteed to be stable.
716- // taskDelete is an experimental endpoint to delete a task by ID (workspace ID) .
716+ // taskDelete is an experimental endpoint to delete a task by ID.
717717// It creates a delete workspace build and returns 202 Accepted if the build was
718718// created.
719719func (api * API )taskDelete (rw http.ResponseWriter ,r * http.Request ) {
@@ -729,36 +729,37 @@ func (api *API) taskDelete(rw http.ResponseWriter, r *http.Request) {
729729return
730730}
731731
732- // For now, taskID = workspaceID, once we have a task data model in
733- // the DB, we can change this lookup.
734- workspaceID := taskID
735- workspace ,err := api .Database .GetWorkspaceByID (ctx ,workspaceID )
736- if httpapi .Is404Error (err ) {
737- httpapi .ResourceNotFound (rw )
738- return
739- }
732+ // Fetch the task from the database to get the workspace ID.
733+ task ,err := api .Database .GetTaskByID (ctx ,taskID )
740734if err != nil {
735+ if httpapi .Is404Error (err ) {
736+ httpapi .ResourceNotFound (rw )
737+ return
738+ }
741739httpapi .Write (ctx ,rw ,http .StatusInternalServerError , codersdk.Response {
742- Message :"Internal error fetchingworkspace ." ,
740+ Message :"Internal error fetchingtask ." ,
743741Detail :err .Error (),
744742})
745743return
746744}
747745
748- data ,err := api .workspaceData (ctx , []database.Workspace {workspace })
749- if err != nil {
746+ if ! task .WorkspaceID .Valid {
750747httpapi .Write (ctx ,rw ,http .StatusInternalServerError , codersdk.Response {
751- Message :"Internal error fetching workspace resources." ,
752- Detail :err .Error (),
748+ Message :"Task does not have an associated workspace." ,
753749})
754750return
755751}
756- if len (data .builds )== 0 || len (data .templates )== 0 {
757- httpapi .ResourceNotFound (rw )
758- return
759- }
760- if data .builds [0 ].HasAITask == nil || ! * data .builds [0 ].HasAITask {
761- httpapi .ResourceNotFound (rw )
752+
753+ workspace ,err := api .Database .GetWorkspaceByID (ctx ,task .WorkspaceID .UUID )
754+ if err != nil {
755+ if httpapi .Is404Error (err ) {
756+ httpapi .ResourceNotFound (rw )
757+ return
758+ }
759+ httpapi .Write (ctx ,rw ,http .StatusInternalServerError , codersdk.Response {
760+ Message :"Internal error fetching workspace." ,
761+ Detail :err .Error (),
762+ })
762763return
763764}
764765
@@ -784,6 +785,17 @@ func (api *API) taskDelete(rw http.ResponseWriter, r *http.Request) {
784785return
785786}
786787
788+ /*
789+ err = api.Database.DeleteTask(ctx, task.ID)
790+ if err != nil {
791+ httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
792+ Message: "Failed to delete task",
793+ Detail: err.Error(),
794+ })
795+ return
796+ }
797+ */
798+
787799// Delete build created successfully.
788800rw .WriteHeader (http .StatusAccepted )
789801}