feat: add project favorite api

This commit is contained in:
vran 2022-02-24 19:30:16 +08:00
parent 496534843a
commit f069d0bdc1
17 changed files with 841 additions and 0 deletions

View File

@ -25,6 +25,15 @@ public interface Routes {
String ADD_OR_REMOVE_SYS_OWNER = BASE + "/users/{userId}/sys_owners"; String ADD_OR_REMOVE_SYS_OWNER = BASE + "/users/{userId}/sys_owners";
} }
interface UserProject {
String LIST = BASE + "/user_projects/favorites";
String ADD = BASE + "/user_projects/favorites/{projectId}";
String REMOVE = BASE + "/user_projects/favorites/{projectId}";
}
interface Group { interface Group {
String LIST = BASE + "/groups"; String LIST = BASE + "/groups";

View File

@ -0,0 +1,55 @@
package com.databasir.api;
import com.databasir.api.config.security.DatabasirUserDetails;
import com.databasir.common.JsonData;
import com.databasir.core.domain.user.data.FavoriteProjectPageCondition;
import com.databasir.core.domain.user.data.FavoriteProjectPageResponse;
import com.databasir.core.domain.user.service.UserProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import static org.springframework.data.domain.Sort.Direction.DESC;
@RestController
@RequiredArgsConstructor
@Validated
public class UserProjectController {
private final UserProjectService userProjectService;
@GetMapping(Routes.UserProject.LIST)
public JsonData<Page<FavoriteProjectPageResponse>> listFavorites(
@PageableDefault(sort = "id", direction = DESC) Pageable pageable,
FavoriteProjectPageCondition condition) {
DatabasirUserDetails user = (DatabasirUserDetails) SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
Integer userId = user.getUserPojo().getId();
return JsonData.ok(userProjectService.listFavorites(pageable, userId, condition));
}
@PostMapping(Routes.UserProject.ADD)
public JsonData<Void> addFavorite(@PathVariable Integer projectId) {
DatabasirUserDetails user = (DatabasirUserDetails) SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
Integer userId = user.getUserPojo().getId();
userProjectService.addFavorites(projectId, userId);
return JsonData.ok();
}
@DeleteMapping(Routes.UserProject.REMOVE)
public JsonData<Void> removeFavorite(@PathVariable Integer projectId) {
DatabasirUserDetails user = (DatabasirUserDetails) SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
Integer userId = user.getUserPojo().getId();
userProjectService.removeFavorites(projectId, userId);
return JsonData.ok();
}
}

View File

@ -0,0 +1,22 @@
package com.databasir.core.domain.user.converter;
import com.databasir.core.domain.user.data.FavoriteProjectPageResponse;
import com.databasir.dao.tables.pojos.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper(componentModel = "spring")
public interface FavoriteProjectPageResponseConverter {
@Mapping(target = "projectId", source = "project.id")
@Mapping(target = "projectName", source = "project.name")
@Mapping(target = "projectDescription", source = "project.description")
@Mapping(target = "groupId", source = "group.id")
@Mapping(target = "groupName", source = "group.name")
@Mapping(target = "createAt", source = "favoriteProject.createAt")
FavoriteProjectPageResponse to(UserFavoriteProjectPojo favoriteProject,
ProjectPojo project,
DataSourcePojo dataSource,
ProjectSyncRulePojo projectSyncRule,
GroupPojo group);
}

View File

@ -0,0 +1,5 @@
package com.databasir.core.domain.user.data;
public class FavoriteProjectCreateRequest {
}

View File

@ -0,0 +1,26 @@
package com.databasir.core.domain.user.data;
import com.databasir.dao.Tables;
import lombok.Data;
import org.jooq.Condition;
import org.jooq.impl.DSL;
import java.util.ArrayList;
import java.util.List;
@Data
public class FavoriteProjectPageCondition {
private String projectNameContains;
public Condition toCondition(Integer userId) {
List<Condition> conditions = new ArrayList<>();
conditions.add(Tables.USER_FAVORITE_PROJECT.USER_ID.eq(userId));
if (projectNameContains != null && !projectNameContains.trim().equals("")) {
conditions.add(Tables.PROJECT.NAME.contains(projectNameContains));
}
return conditions.stream()
.reduce(Condition::and)
.orElse(DSL.trueCondition());
}
}

View File

@ -0,0 +1,29 @@
package com.databasir.core.domain.user.data;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class FavoriteProjectPageResponse {
private Integer projectId;
private String projectName;
private String projectDescription;
private Boolean isAutoSync;
private String autoSyncCron;
private Integer groupId;
private String groupName;
private String databaseType;
private String databaseName;
private LocalDateTime createAt;
}

View File

@ -0,0 +1,85 @@
package com.databasir.core.domain.user.service;
import com.databasir.core.domain.DomainErrors;
import com.databasir.core.domain.user.converter.FavoriteProjectPageResponseConverter;
import com.databasir.core.domain.user.data.FavoriteProjectPageCondition;
import com.databasir.core.domain.user.data.FavoriteProjectPageResponse;
import com.databasir.dao.impl.*;
import com.databasir.dao.tables.pojos.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class UserProjectService {
private final UserFavoriteProjectDao userFavoriteProjectDao;
private final ProjectDao projectDao;
private final ProjectSyncRuleDao projectSyncRuleDao;
private final DataSourceDao dataSourceDao;
private final GroupDao groupDao;
private final FavoriteProjectPageResponseConverter favoriteProjectPageResponseConverter;
public Page<FavoriteProjectPageResponse> listFavorites(Pageable pageable,
Integer userId,
FavoriteProjectPageCondition condition) {
var data = userFavoriteProjectDao.selectByCondition(pageable, condition.toCondition(userId));
// project data
var projectIdList = data.map(UserFavoriteProjectPojo::getProjectId).toList();
var projectPojos = projectDao.selectInIds(projectIdList);
var projectMapById = projectPojos.stream()
.collect(Collectors.toMap(ProjectPojo::getId, Function.identity()));
// dataSource data
var dataSourceMapByProjectId = dataSourceDao.selectInProjectIds(projectIdList)
.stream()
.collect(Collectors.toMap(DataSourcePojo::getProjectId, Function.identity()));
// project sync rule data
var projectSyncRuleMapByProjectId = projectSyncRuleDao.selectInProjectIds(projectIdList)
.stream()
.collect(Collectors.toMap(ProjectSyncRulePojo::getProjectId, Function.identity()));
// group data
var groupIdList = projectPojos.stream().map(ProjectPojo::getGroupId).collect(Collectors.toList());
var groupMapById = groupDao.selectInIds(groupIdList)
.stream()
.collect(Collectors.toMap(GroupPojo::getId, Function.identity()));
// response data
return data.map(favorite -> {
ProjectPojo project = projectMapById.get(favorite.getProjectId());
DataSourcePojo dataSource = dataSourceMapByProjectId.get(favorite.getProjectId());
ProjectSyncRulePojo projectSyncRule = projectSyncRuleMapByProjectId.get(favorite.getProjectId());
GroupPojo group = null;
if (project != null) {
group = groupMapById.get(project.getGroupId());
}
return favoriteProjectPageResponseConverter.to(favorite, project, dataSource, projectSyncRule, group);
});
}
@Transactional
public void addFavorites(Integer projectId, Integer userId) {
if (!projectDao.existsById(projectId)) {
throw DomainErrors.PROJECT_NOT_FOUND.exception();
}
if (!userFavoriteProjectDao.exists(userId, projectId)) {
userFavoriteProjectDao.insert(userId, projectId);
}
}
public void removeFavorites(Integer projectId, Integer userId) {
if (userFavoriteProjectDao.exists(userId, projectId)) {
userFavoriteProjectDao.delete(userId, projectId);
}
}
}

View File

@ -21,6 +21,7 @@ import com.databasir.dao.tables.TableDocument;
import com.databasir.dao.tables.TableIndexDocument; import com.databasir.dao.tables.TableIndexDocument;
import com.databasir.dao.tables.TableTriggerDocument; import com.databasir.dao.tables.TableTriggerDocument;
import com.databasir.dao.tables.User; import com.databasir.dao.tables.User;
import com.databasir.dao.tables.UserFavoriteProject;
import com.databasir.dao.tables.UserRole; import com.databasir.dao.tables.UserRole;
import java.util.Arrays; import java.util.Arrays;
@ -129,6 +130,11 @@ public class Databasir extends SchemaImpl {
*/ */
public final User USER = User.USER; public final User USER = User.USER;
/**
* The table <code>databasir.user_favorite_project</code>.
*/
public final UserFavoriteProject USER_FAVORITE_PROJECT = UserFavoriteProject.USER_FAVORITE_PROJECT;
/** /**
* The table <code>databasir.user_role</code>. * The table <code>databasir.user_role</code>.
*/ */
@ -167,6 +173,7 @@ public class Databasir extends SchemaImpl {
TableIndexDocument.TABLE_INDEX_DOCUMENT, TableIndexDocument.TABLE_INDEX_DOCUMENT,
TableTriggerDocument.TABLE_TRIGGER_DOCUMENT, TableTriggerDocument.TABLE_TRIGGER_DOCUMENT,
User.USER, User.USER,
UserFavoriteProject.USER_FAVORITE_PROJECT,
UserRole.USER_ROLE UserRole.USER_ROLE
); );
} }

