From 0ca26643a25ab39f2a02bc2b858b0d56bc579aaf Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Wed, 31 Jan 2024 14:51:25 +0800 Subject: [PATCH 01/10] feat:implemented delete sr by id --- backend/src/api/handlers/handlers.go | 18 ++++++++++++++++++ backend/src/database/service_request.go | 9 +++++++++ backend/src/main.go | 1 + 3 files changed, 28 insertions(+) diff --git a/backend/src/api/handlers/handlers.go b/backend/src/api/handlers/handlers.go index 5f6efa7e..d71f4551 100644 --- a/backend/src/api/handlers/handlers.go +++ b/backend/src/api/handlers/handlers.go @@ -93,6 +93,24 @@ func CreateServiceRequest(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) } +func DeleteServiceRequest(w http.ResponseWriter, r *http.Request) { + client, err := client.GetMongoClient() + if err != nil { + JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + return + } + vars := mux.Vars(r) + requestId := vars["requestId"] + _, err = database.NewServiceRequest(client).DeleteById(requestId) + w.Header().Set("Content-Type", "application/json") + if err != nil { + JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) + return +} + func CreatePipeline(w http.ResponseWriter, r *http.Request) { pipeline := &dbmodels.PipelineModel{ CreatedOn: time.Now(), diff --git a/backend/src/database/service_request.go b/backend/src/database/service_request.go index 3d917136..b7c2d8bf 100644 --- a/backend/src/database/service_request.go +++ b/backend/src/database/service_request.go @@ -33,6 +33,15 @@ func (sr *ServiceRequest) GetById(id string) (*models.ServiceRequestModel, error return srm, nil } +func (sr *ServiceRequest) DeleteById(id string) (*mongo.DeleteResult, error) { + objectId, _ := primitive.ObjectIDFromHex(id) + res, err := sr.c.Database(DatabaseName).Collection("service_requests").DeleteOne(context.Background(), bson.M{"_id": objectId}) + if err != nil { + return nil, err + } + return res, nil +} + func (sr *ServiceRequest) GetAll() ([]*models.ServiceRequestModel, error) { result, err := sr.c.Database(DatabaseName).Collection("service_requests").Find(context.Background(), bson.M{}) if err != nil { diff --git a/backend/src/main.go b/backend/src/main.go index 17a47466..0a1fdaf0 100644 --- a/backend/src/main.go +++ b/backend/src/main.go @@ -16,6 +16,7 @@ func main() { r.HandleFunc("/api/healthcheck", handlers.HealthCheck).Methods("GET") r.HandleFunc("/api/servicerequest/new", handlers.CreateServiceRequest).Methods("POST").Headers("Content-Type", "application/json") r.HandleFunc("/api/servicerequest/{requestId}", handlers.GetServiceRequest).Methods("GET") + r.HandleFunc("/api/servicerequest/{requestId}", handlers.DeleteServiceRequest).Methods("DELETE") r.HandleFunc("/api/servicerequest", handlers.GetAllServiceRequest).Methods("GET") r.HandleFunc("/api/pipeline", handlers.CreatePipeline).Methods("POST").Headers("Content-Type", "application/json") r.HandleFunc("/api/pipeline", handlers.GetAllPipelines).Methods("GET") From ce846bc8e8fe2df7da8abe5cb4c2b42ff50d70b7 Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Wed, 31 Jan 2024 15:17:28 +0800 Subject: [PATCH 02/10] feat: implemented update service request by id --- backend/src/api/handlers/handlers.go | 27 +++++++++++++++++++++++++ backend/src/database/service_request.go | 16 +++++++++++++++ backend/src/main.go | 1 + 3 files changed, 44 insertions(+) diff --git a/backend/src/api/handlers/handlers.go b/backend/src/api/handlers/handlers.go index d71f4551..79663c19 100644 --- a/backend/src/api/handlers/handlers.go +++ b/backend/src/api/handlers/handlers.go @@ -111,6 +111,33 @@ func DeleteServiceRequest(w http.ResponseWriter, r *http.Request) { return } +func UpdateServiceRequest(w http.ResponseWriter, r *http.Request) { + client, err := client.GetMongoClient() + if err != nil { + JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + return + } + srm := &dbmodels.ServiceRequestModel{ + CreatedOn: time.Now(), + LastUpdated: time.Now(), + Status: dbmodels.Pending, + } + err = json.NewDecoder(r.Body).Decode(srm) + w.Header().Set("Content-Type", "application/json") + if err != nil { + JSONError(w, handlermodels.NewHttpError(err), http.StatusBadRequest) + return + } + vars := mux.Vars(r) + requestId := vars["requestId"] + _, err = database.NewServiceRequest(client).UpdateById(requestId, srm) + if err != nil { + JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) +} + func CreatePipeline(w http.ResponseWriter, r *http.Request) { pipeline := &dbmodels.PipelineModel{ CreatedOn: time.Now(), diff --git a/backend/src/database/service_request.go b/backend/src/database/service_request.go index b7c2d8bf..5a075b40 100644 --- a/backend/src/database/service_request.go +++ b/backend/src/database/service_request.go @@ -42,6 +42,22 @@ func (sr *ServiceRequest) DeleteById(id string) (*mongo.DeleteResult, error) { return res, nil } +func (sr *ServiceRequest) UpdateById(id string, srm *models.ServiceRequestModel) (*mongo.UpdateResult, error) { + objectId, _ := primitive.ObjectIDFromHex(id) + filter := bson.M{"_id": objectId} + update := bson.M{"$set": bson.M{ + "pipeline_id": srm.PipelineId, + "pipeline_version": srm.PipelineVersion, + "last_updated": srm.LastUpdated, + "remarks": srm.Remarks}} + + res, err := sr.c.Database(DatabaseName).Collection("service_requests").UpdateOne(context.Background(), filter, update) + if err != nil { + return nil, err + } + return res, nil +} + func (sr *ServiceRequest) GetAll() ([]*models.ServiceRequestModel, error) { result, err := sr.c.Database(DatabaseName).Collection("service_requests").Find(context.Background(), bson.M{}) if err != nil { diff --git a/backend/src/main.go b/backend/src/main.go index 0a1fdaf0..32e78cef 100644 --- a/backend/src/main.go +++ b/backend/src/main.go @@ -17,6 +17,7 @@ func main() { r.HandleFunc("/api/servicerequest/new", handlers.CreateServiceRequest).Methods("POST").Headers("Content-Type", "application/json") r.HandleFunc("/api/servicerequest/{requestId}", handlers.GetServiceRequest).Methods("GET") r.HandleFunc("/api/servicerequest/{requestId}", handlers.DeleteServiceRequest).Methods("DELETE") + r.HandleFunc("/api/servicerequest/{requestId}", handlers.UpdateServiceRequest).Methods("PUT").Headers("Content-Type", "application/json") r.HandleFunc("/api/servicerequest", handlers.GetAllServiceRequest).Methods("GET") r.HandleFunc("/api/pipeline", handlers.CreatePipeline).Methods("POST").Headers("Content-Type", "application/json") r.HandleFunc("/api/pipeline", handlers.GetAllPipelines).Methods("GET") From 8f5ee3b9cb02bcb950b27a75cb58034980844467 Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Wed, 31 Jan 2024 15:37:23 +0800 Subject: [PATCH 03/10] refactor: added logging for sr handlers --- backend/src/api/handlers/handlers.go | 46 +++++++++++++++++++--------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/backend/src/api/handlers/handlers.go b/backend/src/api/handlers/handlers.go index 79663c19..5d81bed4 100644 --- a/backend/src/api/handlers/handlers.go +++ b/backend/src/api/handlers/handlers.go @@ -33,14 +33,17 @@ func HealthCheck(w http.ResponseWriter, r *http.Request) { func GetAllServiceRequest(w http.ResponseWriter, r *http.Request) { client, err := client.GetMongoClient() if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + logger.Error("[GetAllServiceRequest] Unable to get mongo client", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } allsr, err := database.NewServiceRequest(client).GetAll() if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + logger.Error("[GetAllServiceRequest] Error retrieving all service request", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } + logger.Info("[GetAllServiceRequest] Successfully retrieved service requests", map[string]interface{}{"count": len(allsr)}) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(allsr) w.WriteHeader(http.StatusOK) @@ -49,7 +52,8 @@ func GetAllServiceRequest(w http.ResponseWriter, r *http.Request) { func GetServiceRequest(w http.ResponseWriter, r *http.Request) { client, err := client.GetMongoClient() if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + logger.Error("[GetServiceRequest] Unable to get mongo client", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } vars := mux.Vars(r) @@ -57,9 +61,11 @@ func GetServiceRequest(w http.ResponseWriter, r *http.Request) { sr, err := database.NewServiceRequest(client).GetById(requestId) w.Header().Set("Content-Type", "application/json") if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusBadRequest) + logger.Error("[GetServiceRequest] Unable to retrieve service request", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusBadRequest) return } + logger.Info("[GetServiceRequest] Successfully retrieved service request", map[string]interface{}{"sr": sr}) json.NewEncoder(w).Encode(sr) w.WriteHeader(http.StatusOK) return @@ -68,7 +74,8 @@ func GetServiceRequest(w http.ResponseWriter, r *http.Request) { func CreateServiceRequest(w http.ResponseWriter, r *http.Request) { client, err := client.GetMongoClient() if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + logger.Error("[CreateServiceRequest] Unable to get mongo client", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } srm := &dbmodels.ServiceRequestModel{ @@ -79,16 +86,19 @@ func CreateServiceRequest(w http.ResponseWriter, r *http.Request) { err = json.NewDecoder(r.Body).Decode(srm) w.Header().Set("Content-Type", "application/json") if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusBadRequest) + logger.Error("[CreateServiceRequest] Unable to parse json request body", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("unable to parse json request body")), http.StatusBadRequest) return } res, err := database.NewServiceRequest(client).Create(srm) if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + logger.Error("[CreateServiceRequest] Error creating service request", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } insertedId, _ := res.InsertedID.(primitive.ObjectID) srm.Id = insertedId + logger.Info("[CreateServiceRequest] Successfully created service request", map[string]interface{}{"sr": srm}) event.FireAsync(events.NewNewServiceRequestEvent(srm)) w.WriteHeader(http.StatusCreated) } @@ -96,17 +106,20 @@ func CreateServiceRequest(w http.ResponseWriter, r *http.Request) { func DeleteServiceRequest(w http.ResponseWriter, r *http.Request) { client, err := client.GetMongoClient() if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + logger.Error("[DeleteServiceRequest] Unable to get mongo client", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } vars := mux.Vars(r) requestId := vars["requestId"] - _, err = database.NewServiceRequest(client).DeleteById(requestId) + res, err := database.NewServiceRequest(client).DeleteById(requestId) w.Header().Set("Content-Type", "application/json") if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + logger.Error("[DeleteServiceRequest] Error deleting service request", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } + logger.Info("[DeleteServiceRequest] Successfully deleted service request", map[string]interface{}{"count": res.DeletedCount}) w.WriteHeader(http.StatusOK) return } @@ -114,7 +127,8 @@ func DeleteServiceRequest(w http.ResponseWriter, r *http.Request) { func UpdateServiceRequest(w http.ResponseWriter, r *http.Request) { client, err := client.GetMongoClient() if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + logger.Error("[UpdateServiceRequest] Unable to get mongo client", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } srm := &dbmodels.ServiceRequestModel{ @@ -125,16 +139,19 @@ func UpdateServiceRequest(w http.ResponseWriter, r *http.Request) { err = json.NewDecoder(r.Body).Decode(srm) w.Header().Set("Content-Type", "application/json") if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusBadRequest) + logger.Error("[UpdateServiceRequest] Unable to parse json request body", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("unable to parse json request body")), http.StatusBadRequest) return } vars := mux.Vars(r) requestId := vars["requestId"] - _, err = database.NewServiceRequest(client).UpdateById(requestId, srm) + res, err := database.NewServiceRequest(client).UpdateById(requestId, srm) if err != nil { - JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError) + logger.Error("[UpdateServiceRequest] Error updating service request", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } + logger.Info("[UpdateServiceRequest] Successfully updated service request", map[string]interface{}{"count": res.ModifiedCount}) w.WriteHeader(http.StatusOK) } @@ -147,6 +164,7 @@ func CreatePipeline(w http.ResponseWriter, r *http.Request) { if err != nil { logger.Error("[CreatePipeline] Unable to parse json request body", map[string]interface{}{"err": err}) JSONError(w, handlermodels.NewHttpError(errors.New("unable to parse json request body")), http.StatusBadRequest) + return } client, err := client.GetMongoClient() if err != nil { From 796d6513d1c66ed442b7fd3b074e5e18f5c807fa Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Wed, 31 Jan 2024 16:39:02 +0800 Subject: [PATCH 04/10] refactor: change update sr method to patch instead of put --- backend/src/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/main.go b/backend/src/main.go index 32e78cef..6ea9ccc4 100644 --- a/backend/src/main.go +++ b/backend/src/main.go @@ -17,7 +17,7 @@ func main() { r.HandleFunc("/api/servicerequest/new", handlers.CreateServiceRequest).Methods("POST").Headers("Content-Type", "application/json") r.HandleFunc("/api/servicerequest/{requestId}", handlers.GetServiceRequest).Methods("GET") r.HandleFunc("/api/servicerequest/{requestId}", handlers.DeleteServiceRequest).Methods("DELETE") - r.HandleFunc("/api/servicerequest/{requestId}", handlers.UpdateServiceRequest).Methods("PUT").Headers("Content-Type", "application/json") + r.HandleFunc("/api/servicerequest/{requestId}", handlers.UpdateServiceRequest).Methods("PATCH").Headers("Content-Type", "application/json") r.HandleFunc("/api/servicerequest", handlers.GetAllServiceRequest).Methods("GET") r.HandleFunc("/api/pipeline", handlers.CreatePipeline).Methods("POST").Headers("Content-Type", "application/json") r.HandleFunc("/api/pipeline", handlers.GetAllPipelines).Methods("GET") From e28e2eede9ba7425cf79f39ca494d205087f963c Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Fri, 2 Feb 2024 18:06:44 +0800 Subject: [PATCH 05/10] fix: changed delete sr handler to cancel sr handler --- backend/src/api/handlers/handlers.go | 29 +++++++++++++++---- .../src/database/models/service_request.go | 9 +++--- backend/src/database/service_request.go | 8 +++++ backend/src/main.go | 2 +- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/backend/src/api/handlers/handlers.go b/backend/src/api/handlers/handlers.go index 5d81bed4..2290f46a 100644 --- a/backend/src/api/handlers/handlers.go +++ b/backend/src/api/handlers/handlers.go @@ -11,6 +11,7 @@ import ( handlermodels "github.com/joshtyf/flowforge/src/api/handlers/models" "github.com/joshtyf/flowforge/src/database" "github.com/joshtyf/flowforge/src/database/client" + "github.com/joshtyf/flowforge/src/database/models" dbmodels "github.com/joshtyf/flowforge/src/database/models" "github.com/joshtyf/flowforge/src/events" "github.com/joshtyf/flowforge/src/logger" @@ -103,23 +104,41 @@ func CreateServiceRequest(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) } -func DeleteServiceRequest(w http.ResponseWriter, r *http.Request) { +func CancelStartedServiceRequest(w http.ResponseWriter, r *http.Request) { client, err := client.GetMongoClient() if err != nil { - logger.Error("[DeleteServiceRequest] Unable to get mongo client", map[string]interface{}{"err": err}) + logger.Error("[CancelStartedServiceRequest] Unable to get mongo client", map[string]interface{}{"err": err}) JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } vars := mux.Vars(r) requestId := vars["requestId"] - res, err := database.NewServiceRequest(client).DeleteById(requestId) + status, err := database.NewServiceRequest(client).GetStatus(requestId) w.Header().Set("Content-Type", "application/json") if err != nil { - logger.Error("[DeleteServiceRequest] Error deleting service request", map[string]interface{}{"err": err}) + logger.Error("[CancelStartedServiceRequest] Error getting service request status", map[string]interface{}{"err": err}) JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } - logger.Info("[DeleteServiceRequest] Successfully deleted service request", map[string]interface{}{"count": res.DeletedCount}) + + if status != models.Pending && status != models.Running { + logger.Error("[CancelStartedServiceRequest] Unable to cancel service request as execution has been completed", nil) + JSONError(w, handlermodels.NewHttpError(errors.New("service request execution has been completed")), http.StatusBadRequest) + return + } + + // TODO: implement cancellation of sr + + err = database.NewServiceRequest(client).UpdateStatus(requestId, models.Canceled) + + // TODO: discuss how to handle this error + if err != nil { + logger.Error("[CancelStartedServiceRequest] Unable to update service request status", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) + return + } + + logger.Info("[CancelStartedServiceRequest] Successfully canceled started service request", nil) w.WriteHeader(http.StatusOK) return } diff --git a/backend/src/database/models/service_request.go b/backend/src/database/models/service_request.go index 5652822c..b40e6a08 100644 --- a/backend/src/database/models/service_request.go +++ b/backend/src/database/models/service_request.go @@ -9,10 +9,11 @@ import ( type ServiceRequestStatus string const ( - Pending ServiceRequestStatus = "Pending" - Running ServiceRequestStatus = "Running" - Success ServiceRequestStatus = "Success" - Failure ServiceRequestStatus = "Failure" + Pending ServiceRequestStatus = "Pending" + Running ServiceRequestStatus = "Running" + Success ServiceRequestStatus = "Success" + Failure ServiceRequestStatus = "Failure" + Canceled ServiceRequestStatus = "Canceled" ) type ServiceRequestModel struct { diff --git a/backend/src/database/service_request.go b/backend/src/database/service_request.go index 5a075b40..85a12f6f 100644 --- a/backend/src/database/service_request.go +++ b/backend/src/database/service_request.go @@ -81,3 +81,11 @@ func (sr *ServiceRequest) UpdateStatus(id string, status models.ServiceRequestSt context.Background(), bson.M{"_id": objectId}, bson.M{"$set": bson.M{"status": status}}) return err } + +func (sr *ServiceRequest) GetStatus(id string) (status models.ServiceRequestStatus, err error) { + srm, err := sr.GetById(id) + if err != nil { + return "", err + } + return srm.Status, nil +} diff --git a/backend/src/main.go b/backend/src/main.go index 6ea9ccc4..85caa4e6 100644 --- a/backend/src/main.go +++ b/backend/src/main.go @@ -16,7 +16,7 @@ func main() { r.HandleFunc("/api/healthcheck", handlers.HealthCheck).Methods("GET") r.HandleFunc("/api/servicerequest/new", handlers.CreateServiceRequest).Methods("POST").Headers("Content-Type", "application/json") r.HandleFunc("/api/servicerequest/{requestId}", handlers.GetServiceRequest).Methods("GET") - r.HandleFunc("/api/servicerequest/{requestId}", handlers.DeleteServiceRequest).Methods("DELETE") + r.HandleFunc("/api/servicerequest/{requestId}/cancel", handlers.CancelStartedServiceRequest).Methods("GET") r.HandleFunc("/api/servicerequest/{requestId}", handlers.UpdateServiceRequest).Methods("PATCH").Headers("Content-Type", "application/json") r.HandleFunc("/api/servicerequest", handlers.GetAllServiceRequest).Methods("GET") r.HandleFunc("/api/pipeline", handlers.CreatePipeline).Methods("POST").Headers("Content-Type", "application/json") From da6a6b317cc32bef83f200b6831a1f0e9e2f3fb0 Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Fri, 2 Feb 2024 18:26:30 +0800 Subject: [PATCH 06/10] fix: added not started sr status and additional check for new status under sr cancellation handler --- backend/src/api/handlers/handlers.go | 6 ++++++ backend/src/database/models/service_request.go | 11 ++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/backend/src/api/handlers/handlers.go b/backend/src/api/handlers/handlers.go index 2290f46a..f0aab3c0 100644 --- a/backend/src/api/handlers/handlers.go +++ b/backend/src/api/handlers/handlers.go @@ -127,6 +127,12 @@ func CancelStartedServiceRequest(w http.ResponseWriter, r *http.Request) { return } + if status == models.NotStarted { + logger.Error("[CancelStartedServiceRequest] Unable to cancel service request as execution has not been started", nil) + JSONError(w, handlermodels.NewHttpError(errors.New("service request execution has not been started")), http.StatusBadRequest) + return + } + // TODO: implement cancellation of sr err = database.NewServiceRequest(client).UpdateStatus(requestId, models.Canceled) diff --git a/backend/src/database/models/service_request.go b/backend/src/database/models/service_request.go index b40e6a08..fd782c3f 100644 --- a/backend/src/database/models/service_request.go +++ b/backend/src/database/models/service_request.go @@ -9,11 +9,12 @@ import ( type ServiceRequestStatus string const ( - Pending ServiceRequestStatus = "Pending" - Running ServiceRequestStatus = "Running" - Success ServiceRequestStatus = "Success" - Failure ServiceRequestStatus = "Failure" - Canceled ServiceRequestStatus = "Canceled" + Pending ServiceRequestStatus = "Pending" + Running ServiceRequestStatus = "Running" + Success ServiceRequestStatus = "Success" + Failure ServiceRequestStatus = "Failure" + Canceled ServiceRequestStatus = "Canceled" + NotStarted ServiceRequestStatus = "Not Started" ) type ServiceRequestModel struct { From a1ec6496fd146b93e8ac6c82e2c493dfb320f75b Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Fri, 2 Feb 2024 18:30:27 +0800 Subject: [PATCH 07/10] fix: added status check to update --- backend/src/api/handlers/handlers.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/backend/src/api/handlers/handlers.go b/backend/src/api/handlers/handlers.go index f0aab3c0..5646043d 100644 --- a/backend/src/api/handlers/handlers.go +++ b/backend/src/api/handlers/handlers.go @@ -170,6 +170,17 @@ func UpdateServiceRequest(w http.ResponseWriter, r *http.Request) { } vars := mux.Vars(r) requestId := vars["requestId"] + status, err := database.NewServiceRequest(client).GetStatus(requestId) + if err != nil { + logger.Error("[UpdateServiceRequest] Error getting service request status", map[string]interface{}{"err": err}) + JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) + return + } + if status != models.NotStarted { + logger.Error("[UpdateServiceRequest] Unable to update service request as service request has been started", nil) + JSONError(w, handlermodels.NewHttpError(errors.New("service request has been started")), http.StatusBadRequest) + return + } res, err := database.NewServiceRequest(client).UpdateById(requestId, srm) if err != nil { logger.Error("[UpdateServiceRequest] Error updating service request", map[string]interface{}{"err": err}) From 577eafff493478d52ef5838f8bad2c5feadb12d6 Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Sat, 3 Feb 2024 14:36:17 +0800 Subject: [PATCH 08/10] fix: changed update sr handler and commented out update sr function pending the finalisation of the data form --- backend/src/api/handlers/handlers.go | 6 +----- backend/src/database/service_request.go | 27 ++++++++++++++----------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/backend/src/api/handlers/handlers.go b/backend/src/api/handlers/handlers.go index 5646043d..77c67bd6 100644 --- a/backend/src/api/handlers/handlers.go +++ b/backend/src/api/handlers/handlers.go @@ -156,11 +156,7 @@ func UpdateServiceRequest(w http.ResponseWriter, r *http.Request) { JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } - srm := &dbmodels.ServiceRequestModel{ - CreatedOn: time.Now(), - LastUpdated: time.Now(), - Status: dbmodels.Pending, - } + srm := &dbmodels.ServiceRequestModel{} err = json.NewDecoder(r.Body).Decode(srm) w.Header().Set("Content-Type", "application/json") if err != nil { diff --git a/backend/src/database/service_request.go b/backend/src/database/service_request.go index 85a12f6f..088c35e1 100644 --- a/backend/src/database/service_request.go +++ b/backend/src/database/service_request.go @@ -43,19 +43,22 @@ func (sr *ServiceRequest) DeleteById(id string) (*mongo.DeleteResult, error) { } func (sr *ServiceRequest) UpdateById(id string, srm *models.ServiceRequestModel) (*mongo.UpdateResult, error) { - objectId, _ := primitive.ObjectIDFromHex(id) - filter := bson.M{"_id": objectId} - update := bson.M{"$set": bson.M{ - "pipeline_id": srm.PipelineId, - "pipeline_version": srm.PipelineVersion, - "last_updated": srm.LastUpdated, - "remarks": srm.Remarks}} + // TODO: update the method once form data is finalised + // objectId, _ := primitive.ObjectIDFromHex(id) + // filter := bson.M{"_id": objectId} + // update := bson.M{"$set": bson.M{ + // "pipeline_id": srm.PipelineId, + // "pipeline_version": srm.PipelineVersion, + // "last_updated": srm.LastUpdated, + // "remarks": srm.Remarks}} - res, err := sr.c.Database(DatabaseName).Collection("service_requests").UpdateOne(context.Background(), filter, update) - if err != nil { - return nil, err - } - return res, nil + // res, err := sr.c.Database(DatabaseName).Collection("service_requests").UpdateOne(context.Background(), filter, update) + // if err != nil { + // return nil, err + // } + // return res, nil + + return nil, nil } func (sr *ServiceRequest) GetAll() ([]*models.ServiceRequestModel, error) { From 67137f64faa42a06a484f6ec92d2bc22d2c226d1 Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Sat, 3 Feb 2024 14:38:20 +0800 Subject: [PATCH 09/10] refactor: deleted delete sr by id method --- backend/src/database/service_request.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/backend/src/database/service_request.go b/backend/src/database/service_request.go index 088c35e1..4e316a63 100644 --- a/backend/src/database/service_request.go +++ b/backend/src/database/service_request.go @@ -33,15 +33,6 @@ func (sr *ServiceRequest) GetById(id string) (*models.ServiceRequestModel, error return srm, nil } -func (sr *ServiceRequest) DeleteById(id string) (*mongo.DeleteResult, error) { - objectId, _ := primitive.ObjectIDFromHex(id) - res, err := sr.c.Database(DatabaseName).Collection("service_requests").DeleteOne(context.Background(), bson.M{"_id": objectId}) - if err != nil { - return nil, err - } - return res, nil -} - func (sr *ServiceRequest) UpdateById(id string, srm *models.ServiceRequestModel) (*mongo.UpdateResult, error) { // TODO: update the method once form data is finalised // objectId, _ := primitive.ObjectIDFromHex(id) From d97d690136cee57c35648e25541e309ecf4d7843 Mon Sep 17 00:00:00 2001 From: Zheng_Zhi_Qiang Date: Sat, 3 Feb 2024 15:00:16 +0800 Subject: [PATCH 10/10] refactor: removed get sr status by id method and refactor affected methods --- backend/src/api/handlers/handlers.go | 13 +++++++------ backend/src/database/service_request.go | 10 +--------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/backend/src/api/handlers/handlers.go b/backend/src/api/handlers/handlers.go index 77c67bd6..f2fce67d 100644 --- a/backend/src/api/handlers/handlers.go +++ b/backend/src/api/handlers/handlers.go @@ -113,14 +113,14 @@ func CancelStartedServiceRequest(w http.ResponseWriter, r *http.Request) { } vars := mux.Vars(r) requestId := vars["requestId"] - status, err := database.NewServiceRequest(client).GetStatus(requestId) - w.Header().Set("Content-Type", "application/json") + sr, err := database.NewServiceRequest(client).GetById(requestId) if err != nil { - logger.Error("[CancelStartedServiceRequest] Error getting service request status", map[string]interface{}{"err": err}) + logger.Error("[CancelStartedServiceRequest] Error getting service request", map[string]interface{}{"err": err}) JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } - + status := sr.Status + w.Header().Set("Content-Type", "application/json") if status != models.Pending && status != models.Running { logger.Error("[CancelStartedServiceRequest] Unable to cancel service request as execution has been completed", nil) JSONError(w, handlermodels.NewHttpError(errors.New("service request execution has been completed")), http.StatusBadRequest) @@ -166,12 +166,13 @@ func UpdateServiceRequest(w http.ResponseWriter, r *http.Request) { } vars := mux.Vars(r) requestId := vars["requestId"] - status, err := database.NewServiceRequest(client).GetStatus(requestId) + sr, err := database.NewServiceRequest(client).GetById(requestId) if err != nil { - logger.Error("[UpdateServiceRequest] Error getting service request status", map[string]interface{}{"err": err}) + logger.Error("[UpdateServiceRequest] Error getting service request", map[string]interface{}{"err": err}) JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError) return } + status := sr.Status if status != models.NotStarted { logger.Error("[UpdateServiceRequest] Unable to update service request as service request has been started", nil) JSONError(w, handlermodels.NewHttpError(errors.New("service request has been started")), http.StatusBadRequest) diff --git a/backend/src/database/service_request.go b/backend/src/database/service_request.go index 4e316a63..d1ecaa19 100644 --- a/backend/src/database/service_request.go +++ b/backend/src/database/service_request.go @@ -74,12 +74,4 @@ func (sr *ServiceRequest) UpdateStatus(id string, status models.ServiceRequestSt _, err = sr.c.Database(DatabaseName).Collection("service_requests").UpdateOne( context.Background(), bson.M{"_id": objectId}, bson.M{"$set": bson.M{"status": status}}) return err -} - -func (sr *ServiceRequest) GetStatus(id string) (status models.ServiceRequestStatus, err error) { - srm, err := sr.GetById(id) - if err != nil { - return "", err - } - return srm.Status, nil -} +} \ No newline at end of file