Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.14.0] - 2024-10-08
### Added
- Meta_data dependancies [#125](https://github.com/rokwire/content-building-block/issues/125)

## [1.13.1] - 2024-10-07
### Fixed
- Fixed environment variable [#127](https://github.com/rokwire/content-building-block/issues/127)
Expand Down
4 changes: 2 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Patches for **Content Building Block** in this repository will only be applied t

| Version | Supported |
|----------| ------------------ |
| 1.13.1 | :white_check_mark: |
| < 1.13.1 | :x: |
| 1.14.0 | :white_check_mark: |
| < 1.14.0 | :x: |


## Reporting a Bug or Vulnerability
Expand Down
3 changes: 3 additions & 0 deletions core/interfaces/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type Services interface {
UpdateDataContentItem(claims *tokenauth.Claims, item *model.DataContentItem) (*model.DataContentItem, error)
DeleteDataContentItem(claims *tokenauth.Claims, key string) error
GetDataContentItems(claims *tokenauth.Claims, category string) ([]*model.DataContentItem, error)
CreateOrUpdateMetaData(key string, value map[string]interface{}) (*model.MetaData, error)
GetMetaData(key *string) (*model.MetaData, error)
DeleteMetaData(key string) error

CreateCategory(claims *tokenauth.Claims, item *model.Category) (*model.Category, error)
GetCategory(claims *tokenauth.Claims, name string) (*model.Category, error)
Expand Down
5 changes: 5 additions & 0 deletions core/interfaces/driven.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ type Storage interface {
FindCategory(appID *string, orgID string, name string) (*model.Category, error)
UpdateCategory(appID *string, orgID string, item *model.Category) (*model.Category, error)
DeleteCategory(appID *string, orgID string, key string) error

CreateMetaData(key string, value map[string]interface{}) (*model.MetaData, error)
FindMetaData(key *string) (*model.MetaData, error)
UpdateMetaData(item *model.MetaData, value map[string]interface{}) (*model.MetaData, error)
DeleteMetaData(key string) error
}

// Core BB interface
Expand Down
9 changes: 9 additions & 0 deletions core/model/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ type Category struct {
DateUpdated *time.Time `json:"date_updated,omitempty" bson:"date_updated,omitempty"`
Permissions []string `json:"permissions" bson:"permissions"`
} // @name Category

// MetaData defines a meta_data object
type MetaData struct {
ID string `json:"id" bson:"_id"`
Key string `json:"key" bson:"key"`
Value map[string]interface{} `json:"value" bson:"value"`
DateCreated time.Time `json:"date_created" bson:"date_created"`
DateUpdated *time.Time `json:"date_updated,omitempty" bson:"date_updated,omitempty"`
} // @name MetaData
39 changes: 39 additions & 0 deletions core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,45 @@ func (s *servicesImpl) GetDataContentItems(claims *tokenauth.Claims, category st
return item, nil
}

func (s *servicesImpl) CreateOrUpdateMetaData(key string, value map[string]interface{}) (*model.MetaData, error) {
var metaData *model.MetaData

findMetaData, err := s.app.storage.FindMetaData(&key)
if err != nil {
return nil, err
}
if findMetaData == nil {
metaData, err = s.app.storage.CreateMetaData(key, value)
if err != nil {
return nil, err
}
} else {
metaData, err = s.app.storage.UpdateMetaData(findMetaData, value)
if err != nil {
return nil, err
}
}

return metaData, nil
}

func (s *servicesImpl) GetMetaData(key *string) (*model.MetaData, error) {
item, err := s.app.storage.FindMetaData(key)
if err != nil {
return nil, err
}
return item, nil
}

func (s *servicesImpl) DeleteMetaData(key string) error {
err := s.app.storage.DeleteMetaData(key)
if err != nil {
return err
}

return nil
}

func (s *servicesImpl) CreateDataContentItem(claims *tokenauth.Claims, item *model.DataContentItem) (*model.DataContentItem, error) {

category, err := s.app.storage.FindCategory(&claims.AppID, claims.OrgID, item.Category)
Expand Down
68 changes: 68 additions & 0 deletions driven/storage/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,74 @@ func (sa *Adapter) StoreMultiTenancyData(appID string, orgID string) error {
return nil
}

// CreateMetaData creates meta_data object
func (sa *Adapter) CreateMetaData(key string, value map[string]interface{}) (*model.MetaData, error) {
now := time.Now()
id, _ := uuid.NewUUID()
item := model.MetaData{
ID: id.String(),
Key: key,
Value: value,
DateCreated: now,
}

_, err := sa.db.metaData.InsertOne(sa.context, &item)
if err != nil {
return nil, err
}

return &item, nil
}

// FindMetaData find meta_data object
func (sa *Adapter) FindMetaData(key *string) (*model.MetaData, error) {
filter := bson.D{primitive.E{Key: "key", Value: key}}

var result *model.MetaData
err := sa.db.metaData.FindOne(sa.context, filter, &result, nil)
if err != nil {
return nil, nil
}
return result, nil
}

// DeleteMetaData deletes meta_data object
func (sa *Adapter) DeleteMetaData(key string) error {
filter := bson.D{primitive.E{Key: "key", Value: key}}

result, err := sa.db.metaData.DeleteOne(sa.context, filter, nil)
if err != nil {
return err
}
if result == nil {
return fmt.Errorf("result is nil for meta_data with key %s", key)
}
deletedCount := result.DeletedCount
if deletedCount != 1 {
return fmt.Errorf("error occured while deleting a meta_data with key %s", key)
}
return nil
}

// UpdateMetaData updates a metaData
func (sa *Adapter) UpdateMetaData(item *model.MetaData, value map[string]interface{}) (*model.MetaData, error) {
filter := bson.D{
primitive.E{Key: "key", Value: item.Key}}
update := bson.D{
primitive.E{Key: "$set", Value: bson.D{
primitive.E{Key: "value", Value: value},
primitive.E{Key: "date_updated", Value: time.Now().UTC()},
}},
}
_, err := sa.db.metaData.UpdateOne(sa.context, filter, update, nil)
if err != nil {
log.Printf("error updating category: %s", err)
return nil, err
}

return item, nil
}

func (sa *Adapter) abortTransaction(sessionContext mongo.SessionContext) {
err := sessionContext.AbortTransaction(sessionContext)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions driven/storage/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type database struct {
contentItems *collectionWrapper
dataContentItems *collectionWrapper
categories *collectionWrapper
metaData *collectionWrapper

logger *logs.Logger
}
Expand Down Expand Up @@ -98,6 +99,12 @@ func (m *database) start() error {
return err
}

metaData := &collectionWrapper{database: m, coll: db.Collection("meta_data")}
err = m.applyMetaDataChecks(metaData)
if err != nil {
return err
}

//asign the db, db client and the collections
m.db = db
m.dbClient = client
Expand All @@ -107,6 +114,7 @@ func (m *database) start() error {
m.contentItems = contentItems
m.dataContentItems = dataContentItems
m.categories = categories
m.metaData = metaData

return nil
}
Expand Down Expand Up @@ -191,6 +199,19 @@ func (m *database) applyCategoriesChecks(categories *collectionWrapper) error {
return nil
}

func (m *database) applyMetaDataChecks(metaData *collectionWrapper) error {
log.Println("apply meta_data checks.....")

// Add key index
err := metaData.AddIndex(bson.D{primitive.E{Key: "key", Value: 1}}, false)
if err != nil {
return err
}

log.Println("meta_data checks passed")
return nil
}

// Event

func (m *database) onDataChanged(changeDoc map[string]interface{}) {
Expand Down
3 changes: 3 additions & 0 deletions driver/web/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func (we Adapter) Start() {
//TODO: add /files/upload/multipart for large file uploads
contentRouter.HandleFunc("/files/download", we.coreAuthWrapFunc(we.apisHandler.GetFileContentDownloadURLs, we.auth.coreAuth.standardAuth)).Methods("GET")
contentRouter.HandleFunc("/data", we.coreAuthWrapFunc(we.apisHandler.GetDataContentItems, we.auth.coreAuth.standardAuth)).Methods("GET")
contentRouter.HandleFunc("/meta-data", we.coreAuthWrapFunc(we.apisHandler.CreateOrUpdateMetaData, we.auth.coreAuth.standardAuth)).Methods("POST")
contentRouter.HandleFunc("/meta-data/{key}", we.coreAuthWrapFunc(we.apisHandler.GetMetaData, we.auth.coreAuth.standardAuth)).Methods("GET")
contentRouter.HandleFunc("/meta-data/{key}", we.coreAuthWrapFunc(we.apisHandler.DeleteMetaData, we.auth.coreAuth.standardAuth)).Methods("DELETE")

// handle student guide admin apis
adminSubRouter := contentRouter.PathPrefix("/admin").Subrouter()
Expand Down
111 changes: 110 additions & 1 deletion driver/web/docs/gen/def.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.3
info:
title: Rokwire Content Building Block API
description: Rokwire Content Block API Documentation
version: 1.13.1
version: 1.14.0
servers:
- url: 'https://api.rokwire.illinois.edu/content'
description: Production server
Expand Down Expand Up @@ -2891,6 +2891,99 @@ paths:
description: Unauthorized
'500':
description: Internal error
/meta_data:
post:
tags:
- Client
summary: Create meta data
description: |
Create meta data
security:
- bearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
required:
- key
- data
type: object
properties:
key:
type: string
value:
type: object
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/MetaData'
'400':
description: Bad request
'401':
description: Unauthorized
'500':
description: Internal error
'/meta_data/{key}':
get:
tags:
- Client
summary: Retrieves a health location by id
description: |
Retrieves a health location by id
security:
- bearerAuth: []
parameters:
- name: key
in: path
description: the key of the meta_data
required: false
style: simple
explode: false
schema:
type: string
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/MetaData'
'400':
description: Bad request
'401':
description: Unauthorized
'500':
description: Internal error
delete:
tags:
- Client
summary: Delete meta data object
description: |
Delete meta data object
security:
- bearerAuth: []
parameters:
- name: key
in: path
description: id
required: true
style: simple
explode: false
schema:
type: string
responses:
'200':
description: Success
'400':
description: Bad request
'401':
description: Unauthorized
'500':
description: Internal error
/files/upload:
get:
tags:
Expand Down Expand Up @@ -3155,6 +3248,22 @@ components:
type: string
app_id:
type: string
MetaData:
required:
- key
- data
type: object
properties:
id:
type: string
key:
type: string
value:
type: object
date_created:
type: string
date_updated:
type: string
DataContentItem:
type: object
properties:
Expand Down
8 changes: 6 additions & 2 deletions driver/web/docs/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.3
info:
title: Rokwire Content Building Block API
description: Rokwire Content Block API Documentation
version: 1.13.1
version: 1.14.0
servers:
- url: 'https://api.rokwire.illinois.edu/content'
description: Production server
Expand Down Expand Up @@ -110,7 +110,11 @@ paths:
/data/{key}:
$ref: "./resources/client/data-content-itemsids.yaml"
/files:
$ref: "./resources/client/file-content-items.yaml"
$ref: "./resources/client/file-content-items.yaml"
/meta_data:
$ref: "./resources/client/meta-data.yaml"
/meta_data/{key}:
$ref: "./resources/client/meta-data-id.yaml"
/files/upload:
$ref: "./resources/client/file-content-upload.yaml"
/files/download:
Expand Down
Loading