View File

@ -11,6 +11,7 @@ import com.databasir.dao.tables.TableColumnDocument;
import com.databasir.dao.tables.TableDocument; import com.databasir.dao.tables.TableDocument;
import com.databasir.dao.tables.TableIndexDocument; import com.databasir.dao.tables.TableIndexDocument;
import com.databasir.dao.tables.TableTriggerDocument; import com.databasir.dao.tables.TableTriggerDocument;
import com.databasir.dao.tables.UserFavoriteProject;
import org.jooq.Index; import org.jooq.Index;
import org.jooq.OrderField; import org.jooq.OrderField;
@ -38,4 +39,5 @@ public class Indexes {
public static final Index TABLE_COLUMN_DOCUMENT_IDX_TABLE_DOCUMENT_ID = Internal.createIndex(DSL.name("idx_table_document_id"), TableColumnDocument.TABLE_COLUMN_DOCUMENT, new OrderField[] { TableColumnDocument.TABLE_COLUMN_DOCUMENT.TABLE_DOCUMENT_ID }, false); public static final Index TABLE_COLUMN_DOCUMENT_IDX_TABLE_DOCUMENT_ID = Internal.createIndex(DSL.name("idx_table_document_id"), TableColumnDocument.TABLE_COLUMN_DOCUMENT, new OrderField[] { TableColumnDocument.TABLE_COLUMN_DOCUMENT.TABLE_DOCUMENT_ID }, false);
public static final Index TABLE_INDEX_DOCUMENT_IDX_TABLE_DOCUMENT_ID = Internal.createIndex(DSL.name("idx_table_document_id"), TableIndexDocument.TABLE_INDEX_DOCUMENT, new OrderField[] { TableIndexDocument.TABLE_INDEX_DOCUMENT.TABLE_DOCUMENT_ID }, false); public static final Index TABLE_INDEX_DOCUMENT_IDX_TABLE_DOCUMENT_ID = Internal.createIndex(DSL.name("idx_table_document_id"), TableIndexDocument.TABLE_INDEX_DOCUMENT, new OrderField[] { TableIndexDocument.TABLE_INDEX_DOCUMENT.TABLE_DOCUMENT_ID }, false);
public static final Index TABLE_TRIGGER_DOCUMENT_IDX_TABLE_DOCUMENT_ID = Internal.createIndex(DSL.name("idx_table_document_id"), TableTriggerDocument.TABLE_TRIGGER_DOCUMENT, new OrderField[] { TableTriggerDocument.TABLE_TRIGGER_DOCUMENT.TABLE_DOCUMENT_ID }, false); public static final Index TABLE_TRIGGER_DOCUMENT_IDX_TABLE_DOCUMENT_ID = Internal.createIndex(DSL.name("idx_table_document_id"), TableTriggerDocument.TABLE_TRIGGER_DOCUMENT, new OrderField[] { TableTriggerDocument.TABLE_TRIGGER_DOCUMENT.TABLE_DOCUMENT_ID }, false);
public static final Index USER_FAVORITE_PROJECT_IDX_USER_ID = Internal.createIndex(DSL.name("idx_user_id"), UserFavoriteProject.USER_FAVORITE_PROJECT, new OrderField[] { UserFavoriteProject.USER_FAVORITE_PROJECT.USER_ID }, false);
} }

