This project is an example of how to use Attachment
model and PrivateContentService
for managing files and user uploads.
It extends api services provided by @themost/test
package at https://github.com/themost-framework/test
Clone this repository and install dependencies:
git clone https://github.com/themost-framework/content-service-example
cd content-service-example
npm install
Start the application by running the following command:
npm start
Use postman collection at test/themost-test.postman_collection.json
for testing application services
IMPORTANT NOTE: @themost/test
package recycles the database on each test run.
This example extends test api services and adds 3 new models:
This model represents a file attachment and it is used for storing file metadata. It has the following properties:
id
- the unique identifier of the attachmentname
- the name of the attachmentdescription
- the description of the attachmentcontentType
- the mime type of the attachmentsize
- the size of the attachmenturl
- the url of the attachmentversion
- the version of the attachmentthumbnail
- the url of the thumbnail of the attachmentdateCreated
- the date and time that the attachment was createddateModified
- the date and time that the attachment was modifieddatePublished
- the date and time that the attachment was publishedpublished
- a boolean value which indicates whether the attachment is published or notkeywords
- a collection which contains keywords for the attachmentattachmentType
- the type of the attachment (e.g.image
,video
,audio
,document
,archive
,other
)owner
- a user which is the owner of this attachmentcreatedBy
- a user which created this attachmentmodifiedBy
- a user which modified this attachment
This model represents a type of attachment. It has the following properties:
id
- the unique identifier of the attachment typename
- the name of the attachment typedescription
- the description of the attachment typealternateName
- the alternate name of the attachment type which is also used as a keydescription
- the description of the attachment typedateCreated
- the date and time that the attachment type was createddateModified
- the date and time that the attachment type was modifiedcreatedBy
- a user which created this attachmentmodifiedBy
- a user which modified this attachment
This model represents a customer order and inherits from Order
model provided by @themost/test
package. It has the following properties:
id
- the unique identifier of the ordercustomer
- the customer which placed this orderdateCreated
- the date and time that the order was createddateModified
- the date and time that the order was modifiedcreatedBy
- a user which created this ordermodifiedBy
- a user which modified this orderorderedItem
- the ordered item e.g. a productorderNumber
- the order numberorderStatus
- the status of the orderpaymentMethod
- the payment method of the orderattachments
- a collection of attachments which are associated with this order
ExtraSchemaLoader
is being for loading extra data models during startup.
// now try to embed a new schema loader which adds a collection of models
// get application configuration
const configuration = app.getConfiguration();
/**
* Get the default schema loader strategy
* @type {import('@themost/data').DefaultSchemaLoaderStrategy}
*/
const schemaLoader = configuration.getStrategy(SchemaLoaderStrategy);
// extend DefaultSchemaLoaderStrategy.loaders collection and add our extra schema loader
// which loads models from config/models folder
schemaLoader.loaders.push(
new ExtraSchemaLoader(configuration)
);
PrivateContentService
is being used for managing content uploaded by end users. This service inherits from ApplicationService
and it's being registered also during startup.
// register private content service
app.useService(PrivateContentService);
It extends service router by adding a new route /api/content/private/:file
for downloading files
- Get access token
curl --location 'http://localhost:3000/auth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=alexis.rees@example.com' \
--data-urlencode 'password=secret' \
--data-urlencode 'client_id=9165351833584149' \
--data-urlencode 'client_secret=hTgqFBUhCfHs/quf/wnoB+UpDSfUusKA' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'scope=profile'
- Create new order
curl --location 'http://localhost:3000/api/CustomerOrders' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <access token>' \
--data-raw '{
"orderedItem": {
"name": "Apple MacBook Air (13.3-inch, 2013 Version)"
},
"customer": {
"email": "eric.stokes@example.com"
}
}'
- Add attachment
curl --location 'http://localhost:3000/api/CustomerOrders/811/AddAttachment' \
--header 'Authorization: Bearer <access token>' \
--form 'file=@"/tmp/lorem-ipsum.pdf"' \
--form 'attachmentType[alternateName]="Other"'
- Download attachment
curl --location 'http://localhost:3000/api/content/private/5JXFsvwnxNJf' \
--header 'Authorization: Bearer <access token>'