Skip to content

Commit

Permalink
feat(sandside): #225 add UserSettings table and repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Marthym committed Aug 18, 2024
1 parent 77ad9e0 commit 229d0e9
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 6 deletions.
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);
}
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
) {
}
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);
}
}
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);
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import fr.ght1pc9kc.baywatch.security.api.PasswordService;
import fr.ght1pc9kc.baywatch.security.api.model.PasswordEvaluation;
import fr.ght1pc9kc.baywatch.security.infra.adapters.UserMapper;
import fr.ght1pc9kc.baywatch.security.infra.mappers.UserMapper;
import fr.ght1pc9kc.baywatch.security.infra.model.UserForm;
import lombok.RequiredArgsConstructor;
import org.springframework.graphql.data.method.annotation.Argument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import fr.ght1pc9kc.baywatch.security.api.UserService;
import fr.ght1pc9kc.baywatch.security.api.model.Permission;
import fr.ght1pc9kc.baywatch.security.api.model.User;
import fr.ght1pc9kc.baywatch.security.infra.adapters.UserMapper;
import fr.ght1pc9kc.baywatch.security.infra.mappers.UserMapper;
import fr.ght1pc9kc.baywatch.security.infra.exceptions.AlreadyExistsException;
import fr.ght1pc9kc.baywatch.security.infra.model.UserForm;
import fr.ght1pc9kc.baywatch.security.infra.model.UserSearchRequest;
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.ght1pc9kc.baywatch.security.infra.adapters;
package fr.ght1pc9kc.baywatch.security.infra.mappers;

import fr.ght1pc9kc.baywatch.common.domain.DateUtils;
import fr.ght1pc9kc.baywatch.dsl.tables.records.UsersRecord;
Expand Down
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import fr.ght1pc9kc.baywatch.security.api.model.User;
import fr.ght1pc9kc.baywatch.security.domain.exceptions.ConstraintViolationPersistenceException;
import fr.ght1pc9kc.baywatch.security.domain.ports.UserPersistencePort;
import fr.ght1pc9kc.baywatch.security.infra.adapters.UserMapper;
import fr.ght1pc9kc.baywatch.security.infra.mappers.UserMapper;
import fr.ght1pc9kc.entity.api.Entity;
import fr.ght1pc9kc.juery.jooq.filter.JooqConditionVisitor;
import fr.ght1pc9kc.juery.jooq.pagination.JooqPagination;
Expand Down
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));
}
}
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)
);
9 changes: 9 additions & 0 deletions sandside/src/main/resources/graphql/security/_model.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ type User {
roles: [String]
}

type UserSettings {
_id: ID
preferredLocale: String
}

input UserSettingsForm {
preferredLocale: String
}

type Session {
user: User
maxAge: Int
Expand Down
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import fr.ght1pc9kc.baywatch.security.api.UserService;
import fr.ght1pc9kc.baywatch.security.api.model.Permission;
import fr.ght1pc9kc.baywatch.security.api.model.User;
import fr.ght1pc9kc.baywatch.security.infra.adapters.UserMapper;
import fr.ght1pc9kc.baywatch.security.infra.mappers.UserMapper;
import fr.ght1pc9kc.baywatch.security.infra.config.PermissionMixin;
import fr.ght1pc9kc.baywatch.security.infra.config.UserMixin;
import fr.ght1pc9kc.baywatch.security.infra.model.UserForm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import fr.ght1pc9kc.baywatch.security.api.model.Role;
import fr.ght1pc9kc.baywatch.security.api.model.User;
import fr.ght1pc9kc.baywatch.security.domain.exceptions.ConstraintViolationPersistenceException;
import fr.ght1pc9kc.baywatch.security.infra.adapters.UserMapper;
import fr.ght1pc9kc.baywatch.security.infra.mappers.UserMapper;
import fr.ght1pc9kc.baywatch.tests.samples.UserSamples;
import fr.ght1pc9kc.baywatch.tests.samples.infra.FeedRecordSamples;
import fr.ght1pc9kc.baywatch.tests.samples.infra.FeedsUsersRecordSample;
Expand Down

0 comments on commit 229d0e9

Please sign in to comment.