A concept about CMS
A concept and simple demo about Content Management System.
Criteria
- Think about an idea then implement this
- Understand Database first concept
- Implement RestAPI enpoints with logic handle in Controller layer
Source
https://github.com/duycs/demo-cms
Keywords
- ASP.NET Core
- Content management system (CMS)
- Database first
What is the CMS?
- A content management system (CMS) is a software application that can be used to manage the creation and modification of digital content.
- A content management system (CMS) typically has two major components: a content management application (CMA), as the front-end user interface that allows a user, even with limited expertise, to add, modify, and remove content from a website without the intervention of a webmaster; and a content delivery application (CDA), that compiles the content and updates the website.
I have an idea to create a structure CMS
If you a developer, you know about Object-oriented programming
- Object-oriented programming: is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).
Object as you know
-
In the class-based object-oriented programming paradigm, object refers to a particular instance of a class, where the object can be a combination of variables, functions, and data structures.
-
In relational database management, an object can be a table or column, or an association between data and a database entity (such as relating a person’s age to a specific person.
Example
- Example 1: Class Object
Here is a class object:
public class Person { public string Name { get; set; } public int Age { get; set; } }
or common keywords like that
access_modifier object_type object_name { access_modifier field_type filed_name field_value; }
- Example 2: Layout page One page layout have many fields, each field have label and value, these fileds are title, image banner, body…
So content in the layout, I mean CMS can be thought of as is an object
- From example about Class object and Layout page above, layout page same as an object, assumption all keyword as tables is single without relation:
access_modifier (I set name is permission table) object_type object_name field_type filed_name field_value
- After think about relation, we should have relation tables: object_name is one instance of many objects, so we have objects table with properties: object_type, permission_id, filed_id. filed_name is one instance of many fileds, so we have fields table with properties field_type, permission_id, field_value. One object have many fields, so we have object_fields table with properties: object_id, field_id.
A sample schema for this idea
- You can see cms.sql at this project folder, you use this file to generate your local database.
- Or, I have created a schema template for this idea, you can use MySQL Workbench to connect with this connection infomation: Server=mysql-6037-0.cloudclusters.net;port=10001;Database=cms;user=admin;password=abc@1234;
You can think different! You should create new design is better!
What is database first?
- This is an approach to create Entity data model, it creates model codes (classes, properties, DbContext etc.) from the existing database in the project.
Now after have an existing database, we will use database first approach
Setup your local development
- Net core 2.2
- Visual studio code
- MySQL
Steps
- Create new project ASP.NET Core
- Create a new folder for your project.
- At root folder add file global.json to use .netcore 2.2
{ "sdk": { "version": "2.2.100" } }
- At the folder, open Command/Terminal then run this command to create a template webapi project:
dotnet new webapi
- Add packageReferences EF core
- Add packageReference to file .csproj use CLI EF core: ```
- At the folder, open Command/Terminal then run this command to restore again:
dotnet restore
3. CLI generate Entities Models
- We have template CLI to scaffold:
dotnet ef dbcontext scaffold [ConnectionString] MySql.Data.EntityFrameworkCore -c [ContextName] -o [OutputFolder]
- At the folder, open Command/Terminal then run this command to create a models:
dotnet ef dbcontext scaffold “Server=mysql-6037-0.cloudclusters.net;port=10001;Database=cms;user=admin;password=abc@1234;CharSet=utf8;” MySql.Data.EntityFrameworkCore -c CmsContext -o Models -f
4. You will see Models folder be generated with class files
- A CmsContext class description all tables of schema and relation if have
- Classes corresponding to tables in the database schema
5. We will add some layer and code to use this CmsContext to working with data
### You can fork/clone this demo to run at your local
1. foke/clone this project
2. At the folder, open Command/Terminal then run this command to run it:
dotnet run ```
- Go to
http://localhost:5000
to view swagger enpoint, have CRUD of some objects:- Objects
- ObjectFields
- You can review code at Controller classes and attention to:
- Routing attribute: example
[Route("api/[controller]")]
and[HttpGet("{id}")]
is meaning router to/api/object-name/id
- Method action: example
HttpGet
,HttpPost
,HttpPut
orHttpDelete
with logic get, create, update or delete a record. - Status code examples:
BadRequest
: client sent wrong request,
NotFound
: not found object after query,
NoContent
: no content if want to return list after query,
Ok
: excute anything which logic successful, simple is find and return object existing.\ - Logic query use Context database and Linq syntax.
- Routing attribute: example
We have many enpoint don’t implement in the controller layer. Can you finish them?
- Fields
- FieldType
- ObjectType
- Permission
Referecnces
- https://docs.developers.optimizely.com/content-cloud/v12.0.0-content-cloud/docs/what-is-a-cms
- https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-scaffold-example.html
- https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx