Skip to content

Commit

Permalink
Merge branch 'feature/dataset-publish' into cmd-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
eldeal committed Feb 22, 2018
2 parents 8afac51 + 41bb3d7 commit b5cad28
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 154 deletions.
91 changes: 37 additions & 54 deletions api/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,30 +311,18 @@ func (api *DatasetAPI) addDataset(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

dataset.State = models.CreatedState
dataset.ID = datasetID

var accessRights string
if dataset.Links != nil {
if dataset.Links.AccessRights != nil {
if dataset.Links.AccessRights.HRef != "" {
accessRights = dataset.Links.AccessRights.HRef
}
}
if dataset.Links == nil {
dataset.Links = &models.DatasetLinks{}
}

dataset.ID = datasetID
dataset.Links = &models.DatasetLinks{
Editions: &models.LinkObject{
HRef: fmt.Sprintf("%s/datasets/%s/editions", api.host, datasetID),
},
Self: &models.LinkObject{
HRef: fmt.Sprintf("%s/datasets/%s", api.host, datasetID),
},
}

if accessRights != "" {
dataset.Links.AccessRights = &models.LinkObject{
HRef: accessRights,
}
dataset.Links.Editions = &models.LinkObject{
HRef: fmt.Sprintf("%s/datasets/%s/editions", api.host, datasetID),
}

dataset.Links.Self = &models.LinkObject{
HRef: fmt.Sprintf("%s/datasets/%s", api.host, datasetID),
}

dataset.LastUpdated = time.Now()
Expand Down Expand Up @@ -379,12 +367,27 @@ func (api *DatasetAPI) putDataset(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()

if err := api.dataStore.Backend.UpdateDataset(datasetID, dataset); err != nil {
log.ErrorC("failed to update dataset resource", err, log.Data{"dataset_id": datasetID})
currentDataset, err := api.dataStore.Backend.GetDataset(datasetID)
if err != nil {
log.ErrorC("failed to find dataset", err, log.Data{"dataset_id": datasetID})
handleErrorType(datasetDocType, err, w)
return
}

if dataset.State == models.PublishedState {
if err := api.publishDataset(currentDataset, nil); err != nil {
log.ErrorC("failed to update dataset document to published", err, log.Data{"dataset_id": datasetID})
handleErrorType(versionDocType, err, w)
return
}
} else {
if err := api.dataStore.Backend.UpdateDataset(datasetID, dataset, currentDataset.Next.State); err != nil {
log.ErrorC("failed to update dataset resource", err, log.Data{"dataset_id": datasetID})
handleErrorType(datasetDocType, err, w)
return
}
}

setJSONContentType(w)
w.WriteHeader(http.StatusOK)
log.Debug("update dataset", log.Data{"dataset_id": datasetID})
Expand All @@ -404,7 +407,8 @@ func (api *DatasetAPI) putVersion(w http.ResponseWriter, r *http.Request) {
return
}

if err = api.dataStore.Backend.CheckDatasetExists(datasetID, ""); err != nil {
currentDataset, err := api.dataStore.Backend.GetDataset(datasetID)
if err != nil {
log.ErrorC("failed to find dataset", err, log.Data{"dataset_id": datasetID, "edition": edition, "version": version})
handleErrorType(versionDocType, err, w)
return
Expand Down Expand Up @@ -447,7 +451,7 @@ func (api *DatasetAPI) putVersion(w http.ResponseWriter, r *http.Request) {
}

// Pass in newVersion variable to include relevant data needed for update on dataset API (e.g. links)
if err := api.publishDataset(datasetID, newVersion); err != nil {
if err := api.publishDataset(currentDataset, newVersion); err != nil {
log.ErrorC("failed to update dataset document once version state changes to publish", err, log.Data{"dataset_id": datasetID, "edition": edition, "version": version})
handleErrorType(versionDocType, err, w)
return
Expand Down Expand Up @@ -591,37 +595,16 @@ func createNewVersionDoc(currentVersion *models.Version, version *models.Version
return version
}

func (api *DatasetAPI) publishDataset(id string, version *models.Version) error {
currentDataset, err := api.dataStore.Backend.GetDataset(id)
if err != nil {
log.ErrorC("unable to update dataset", err, log.Data{"dataset_id": id})
return err
}
func (api *DatasetAPI) publishDataset(currentDataset *models.DatasetUpdate, version *models.Version) error {
if version != nil {
currentDataset.Next.CollectionID = version.CollectionID

var accessRights string

if currentDataset.Next.Links != nil {
if currentDataset.Next.Links.AccessRights != nil {
accessRights = currentDataset.Next.Links.AccessRights.HRef
}
}

currentDataset.Next.CollectionID = version.CollectionID
currentDataset.Next.Links = &models.DatasetLinks{
AccessRights: &models.LinkObject{
HRef: accessRights,
},
Editions: &models.LinkObject{
HRef: fmt.Sprintf("%s/datasets/%s/editions", api.host, version.Links.Dataset.ID),
},
LatestVersion: &models.LinkObject{
currentDataset.Next.Links.LatestVersion = &models.LinkObject{
ID: version.Links.Version.ID,
HRef: version.Links.Version.HRef,
},
Self: &models.LinkObject{
HRef: fmt.Sprintf("%s/datasets/%s", api.host, version.Links.Dataset.ID),
},
}
}

currentDataset.Next.State = models.PublishedState
currentDataset.Next.LastUpdated = time.Now()

Expand All @@ -635,8 +618,8 @@ func (api *DatasetAPI) publishDataset(id string, version *models.Version) error
Next: currentDataset.Next,
}

if err := api.dataStore.Backend.UpsertDataset(id, newDataset); err != nil {
log.ErrorC("unable to update dataset", err, log.Data{"dataset_id": id})
if err := api.dataStore.Backend.UpsertDataset(currentDataset.ID, newDataset); err != nil {
log.ErrorC("unable to update dataset", err, log.Data{"dataset_id": currentDataset.ID})
return err
}

Expand Down
Loading

0 comments on commit b5cad28

Please sign in to comment.