Skip to content

Commit

Permalink
fix(persistence): fix errors due mapping and incorrect saving to tables
Browse files Browse the repository at this point in the history
  • Loading branch information
bas-kirill committed Sep 2, 2024
1 parent 0483f82 commit 7c3ae25
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 91 deletions.
31 changes: 19 additions & 12 deletions client/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const I18N_INSTRUMENT_DATE_TO = "instrument.date.to";
export const I18N_INSTRUMENT_CARD_SHOW_BUTTON = "instrument.card.show.button";
export const I18N_INSTRUMENT_CARD_REMOVE_BUTTON = "instrument.remove.button";
export const I18N_INSTRUMENT_CARD_EDIT_BUTTON = "instrument.edit.button";
export const I18N_INSTRUMENT_CARD_FAVORITE_BUTTON = "instrument.favorite.button";
export const I18N_INSTRUMENT_CARD_FAVORITE_BUTTON =
"instrument.favorite.button";

export const I18N_LOGIN_INPUT = "login.login.input";
export const I18N_LOGIN_PASSWORD_INPUT = "login.login.password";
Expand Down Expand Up @@ -63,9 +64,12 @@ const resources = {
[I18N_HOME_SEARCH_BAR_INPUT]: "What instrument?",
[I18N_HOME_SEARCH_BAR_BUTTON]: "Search",
[I18N_REASONS_H1]: "Why Choose Us for Your Musical Needs",
[I18N_REASONS_FIRST]: "We offer a wide range of high-quality instruments for all skill levels",
[I18N_REASONS_SECOND]: "Our expert staff provides personalized advice and service",
[I18N_REASONS_THIRD]: "Enjoy competitive prices and exclusive deals on top brands",
[I18N_REASONS_FIRST]:
"We offer a wide range of high-quality instruments for all skill levels",
[I18N_REASONS_SECOND]:
"Our expert staff provides personalized advice and service",
[I18N_REASONS_THIRD]:
"Enjoy competitive prices and exclusive deals on top brands",
[I18N_TRENDS_H1]: "Trending Instruments",
[I18N_INSTRUMENT_TYPE_FILTER]: "Type",
[I18N_INSTRUMENT_CARD_MANUFACTURER]: "Manufacturer",
Expand Down Expand Up @@ -98,7 +102,7 @@ const resources = {
[I18N_NAVBAR_NEXT]: "Next",

[I18N_FAVORITE_H1]: "Favorite",
}
},
},
ru: {
translation: {
Expand All @@ -111,9 +115,12 @@ const resources = {
[I18N_HOME_SEARCH_BAR_INPUT]: "Какой инструмент?",
[I18N_HOME_SEARCH_BAR_BUTTON]: "Поиск",
[I18N_REASONS_H1]: "Почему вы выберете нас",
[I18N_REASONS_FIRST]: "Мы предлагаем широкий ассортимент высококачественных инструментов для всех уровней квалификации",
[I18N_REASONS_SECOND]: "Наш квалифицированный персонал предоставляет индивидуальные консультации и обслуживание",
[I18N_REASONS_THIRD]: "Наслаждайтесь конкурентоспособными ценами и эксклюзивными предложениями от ведущих брендов",
[I18N_REASONS_FIRST]:
"Мы предлагаем широкий ассортимент высококачественных инструментов для всех уровней квалификации",
[I18N_REASONS_SECOND]:
"Наш квалифицированный персонал предоставляет индивидуальные консультации и обслуживание",
[I18N_REASONS_THIRD]:
"Наслаждайтесь конкурентоспособными ценами и эксклюзивными предложениями от ведущих брендов",
[I18N_TRENDS_H1]: "Тренды",
[I18N_INSTRUMENT_TYPE_FILTER]: "Тип",
[I18N_INSTRUMENT_CARD_MANUFACTURER]: "Производитель",
Expand Down Expand Up @@ -144,8 +151,8 @@ const resources = {
[I18N_LOGOUT_BUTTON]: "Выйти",

[I18N_FAVORITE_H1]: "Любимое",
}
}
},
},
};

i18n
Expand All @@ -155,8 +162,8 @@ i18n
lng: window.navigator.language,
fallbackLng: "en",
interpolation: {
escapeValue: false // react already safes from xss
}
escapeValue: false, // react already safes from xss
},
});

