Blog platform based on Spring Boot and Vue3: user management, article review and management, column review and management, data statistics and analysis module

Table of contents

1. User Management

1. Query user list

2. Modify user information

2. Article review and management

1. Query the list of articles to be reviewed

2. Review articles

3. Column review and management

1. Query the list of columns to be reviewed

2. Review column

4. Data statistics and analysis

1. User registration statistics

2. Article publishing statistics

5. Comment management and review

1. Query the comment list

3. Delete comments

6. Website Settings and Configuration


In our blog platform, administrators need to handle functions such as user management, article review and management, column review and management, and data statistics and analysis. This article describes how to implement these functions.

1. User Management

1. Query user list

The administrator can view all user information, including user name, email address, registration time, etc. We UserControllercan add a getUsersmethod to query the list of users.

@GetMapping("/admin/users")
@PreAuthorize("hasRole('ADMIN')")
public Page<User> getUsers(Pageable pageable) {
    return userRepository.findAll(pageable);
}

2. Modify user information

The administrator can modify the user's role, status and other information. We can UserControlleradd a updateUsermethod to update user information.

@PutMapping("/admin/users/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> updateUser(@PathVariable Long id, @RequestBody UserUpdateRequest request) {
    Optional<User> optionalUser = userRepository.findById(id);
    if (!optionalUser.isPresent()) {
        return ResponseEntity.notFound().build();
    }

    User user = optionalUser.get();
    // 更新用户信息,如角色、状态等
    // ...

    userRepository.save(user);
    return ResponseEntity.ok().build();
}

2. Article review and management

1. Query the list of articles to be reviewed

Administrators need to review articles posted by users. We ArticleControllercan add a getPendingArticlesmethod to query the list of pending articles.

@GetMapping("/admin/pending-articles")
@PreAuthorize("hasRole('ADMIN')")
public Page<Article> getPendingArticles(Pageable pageable) {
    return articleRepository.findByStatus(ArticleStatus.PENDING, pageable);
}

2. Review articles

After an article is reviewed by an administrator, the article status can be changed to Published or Rejected. We ArticleControllercan add a reviewArticlemethod to update the article status.

@PutMapping("/admin/articles/{id}/review")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> reviewArticle(@PathVariable Long id, @RequestParam ArticleStatus status) {
    Optional<Article> optionalArticle = articleRepository.findById(id);
    if (!optionalArticle.isPresent()) {
        return ResponseEntity.notFound().build();
    }

    Article article = optionalArticle.get();
    article.setStatus(status);
    articleRepository.save(article);

    return ResponseEntity.ok().build();
}

3. Column review and management

1. Query the list of columns to be reviewed

Admins need to review user-created columns. We can add a method ColumnControllerin getPendingColumnsto query the list of pending columns.

@GetMapping("/admin/pending-columns")
@PreAuthorize("hasRole('ADMIN')")
public Page<Column> getPendingColumns(Pageable pageable) {
    return columnRepository.findByStatus(ColumnStatus.PENDING, pageable);
}

2. Review column

After an administrator reviews a column, they can change the status of the column to Published or Rejected. We ColumnControllercan add a reviewColumnmethod to update column state.

@PutMapping("/admin/columns/{id}/review")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> reviewColumn(@PathVariable Long id, @RequestParam ColumnStatus status) {
    Optional<Column> optionalColumn = columnRepository.findById(id);
    if (!optionalColumn.isPresent()) {
        return ResponseEntity.notFound().build();
    }

    Column column = optionalColumn.get();
    column.setStatus(status);
    columnRepository.save(column);

    return ResponseEntity.ok().build();
}

In this rewritten review column code, we first look up the column to be reviewed by ID. If the corresponding column cannot be found, a 404 status code is returned. If a pending column is found, we update its status to the status in the request (eg: published or rejected), and then save the updated column information. Finally, a 200 status code is returned to indicate that the operation was successful.

4. Data statistics and analysis

In order to help administrators better understand the operation of the platform, we can provide some data statistics and analysis functions. Here are some common statistical metrics:

1. User registration statistics

We can provide the function of counting the number of user registrations by day, week, and month. UserRepositoryAdd a method in to query the number of user registrations within a specified time range.

public interface UserRepository extends JpaRepository<User, Long> {
    // ... 其他方法 ...

    @Query("SELECT COUNT(u) FROM User u WHERE u.createdAt BETWEEN :startDate AND :endDate")
    long countUsersByDateRange(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);
}

Add  AdminController a method to get user registration statistics.

 
 
@GetMapping("/admin/stats/user-registrations")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> getUserRegistrationStats(
        @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
        @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
    long count = userRepository.countUsersByDateRange(startDate, endDate);
    return ResponseEntity.ok(count);
}

2. Article publishing statistics

We can provide the function of counting the number of published articles by day, week, and month. ArticleRepositoryAdd a method in to query the number of published articles in the specified time range.