View File

@ -21,6 +21,7 @@ import com.databasir.dao.tables.TableDocument;
import com.databasir.dao.tables.TableIndexDocument; import com.databasir.dao.tables.TableIndexDocument;
import com.databasir.dao.tables.TableTriggerDocument; import com.databasir.dao.tables.TableTriggerDocument;
import com.databasir.dao.tables.User; import com.databasir.dao.tables.User;
import com.databasir.dao.tables.UserFavoriteProject;
import com.databasir.dao.tables.UserRole; import com.databasir.dao.tables.UserRole;
import com.databasir.dao.tables.records.DataSourcePropertyRecord; import com.databasir.dao.tables.records.DataSourcePropertyRecord;
import com.databasir.dao.tables.records.DataSourceRecord; import com.databasir.dao.tables.records.DataSourceRecord;
@ -38,6 +39,7 @@ import com.databasir.dao.tables.records.TableColumnDocumentRecord;
import com.databasir.dao.tables.records.TableDocumentRecord; import com.databasir.dao.tables.records.TableDocumentRecord;
import com.databasir.dao.tables.records.TableIndexDocumentRecord; import com.databasir.dao.tables.records.TableIndexDocumentRecord;
import com.databasir.dao.tables.records.TableTriggerDocumentRecord; import com.databasir.dao.tables.records.TableTriggerDocumentRecord;
import com.databasir.dao.tables.records.UserFavoriteProjectRecord;
import com.databasir.dao.tables.records.UserRecord; import com.databasir.dao.tables.records.UserRecord;
import com.databasir.dao.tables.records.UserRoleRecord; import com.databasir.dao.tables.records.UserRoleRecord;
@ -83,6 +85,7 @@ public class Keys {
public static final UniqueKey<UserRecord> KEY_USER_PRIMARY = Internal.createUniqueKey(User.USER, DSL.name("KEY_user_PRIMARY"), new TableField[] { User.USER.ID }, true); public static final UniqueKey<UserRecord> KEY_USER_PRIMARY = Internal.createUniqueKey(User.USER, DSL.name("KEY_user_PRIMARY"), new TableField[] { User.USER.ID }, true);
public static final UniqueKey<UserRecord> KEY_USER_UK_EMAIL = Internal.createUniqueKey(User.USER, DSL.name("KEY_user_uk_email"), new TableField[] { User.USER.EMAIL }, true); public static final UniqueKey<UserRecord> KEY_USER_UK_EMAIL = Internal.createUniqueKey(User.USER, DSL.name("KEY_user_uk_email"), new TableField[] { User.USER.EMAIL }, true);
public static final UniqueKey<UserRecord> KEY_USER_UK_USERNAME = Internal.createUniqueKey(User.USER, DSL.name("KEY_user_uk_username"), new TableField[] { User.USER.USERNAME }, true); public static final UniqueKey<UserRecord> KEY_USER_UK_USERNAME = Internal.createUniqueKey(User.USER, DSL.name("KEY_user_uk_username"), new TableField[] { User.USER.USERNAME }, true);
public static final UniqueKey<UserFavoriteProjectRecord> KEY_USER_FAVORITE_PROJECT_PRIMARY = Internal.createUniqueKey(UserFavoriteProject.USER_FAVORITE_PROJECT, DSL.name("KEY_user_favorite_project_PRIMARY"), new TableField[] { UserFavoriteProject.USER_FAVORITE_PROJECT.ID }, true);
public static final UniqueKey<UserRoleRecord> KEY_USER_ROLE_PRIMARY = Internal.createUniqueKey(UserRole.USER_ROLE, DSL.name("KEY_user_role_PRIMARY"), new TableField[] { UserRole.USER_ROLE.ID }, true); public static final UniqueKey<UserRoleRecord> KEY_USER_ROLE_PRIMARY = Internal.createUniqueKey(UserRole.USER_ROLE, DSL.name("KEY_user_role_PRIMARY"), new TableField[] { UserRole.USER_ROLE.ID }, true);
public static final UniqueKey<UserRoleRecord> KEY_USER_ROLE_UK_USER_ID_GROUP_ID_ROLE = Internal.createUniqueKey(UserRole.USER_ROLE, DSL.name("KEY_user_role_uk_user_id_group_id_role"), new TableField[] { UserRole.USER_ROLE.USER_ID, UserRole.USER_ROLE.GROUP_ID, UserRole.USER_ROLE.ROLE }, true); public static final UniqueKey<UserRoleRecord> KEY_USER_ROLE_UK_USER_ID_GROUP_ID_ROLE = Internal.createUniqueKey(UserRole.USER_ROLE, DSL.name("KEY_user_role_uk_user_id_group_id_role"), new TableField[] { UserRole.USER_ROLE.USER_ID, UserRole.USER_ROLE.GROUP_ID, UserRole.USER_ROLE.ROLE }, true);
} }