export default i18n;
27 changes: 15 additions & 12 deletions client/src/pages/home/ui/Home.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,46 @@ import {
I18N_HOME_SEARCH_BAR_BUTTON,
I18N_HOME_SEARCH_BAR_INPUT,
I18N_REASONS_FIRST,
I18N_REASONS_H1, I18N_REASONS_SECOND, I18N_REASONS_THIRD, I18N_TRENDS_H1
I18N_REASONS_H1,
I18N_REASONS_SECOND,
I18N_REASONS_THIRD,
I18N_TRENDS_H1,
} from "../../../i18n";

const images = [
{
image: saxophone,
caption: "Saxophone"
caption: "Saxophone",
},
{
image: guitar,
caption: "Guitar"
caption: "Guitar",
},
{
image: rock_guitar,
caption: "Rock Guitar"
caption: "Rock Guitar",
},
{
image: violin,
caption: "Violin"
}
caption: "Violin",
},
];

const trendingInstrumentsResponsiveSettings = [
{
breakpoint: 571,
settings: {
slidesToShow: 3,
slidesToScroll: 1
}
slidesToScroll: 1,
},
},
{
breakpoint: 570,
settings: {
slidesToShow: 1,
slidesToScroll: 1
}
}
slidesToScroll: 1,
},
},
];