public interface ArticleRepository extends JpaRepository<Article, Long> {
    // ... 其他方法 ...

    @Query("SELECT COUNT(a) FROM Article a WHERE a.status = 'PUBLISHED' AND a.createdAt BETWEEN :startDate AND :endDate")
    long countPublishedArticlesByDateRange(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);
}

Add  AdminController a method to get article publishing statistics.

 
 
@GetMapping("/admin/stats/published-articles")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> getPublishedArticleStats(
        @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
        @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
    long count = articleRepository.countPublishedArticlesByDateRange(startDate, endDate);
    return ResponseEntity.ok(count);
}

In this way, we have realized the functions of user management, article review and management, column review and management, and data statistics and analysis in the administrator module. In actual projects, you can also add more management functions and statistical indicators as needed.

5. Comment management and review

Administrators also need to handle comment management and review, including functions such as viewing comment lists, reviewing comments, and deleting comments.

1. Query the comment list

We CommentControllercan add a getCommentsmethod to query the list of comments.

@GetMapping("/admin/comments")
@PreAuthorize("hasRole('ADMIN')")
public Page<Comment> getComments(Pageable pageable) {
    return commentRepository.findAll(pageable);
}

1. Moderate comments

Admins can review comments and change their status to published or dismissed. We can CommentControlleradd a reviewCommentmethod to update the comment status.

@PutMapping("/admin/comments/{id}/review")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> reviewComment(@PathVariable Long id, @RequestParam CommentStatus status) {
    Optional<Comment> optionalComment = commentRepository.findById(id);
    if (!optionalComment.isPresent()) {
        return ResponseEntity.notFound().build();
    }

    Comment comment = optionalComment.get();
    comment.setStatus(status);
    commentRepository.save(comment);

    return ResponseEntity.ok().build();
}

3. Delete comments

Admins can delete inappropriate comments. We can CommentControlleradd a deleteCommentmethod to delete comments.

@DeleteMapping("/admin/comments/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> deleteComment(@PathVariable Long id) {
    Optional<Comment> optionalComment = commentRepository.findById(id);
    if (!optionalComment.isPresent()) {
        return ResponseEntity.notFound().build();
    }

    commentRepository.delete(optionalComment.get());
    return ResponseEntity.ok().build();
}

Through the above code, we have realized the functions of comment management and review. Admins can view the list of comments, moderate comments, and delete comments. According to actual needs, you can also add other management functions, such as replying to comments, viewing report records, etc.

So far, we have completed the main functional modules of the blog platform, including user modules, article modules, column modules, and administrator modules. You can continue to expand functions, optimize user experience, and create a fully functional and easy-to-use blogging platform.

6. Website Settings and Configuration

In order to allow administrators to better manage the website, we can provide some website setting and configuration functions, such as website title, description, filing information, etc. We can create an Settingsentity class to store the configuration information of the website.

@Entity
public class Setting {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String key;
    private String value;
    private String description;

    // getter 和 setter ...
}

SettingRepository Add related query methods in

public interface SettingRepository extends JpaRepository<Setting, Long> {
    Optional<Setting> findByKey(String key);
}

Add  AdminController a method to get the website settings.

 
 
@GetMapping("/admin/settings")
@PreAuthorize("hasRole('ADMIN')")
public List<Setting> getSettings() {
    return settingRepository.findAll();
}

Add a method for updating website settings.

@PutMapping("/admin/settings")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> updateSettings(@RequestBody List<Setting> settings) {
    for (Setting setting : settings) {
        Optional<Setting> optionalSetting = settingRepository.findByKey(setting.getKey());
        if (optionalSetting.isPresent()) {
            Setting storedSetting = optionalSetting.get();
            storedSetting.setValue(setting.getValue());
            settingRepository.save(storedSetting);
        }
    }

    return ResponseEntity.ok().build();
}

So far, we have implemented the website setting and configuration functions. Administrators can view and modify a site's settings to adjust the site's information as needed. At the same time, we can display these setting information on the front-end page to improve the user experience of the website.

Summarize

In this column, we introduce how to use modern technology stacks such as Spring Boot and Vue3 to build a blogging platform similar to CSDN. We have implemented multiple functional modules such as user experience and interaction design, article publishing and editing, article list display, article classification and tag management, article search and recommendation, article reading statistics, comments and replies, and column creation and management. At the same time, we also provide administrator management functions, including user management, article review and management, column review and management, data statistics and analysis, comment management and review, website setting and configuration, etc.

This column aims to help readers understand how to build a blog platform and the implementation methods of each functional module. I hope this column can provide you with some help in the process of learning and practice. Of course, there may be more functional requirements and technical challenges in actual projects, which require you to keep learning, exploring and practicing. I wish you go farther and farther on the road of technology!

Guess you like

Origin blog.csdn.net/m0_68036862/article/details/130103964