View File

@ -21,6 +21,7 @@ import com.databasir.dao.tables.TableDocument;
import com.databasir.dao.tables.TableIndexDocument; import com.databasir.dao.tables.TableIndexDocument;
import com.databasir.dao.tables.TableTriggerDocument; import com.databasir.dao.tables.TableTriggerDocument;
import com.databasir.dao.tables.User; import com.databasir.dao.tables.User;
import com.databasir.dao.tables.UserFavoriteProject;
import com.databasir.dao.tables.UserRole; import com.databasir.dao.tables.UserRole;
@ -115,6 +116,11 @@ public class Tables {
*/ */
public static final User USER = User.USER; public static final User USER = User.USER;
/**
* The table <code>databasir.user_favorite_project</code>.
*/
public static final UserFavoriteProject USER_FAVORITE_PROJECT = UserFavoriteProject.USER_FAVORITE_PROJECT;
/** /**
* The table <code>databasir.user_role</code>. * The table <code>databasir.user_role</code>.
*/ */

View File

@ -0,0 +1,163 @@
/*
* This file is generated by jOOQ.
*/
package com.databasir.dao.tables;
import com.databasir.dao.Databasir;
import com.databasir.dao.Indexes;
import com.databasir.dao.Keys;
import com.databasir.dao.tables.records.UserFavoriteProjectRecord;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Identity;
import org.jooq.Index;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Row4;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.UniqueKey;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class UserFavoriteProject extends TableImpl<UserFavoriteProjectRecord> {
private static final long serialVersionUID = 1L;
/**
* The reference instance of <code>databasir.user_favorite_project</code>
*/
public static final UserFavoriteProject USER_FAVORITE_PROJECT = new UserFavoriteProject();
/**
* The class holding records for this type
*/
@Override
public Class<UserFavoriteProjectRecord> getRecordType() {
return UserFavoriteProjectRecord.class;
}
/**
* The column <code>databasir.user_favorite_project.id</code>.
*/
public final TableField<UserFavoriteProjectRecord, Integer> ID = createField(DSL.name("id"), SQLDataType.INTEGER.nullable(false).identity(true), this, "");
/**
* The column <code>databasir.user_favorite_project.user_id</code>.
*/
public final TableField<UserFavoriteProjectRecord, Integer> USER_ID = createField(DSL.name("user_id"), SQLDataType.INTEGER.nullable(false), this, "");
/**
* The column <code>databasir.user_favorite_project.project_id</code>.
*/
public final TableField<UserFavoriteProjectRecord, Integer> PROJECT_ID = createField(DSL.name("project_id"), SQLDataType.INTEGER.nullable(false), this, "");
/**
* The column <code>databasir.user_favorite_project.create_at</code>.
*/
public final TableField<UserFavoriteProjectRecord, LocalDateTime> CREATE_AT = createField(DSL.name("create_at"), SQLDataType.LOCALDATETIME(0).nullable(false).defaultValue(DSL.field("CURRENT_TIMESTAMP", SQLDataType.LOCALDATETIME)), this, "");
private UserFavoriteProject(Name alias, Table<UserFavoriteProjectRecord> aliased) {
this(alias, aliased, null);
}
private UserFavoriteProject(Name alias, Table<UserFavoriteProjectRecord> aliased, Field<?>[] parameters) {
super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
}
/**
* Create an aliased <code>databasir.user_favorite_project</code> table
* reference
*/
public UserFavoriteProject(String alias) {
this(DSL.name(alias), USER_FAVORITE_PROJECT);
}
/**
* Create an aliased <code>databasir.user_favorite_project</code> table
* reference
*/
public UserFavoriteProject(Name alias) {
this(alias, USER_FAVORITE_PROJECT);
}
/**
* Create a <code>databasir.user_favorite_project</code> table reference
*/
public UserFavoriteProject() {
this(DSL.name("user_favorite_project"), null);
}
public <O extends Record> UserFavoriteProject(Table<O> child, ForeignKey<O, UserFavoriteProjectRecord> key) {
super(child, key, USER_FAVORITE_PROJECT);
}
@Override
public Schema getSchema() {
return aliased() ? null : Databasir.DATABASIR;
}
@Override
public List<Index> getIndexes() {
return Arrays.asList(Indexes.USER_FAVORITE_PROJECT_IDX_USER_ID);
}
@Override
public Identity<UserFavoriteProjectRecord, Integer> getIdentity() {
return (Identity<UserFavoriteProjectRecord, Integer>) super.getIdentity();
}
@Override
public UniqueKey<UserFavoriteProjectRecord> getPrimaryKey() {
return Keys.KEY_USER_FAVORITE_PROJECT_PRIMARY;
}
@Override
public UserFavoriteProject as(String alias) {
return new UserFavoriteProject(DSL.name(alias), this);
}
@Override
public UserFavoriteProject as(Name alias) {
return new UserFavoriteProject(alias, this);
}
/**
* Rename this table
*/
@Override
public UserFavoriteProject rename(String name) {
return new UserFavoriteProject(DSL.name(name), null);
}
/**
* Rename this table
*/
@Override
public UserFavoriteProject rename(Name name) {
return new UserFavoriteProject(name, null);
}
// -------------------------------------------------------------------------
// Row4 type methods
// -------------------------------------------------------------------------
@Override
public Row4<Integer, Integer, Integer, LocalDateTime> fieldsRow() {
return (Row4) super.fieldsRow();
}
}

View File

@ -0,0 +1,113 @@
/*
* This file is generated by jOOQ.
*/
package com.databasir.dao.tables.pojos;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class UserFavoriteProjectPojo implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Integer userId;
private Integer projectId;
private LocalDateTime createAt;
public UserFavoriteProjectPojo() {}
public UserFavoriteProjectPojo(UserFavoriteProjectPojo value) {
this.id = value.id;
this.userId = value.userId;
this.projectId = value.projectId;
this.createAt = value.createAt;
}
public UserFavoriteProjectPojo(
Integer id,
Integer userId,
Integer projectId,
LocalDateTime createAt
) {
this.id = id;
this.userId = userId;
this.projectId = projectId;
this.createAt = createAt;
}
/**
* Getter for <code>databasir.user_favorite_project.id</code>.
*/
public Integer getId() {
return this.id;
}
/**
* Setter for <code>databasir.user_favorite_project.id</code>.
*/
public void setId(Integer id) {
this.id = id;
}
/**
* Getter for <code>databasir.user_favorite_project.user_id</code>.
*/
public Integer getUserId() {
return this.userId;
}
/**
* Setter for <code>databasir.user_favorite_project.user_id</code>.
*/
public void setUserId(Integer userId) {
this.userId = userId;
}
/**
* Getter for <code>databasir.user_favorite_project.project_id</code>.
*/
public Integer getProjectId() {
return this.projectId;
}
/**
* Setter for <code>databasir.user_favorite_project.project_id</code>.
*/
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
/**
* Getter for <code>databasir.user_favorite_project.create_at</code>.
*/
public LocalDateTime getCreateAt() {
return this.createAt;
}
/**
* Setter for <code>databasir.user_favorite_project.create_at</code>.
*/
public void setCreateAt(LocalDateTime createAt) {
this.createAt = createAt;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("UserFavoriteProjectPojo (");
sb.append(id);
sb.append(", ").append(userId);
sb.append(", ").append(projectId);
sb.append(", ").append(createAt);
sb.append(")");
return sb.toString();
}
}

