mirror of
https://github.com/HangarMC/Hangar.git
synced 2025-03-07 15:31:00 +08:00
work on single project request (API)
This commit is contained in:
parent
6d39a6c176
commit
53e3a0a154
@ -2,6 +2,9 @@ package me.minidigger.hangar.controller.api;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import me.minidigger.hangar.db.dao.HangarDao;
|
||||||
|
import me.minidigger.hangar.db.dao.ProjectDao;
|
||||||
|
import me.minidigger.hangar.service.project.ProjectService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -28,13 +31,15 @@ public class ProjectsApiController implements ProjectsApi {
|
|||||||
private static final Logger log = LoggerFactory.getLogger(ProjectsApiController.class);
|
private static final Logger log = LoggerFactory.getLogger(ProjectsApiController.class);
|
||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
private final ProjectService projectService;
|
||||||
|
|
||||||
private final HttpServletRequest request;
|
private final HttpServletRequest request;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ProjectsApiController(ObjectMapper objectMapper, HttpServletRequest request) {
|
public ProjectsApiController(ObjectMapper objectMapper, HttpServletRequest request, ProjectService projectService) {
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
this.projectService = projectService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -60,12 +65,18 @@ public class ProjectsApiController implements ProjectsApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<Project> showProject(String pluginId) {
|
public ResponseEntity<Project> showProject(String pluginId) {
|
||||||
try {
|
Project project = projectService.getProjectApi(pluginId);
|
||||||
return new ResponseEntity<>(objectMapper.readValue("{\n \"icon_url\" : \"icon_url\",\n \"plugin_id\" : \"plugin_id\",\n \"settings\" : {\n \"license\" : {\n \"name\" : \"name\",\n \"url\" : \"url\"\n },\n \"sources\" : \"sources\",\n \"forum_sync\" : true,\n \"issues\" : \"issues\",\n \"support\" : \"support\",\n \"homepage\" : \"homepage\"\n },\n \"last_updated\" : \"2000-01-23T04:56:07.000+00:00\",\n \"visibility\" : \"public\",\n \"user_actions\" : {\n \"starred\" : true,\n \"watching\" : true\n },\n \"created_at\" : \"2000-01-23T04:56:07.000+00:00\",\n \"description\" : \"description\",\n \"promoted_versions\" : [ {\n \"version\" : \"version\",\n \"tags\" : [ {\n \"data\" : \"data\",\n \"color\" : {\n \"background\" : \"background\",\n \"foreground\" : \"foreground\"\n },\n \"name\" : \"name\",\n \"display_data\" : \"display_data\",\n \"minecraft_version\" : \"minecraft_version\"\n }, {\n \"data\" : \"data\",\n \"color\" : {\n \"background\" : \"background\",\n \"foreground\" : \"foreground\"\n },\n \"name\" : \"name\",\n \"display_data\" : \"display_data\",\n \"minecraft_version\" : \"minecraft_version\"\n } ]\n }, {\n \"version\" : \"version\",\n \"tags\" : [ {\n \"data\" : \"data\",\n \"color\" : {\n \"background\" : \"background\",\n \"foreground\" : \"foreground\"\n },\n \"name\" : \"name\",\n \"display_data\" : \"display_data\",\n \"minecraft_version\" : \"minecraft_version\"\n }, {\n \"data\" : \"data\",\n \"color\" : {\n \"background\" : \"background\",\n \"foreground\" : \"foreground\"\n },\n \"name\" : \"name\",\n \"display_data\" : \"display_data\",\n \"minecraft_version\" : \"minecraft_version\"\n } ]\n } ],\n \"stats\" : {\n \"downloads\" : 5,\n \"recent_downloads\" : 7,\n \"recent_views\" : 2,\n \"watchers\" : 3,\n \"stars\" : 9,\n \"views\" : 5\n },\n \"name\" : \"name\",\n \"namespace\" : {\n \"owner\" : \"owner\",\n \"slug\" : \"slug\"\n },\n \"category\" : \"admin_tools\"\n}", Project.class), HttpStatus.OK); // TODO Implement me
|
if (project == null) {
|
||||||
} catch (IOException e) {
|
log.error("Couldn't find a project for that pluginId");
|
||||||
log.error("Couldn't serialize response for content type application/json", e);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
}
|
||||||
|
return new ResponseEntity<>(projectService.getProjectApi(pluginId), HttpStatus.OK);
|
||||||
|
// try {
|
||||||
|
// return new ResponseEntity<>(objectMapper.readValue("{\n \"icon_url\" : \"icon_url\",\n \"plugin_id\" : \"plugin_id\",\n \"settings\" : {\n \"license\" : {\n \"name\" : \"name\",\n \"url\" : \"url\"\n },\n \"sources\" : \"sources\",\n \"forum_sync\" : true,\n \"issues\" : \"issues\",\n \"support\" : \"support\",\n \"homepage\" : \"homepage\"\n },\n \"last_updated\" : \"2000-01-23T04:56:07.000+00:00\",\n \"visibility\" : \"public\",\n \"user_actions\" : {\n \"starred\" : true,\n \"watching\" : true\n },\n \"created_at\" : \"2000-01-23T04:56:07.000+00:00\",\n \"description\" : \"description\",\n \"promoted_versions\" : [ {\n \"version\" : \"version\",\n \"tags\" : [ {\n \"data\" : \"data\",\n \"color\" : {\n \"background\" : \"background\",\n \"foreground\" : \"foreground\"\n },\n \"name\" : \"name\",\n \"display_data\" : \"display_data\",\n \"minecraft_version\" : \"minecraft_version\"\n }, {\n \"data\" : \"data\",\n \"color\" : {\n \"background\" : \"background\",\n \"foreground\" : \"foreground\"\n },\n \"name\" : \"name\",\n \"display_data\" : \"display_data\",\n \"minecraft_version\" : \"minecraft_version\"\n } ]\n }, {\n \"version\" : \"version\",\n \"tags\" : [ {\n \"data\" : \"data\",\n \"color\" : {\n \"background\" : \"background\",\n \"foreground\" : \"foreground\"\n },\n \"name\" : \"name\",\n \"display_data\" : \"display_data\",\n \"minecraft_version\" : \"minecraft_version\"\n }, {\n \"data\" : \"data\",\n \"color\" : {\n \"background\" : \"background\",\n \"foreground\" : \"foreground\"\n },\n \"name\" : \"name\",\n \"display_data\" : \"display_data\",\n \"minecraft_version\" : \"minecraft_version\"\n } ]\n } ],\n \"stats\" : {\n \"downloads\" : 5,\n \"recent_downloads\" : 7,\n \"recent_views\" : 2,\n \"watchers\" : 3,\n \"stars\" : 9,\n \"views\" : 5\n },\n \"name\" : \"name\",\n \"namespace\" : {\n \"owner\" : \"owner\",\n \"slug\" : \"slug\"\n },\n \"category\" : \"admin_tools\"\n}", Project.class), HttpStatus.OK); // TODO Implement me
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// log.error("Couldn't serialize response for content type application/json", e);
|
||||||
|
// return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package me.minidigger.hangar.db.dao;
|
package me.minidigger.hangar.db.dao;
|
||||||
|
|
||||||
|
import me.minidigger.hangar.db.model.ProjectsTable;
|
||||||
|
import me.minidigger.hangar.model.generated.ProjectStatsAll;
|
||||||
import me.minidigger.hangar.service.project.ProjectFactory.InvalidProjectReason;
|
import me.minidigger.hangar.service.project.ProjectFactory.InvalidProjectReason;
|
||||||
import org.jdbi.v3.sqlobject.config.RegisterBeanMapper;
|
import org.jdbi.v3.sqlobject.config.RegisterBeanMapper;
|
||||||
import org.jdbi.v3.sqlobject.customizer.BindBean;
|
import org.jdbi.v3.sqlobject.customizer.BindBean;
|
||||||
@ -9,8 +11,6 @@ import org.jdbi.v3.sqlobject.statement.SqlQuery;
|
|||||||
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
|
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import me.minidigger.hangar.db.model.ProjectsTable;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
@ -28,9 +28,16 @@ public interface ProjectDao {
|
|||||||
@SqlQuery("select * from projects where lower(owner_name) = lower(:author) AND lower(slug) = lower(:slug)")
|
@SqlQuery("select * from projects where lower(owner_name) = lower(:author) AND lower(slug) = lower(:slug)")
|
||||||
ProjectsTable getBySlug(String author, String slug);
|
ProjectsTable getBySlug(String author, String slug);
|
||||||
|
|
||||||
|
@SqlQuery("SELECT * FROM projects WHERE plugin_id = :pluginId")
|
||||||
|
ProjectsTable getByPluginId(String pluginId);
|
||||||
|
|
||||||
@SqlQuery("SELECT COUNT(*) FROM projects WHERE owner_id = :id")
|
@SqlQuery("SELECT COUNT(*) FROM projects WHERE owner_id = :id")
|
||||||
int getProjectCountByUserId(long id);
|
int getProjectCountByUserId(long id);
|
||||||
|
|
||||||
@SqlQuery("SELECT * FROM projects WHERE owner_id = :id")
|
@SqlQuery("SELECT * FROM projects WHERE owner_id = :id")
|
||||||
List<ProjectsTable> getProjectsByUserId(long id);
|
List<ProjectsTable> getProjectsByUserId(long id);
|
||||||
|
|
||||||
|
@RegisterBeanMapper(ProjectStatsAll.class)
|
||||||
|
@SqlQuery("SELECT * FROM (SELECT COUNT(*) as watchers FROM project_watchers pw WHERE pw.project_id = :id) as w, (SELECT COUNT(*) as stars FROM project_stars ps WHERE ps.project_id = :id) as s, (SELECT COUNT(*) as views FROM project_views_individual pvi WHERE pvi.project_id = :id) as v, (SELECT COUNT(*) as downloads FROM project_versions_downloads_individual pvdi WHERE pvdi.project_id = :id) as d")
|
||||||
|
ProjectStatsAll getProjectStats(long id);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package me.minidigger.hangar.db.model;
|
package me.minidigger.hangar.db.model;
|
||||||
|
|
||||||
|
|
||||||
|
import me.minidigger.hangar.model.Category;
|
||||||
|
import me.minidigger.hangar.model.Visibility;
|
||||||
import org.jdbi.v3.core.enums.EnumByOrdinal;
|
import org.jdbi.v3.core.enums.EnumByOrdinal;
|
||||||
|
import org.jdbi.v3.core.mapper.reflect.ColumnName;
|
||||||
|
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
|
|
||||||
import me.minidigger.hangar.db.customtypes.JSONB;
|
|
||||||
import me.minidigger.hangar.model.Category;
|
|
||||||
|
|
||||||
public class ProjectsTable {
|
public class ProjectsTable {
|
||||||
|
|
||||||
private long id;
|
private long id;
|
||||||
@ -22,7 +22,7 @@ public class ProjectsTable {
|
|||||||
private long postId;
|
private long postId;
|
||||||
private Category category;
|
private Category category;
|
||||||
private String description;
|
private String description;
|
||||||
private long visibility;
|
private Visibility visibility;
|
||||||
private Object notes; // TODO jsonb
|
private Object notes; // TODO jsonb
|
||||||
private String keywords;
|
private String keywords;
|
||||||
private String homepage;
|
private String homepage;
|
||||||
@ -37,7 +37,7 @@ public class ProjectsTable {
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProjectsTable(String pluginId, String name, String slug, String ownerName, long ownerId, Category category, String description, long visibility) {
|
public ProjectsTable(String pluginId, String name, String slug, String ownerName, long ownerId, Category category, String description, Visibility visibility) {
|
||||||
this.pluginId = pluginId;
|
this.pluginId = pluginId;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.slug = slug;
|
this.slug = slug;
|
||||||
@ -157,12 +157,13 @@ public class ProjectsTable {
|
|||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EnumByOrdinal
|
||||||
public long getVisibility() {
|
public Visibility getVisibility() {
|
||||||
return visibility;
|
return visibility;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVisibility(long visibility) {
|
@EnumByOrdinal
|
||||||
|
public void setVisibility(Visibility visibility) {
|
||||||
this.visibility = visibility;
|
this.visibility = visibility;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,15 +4,15 @@ import com.fasterxml.jackson.annotation.JsonCreator;
|
|||||||
import com.fasterxml.jackson.annotation.JsonValue;
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
public enum Visibility {
|
public enum Visibility {
|
||||||
PUBLIC(1, "public", false, ""),
|
PUBLIC(0, "public", false, ""),
|
||||||
|
|
||||||
NEW(2, "new", false, "project-new"),
|
NEW(1, "new", false, "project-new"),
|
||||||
|
|
||||||
NEEDSCHANGES(3, "needsChanges", true, "striped project-needsChanges"),
|
NEEDSCHANGES(2, "needsChanges", true, "striped project-needsChanges"),
|
||||||
|
|
||||||
NEEDSAPPROVAL(4, "needsApproval", false, "striped project-needsChanges"),
|
NEEDSAPPROVAL(3, "needsApproval", false, "striped project-needsChanges"),
|
||||||
|
|
||||||
SOFTDELETE(5, "softDelete", true, "striped project-hidden");
|
SOFTDELETE(4, "softDelete", true, "striped project-hidden");
|
||||||
|
|
||||||
private long value;
|
private long value;
|
||||||
private String name;
|
private String name;
|
||||||
|
@ -58,7 +58,7 @@ public class ProjectData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Visibility getVisibility() {
|
public Visibility getVisibility() {
|
||||||
return Visibility.fromId(getProject().getVisibility());
|
return getProject().getVisibility();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFullSlug() {
|
public String getFullSlug() {
|
||||||
|
@ -0,0 +1,59 @@
|
|||||||
|
package me.minidigger.hangar.model.viewhelpers;
|
||||||
|
|
||||||
|
public class ProjectStats {
|
||||||
|
|
||||||
|
private long views;
|
||||||
|
private long downloads;
|
||||||
|
private long recentView;
|
||||||
|
private long recentDownloads;
|
||||||
|
private long stars;
|
||||||
|
private long watchers;
|
||||||
|
|
||||||
|
public long getViews() {
|
||||||
|
return views;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setViews(long views) {
|
||||||
|
this.views = views;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDownloads() {
|
||||||
|
return downloads;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDownloads(long downloads) {
|
||||||
|
this.downloads = downloads;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getRecentView() {
|
||||||
|
return recentView;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecentView(long recentView) {
|
||||||
|
this.recentView = recentView;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getRecentDownloads() {
|
||||||
|
return recentDownloads;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecentDownloads(long recentDownloads) {
|
||||||
|
this.recentDownloads = recentDownloads;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getStars() {
|
||||||
|
return stars;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStars(long stars) {
|
||||||
|
this.stars = stars;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getWatchers() {
|
||||||
|
return watchers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWatchers(long watchers) {
|
||||||
|
this.watchers = watchers;
|
||||||
|
}
|
||||||
|
}
|
@ -46,7 +46,7 @@ public class ProjectFactory {
|
|||||||
|
|
||||||
public ProjectsTable createProject(UsersTable ownerUser, String name, String pluginId, Category category, String description) {
|
public ProjectsTable createProject(UsersTable ownerUser, String name, String pluginId, Category category, String description) {
|
||||||
String slug = StringUtils.slugify(name);
|
String slug = StringUtils.slugify(name);
|
||||||
ProjectsTable projectsTable = new ProjectsTable(pluginId, name, slug, ownerUser.getName(), ownerUser.getId(), category, description, Visibility.NEW.getValue());
|
ProjectsTable projectsTable = new ProjectsTable(pluginId, name, slug, ownerUser.getName(), ownerUser.getId(), category, description, Visibility.NEW);
|
||||||
|
|
||||||
ProjectChannelsTable channelsTable = new ProjectChannelsTable(hangarConfig.channels.getNameDefault(), hangarConfig.channels.getColorDefault().getValue(), -1);
|
ProjectChannelsTable channelsTable = new ProjectChannelsTable(hangarConfig.channels.getNameDefault(), hangarConfig.channels.getColorDefault().getValue(), -1);
|
||||||
|
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package me.minidigger.hangar.service.project;
|
package me.minidigger.hangar.service.project;
|
||||||
|
|
||||||
|
import me.minidigger.hangar.model.generated.Project;
|
||||||
|
import me.minidigger.hangar.model.generated.ProjectNamespace;
|
||||||
|
import me.minidigger.hangar.model.generated.ProjectSettings;
|
||||||
|
import me.minidigger.hangar.model.generated.UserActions;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -69,4 +73,28 @@ public class ProjectService {
|
|||||||
// TODO get project page
|
// TODO get project page
|
||||||
return new ProjectPagesTable(1, OffsetDateTime.now(), projectId, "Home", slug, "# Test\n This is a test", false, null);
|
return new ProjectPagesTable(1, OffsetDateTime.now(), projectId, "Home", slug, "# Test\n This is a test", false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Project getProjectApi(String pluginId) {
|
||||||
|
ProjectsTable projectsTable = projectDao.get().getByPluginId(pluginId);
|
||||||
|
if (projectsTable == null) return null;
|
||||||
|
Project project = new Project();
|
||||||
|
project.setCreatedAt(projectsTable.getCreatedAt());
|
||||||
|
project.setPluginId(projectsTable.getPluginId());
|
||||||
|
project.setName(projectsTable.getName());
|
||||||
|
ProjectNamespace projectNamespace = new ProjectNamespace();
|
||||||
|
projectNamespace.setOwner(userDao.get().getById(projectsTable.getOwnerId()).getName());
|
||||||
|
projectNamespace.setSlug(projectsTable.getSlug());
|
||||||
|
project.setNamespace(projectNamespace);
|
||||||
|
|
||||||
|
project.setPromotedVersions(new ArrayList<>()); // TODO implement
|
||||||
|
project.setStats(projectDao.get().getProjectStats(projectsTable.getId()));
|
||||||
|
project.setCategory(projectsTable.getCategory());
|
||||||
|
project.setDescription(projectsTable.getDescription());
|
||||||
|
project.setLastUpdated(OffsetDateTime.now()); // TODO implement
|
||||||
|
project.setVisibility(projectsTable.getVisibility());
|
||||||
|
project.setUserActions(new UserActions()); // TODO implement
|
||||||
|
project.setSettings(new ProjectSettings()); // TODO implement
|
||||||
|
project.setIconUrl(""); // TODO implement
|
||||||
|
return project;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user