export function HomePage() {
Expand All @@ -66,7 +69,7 @@ export function HomePage() {
<div
className={styles.home_logo}
style={{
background: `url(${homeLogo}) no-repeat center, linear-gradient(blue, cyan)`
background: `url(${homeLogo}) no-repeat center, linear-gradient(blue, cyan)`,
}}
>
<form
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/login/ui/Login.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
I18N_LOGIN_BUTTON,
I18N_LOGIN_INPUT,
I18N_LOGIN_PASSWORD_INPUT,
I18N_REGISTRATION_BUTTON
I18N_REGISTRATION_BUTTON,
} from "../../../i18n";

export function LoginPage() {
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/profile/ui/Profile.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
I18N_DARK_MODE_BUTTON,
I18N_LOGOUT_BUTTON,
I18N_PROFILE_NAME_SPAN,
I18N_PROFILE_ROLE_SPAN
I18N_PROFILE_ROLE_SPAN,
} from "../../../i18n";
import { useTranslation } from "react-i18next";

Expand Down
11 changes: 5 additions & 6 deletions client/src/shared/instrument-card-actions/ui/Favorite.button.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React, { useEffect, useRef } from "react";
import React, { useEffect, useState } from "react";
import styles from "./styles/Favorite.button.module.css";
import actionBtnStyle from "./styles/Action.button.module.css";
import { useState } from "react";
import {
AddFavoriteApi,
ListFavoriteApi,
RemoveFavoriteApi,
} from "generated/api";
import { InstrumentId } from "generated/model";
import Jwt from "domain/model/jwt";
import { COOKIE_JWT_KEY } from "shared/config/frontend";
import { getCookie } from "shared/cookie/cookie";
import { apiConfig } from "shared/config/api";
import { useTranslation } from "react-i18next";
import { I18N_HEADER_FAVORITE_BUTTON, I18N_INSTRUMENT_CARD_REMOVE_BUTTON } from "../../../i18n";
import {
I18N_HEADER_FAVORITE_BUTTON,

} from "../../../i18n";

interface Props {
instrumentId: InstrumentId;
Expand All @@ -25,7 +25,6 @@ const addFavorite = new AddFavoriteApi(apiConfig);

export const FavoriteButton = (props: Props) => {
const { t } = useTranslation();
const jwt = useRef<string | undefined>(getCookie(COOKIE_JWT_KEY));
const [favorite, setFavorite] = useState<boolean>();

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { useDarkMode } from "shared/dark-mode/use-dark-mode";
import { apiConfig } from "shared/config/api";
import Jwt from "domain/model/jwt";
import { DeleteInstrumentByIdApi } from "generated/api/delete-instrument-by-id-api";
import { I18N_INSTRUMENT_CARD_EDIT_BUTTON, I18N_INSTRUMENT_CARD_REMOVE_BUTTON } from "../../../i18n";
import {
I18N_INSTRUMENT_CARD_REMOVE_BUTTON,
} from "../../../i18n";
import { useTranslation } from "react-i18next";

interface Props {
Expand Down
3 changes: 2 additions & 1 deletion client/src/widgets/header/ui/Header.widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
I18N_HEADER_CATALOGUE_BUTTON,
I18N_HEADER_FAVORITE_BUTTON,
I18N_HEADER_HOME_BUTTON,
I18N_HEADER_LOGIN_BUTTON, I18N_HEADER_PROFILE_BUTTON
I18N_HEADER_LOGIN_BUTTON,
I18N_HEADER_PROFILE_BUTTON,
} from "../../../i18n";
import { useTranslation } from "react-i18next";

Expand Down
10 changes: 2 additions & 8 deletions server/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import io.gitlab.arturbosch.detekt.Detekt
import org.jooq.meta.jaxb.MatcherRule
import org.jooq.meta.jaxb.MatcherTransformType
import org.jooq.meta.jaxb.Matchers
import org.jooq.meta.jaxb.MatchersTableType
import org.jooq.meta.jaxb.Property

val rootProjectDir = "$projectDir/../.."
Expand Down Expand Up @@ -70,10 +66,8 @@ dependencies {
implementation("jakarta.validation:jakarta.validation-api:3.1.0") // `useSpringBoot3` param requires it
implementation("org.postgresql:postgresql")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
// implementation("javax.annotation:jsr305:1.0")
jooqGenerator("org.postgresql:postgresql")
implementation("org.jooq:jooq:3.19.11")
jooqGenerator("org.jooq:jooq-meta-extensions:3.19.11")
jooqGenerator("org.jooq:jooq-codegen:3.19.11")
}

tasks.named<Test>("test") {
Expand Down Expand Up @@ -172,7 +166,7 @@ jooq {
properties.apply {
add(Property().apply {
key = "scripts"
value = "src/main/resources/db/schema.sql"
value = "./src/main/resources/db/schema.sql"
})
add(Property().apply {
key = "defaultNameCase"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import mu.muse.persistence.instrument.jooq.JooqPostgresInstrumentRepository
import mu.muse.persistence.user.jooq.JooqPostgresUserIdGenerator
import mu.muse.persistence.user.jooq.JooqPostgresUserRepository
import org.jooq.DSLContext
import org.jooq.SQLDialect
import org.jooq.impl.DSL
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import javax.sql.DataSource

@Configuration
class PersistenceConfiguration {
Expand Down Expand Up @@ -37,10 +40,14 @@ class PersistenceConfiguration {
}

@Bean
fun dataSource(hikariConfig: HikariConfig) = with(HikariDataSource(hikariConfig)) {
maximumPoolSize = MAXIMUM_POOL_SIZE
this
}
fun dataSource(hikariConfig: HikariConfig): DataSource =
with(HikariDataSource(hikariConfig)) {
maximumPoolSize = MAXIMUM_POOL_SIZE
this
}

@Bean
fun dslContext(dataSource: DataSource): DSLContext = DSL.using(dataSource, SQLDialect.POSTGRES)

@Bean
fun userIdGenerator(dslContext: DSLContext) = JooqPostgresUserIdGenerator(dslContext)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package mu.muse.persistence.instrument.jooq

import mu.muse.codegen.jooq.sequences.INSTRUMENT_ID_SEQ
import mu.muse.codegen.jooq.public.sequences.INSTRUMENT_ID_SEQ
import mu.muse.domain.IdGenerator
import mu.muse.domain.instrument.InstrumentId
import org.jooq.DSLContext
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate

class JooqPostgresInstrumentIdGenerator(
private val dslContext: DSLContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mu.muse.persistence.instrument.jooq

import mu.muse.codegen.jooq.tables.records.InstrumentsRecord
import mu.muse.codegen.jooq.tables.references.INSTRUMENTS
import mu.muse.codegen.jooq.public.tables.Instruments.Companion.INSTRUMENTS
import mu.muse.codegen.jooq.public.tables.pojos.Instruments
import mu.muse.common.types.Version
import mu.muse.domain.instrument.Country
import mu.muse.domain.instrument.Instrument
Expand All @@ -19,6 +19,7 @@ import mu.muse.usecase.access.instrument.InstrumentRemover
import org.jooq.DSLContext
import org.jooq.impl.DSL.selectCount
import java.time.OffsetDateTime
import java.time.ZoneOffset

class JooqPostgresInstrumentRepository(
private val dslContext: DSLContext,
Expand All @@ -27,15 +28,15 @@ class JooqPostgresInstrumentRepository(
override fun findAll(): List<Instrument> {
val instrumentRecords = dslContext
.selectFrom(INSTRUMENTS)
.fetchInto(InstrumentsRecord::class.java)
.fetchInto(Instruments::class.java)
return instrumentRecords.map { it.toInstrument() }
}

override fun findById(id: InstrumentId): Instrument? {
val instrumentRecordResult = runCatching {
dslContext
.selectFrom(INSTRUMENTS)
.fetchSingle()
.fetchSingleInto(Instruments::class.java)
}

val instrumentRecordRaw = instrumentRecordResult.getOrNull() ?: return null
Expand All @@ -49,15 +50,15 @@ class JooqPostgresInstrumentRepository(
val instrumentRecords = dslContext
.selectFrom(INSTRUMENTS)
.where(INSTRUMENTS.INSTRUMENT_ID.`in`(ids.map { it.toLongValue() }))
.fetchInto(InstrumentsRecord::class.java)
.fetchInto(Instruments::class.java)
return instrumentRecords.map { it.toInstrument() }
}


override fun findByCriteria(criteria: InstrumentExtractor.Criteria): List<Instrument> {
val instrumentRecords = dslContext
.selectFrom(INSTRUMENTS)
.fetchInto(InstrumentsRecord::class.java)
.fetchInto(Instruments::class.java)
val instruments = instrumentRecords.map { it.toInstrument() }
return instruments.filter { it matches criteria }
}
Expand Down Expand Up @@ -91,8 +92,8 @@ class JooqPostgresInstrumentRepository(
instrument.name.toStringValue(),
instrument.type.i18nCode,
instrument.manufacturerType.i18nCode,
OffsetDateTime.from(instrument.manufactureDate.toInstantValue()),
OffsetDateTime.from(instrument.releaseDate.toInstantValue()),
OffsetDateTime.ofInstant(instrument.manufactureDate.toInstantValue(), ZoneOffset.UTC),
OffsetDateTime.ofInstant(instrument.releaseDate.toInstantValue(), ZoneOffset.UTC),
instrument.country.i18nCode,
instrument.materialTypes.map { it.i18nCode }.toTypedArray(),
instrument.image.toStringValue(),
Expand All @@ -102,17 +103,18 @@ class JooqPostgresInstrumentRepository(
.set(INSTRUMENTS.INSTRUMENT_NAME, instrument.name.toStringValue())
.set(INSTRUMENTS.INSTRUMENT_I18N_CODE, instrument.type.i18nCode)
.set(INSTRUMENTS.MANUFACTURER_I18N_CODE, instrument.manufacturerType.i18nCode)
.set(INSTRUMENTS.MANUFACTURER_DATE, OffsetDateTime.from(instrument.manufactureDate.toInstantValue()))
.set(INSTRUMENTS.RELEASE_DATE, OffsetDateTime.from(instrument.releaseDate.toInstantValue()))
.set(INSTRUMENTS.MANUFACTURER_DATE, OffsetDateTime.ofInstant(instrument.manufactureDate.toInstantValue(), ZoneOffset.UTC))
.set(INSTRUMENTS.RELEASE_DATE, OffsetDateTime.ofInstant(instrument.releaseDate.toInstantValue(), ZoneOffset.UTC))
.set(INSTRUMENTS.COUNTRY_I18N_CODE, instrument.country.i18nCode)
.set(INSTRUMENTS.MATERIALS, instrument.materialTypes.map { it.i18nCode }.toTypedArray())
.set(INSTRUMENTS.IMAGE, instrument.image.toStringValue())
.execute()
}
}


fun Array<String?>.toBasicMaterials() = this.toList().map { Material.Type.fromI18nCode(it) }
fun InstrumentsRecord.toInstrument() = Instrument(
fun Instruments.toInstrument() = Instrument(
id = InstrumentId.from(this.instrumentId),
name = InstrumentName.from(this.instrumentName),
type = Instrument.Type.fromI18nCode(this.instrumentI18nCode),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mu.muse.persistence.user.jooq

import mu.muse.codegen.jooq.sequences.USER_ID_SEQ
import mu.muse.codegen.jooq.public.sequences.USER_ID_SEQ
import mu.muse.domain.IdGenerator
import mu.muse.domain.user.UserId
import org.jooq.DSLContext
Expand Down
Loading

0 comments on commit 7c3ae25

Please sign in to comment.