View File

@ -0,0 +1,235 @@
/*
* This file is generated by jOOQ.
*/
package com.databasir.dao.tables.records;
import com.databasir.dao.tables.UserFavoriteProject;
import com.databasir.dao.tables.pojos.UserFavoriteProjectPojo;
import java.time.LocalDateTime;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Record4;
import org.jooq.Row4;
import org.jooq.impl.UpdatableRecordImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class UserFavoriteProjectRecord extends UpdatableRecordImpl<UserFavoriteProjectRecord> implements Record4<Integer, Integer, Integer, LocalDateTime> {
private static final long serialVersionUID = 1L;
/**
* Setter for <code>databasir.user_favorite_project.id</code>.
*/
public void setId(Integer value) {
set(0, value);
}
/**
* Getter for <code>databasir.user_favorite_project.id</code>.
*/
public Integer getId() {
return (Integer) get(0);
}
/**
* Setter for <code>databasir.user_favorite_project.user_id</code>.
*/
public void setUserId(Integer value) {
set(1, value);
}
/**
* Getter for <code>databasir.user_favorite_project.user_id</code>.
*/
public Integer getUserId() {
return (Integer) get(1);
}
/**
* Setter for <code>databasir.user_favorite_project.project_id</code>.
*/
public void setProjectId(Integer value) {
set(2, value);
}
/**
* Getter for <code>databasir.user_favorite_project.project_id</code>.
*/
public Integer getProjectId() {
return (Integer) get(2);
}
/**
* Setter for <code>databasir.user_favorite_project.create_at</code>.
*/
public void setCreateAt(LocalDateTime value) {
set(3, value);
}
/**
* Getter for <code>databasir.user_favorite_project.create_at</code>.
*/
public LocalDateTime getCreateAt() {
return (LocalDateTime) get(3);
}
// -------------------------------------------------------------------------
// Primary key information
// -------------------------------------------------------------------------
@Override
public Record1<Integer> key() {
return (Record1) super.key();
}
// -------------------------------------------------------------------------
// Record4 type implementation
// -------------------------------------------------------------------------
@Override
public Row4<Integer, Integer, Integer, LocalDateTime> fieldsRow() {
return (Row4) super.fieldsRow();
}
@Override
public Row4<Integer, Integer, Integer, LocalDateTime> valuesRow() {
return (Row4) super.valuesRow();
}
@Override
public Field<Integer> field1() {
return UserFavoriteProject.USER_FAVORITE_PROJECT.ID;
}
@Override
public Field<Integer> field2() {
return UserFavoriteProject.USER_FAVORITE_PROJECT.USER_ID;
}
@Override
public Field<Integer> field3() {
return UserFavoriteProject.USER_FAVORITE_PROJECT.PROJECT_ID;
}
@Override
public Field<LocalDateTime> field4() {
return UserFavoriteProject.USER_FAVORITE_PROJECT.CREATE_AT;
}
@Override
public Integer component1() {
return getId();
}
@Override
public Integer component2() {
return getUserId();
}
@Override
public Integer component3() {
return getProjectId();
}
@Override
public LocalDateTime component4() {
return getCreateAt();
}
@Override
public Integer value1() {
return getId();
}
@Override
public Integer value2() {
return getUserId();
}
@Override
public Integer value3() {
return getProjectId();
}
@Override
public LocalDateTime value4() {
return getCreateAt();
}
@Override
public UserFavoriteProjectRecord value1(Integer value) {
setId(value);
return this;
}
@Override
public UserFavoriteProjectRecord value2(Integer value) {
setUserId(value);
return this;
}
@Override
public UserFavoriteProjectRecord value3(Integer value) {
setProjectId(value);
return this;
}
@Override
public UserFavoriteProjectRecord value4(LocalDateTime value) {
setCreateAt(value);
return this;
}
@Override
public UserFavoriteProjectRecord values(Integer value1, Integer value2, Integer value3, LocalDateTime value4) {
value1(value1);
value2(value2);
value3(value3);
value4(value4);
return this;
}
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
/**
* Create a detached UserFavoriteProjectRecord
*/
public UserFavoriteProjectRecord() {
super(UserFavoriteProject.USER_FAVORITE_PROJECT);
}
/**
* Create a detached, initialised UserFavoriteProjectRecord
*/
public UserFavoriteProjectRecord(Integer id, Integer userId, Integer projectId, LocalDateTime createAt) {
super(UserFavoriteProject.USER_FAVORITE_PROJECT);
setId(id);
setUserId(userId);
setProjectId(projectId);
setCreateAt(createAt);
}
/**
* Create a detached, initialised UserFavoriteProjectRecord
*/
public UserFavoriteProjectRecord(UserFavoriteProjectPojo value) {
super(UserFavoriteProject.USER_FAVORITE_PROJECT);
if (value != null) {
setId(value.getId());
setUserId(value.getUserId());
setProjectId(value.getProjectId());
setCreateAt(value.getCreateAt());
}
}
}

