From 9a9f7d81653474151ea93b9d0b9723633cb8d2e1 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sun, 23 Aug 2020 14:31:17 -0700 Subject: [PATCH] api v1 work --- .../papermc/hangar/controller/Apiv1Controller.java | 14 ++++++++++---- .../io/papermc/hangar/db/dao/api/V1ApiDao.java | 4 ++++ .../papermc/hangar/service/api/V1ApiService.java | 5 +++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/papermc/hangar/controller/Apiv1Controller.java b/src/main/java/io/papermc/hangar/controller/Apiv1Controller.java index 133bfd847..f537ea285 100644 --- a/src/main/java/io/papermc/hangar/controller/Apiv1Controller.java +++ b/src/main/java/io/papermc/hangar/controller/Apiv1Controller.java @@ -11,6 +11,7 @@ import io.papermc.hangar.db.model.ProjectVersionsTable; import io.papermc.hangar.db.model.ProjectsTable; import io.papermc.hangar.db.model.UserProjectRolesTable; import io.papermc.hangar.db.model.UsersTable; +import io.papermc.hangar.model.TagColor; import io.papermc.hangar.model.Visibility; import io.papermc.hangar.service.UserService; import io.papermc.hangar.service.api.V1ApiService; @@ -42,6 +43,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.stream.Collector; import java.util.stream.Collectors; @@ -136,13 +138,17 @@ public class Apiv1Controller extends HangarController { } @RequestMapping("/api/v1/tags/{tagId}") - public Object tagColor(@PathVariable Object tagId) { - return null; // TODO implement tagColor request controller + public ResponseEntity tagColor(@PathVariable("tagId") TagColor tag) { + ObjectNode tagColor = mapper.createObjectNode(); + tagColor.set("id", mapper.valueToTree(tag.ordinal())); + tagColor.set("backgroundColor", mapper.valueToTree(tag.getBackground())); + tagColor.set("foregroundColor", mapper.valueToTree(tag.getForeground())); + return ResponseEntity.of(Optional.of(tagColor)); } @RequestMapping("/api/v1/users") - public Object listUsers(@RequestParam Object limit, @RequestParam Object offset) { - return null; // TODO implement listUsers request controller + public ResponseEntity listUsers(@RequestParam(defaultValue = "0") int offset, @RequestParam(required = false) Integer limit) { + return ResponseEntity.of(Optional.of(writeUsers(v1ApiService.getUsers(offset, limit)))); } @RequestMapping("/api/v1/users/{user}") diff --git a/src/main/java/io/papermc/hangar/db/dao/api/V1ApiDao.java b/src/main/java/io/papermc/hangar/db/dao/api/V1ApiDao.java index 5ca95632f..0a668208c 100644 --- a/src/main/java/io/papermc/hangar/db/dao/api/V1ApiDao.java +++ b/src/main/java/io/papermc/hangar/db/dao/api/V1ApiDao.java @@ -62,4 +62,8 @@ public interface V1ApiDao { @RegisterBeanMapper(ProjectVersionTagsTable.class) @SqlQuery("SELECT pv.id pv_id, pvt.* FROM project_version_tags pvt JOIN project_versions pv ON pv.id = pvt.version_id WHERE pv.visibility = 0 AND pv.id IN ()") List> getVersionsTags(@BindList(onEmpty = EmptyHandling.NULL_STRING) List versionIds); + + @RegisterBeanMapper(UsersTable.class) + @SqlQuery("SELECT * FROM users OFFSET :offset LIMIT :limit") + List getUsers(int offset, Integer limit); } diff --git a/src/main/java/io/papermc/hangar/service/api/V1ApiService.java b/src/main/java/io/papermc/hangar/service/api/V1ApiService.java index c90b92dbf..0da908335 100644 --- a/src/main/java/io/papermc/hangar/service/api/V1ApiService.java +++ b/src/main/java/io/papermc/hangar/service/api/V1ApiService.java @@ -31,6 +31,11 @@ public class V1ApiService { return v1ApiDao.get().getProjectsForUsers(userIds).stream().collect(Collectors.groupingBy(ProjectsTable::getOwnerId)); } + public List getUsers(int offset, Integer limit) { + return v1ApiDao.get().getUsers(offset, limit); + } + + // TODO better way for this? public Map> getStarredPlugins(List userIds) { return v1ApiDao.get().getStarredPlugins(userIds).stream()