How to create a blog project in D language
This article introduces the knowledge of "how to create a blog project in D language". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. First, pull the skeleton project as the basis, and the file is named myprogect
Git clone https://github.com/huntlabs/hunt-skeleton.git myprojectcd myprojectdub run-v
two。 Modify the relevant configuration of the database in the application.conf file under config in the project
Hunt.database.default.driver=mysqlhunt.database.default.host=127.0.0.1hunt.database.default.port=3306 / / Interface hunt.database.default.database=myblog / / Database name hunt.database.default.username=root / / Database user name hunt.database.default.password= / / Database password hunt.database.default.charset=utf8 hunt.database.default.prefix=hunt.database.default.enabled=true
3. First of all, realize the Blog project background management; determine the project background function and create an admin.routes file under config to define the route
GET / user/list user.users / / user list page * / user/add user.add / / user add operation GET / user/delete user.remove / / user delete operation GET / user/update user.update / / user modification page POST / user/doup user.doupdate / / user modification action GET / blog/list blog.blogs / / article list page * / blog/add blog.add / / article Add page POST / blog/doadd/ blog/doadd/ / article add action
4. Build a Blog module under source--app, create a new model model in Blog, and create Blog.d and User.d files under model to store the data in the corresponding data table.
/ / Blog.dmodule app.blog.model.Blog; import hunt.entity;import app.model.User;@Table ("blog") class Blog: Model {mixin MakeModel; @ AutoIncrement @ PrimaryKey int id; / / Primary key, auto-increment long created; / / time string title; / / article title string description; / / description string picture; / / picture string content / / content @ OneToOne () @ JoinColumn ("user_id") User user;// associate User table} / / User.dmodule app.blog.model.User;import hunt.entity;@Table ("user") class User through user_id: Model {mixin MakeModel; @ AutoIncrement @ PrimaryKey int id; string username; / / username string password; / / password string email; / / mailbox}
5. Based on model, create new repository files under source--app--blog, create corresponding BlogRepository.d and UserRepository.d files, and manipulate data
/ / BlogRepository.dmodule app.blog.repository.BlogRepository;import app.blog.model.Blog; import hunt.entity;class BlogRepository: EntityRepository! (Blog, int) {} / / UserRepository.dmodule app.blog.repository.UserRepository;import app.blog.model.User; import hunt.entity;class UserRepository: EntityRepository! (User,int) {}
6. Create a new admin file under source--app--blog, and create BlogController.d and UserController.d in admin to achieve background management, including the management of articles and browsing, adding, deleting and modifying of users.
/ / BlogController.dmodule app.blog.controller.admin.BlogController;import hunt.framework;import app.blog.form.BlogForm;import app.blog.model.Blog;import app.blog.repository.BlogRepository;import app.blog.repository.UserRepository;class BlogController: Controller {mixin MakeController; / / add @ Action string add (BlogForm form) {if (request.method = = HttpMethod.POST) {auto result = form.valid () If (result.isValid) {auto blog = new Blog; blog.title = form.tit; blog.description = form.dept; blog.picture = form.pic; blog.content = form.cont; blog.created = time () Auto repo = new BlogRepository; repo.save (blog); return "successed!";} view.assign ("messages", result.messages ());} view.assign ("blog", (form is null)? New BlogForm: form); view.assign ("users", (new UserRepository). FindAll ()); / find out all User information return view.render ("blog_add");} / / articles list browse @ Action string blogs () {auto repo = new BlogRepository; auto blogs = repo.findAll (); / / find all article information view.assign ("blogs", blogs) Return view.render ("blog_list"); / / Jump to the list page}} / / UserController.dmodule app.blog.controller.admin.UserController;import hunt.framework;import app.blog.form.UserForm;import app.blog.model.User;import app.blog.repository.UserRepository;class UserController: Controller {mixin MakeController / / users add @ Action string add (UserForm form) {if (request.method = = HttpMethod.POST) {auto result = form.valid (); if (result.isValid) {auto user = new User; user.username = form.um; user.password = form.pwd User.email = form.mail; auto repo = new UserRepository; repo.save (user); return "successed";} view.assign ("messages", result.messages ());} view.assign ("user", (form is null)? New UserForm: form); return view.render ("user_add");} / / user list browsing @ Action string users () {auto repo = new UserRepository; auto users = repo.findAll (); view.assign ("users", users); return view.render ("user_list") } / / user delete operation @ Action string remove (int id) {auto repo = new UserRepository; repo.removeById (id); / / delete user information return "deleted" according to id;} / / user modification list @ Action string update (int id) {auto repo = new UserRepository; auto user = repo.find (id) / / find the information to be modified through id view.assign ("user", user); return view.render ("user_up"); / / Jump to the modification page} / / user modification action @ Action string doupdate (UserForm form) {auto repo = new UserRepository; auto user = repo.find (form.id.toggint); user.username = form.um User.password = form.pwd; user.email = form.mail;// to modify repo.save (user); / / Save return "successed";}}
7. Create a new admin under view, and create a new header.html, footer.html, sidebar.html display list and directory CSS style in admin; blog_list.html, blog_add.html, user_add.html, user_list.html, user_up.html to achieve the function list; display part of the code
/ blog_list.html title name description picture content {% for blog in blogs%} {{blog.title}} {{blog.user.username}} {{blog.description}} {blog.picture}} {{blog.content}} Read Full {% endfor%} title name description picture content / / blog_add.html {% if messages%} {% for key Message in messages%} {{key}}: {{message} {% endfor%} {% endif%} title name-- Please select-- {% for user in users%} {{user.username}} {% endfor%} Description picture content add article / / user_add.html {% if messages%} {% for key Message in messages%} {{key}}: {{message}} {% endfor%} {% endif%} Username Password Email address Add user / / user_list.html username email Delete Update {% for user in users%} {{user.username}} {{user.email}} Delete Update {% endfor%} username email Delete Update / / user_up.html
Username:
Password:
Email:
8. Create a new form file under source--app--blog, create form form BlogForm.d and UserForm.d files, which are consistent with the field values of name in the template page, and define the range of fields.
/ / BlogForm.dmodule app.blog.form.BlogForm;import hunt.framework;class BlogForm: Form {mixin MakeForm; string id; string uid; / / the value of user_id @ NotEmpty / / name associated with the User table cannot be empty string nm; @ Length (6, title 18) / / title is between 6 and 18 digits string tit; @ Length (10100) / / description is between 10 and 100 digits string dept; string pic / / Picture @ Length (10200) / / content values between 10 and 200 digits string cont;} / / UserForm.dmodule app.blog.form.UserForm;import hunt.framework;class UserForm: Form {mixin MakeForm; string id; @ Length (3,18) / / username values between 3 and 18 digits string um; @ Length (6,16) / / password values between 6 and 16 digits string pwd @ Length (6,32) / / the value of the mailbox is between 6 and 32 bits string mail;}
9. Conduct the foreground management of the Blog project; determine the functions that the foreground can achieve, and define the route in the routes under config
GET / add blog.add / / article add page POST / blog/add blog.doadd / / article add action GET / blog/delete blog.remove / / article delete operation GET / blog/list blog.blogs / / article list browse page POST / blog/doup blog.doupdate / / article modification operation GET / blog/update blog.update / / article modification page
10. Create BlogController.d files under source--app--controller to achieve Blog foreground management, including browsing, adding, modifying and deleting functions.
Module app.blog.controller.BlogController;import app.blog.form.BlogForm;import app.blog.model.Blog;import app.blog.repository.BlogRepository;import hunt.framework;class BlogController: Controller {mixin MakeController; / / articles add @ Action string add () / Jump to add page to add {return view.render ("blog_add");} @ Action string doadd (BlogForm form) / / add action {auto blog = new Blog Blog.title = form.tit; blog.description = form.dept; blog.picture = form.pic; blog.content = form.cont; blog.created = time (); / / time is the current time auto repo = new BlogRepository; repo.save (blog); / / Save return "successed!" } / / list of articles @ Action string blogs () {auto repo = new BlogRepository; auto blogs = repo.findAll (); / / find all the article information view.assign ("blogs", blogs); return view.render ("blog_list"); / / Jump to the list page} / / delete @ Action string remove (int id) {auto repo = new BlogRepository Repo.removeById (id); / / delete the article information return "deleted" via id;} / / modify @ Action string update (int id) / / modify the page {auto repo = new BlogRepository; auto blog = repo.find (id); / / find the article information view.assign ("blog", blog) that needs to be modified through id. Return view.render ("blog_up"); / / Jump to the modification page} @ Action string doupdate (BlogForm form) / / modify operation {auto repo = new BlogRepository; auto blog = repo.find (form.id.toggint); blog.title = form.tit; blog.description = form.dept; blog.picture = form.pic; blog.content = form.cont / / modify repo.save (blog); / / Save return "successed";}}
11. Create footer.html,header.html in default under views to store CSS styles in template pages. Blog_add.html and blog_up.html,blog_list.html display blog add, modify, and list template pages, respectively.
/ blog_list.html {% include "header.html"%} Page Heading Secondary Text {% for blog in blogs%}
{{blog.title}} {{blog.description}}
Read More → Posted on {{date ("Mmurd H:i:s", blog.created)}} {{blog.user.username}} {% endfor%} {% include "footer.html"%} / / blog_add.html
Name:
Title:
Description:
Picture:
Content:
/ / blog_up.html
Name:
Title:
Description:
Picture:
Content:
This is the end of "how to create a blog project in D language". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!