-
Notifications
You must be signed in to change notification settings - Fork 22
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
1
commit 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
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
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()); | ||
| } | ||
| } | ||
| } |
80 changes: 80 additions & 0 deletions
80
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,80 @@ | ||
| 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.UserListMember; | ||
| import cwms.cda.data.dto.auth.userlists.UserListMembers; | ||
| 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 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<Number> listOfficeCode = field(name(atUserLists.getName(), "db_office_code"), Number.class); | ||
| Field<String> listUserListId = field(name(atUserLists.getName(), "user_list_id"), String.class); | ||
| Field<Number> officeCode = field(name(cwmsOffice.getName(), "office_code"), Number.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()); | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
cwms-data-api/src/main/java/cwms/cda/data/dto/auth/userlists/UserListMember.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,63 @@ | ||
| 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; | ||
|
|
||
| @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) | ||
| @FormattableWith(contentType = Formats.JSONV1, formatter = JsonV1.class, | ||
| aliases = {Formats.DEFAULT, Formats.JSON}) | ||
| public final class UserListMember 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; | ||
|
|
||
| @JsonProperty(required = true) | ||
| @Schema(description = "The user identifier for the member.") | ||
| private final String userId; | ||
|
|
||
| @Schema(description = "The user's display name.") | ||
| private final String fullName; | ||
|
|
||
| @Schema(description = "The user's email address.") | ||
| private final String email; | ||
|
|
||
| public UserListMember(String officeId, String userListId, String userId, String fullName, | ||
| String email) { | ||
| this.officeId = officeId; | ||
| this.userListId = userListId; | ||
| this.userId = userId; | ||
| this.fullName = fullName; | ||
| this.email = email; | ||
| } | ||
|
|
||
| public String getOfficeId() { | ||
| return officeId; | ||
| } | ||
|
|
||
| public String getUserListId() { | ||
| return userListId; | ||
| } | ||
|
|
||
| public String getUserId() { | ||
| return userId; | ||
| } | ||
|
|
||
| public String getFullName() { | ||
| return fullName; | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
cwms-data-api/src/main/java/cwms/cda/data/dto/auth/userlists/UserListMembers.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,32 @@ | ||
| package cwms.cda.data.dto.auth.userlists; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.annotation.JsonRootName; | ||
| 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.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| @JsonRootName("user-list-members") | ||
| @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) | ||
| @FormattableWith(contentType = Formats.JSONV1, formatter = JsonV1.class, | ||
| aliases = {Formats.DEFAULT, Formats.JSON}) | ||
| public final class UserListMembers extends CwmsDTOBase { | ||
|
|
||
| @JsonProperty(required = true) | ||
| @Schema(description = "Members in the requested user list.") | ||
| private final List<UserListMember> members; | ||
|
|
||
| public UserListMembers(List<UserListMember> members) { | ||
| this.members = List.copyOf(members); | ||
| } | ||
|
|
||
| public List<UserListMember> getMembers() { | ||
| return Collections.unmodifiableList(members); | ||
| } | ||
| } |
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.