Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: separated sr creation and starting of sr #52

Merged
merged 3 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions backend/src/api/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ func CreateServiceRequest(w http.ResponseWriter, r *http.Request) {
JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError)
return
}
logger.Info("[CreateServiceRequest] Successfully created service request", map[string]interface{}{"sr": srm})
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)
json.NewEncoder(w).Encode(srm)
}
Expand All @@ -122,13 +121,13 @@ func CancelStartedServiceRequest(w http.ResponseWriter, r *http.Request) {
}
status := sr.Status
w.Header().Set("Content-Type", "application/json")
if status != models.Pending && status != models.Running {
if status != dbmodels.Pending && status != dbmodels.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
}

if status == models.NotStarted {
if status == dbmodels.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
Expand Down Expand Up @@ -174,7 +173,7 @@ func UpdateServiceRequest(w http.ResponseWriter, r *http.Request) {
return
}
status := sr.Status
if status != models.NotStarted {
if status != dbmodels.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
Expand All @@ -189,6 +188,30 @@ func UpdateServiceRequest(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

func StartServiceRequest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
requestId := vars["requestId"]
client, err := client.GetMongoClient()
if err != nil {
logger.Error("[StartServiceRequest] Unable to get mongo client", map[string]interface{}{"err": err})
JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError)
return
}
srm, err := database.NewServiceRequest(client).GetById(requestId)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should check the current status of this service request. we don't want to start a NewServiceRequestEvent on an already started service request

if err != nil {
logger.Error("[StartServiceRequest] Unable retrieve service request", map[string]interface{}{"err": err})
JSONError(w, handlermodels.NewHttpError(errors.New("internal server error")), http.StatusInternalServerError)
return
}
if srm.Status != dbmodels.NotStarted {
logger.Error("[StartServiceRequest] Service request has already been started", nil)
JSONError(w, handlermodels.NewHttpError(errors.New("service request started")), http.StatusBadRequest)
return
}
event.FireAsync(events.NewNewServiceRequestEvent(srm))
w.WriteHeader(http.StatusOK)
}

func CreatePipeline(w http.ResponseWriter, r *http.Request) {
pipeline := &dbmodels.PipelineModel{
CreatedOn: time.Now(),
Expand Down
1 change: 1 addition & 0 deletions backend/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
r.HandleFunc("/api/service_request", handlers.CreateServiceRequest).Methods("POST").Headers("Content-Type", "application/json")
r.HandleFunc("/api/service_request/{requestId}", handlers.GetServiceRequest).Methods("GET")
r.HandleFunc("/api/service_request", handlers.GetAllServiceRequest).Methods("GET")
r.HandleFunc("/api/service_request/{requestId}/start", handlers.StartServiceRequest).Methods("GET")
r.HandleFunc("/api/service_request/{requestId}/approve", handlers.ApproveServiceRequest).Methods("POST").Headers("Content-Type", "application/json")
r.HandleFunc("/api/service_request/{requestId}/cancel", handlers.CancelStartedServiceRequest).Methods("GET")
r.HandleFunc("/api/service_request/{requestId}", handlers.UpdateServiceRequest).Methods("PATCH").Headers("Content-Type", "application/json")
Expand Down
Loading