View File

@ -28,6 +28,10 @@ public abstract class BaseDao<R> {
return getDslContext().fetchExists(table, identity().eq(id)); return getDslContext().fetchExists(table, identity().eq(id));
} }
public boolean exists(Condition condition) {
return getDslContext().fetchExists(table, condition);
}
public <T> T insertAndReturnId(R pojo) { public <T> T insertAndReturnId(R pojo) {
Record record = getDslContext().newRecord(table, pojo); Record record = getDslContext().newRecord(table, pojo);
UpdatableRecord<?> updatableRecord = (UpdatableRecord<?>) record; UpdatableRecord<?> updatableRecord = (UpdatableRecord<?>) record;
@ -46,6 +50,12 @@ public abstract class BaseDao<R> {
return Arrays.stream(getDslContext().batchInsert(records).execute()).sum(); return Arrays.stream(getDslContext().batchInsert(records).execute()).sum();
} }
public int delete(Condition condition) {
return getDslContext()
.deleteFrom(table).where(condition)
.execute();
}
public <T extends Serializable> int deleteById(T id) { public <T extends Serializable> int deleteById(T id) {
return getDslContext() return getDslContext()
.deleteFrom(table).where(identity().eq(id)) .deleteFrom(table).where(identity().eq(id))

View File

@ -0,0 +1,62 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.UserFavoriteProjectPojo;
import lombok.Getter;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import java.util.List;
import static com.databasir.dao.Tables.*;
@Repository
public class UserFavoriteProjectDao extends BaseDao<UserFavoriteProjectPojo> {
@Autowired
@Getter
private DSLContext dslContext;
public UserFavoriteProjectDao() {
super(USER_FAVORITE_PROJECT, UserFavoriteProjectPojo.class);
}
public boolean exists(Integer userId, Integer projectId) {
return exists(USER_FAVORITE_PROJECT.USER_ID.eq(userId)
.and(USER_FAVORITE_PROJECT.PROJECT_ID.eq(projectId)));
}
public void insert(Integer userId, Integer projectId) {
UserFavoriteProjectPojo pojo = new UserFavoriteProjectPojo();
pojo.setUserId(userId);
pojo.setProjectId(projectId);
this.insertAndReturnId(pojo);
}
public void delete(Integer userId, Integer projectId) {
this.delete(USER_FAVORITE_PROJECT.USER_ID.eq(userId)
.and(USER_FAVORITE_PROJECT.PROJECT_ID.eq(projectId)));
}
public Page<UserFavoriteProjectPojo> selectByCondition(Pageable request, Condition condition) {
int total = getDslContext()
.selectCount().from(USER_FAVORITE_PROJECT)
.innerJoin(USER).on(USER.ID.eq(USER_FAVORITE_PROJECT.USER_ID))
.innerJoin(PROJECT).on(PROJECT.ID.eq(USER_FAVORITE_PROJECT.PROJECT_ID))
.where(PROJECT.DELETED.eq(false).and(condition))
.fetchOne(0, int.class);
List<UserFavoriteProjectPojo> data = getDslContext()
.select(USER_FAVORITE_PROJECT.fields()).from(USER_FAVORITE_PROJECT)
.innerJoin(USER).on(USER.ID.eq(USER_FAVORITE_PROJECT.USER_ID))
.innerJoin(PROJECT).on(PROJECT.ID.eq(USER_FAVORITE_PROJECT.PROJECT_ID))
.where(PROJECT.DELETED.eq(false).and(condition))
.orderBy(getSortFields(request.getSort()))
.offset(request.getOffset()).limit(request.getPageSize())
.fetchInto(UserFavoriteProjectPojo.class);
return new PageImpl<>(data, request, total);
}
}

View File

@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS user_favorite_project
(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
user_id INT NOT NULL,
project_id INT NOT NULL,
create_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE uk_user_id_project_id (user_id, project_id)
) CHARSET utf8mb4
COLLATE utf8mb4_unicode_ci;