Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.
Open
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
10 changes: 10 additions & 0 deletions gateway/services/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ var UploadRoutes = arbor.RouteCollection{
"/upload/blobstore/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole, models.BlobstoreRole}), middleware.IdentificationMiddleware).ThenFunc(UpdateBlob).ServeHTTP,
},
arbor.Route {
"PatchBlob",
"PATCH",
"/upload/blobstore/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(PatchBlob).ServeHTTP,
},
arbor.Route{
"GetBlob",
"GET",
Expand Down Expand Up @@ -102,3 +108,7 @@ func GetBlob(w http.ResponseWriter, r *http.Request) {
func DeleteBlob(w http.ResponseWriter, r *http.Request) {
arbor.DELETE(w, config.UPLOAD_SERVICE+r.URL.String(), InfoFormat, "", r)
}

func PatchBlob(w http.ResponseWriter, r *http.Request) {
arbor.PATCH(w, config.UPLOAD_SERVICE+r.URL.String(), InfoFormat, "", r)
}
30 changes: 30 additions & 0 deletions services/upload/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func SetupController(route *mux.Route) {

router.HandleFunc("/blobstore/", CreateBlob).Methods("POST")
router.HandleFunc("/blobstore/", UpdateBlob).Methods("PUT")
router.HandleFunc("/blobstore/", PatchBlob).Methods("PATCH")
router.HandleFunc("/blobstore/{id}/", GetBlob).Methods("GET")
router.HandleFunc("/blobstore/{id}/", DeleteBlob).Methods("DELETE")
}
Expand Down Expand Up @@ -196,6 +197,35 @@ func UpdateBlob(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(stored_blob)
}

/*
Endpoint to patch a blob
*/
func PatchBlob(w, http.ResponseWriter, r *http.Request) {
var blob models.Blob
json.NewDecoder(r.Body).Decode(&Blob)

if blob.ID == "" {
errors.WriteError(w, r, errors.InternalError("Must set an id for the blob.", "Must set an id for the blob."))
return
}

err := service.PatchBlob(blob)

if err != nil {
errors.WriteError(w, r, errors.InternalError(err.Error(), "Unable to create blob."))
return
}

stored_blob, err := service.GetBlob(blob.ID)

if err != nil {
errors.WriteError(w, r, errors.InternalError(err.Error(), "Unable to retrieve blob."))
return
}

json.NewEncoder(w).Encode(stored_blob)
}

/*
Endpoint to delete a blob
*/
Expand Down
41 changes: 41 additions & 0 deletions services/upload/service/upload_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,47 @@ func UpdateBlob(blob models.Blob) error {
return err
}

/*
Patches blob with the given id
*/
func PatchBlob(blob models.Blob) error {
blob_data, err_blob_data := GetBlob(blob.ID)
blob_updated_data := map[string]interface{}{}
blob_unupdated_data := map[string]interface{}{}
// Checks for condition if blob doesn't exist
if err_blob_data != nil {
return err_blob_data
}
// Block deals with updated data object
json_updated_data, err_json_updated_data := json.Marshal(blob_data.Data)
if err_json_updated_data != nil {
return err_json_updated_data
}
json.Unmarshal([]byte(json_updated_data), &blob_updated_data)

// Block deals with unupdated data object
json_unupdated_data, err_json_unupdated_data := json.Marshal(blob.Data)
if err_json_unupdated_data != nil {
return err_json_unupdated_data
}
json.Unmarshal([]byte(json_unupdated_data), &blob_unupdated_data)

//Replaces values of unupdated to updated data object
for blobDataKey := range blob_unupdated_data {
blob_updated_data[blobDataKey] = blob_unupdated_data[blobDataKey]
}
selector := database.QuerySelector {
"id": blob.ID
}
patched_blob_data := models.Blob {
ID: blob.ID
Data: blob_updated_data,
}

err = db.update("blobstore", selector, &patched_blob_data)
return err
}

/*
Deletes the blob with the given id
Returns the blob that was deleted
Expand Down