-
Notifications
You must be signed in to change notification settings - Fork 24
Add initial user-lists API endpoint #1734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
krowvin
wants to merge
2
commits into
develop
Choose a base branch
from
1733-user-lists
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
cwms-data-api/src/main/java/cwms/cda/api/auth/userlists/UserListController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package cwms.cda.api.auth.userlists; | ||
|
|
||
| import static cwms.cda.api.Controllers.GET_ONE; | ||
| import static cwms.cda.api.Controllers.OFFICE; | ||
| import static cwms.cda.api.Controllers.STATUS_200; | ||
| import static cwms.cda.api.Controllers.USER_LIST_ID; | ||
| import static cwms.cda.data.dao.JooqDao.getDslContext; | ||
|
|
||
| import com.codahale.metrics.MetricRegistry; | ||
| import com.codahale.metrics.Timer; | ||
| import cwms.cda.api.Controllers; | ||
| import cwms.cda.api.errors.NotFoundException; | ||
| import cwms.cda.api.errors.RequiredQueryParameterException; | ||
| import cwms.cda.data.dao.UserListDao; | ||
| import cwms.cda.data.dto.Office; | ||
| import cwms.cda.data.dto.auth.userlists.UserList; | ||
| import cwms.cda.formatters.ContentType; | ||
| import cwms.cda.formatters.Formats; | ||
| import io.javalin.core.util.Header; | ||
| import io.javalin.http.Context; | ||
| import io.javalin.http.Handler; | ||
| import io.javalin.plugin.openapi.annotations.HttpMethod; | ||
| import io.javalin.plugin.openapi.annotations.OpenApi; | ||
| import io.javalin.plugin.openapi.annotations.OpenApiContent; | ||
| import io.javalin.plugin.openapi.annotations.OpenApiParam; | ||
| import io.javalin.plugin.openapi.annotations.OpenApiResponse; | ||
| import io.javalin.plugin.openapi.annotations.OpenApiSecurity; | ||
| import org.jooq.DSLContext; | ||
|
|
||
| public final class UserListController implements Handler { | ||
| public static final String TAG = "User Management"; | ||
| private final MetricRegistry metrics; | ||
|
|
||
| public UserListController(MetricRegistry metrics) { | ||
| this.metrics = metrics; | ||
| } | ||
|
|
||
| private Timer.Context markAndTime(String subject) { | ||
| return Controllers.markAndTime(metrics, getClass().getName(), subject); | ||
| } | ||
|
|
||
| @OpenApi( | ||
| pathParams = { | ||
| @OpenApiParam(name = USER_LIST_ID, required = true, | ||
| description = "The identifier of the user list to retrieve.") | ||
| }, | ||
| queryParams = { | ||
| @OpenApiParam(name = OFFICE, required = true, | ||
| description = "The office that owns the requested user list.") | ||
| }, | ||
| responses = { | ||
| @OpenApiResponse( | ||
| status = STATUS_200, | ||
| content = { | ||
| @OpenApiContent(from = UserList.class, type = Formats.JSON) | ||
| } | ||
| ) | ||
| }, | ||
| security = { | ||
| @OpenApiSecurity(name = "gets overridden allows lock icon.") | ||
| }, | ||
| description = "Retrieve user list metadata.", | ||
| method = HttpMethod.GET, | ||
| tags = {TAG} | ||
| ) | ||
| @Override | ||
| public void handle(Context ctx) { | ||
| try (final Timer.Context ignored = markAndTime(GET_ONE)) { | ||
| String office = ctx.queryParam(OFFICE); | ||
| if (office == null || office.isBlank()) { | ||
| throw new RequiredQueryParameterException(OFFICE); | ||
| } | ||
|
|
||
| final String officeId = ctx.queryParamAsClass(OFFICE, String.class) | ||
| .check(Office::validOfficeNotNull, "Invalid office provided") | ||
| .get(); | ||
|
|
||
| String userListId = ctx.pathParam(USER_LIST_ID); | ||
| DSLContext dsl = getDslContext(ctx); | ||
| UserListDao dao = new UserListDao(dsl); | ||
| UserList userList = dao.getUserList(officeId, userListId) | ||
| .orElseThrow(() -> new NotFoundException("User list not found: " | ||
| + officeId + "/" + userListId)); | ||
|
|
||
| String formatHeader = ctx.header(Header.ACCEPT); | ||
| ContentType contentType = Formats.parseHeader(formatHeader, UserList.class); | ||
| String result = Formats.format(contentType, userList); | ||
|
|
||
| ctx.result(result); | ||
| ctx.contentType(contentType.toString()); | ||
| } | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
cwms-data-api/src/main/java/cwms/cda/api/auth/userlists/UserListMembersController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package cwms.cda.api.auth.userlists; | ||
|
|
||
| import static cwms.cda.api.Controllers.GET_ONE; | ||
| import static cwms.cda.api.Controllers.OFFICE; | ||
| import static cwms.cda.api.Controllers.STATUS_200; | ||
| import static cwms.cda.api.Controllers.USER_LIST_ID; | ||
| import static cwms.cda.data.dao.JooqDao.getDslContext; | ||
|
|
||
| import com.codahale.metrics.MetricRegistry; | ||
| import com.codahale.metrics.Timer; | ||
| import cwms.cda.api.Controllers; | ||
| import cwms.cda.api.errors.RequiredQueryParameterException; | ||
| import cwms.cda.data.dao.UserListDao; | ||
| import cwms.cda.data.dto.Office; | ||
| import cwms.cda.data.dto.auth.userlists.UserListMembers; | ||
| import cwms.cda.formatters.ContentType; | ||
| import cwms.cda.formatters.Formats; | ||
| import io.javalin.core.util.Header; | ||
| import io.javalin.http.Context; | ||
| import io.javalin.http.Handler; | ||
| import io.javalin.plugin.openapi.annotations.HttpMethod; | ||
| import io.javalin.plugin.openapi.annotations.OpenApi; | ||
| import io.javalin.plugin.openapi.annotations.OpenApiContent; | ||
| import io.javalin.plugin.openapi.annotations.OpenApiParam; | ||
| import io.javalin.plugin.openapi.annotations.OpenApiResponse; | ||
| import io.javalin.plugin.openapi.annotations.OpenApiSecurity; | ||
| import org.jooq.DSLContext; | ||
|
|
||
| public final class UserListMembersController implements Handler { | ||
| public static final String TAG = "User Management"; | ||
| private final MetricRegistry metrics; | ||
|
|
||
| public UserListMembersController(MetricRegistry metrics) { | ||
| this.metrics = metrics; | ||
| } | ||
|
|
||
| private Timer.Context markAndTime(String subject) { | ||
| return Controllers.markAndTime(metrics, getClass().getName(), subject); | ||
| } | ||
|
|
||
| @OpenApi( | ||
| pathParams = { | ||
| @OpenApiParam(name = USER_LIST_ID, required = true, | ||
| description = "The identifier of the user list to retrieve members for.") | ||
| }, | ||
| queryParams = { | ||
| @OpenApiParam(name = OFFICE, required = true, | ||
| description = "The office that owns the requested user list.") | ||
| }, | ||
| responses = { | ||
| @OpenApiResponse( | ||
| status = STATUS_200, | ||
| content = { | ||
| @OpenApiContent(from = UserListMembers.class, type = Formats.JSON) | ||
| } | ||
| ) | ||
| }, | ||
| security = { | ||
| @OpenApiSecurity(name = "gets overridden allows lock icon.") | ||
| }, | ||
| description = "Retrieve the members of a user list.", | ||
| method = HttpMethod.GET, | ||
| tags = {TAG} | ||
| ) | ||
| @Override | ||
| public void handle(Context ctx) { | ||
| try (final Timer.Context ignored = markAndTime(GET_ONE)) { | ||
| String office = ctx.queryParam(OFFICE); | ||
| if (office == null || office.isBlank()) { | ||
| throw new RequiredQueryParameterException(OFFICE); | ||
| } | ||
|
|
||
| office = ctx.queryParamAsClass(OFFICE, String.class) | ||
| .check(Office::validOfficeNotNull, "Invalid office provided") | ||
| .get(); | ||
|
|
||
| String userListId = ctx.pathParam(USER_LIST_ID); | ||
| DSLContext dsl = getDslContext(ctx); | ||
| UserListDao dao = new UserListDao(dsl); | ||
| UserListMembers members = dao.getMembers(office, userListId); | ||
|
|
||
| String formatHeader = ctx.header(Header.ACCEPT); | ||
| ContentType contentType = Formats.parseHeader(formatHeader, UserListMembers.class); | ||
| String result = Formats.format(contentType, members); | ||
|
|
||
| ctx.result(result); | ||
| ctx.contentType(contentType.toString()); | ||
| } | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
cwms-data-api/src/main/java/cwms/cda/data/dao/UserListDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package cwms.cda.data.dao; | ||
|
|
||
| import static org.jooq.impl.DSL.field; | ||
| import static org.jooq.impl.DSL.name; | ||
| import static org.jooq.impl.DSL.selectOne; | ||
| import static org.jooq.impl.DSL.table; | ||
| import static org.jooq.impl.DSL.upper; | ||
|
|
||
| import cwms.cda.api.errors.NotFoundException; | ||
| import cwms.cda.data.dto.auth.userlists.UserList; | ||
| import cwms.cda.data.dto.auth.userlists.UserListMember; | ||
| import cwms.cda.data.dto.auth.userlists.UserListMembers; | ||
| import java.sql.Timestamp; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.jooq.Condition; | ||
| import org.jooq.DSLContext; | ||
| import org.jooq.Field; | ||
| import org.jooq.Table; | ||
|
|
||
| public final class UserListDao extends Dao<UserListMember> { | ||
|
|
||
| private final Table<?> avUserListMembers = table(name("CWMS_20", "AV_USER_LIST_MEMBERS")).as("ulm"); | ||
| private final Table<?> atUserLists = table(name("CWMS_20", "AT_USER_LISTS")).as("ul"); | ||
| private final Table<?> cwmsOffice = table(name("CWMS_20", "CWMS_OFFICE")).as("co"); | ||
|
|
||
| public UserListDao(DSLContext dsl) { | ||
| super(dsl); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<UserListMember> getByUniqueName(String uniqueName, String office) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| public Optional<UserList> getUserList(String officeId, String userListId) { | ||
| Field<Long> listOfficeCode = field(name(atUserLists.getName(), "DB_OFFICE_CODE"), Long.class); | ||
| Field<String> listUserListId = field(name(atUserLists.getName(), "USER_LIST_ID"), String.class); | ||
| Field<String> listDescription = field(name(atUserLists.getName(), "USER_LIST_DESC"), String.class); | ||
| Field<String> listOwner = field(name(atUserLists.getName(), "OWNED_BY_USERID"), String.class); | ||
| Field<Timestamp> listCreatedAt = field(name(atUserLists.getName(), "CREATED_AT"), Timestamp.class); | ||
| Field<Timestamp> listUpdatedAt = field(name(atUserLists.getName(), "UPDATED_AT"), Timestamp.class); | ||
| Field<Long> officeCode = field(name(cwmsOffice.getName(), "OFFICE_CODE"), Long.class); | ||
| Field<String> officeName = field(name(cwmsOffice.getName(), "OFFICE_ID"), String.class); | ||
|
|
||
| return dsl.select(officeName, listUserListId, listDescription, listOwner, listCreatedAt, listUpdatedAt) | ||
| .from(atUserLists) | ||
| .join(cwmsOffice).on(listOfficeCode.eq(officeCode)) | ||
| .where(ignoreCaseEq(officeName, officeId)) | ||
| .and(ignoreCaseEq(listUserListId, userListId)) | ||
| .fetchOptional(record -> new UserList( | ||
| record.get(officeName), | ||
| record.get(listUserListId), | ||
| record.get(listDescription), | ||
| record.get(listOwner), | ||
| record.get(listCreatedAt).toInstant(), | ||
| Optional.ofNullable(record.get(listUpdatedAt)).map(Timestamp::toInstant).orElse(null) | ||
| )); | ||
| } | ||
|
|
||
| public UserListMembers getMembers(String officeId, String userListId) { | ||
| if (!userListExists(officeId, userListId)) { | ||
| throw new NotFoundException("User list not found: " + officeId + "/" + userListId); | ||
| } | ||
|
|
||
| Field<String> viewOfficeId = field(name(avUserListMembers.getName(), "OFFICE_ID"), String.class); | ||
| Field<String> viewUserListId = field(name(avUserListMembers.getName(), "USER_LIST_ID"), String.class); | ||
| Field<String> viewUserId = field(name(avUserListMembers.getName(), "USER_ID"), String.class); | ||
| Field<String> viewFullName = field(name(avUserListMembers.getName(), "FULL_NAME"), String.class); | ||
| Field<String> viewEmail = field(name(avUserListMembers.getName(), "EMAIL"), String.class); | ||
|
|
||
| List<UserListMember> members = dsl.select(viewOfficeId, viewUserListId, viewUserId, viewFullName, | ||
| viewEmail) | ||
| .from(avUserListMembers) | ||
| .where(ignoreCaseEq(viewOfficeId, officeId)) | ||
| .and(ignoreCaseEq(viewUserListId, userListId)) | ||
| .orderBy(viewFullName.asc().nullsLast(), viewUserId.asc()) | ||
| .fetch(record -> new UserListMember( | ||
| record.get(viewOfficeId), | ||
| record.get(viewUserListId), | ||
| record.get(viewUserId), | ||
| record.get(viewFullName), | ||
| record.get(viewEmail) | ||
| )); | ||
|
|
||
| return new UserListMembers(members); | ||
| } | ||
|
|
||
| private boolean userListExists(String officeId, String userListId) { | ||
| Field<Long> listOfficeCode = field(name(atUserLists.getName(), "DB_OFFICE_CODE"), Long.class); | ||
| Field<String> listUserListId = field(name(atUserLists.getName(), "USER_LIST_ID"), String.class); | ||
| Field<Long> officeCode = field(name(cwmsOffice.getName(), "OFFICE_CODE"), Long.class); | ||
| Field<String> officeName = field(name(cwmsOffice.getName(), "OFFICE_ID"), String.class); | ||
|
|
||
| return dsl.fetchExists( | ||
| selectOne() | ||
| .from(atUserLists) | ||
| .join(cwmsOffice).on(listOfficeCode.eq(officeCode)) | ||
| .where(ignoreCaseEq(officeName, officeId)) | ||
| .and(ignoreCaseEq(listUserListId, userListId)) | ||
| ); | ||
| } | ||
|
|
||
| private static Condition ignoreCaseEq(Field<String> field, String value) { | ||
| return upper(field).eq(value.toUpperCase()); | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
cwms-data-api/src/main/java/cwms/cda/data/dto/auth/userlists/UserList.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package cwms.cda.data.dto.auth.userlists; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import cwms.cda.data.dto.CwmsDTOBase; | ||
| import cwms.cda.formatters.Formats; | ||
| import cwms.cda.formatters.annotations.FormattableWith; | ||
| import cwms.cda.formatters.json.JsonV1; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import java.time.Instant; | ||
|
|
||
| @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) | ||
| @FormattableWith(contentType = Formats.JSONV1, formatter = JsonV1.class, | ||
| aliases = {Formats.DEFAULT, Formats.JSON}) | ||
| public final class UserList extends CwmsDTOBase { | ||
|
|
||
| @JsonProperty(required = true) | ||
| @Schema(description = "The owning CWMS office identifier for the user list.") | ||
| private final String officeId; | ||
|
|
||
| @JsonProperty(required = true) | ||
| @Schema(description = "The identifier of the user list.") | ||
| private final String userListId; | ||
|
|
||
| @Schema(description = "The user list description.") | ||
| private final String description; | ||
|
|
||
| @Schema(description = "The user id of the list owner.") | ||
| private final String ownedByUserId; | ||
|
|
||
| @JsonProperty(required = true) | ||
| @Schema(description = "The time the user list was created.") | ||
| private final Instant createdAt; | ||
|
|
||
| @Schema(description = "The time the user list was last updated.") | ||
| private final Instant updatedAt; | ||
|
|
||
| public UserList(String officeId, String userListId, String description, String ownedByUserId, | ||
| Instant createdAt, Instant updatedAt) { | ||
| this.officeId = officeId; | ||
| this.userListId = userListId; | ||
| this.description = description; | ||
| this.ownedByUserId = ownedByUserId; | ||
| this.createdAt = createdAt; | ||
| this.updatedAt = updatedAt; | ||
| } | ||
|
|
||
| public String getOfficeId() { | ||
| return officeId; | ||
| } | ||
|
|
||
| public String getUserListId() { | ||
| return userListId; | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| public String getOwnedByUserId() { | ||
| return ownedByUserId; | ||
| } | ||
|
|
||
| public Instant getCreatedAt() { | ||
| return createdAt; | ||
| } | ||
|
|
||
| public Instant getUpdatedAt() { | ||
| return updatedAt; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
long term, I'd probably group these by "/user/list" rather that method -> "/user/..."
But that's just me.