Feature/favorite project (#25)

* feat: add project favorite api

* feat:update frontend resources
This commit is contained in:
vran
2022-02-24 19:37:41 +08:00
committed by GitHub
parent 496534843a
commit decd76dd42
57 changed files with 869 additions and 25 deletions

View File

@@ -28,6 +28,10 @@ public abstract class BaseDao<R> {
return getDslContext().fetchExists(table, identity().eq(id));
}
public boolean exists(Condition condition) {
return getDslContext().fetchExists(table, condition);
}
public <T> T insertAndReturnId(R pojo) {
Record record = getDslContext().newRecord(table, pojo);
UpdatableRecord<?> updatableRecord = (UpdatableRecord<?>) record;
@@ -46,6 +50,12 @@ public abstract class BaseDao<R> {
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) {
return getDslContext()
.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;