-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/implement update delete service request #47
Changes from 4 commits
0ca2664
ce846bc
8f5ee3b
796d651
88bee4f
e28e2ee
da6a6b3
a1ec649
577eaff
67137f6
d97d690
bacda7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,17 +52,20 @@ 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) | ||
requestId := vars["requestId"] | ||
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,20 +86,75 @@ 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) | ||
} | ||
|
||
func DeleteServiceRequest(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}) | ||
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) | ||
w.Header().Set("Content-Type", "application/json") | ||
if err != nil { | ||
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 | ||
} | ||
|
||
func UpdateServiceRequest(w http.ResponseWriter, r *http.Request) { | ||
client, err := client.GetMongoClient() | ||
if err != nil { | ||
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{ | ||
CreatedOn: time.Now(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should not update |
||
LastUpdated: time.Now(), | ||
Status: dbmodels.Pending, | ||
} | ||
err = json.NewDecoder(r.Body).Decode(srm) | ||
w.Header().Set("Content-Type", "application/json") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually why do we still set this here when you already set it inside the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I specify in case we need to return a response that is not an error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay. then maybe we need a new method for returning data |
||
if err != nil { | ||
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"] | ||
res, err := database.NewServiceRequest(client).UpdateById(requestId, srm) | ||
if err != nil { | ||
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) | ||
} | ||
|
||
func CreatePipeline(w http.ResponseWriter, r *http.Request) { | ||
pipeline := &dbmodels.PipelineModel{ | ||
CreatedOn: time.Now(), | ||
|
@@ -102,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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,31 @@ func (sr *ServiceRequest) GetById(id string) (*models.ServiceRequestModel, error | |
return srm, nil | ||
} | ||
|
||
func (sr *ServiceRequest) DeleteById(id string) (*mongo.DeleteResult, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should remove this method |
||
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) { | ||
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}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is flawed actually. pipeline_id and version should never be updated. It's a fixed 1-to-1 mapping. The stuff that can be updated are the user parameters to the form input. None of these parameters you use are updatable. |
||
|
||
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 { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we won't allow for deletion. this is for logging purposes. but we can have a cancel. however, the cancellation logic will be much more involved and implemented at a later date.