-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sandside): #225 add UserSettings table and repository
- Loading branch information
Showing
17 changed files
with
202 additions
and
6 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
sandside/src/main/java/fr/ght1pc9kc/baywatch/security/api/UserSettingsService.java
This file contains 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,11 @@ | ||
package fr.ght1pc9kc.baywatch.security.api; | ||
|
||
import fr.ght1pc9kc.baywatch.security.api.model.UserSettings; | ||
import fr.ght1pc9kc.entity.api.Entity; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface UserSettingsService { | ||
Mono<Entity<UserSettings>> get(String userId); | ||
|
||
Mono<Entity<UserSettings>> update(String userId, UserSettings userSettings); | ||
} |
8 changes: 8 additions & 0 deletions
8
sandside/src/main/java/fr/ght1pc9kc/baywatch/security/api/model/UserSettings.java
This file contains 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,8 @@ | ||
package fr.ght1pc9kc.baywatch.security.api.model; | ||
|
||
import java.util.Locale; | ||
|
||
public record UserSettings( | ||
Locale preferredLocale | ||
) { | ||
} |
23 changes: 23 additions & 0 deletions
23
sandside/src/main/java/fr/ght1pc9kc/baywatch/security/domain/UserSettingsServiceImpl.java
This file contains 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,23 @@ | ||
package fr.ght1pc9kc.baywatch.security.domain; | ||
|
||
import fr.ght1pc9kc.baywatch.security.api.UserSettingsService; | ||
import fr.ght1pc9kc.baywatch.security.api.model.UserSettings; | ||
import fr.ght1pc9kc.baywatch.security.domain.ports.UserSettingsPersistencePort; | ||
import fr.ght1pc9kc.entity.api.Entity; | ||
import lombok.RequiredArgsConstructor; | ||
import reactor.core.publisher.Mono; | ||
|
||
@RequiredArgsConstructor | ||
public class UserSettingsServiceImpl implements UserSettingsService { | ||
private final UserSettingsPersistencePort userServicePersistence; | ||
|
||
@Override | ||
public Mono<Entity<UserSettings>> get(String userId) { | ||
return userServicePersistence.get(userId); | ||
} | ||
|
||
@Override | ||
public Mono<Entity<UserSettings>> update(String userId, UserSettings userSettings) { | ||
return userServicePersistence.persist(userId, userSettings); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...rc/main/java/fr/ght1pc9kc/baywatch/security/domain/ports/UserSettingsPersistencePort.java
This file contains 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,11 @@ | ||
package fr.ght1pc9kc.baywatch.security.domain.ports; | ||
|
||
import fr.ght1pc9kc.baywatch.security.api.model.UserSettings; | ||
import fr.ght1pc9kc.entity.api.Entity; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface UserSettingsPersistencePort { | ||
Mono<Entity<UserSettings>> get(String id); | ||
|
||
Mono<Entity<UserSettings>> persist(String id, UserSettings userSettings); | ||
} |
17 changes: 17 additions & 0 deletions
17
...c/main/java/fr/ght1pc9kc/baywatch/security/infra/adapters/UserSettingsServiceAdapter.java
This file contains 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,17 @@ | ||
package fr.ght1pc9kc.baywatch.security.infra.adapters; | ||
|
||
import fr.ght1pc9kc.baywatch.security.api.UserSettingsService; | ||
import fr.ght1pc9kc.baywatch.security.domain.UserSettingsServiceImpl; | ||
import fr.ght1pc9kc.baywatch.security.domain.ports.UserSettingsPersistencePort; | ||
import lombok.experimental.Delegate; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserSettingsServiceAdapter implements UserSettingsService { | ||
@Delegate | ||
private final UserSettingsService delegate; | ||
|
||
public UserSettingsServiceAdapter(UserSettingsPersistencePort persistencePort) { | ||
this.delegate = new UserSettingsServiceImpl(persistencePort); | ||
} | ||
} |
This file contains 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 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
31 changes: 31 additions & 0 deletions
31
...rc/main/java/fr/ght1pc9kc/baywatch/security/infra/controllers/UserSettingsController.java
This file contains 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,31 @@ | ||
package fr.ght1pc9kc.baywatch.security.infra.controllers; | ||
|
||
import fr.ght1pc9kc.baywatch.security.api.UserSettingsService; | ||
import fr.ght1pc9kc.baywatch.security.api.model.UserSettings; | ||
import fr.ght1pc9kc.entity.api.Entity; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.graphql.data.method.annotation.Argument; | ||
import org.springframework.graphql.data.method.annotation.MutationMapping; | ||
import org.springframework.graphql.data.method.annotation.QueryMapping; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.stereotype.Controller; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
@PreAuthorize("isAuthenticated()") | ||
public class UserSettingsController { | ||
|
||
private final UserSettingsService userSettingsService; | ||
|
||
@QueryMapping | ||
public Mono<Entity<UserSettings>> userSettingsGet(@Argument("userId") String userId) { | ||
return userSettingsService.get(userId); | ||
} | ||
|
||
@MutationMapping | ||
public Mono<Entity<UserSettings>> userSettingsUpdate( | ||
@Argument("userId") String userId, @Argument("settings") UserSettings settings) { | ||
return userSettingsService.update(userId, settings); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...h/security/infra/adapters/UserMapper.java → ...ch/security/infra/mappers/UserMapper.java
This file contains 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
24 changes: 24 additions & 0 deletions
24
sandside/src/main/java/fr/ght1pc9kc/baywatch/security/infra/mappers/UserSettingsMapper.java
This file contains 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,24 @@ | ||
package fr.ght1pc9kc.baywatch.security.infra.mappers; | ||
|
||
import fr.ght1pc9kc.baywatch.dsl.tables.records.UsersSettingsRecord; | ||
import fr.ght1pc9kc.baywatch.security.api.model.UserSettings; | ||
import fr.ght1pc9kc.entity.api.Entity; | ||
import org.jooq.Record; | ||
import org.mapstruct.Mapper; | ||
|
||
import java.util.Locale; | ||
|
||
import static fr.ght1pc9kc.baywatch.dsl.tables.UsersSettings.USERS_SETTINGS; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface UserSettingsMapper { | ||
default Entity<UserSettings> getUserSettingsEntity(Record r) { | ||
return Entity.identify(new UserSettings(Locale.forLanguageTag(r.get(USERS_SETTINGS.USSE_PREFERRED_LOCALE)))) | ||
.withId(r.get(USERS_SETTINGS.USSE_USER_ID)); | ||
} | ||
|
||
default UsersSettingsRecord getUserSettingsRecord(UserSettings settings) { | ||
return USERS_SETTINGS.newRecord() | ||
.setUssePreferredLocale(settings.preferredLocale().toLanguageTag()); | ||
} | ||
} |
This file contains 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
47 changes: 47 additions & 0 deletions
47
...rc/main/java/fr/ght1pc9kc/baywatch/security/infra/persistence/UserSettingsRepository.java
This file contains 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,47 @@ | ||
package fr.ght1pc9kc.baywatch.security.infra.persistence; | ||
|
||
import fr.ght1pc9kc.baywatch.common.infra.DatabaseQualifier; | ||
import fr.ght1pc9kc.baywatch.dsl.tables.records.UsersSettingsRecord; | ||
import fr.ght1pc9kc.baywatch.security.api.model.UserSettings; | ||
import fr.ght1pc9kc.baywatch.security.domain.ports.UserSettingsPersistencePort; | ||
import fr.ght1pc9kc.baywatch.security.infra.mappers.UserSettingsMapper; | ||
import fr.ght1pc9kc.entity.api.Entity; | ||
import lombok.RequiredArgsConstructor; | ||
import org.jooq.DSLContext; | ||
import org.springframework.stereotype.Repository; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Scheduler; | ||
|
||
import static fr.ght1pc9kc.baywatch.dsl.tables.UsersSettings.USERS_SETTINGS; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
@SuppressWarnings("BlockingMethodInNonBlockingContext") | ||
public class UserSettingsRepository implements UserSettingsPersistencePort { | ||
private final DSLContext dsl; | ||
private final @DatabaseQualifier Scheduler dbScheduler; | ||
private final UserSettingsMapper mapper; | ||
|
||
public Mono<Entity<UserSettings>> get(String id) { | ||
return Mono.fromCallable(() -> | ||
dsl.select() | ||
.from(USERS_SETTINGS) | ||
.where(USERS_SETTINGS.USSE_USER_ID.eq(id)) | ||
.fetchOne()).subscribeOn(dbScheduler) | ||
.map(mapper::getUserSettingsEntity); | ||
} | ||
|
||
public Mono<Entity<UserSettings>> persist(String id, UserSettings userSettings) { | ||
UsersSettingsRecord userSettingsRecord = mapper.getUserSettingsRecord(userSettings); | ||
UsersSettingsRecord updateRecord = userSettingsRecord.copy(); | ||
userSettingsRecord.setUsseUserId(id); | ||
return Mono.fromCallable(() -> | ||
dsl.insertInto(USERS_SETTINGS) | ||
.set(userSettingsRecord) | ||
.onDuplicateKeyUpdate() | ||
.set(updateRecord) | ||
.execute()) | ||
.subscribeOn(dbScheduler) | ||
.then(get(id)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
sandside/src/main/resources/db/migration/V2_1_202408181554__user_settings.sql
This file contains 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,8 @@ | ||
create table USERS_SETTINGS | ||
( | ||
USSE_USER_ID VARCHAR(64) not null primary key, | ||
USSE_PREFERRED_LOCALE TEXT, | ||
|
||
constraint FK_USSE_USER_ID | ||
foreign key (USSE_USER_ID) references USERS (USER_ID) | ||
); |
This file contains 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
7 changes: 7 additions & 0 deletions
7
sandside/src/main/resources/graphql/security/settings.graphqls
This file contains 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,7 @@ | ||
extend type Mutation { | ||
userSettingsUpdate(userId: ID): UserSettings | ||
} | ||
|
||
extend type Query { | ||
userSettingsGet(userSettings: UserSettings): UserSettings | ||
} |
This file contains 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 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