Insert data to link table
Updated at 1691742532000Overview
The ezy_links
table is used to store URLs, typically used to provide functionality related to articles and SEO. You can perform this in two ways:
- Use the
LinkService
class to immediately add a record. - Create an appender class to periodically check and add records.
Using the LinkService Class
If you want to immediately add a record to the ezy_links
table, you can use the pre-existing LinkService
class in EzyPlatform and call the saveLink
method. In the example below, I am developing a feature to create a post, and I want to immediately add a record to the ezy_links
table:
public class PostService { private final LinkService linkService; // ... public PostService( LinkService linkService, // ... ) { this.linkService = linkService; // ... } private void savePostLink(String slug, Post post) { if (PostStatus.of(post.getStatus()) != PostStatus.PUBLISHED) { return; } linkService.saveLink( SaveLinkModel.builder() .linkType(post.getPostType()) .linkUri( postLinkExtractor.extract( new EzyPair<>(post.getPostType(), slug) ) ) .description("Created automatically by ezyarticle") .linkImageId(post.getFeaturedImageId()) .build() ); } }
Creating an Appender Class
There are times when you don't want or can't immediately add data to the ezy_links
table. For example, when upgrading your product and you don't want to modify the source code, you can create an appender class to periodically check and add data to the ezy_links
table. To create an appender class, you can inherit from one of the following classes:
AdminLastUpdatedAtLinkAppender
: If your data contains information about updated timestamps.AdminAuthorLinkAppender
: If you want to include author information in theezy_links
table; in reality, author information is retrieved from theezy_users
table.AdminLinkAppender
: If your data contains information other thanupdated_at
to determine if there's a new update.
Inheriting AdminLastUpdatedAtLinkAppender
Suppose you have a table named ezyweb_projects
with a field updated_at
as follows:
CREATE TABLE IF NOT EXISTS ezyweb_projects ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `name` varchar(128) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`), INDEX `index_created_at` (`created_at`), INDEX `index_updated_at` (`updated_at`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
You can create an appender class to save data to the ezy_links
table as follows:
@EzySingleton public class AdminEzyWebProjectLinkAppender extends AdminLastUpdatedAtLinkAppender<Project> { private final AdminProjectRepository projectRepository; public AdminEzyWebProjectLinkAppender( ClockProxy clock, ObjectMapper objectMapper, AdminSettingService settingService, AdminLinkRepository linkRepository, AdminProjectRepository projectRepository ) { super(clock, objectMapper, settingService, linkRepository); this.projectRepository = projectRepository; } @Override protected List<Project> getValueList(LastUpdatedAtPageToken pageToken) { return pageToken.isFetchGreaterThanOrEquals() ? projectRepository.findReleasedProjectsByLastUpdatedAtGte( pageToken.getUpdatedAt(), Next.limit(pageToken.getLimit()) ) : projectRepository.findReleasedProjectsByLastUpdatedAtGt( pageToken.getUpdatedAt(), Next.limit(pageToken.getLimit()) ); } @Override protected Link toLink(Project value) { Link link = new Link(); link.setLinkType("PROJECT"); link.setLinkUri("/market/items/" + value.getName()); link.setImageId(value.getBannerImageId()); link.setDescription(value.getDisplayName()); return link; } @Override protected LocalDateTime extractLastUpdatedAt(Project value) { return value.getUpdatedAt(); } @Override protected String getAppenderNamePrefix() { return "ezylink_project"; } } @EzyRepository public interface AdminProjectRepository extends EzyDatabaseRepository<Long, Project> { @EzyQuery( "SELECT e FROM Project e " + "WHERE e.status = 'RELEASED' AND e.updatedAt > ?0 " + "ORDER BY e.updatedAt ASC" ) List<Project> findReleasedProjectsByLastUpdatedAtGt( LocalDateTime lastUpdatedAt, Next next ); @EzyQuery( "SELECT e FROM Project e " + "WHERE e.status = 'RELEASED' AND e.updatedAt >= ?0 " + "ORDER BY e.updatedAt ASC" ) List<Project> findReleasedProjectsByLastUpdatedAtGte( LocalDateTime lastUpdatedAt, Next next ); }
Inheriting AdminAuthorLinkAppender
Suppose you need to add URLs for users who are authors of published articles on your website. You can create an appender class inheriting AdminEzyWebAuthorLinkAppender
as follows:
@EzySingleton public class AdminEzyWebAuthorLinkAppender extends AdminAuthorLinkAppender { public AdminEzyWebAuthorLinkAppender( ClockProxy clock, ObjectMapper objectMapper, AdminSettingService settingService, AdminLinkRepository linkRepository, AdminUserRepository userRepository ) { super( clock, objectMapper, settingService, linkRepository, userRepository ); } }
Inheriting AdminLinkAppender
Suppose you want to add URLs for user roles to the ezy_links
table, but the ezy_user_roles
table below:
CREATE TABLE IF NOT EXISTS `ezy_user_roles` ( `role_id` bigint unsigned NOT NULL, `user_id` bigint unsigned NOT NULL, `created_at` datetime NOT NULL, PRIMARY KEY (`role_id`, `user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
Lacks the updated_at
field to determine whether a record has been updated. You can create an appender class inheriting AdminLinkAppender
and implement the following methods:
public class UserRoleLinkAppender extends AdminLinkAppender<UserRole, String> { public UserRoleLinkAppender( ClockProxy clock, ObjectMapper objectMapper, AdminSettingService settingService, AdminLinkRepository linkRepository ) { super(clock, objectMapper, settingService, linkRepository); } @Override protected List<UserRole> getValueList(String pageToken) { // need to implement } @Override protected String extractNewLastPageToken(List<UserRole> valueList, String currentLastPageToken) { // need to implement } @Override protected String getAppenderNamePrefix() { // need to implement } @Override protected String defaultPageToken() { // need to implement } @Override protected Class<String> pageTokenType() { // need to implement } @Override protected Link toLink(UserRole value) { // need to implement } }