mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-06-08 16:44:21 +02:00
Factorize service accessors
This commit is contained in:
parent
1844c6acad
commit
c64bf376f8
@ -6,147 +6,146 @@ import { axiosConfig } from "./auth"
|
|||||||
|
|
||||||
export type ElementWithId = unknown & { id: number }
|
export type ElementWithId = unknown & { id: number }
|
||||||
|
|
||||||
export type ElementTranslation = { [englishProp: string]: string }
|
export type ElementTranslation<Element> = { [k in keyof Element]: string }
|
||||||
|
|
||||||
export function get<Element>(
|
export default function getServiceAccessors<
|
||||||
elementName: string,
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
translation: ElementTranslation
|
ElementNoId extends object,
|
||||||
): (id: number) => Promise<{
|
Element extends ElementNoId & ElementWithId
|
||||||
data?: Element
|
>(elementName: string, translation: { [k in keyof Element]: string }): any {
|
||||||
error?: Error
|
function get(): (id: number) => Promise<{
|
||||||
}> {
|
|
||||||
interface ElementGetResponse {
|
|
||||||
data?: Element
|
data?: Element
|
||||||
error?: Error
|
error?: Error
|
||||||
}
|
}> {
|
||||||
return async (id: number): Promise<ElementGetResponse> => {
|
interface ElementGetResponse {
|
||||||
try {
|
data?: Element
|
||||||
const { data } = await axios.get(`${config.API_URL}/${elementName}Get`, {
|
error?: Error
|
||||||
...axiosConfig,
|
}
|
||||||
params: { id },
|
return async (id: number): Promise<ElementGetResponse> => {
|
||||||
})
|
try {
|
||||||
if (!data) {
|
const { data } = await axios.get(`${config.API_URL}/${elementName}Get`, {
|
||||||
return { data }
|
...axiosConfig,
|
||||||
|
params: { id },
|
||||||
|
})
|
||||||
|
if (!data) {
|
||||||
|
return { data }
|
||||||
|
}
|
||||||
|
const englishData = _.mapValues(
|
||||||
|
translation,
|
||||||
|
(frenchProp: string) => data[frenchProp]
|
||||||
|
) as Element
|
||||||
|
return { data: englishData }
|
||||||
|
} catch (error) {
|
||||||
|
return { error: error as Error }
|
||||||
}
|
}
|
||||||
const englishData = _.mapValues(
|
|
||||||
translation,
|
|
||||||
(frenchProp: string) => data[frenchProp]
|
|
||||||
) as Element
|
|
||||||
return { data: englishData }
|
|
||||||
} catch (error) {
|
|
||||||
return { error: error as Error }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export function listGet<Element>(
|
function listGet(): () => Promise<{
|
||||||
elementName: string,
|
|
||||||
translation: ElementTranslation
|
|
||||||
): () => Promise<{
|
|
||||||
data?: Element[]
|
|
||||||
error?: Error
|
|
||||||
}> {
|
|
||||||
interface ElementListGetResponse {
|
|
||||||
data?: Element[]
|
data?: Element[]
|
||||||
error?: Error
|
error?: Error
|
||||||
}
|
}> {
|
||||||
return async (): Promise<ElementListGetResponse> => {
|
interface ElementListGetResponse {
|
||||||
try {
|
data?: Element[]
|
||||||
const { data } = await axios.get(`${config.API_URL}/${elementName}ListGet`, axiosConfig)
|
error?: Error
|
||||||
if (!data) {
|
}
|
||||||
return { data }
|
return async (): Promise<ElementListGetResponse> => {
|
||||||
}
|
try {
|
||||||
|
const { data } = await axios.get(
|
||||||
|
`${config.API_URL}/${elementName}ListGet`,
|
||||||
|
axiosConfig
|
||||||
|
)
|
||||||
|
if (!data) {
|
||||||
|
return { data }
|
||||||
|
}
|
||||||
|
|
||||||
const englishDataList = data.map(
|
const englishDataList = data.map(
|
||||||
(frenchData: any) =>
|
(frenchData: any) =>
|
||||||
_.mapValues(
|
_.mapValues(
|
||||||
translation,
|
translation,
|
||||||
(frenchProp: string) => frenchData[frenchProp]
|
(frenchProp: string) => frenchData[frenchProp]
|
||||||
) as Element
|
) as Element
|
||||||
)
|
)
|
||||||
return { data: englishDataList }
|
return { data: englishDataList }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return { error: error as Error }
|
return { error: error as Error }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
export function add<ElementNoId extends object, Element extends ElementNoId & ElementWithId>(
|
function add(): (volunteerWithoutId: ElementNoId) => Promise<{
|
||||||
elementName: string,
|
|
||||||
translation: ElementTranslation
|
|
||||||
): (volunteerWithoutId: ElementNoId) => Promise<{
|
|
||||||
data?: Element
|
|
||||||
error?: Error
|
|
||||||
}> {
|
|
||||||
interface ElementGetResponse {
|
|
||||||
data?: Element
|
data?: Element
|
||||||
error?: Error
|
error?: Error
|
||||||
}
|
}> {
|
||||||
return async (volunteerWithoutId: ElementNoId): Promise<ElementGetResponse> => {
|
interface ElementGetResponse {
|
||||||
try {
|
data?: Element
|
||||||
const invertedTranslationWithoutId = _.invert(_.omit(translation, "id"))
|
error?: Error
|
||||||
const frenchDataWithoutId = _.mapValues(
|
}
|
||||||
invertedTranslationWithoutId,
|
return async (volunteerWithoutId: ElementNoId): Promise<ElementGetResponse> => {
|
||||||
(englishProp: string, _frenchProp: string) =>
|
try {
|
||||||
(volunteerWithoutId as any)[englishProp]
|
const invertedTranslationWithoutId = _.invert(_.omit(translation, "id"))
|
||||||
)
|
const frenchDataWithoutId = _.mapValues(
|
||||||
|
invertedTranslationWithoutId,
|
||||||
|
(englishProp: string, _frenchProp: string) =>
|
||||||
|
(volunteerWithoutId as any)[englishProp]
|
||||||
|
)
|
||||||
|
|
||||||
const { data } = await axios.post(
|
const { data } = await axios.post(
|
||||||
`${config.API_URL}/${elementName}Add`,
|
`${config.API_URL}/${elementName}Add`,
|
||||||
frenchDataWithoutId,
|
frenchDataWithoutId,
|
||||||
axiosConfig
|
axiosConfig
|
||||||
)
|
)
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return { data }
|
return { data }
|
||||||
|
}
|
||||||
|
|
||||||
|
const englishData = _.mapValues(
|
||||||
|
translation,
|
||||||
|
(frenchProp: string) => data[frenchProp]
|
||||||
|
) as Element
|
||||||
|
return { data: englishData }
|
||||||
|
} catch (error) {
|
||||||
|
return { error: error as Error }
|
||||||
}
|
}
|
||||||
|
|
||||||
const englishData = _.mapValues(
|
|
||||||
translation,
|
|
||||||
(frenchProp: string) => data[frenchProp]
|
|
||||||
) as Element
|
|
||||||
return { data: englishData }
|
|
||||||
} catch (error) {
|
|
||||||
return { error: error as Error }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export function set<Element>(
|
function set(): (volunteer: Element) => Promise<{
|
||||||
elementName: string,
|
|
||||||
translation: ElementTranslation
|
|
||||||
): (volunteer: Element) => Promise<{
|
|
||||||
data?: Element
|
|
||||||
error?: Error
|
|
||||||
}> {
|
|
||||||
interface ElementGetResponse {
|
|
||||||
data?: Element
|
data?: Element
|
||||||
error?: Error
|
error?: Error
|
||||||
}
|
}> {
|
||||||
return async (volunteer: Element): Promise<ElementGetResponse> => {
|
interface ElementGetResponse {
|
||||||
try {
|
data?: Element
|
||||||
const invertedTranslation = _.invert(translation)
|
error?: Error
|
||||||
const frenchData = _.mapValues(
|
}
|
||||||
invertedTranslation,
|
return async (volunteer: Element): Promise<ElementGetResponse> => {
|
||||||
(englishProp: string) => (volunteer as any)[englishProp]
|
try {
|
||||||
)
|
const invertedTranslation = _.invert(translation)
|
||||||
|
const frenchData = _.mapValues(
|
||||||
|
invertedTranslation,
|
||||||
|
(englishProp: string) => (volunteer as any)[englishProp]
|
||||||
|
)
|
||||||
|
|
||||||
const { data } = await axios.post(
|
const { data } = await axios.post(
|
||||||
`${config.API_URL}/${elementName}Set`,
|
`${config.API_URL}/${elementName}Set`,
|
||||||
frenchData,
|
frenchData,
|
||||||
axiosConfig
|
axiosConfig
|
||||||
)
|
)
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return { data }
|
return { data }
|
||||||
|
}
|
||||||
|
|
||||||
|
const englishData = _.mapValues(
|
||||||
|
translation,
|
||||||
|
(frenchProp: string) => data[frenchProp]
|
||||||
|
) as Element
|
||||||
|
return { data: englishData }
|
||||||
|
} catch (error) {
|
||||||
|
return { error: error as Error }
|
||||||
}
|
}
|
||||||
|
|
||||||
const englishData = _.mapValues(
|
|
||||||
translation,
|
|
||||||
(frenchProp: string) => data[frenchProp]
|
|
||||||
) as Element
|
|
||||||
return { data: englishData }
|
|
||||||
} catch (error) {
|
|
||||||
return { error: error as Error }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return { listGet, get, set, add }
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { get, listGet, add, set } from "./accessors"
|
import getServiceAccessors from "./accessors"
|
||||||
|
|
||||||
export class JavGame {
|
export class JavGame {
|
||||||
id = 0
|
id = 0
|
||||||
@ -54,10 +54,12 @@ const elementName = "JavGame"
|
|||||||
|
|
||||||
export type JavGameWithoutId = Omit<JavGame, "id">
|
export type JavGameWithoutId = Omit<JavGame, "id">
|
||||||
|
|
||||||
export const javGameGet = get<JavGame>(elementName, translationJavGame)
|
const { listGet, get, set, add } = getServiceAccessors<JavGameWithoutId, JavGame>(
|
||||||
|
elementName,
|
||||||
|
translationJavGame
|
||||||
|
)
|
||||||
|
|
||||||
export const javGameListGet = listGet<JavGame>(elementName, translationJavGame)
|
export const javGameListGet = listGet()
|
||||||
|
export const javGameGet = get()
|
||||||
export const javGameAdd = add<JavGameWithoutId, JavGame>(elementName, translationJavGame)
|
export const javGameAdd = add()
|
||||||
|
export const javGameSet = set()
|
||||||
export const javGameSet = set<JavGame>(elementName, translationJavGame)
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { get, listGet, add, set } from "./accessors"
|
import getServiceAccessors from "./accessors"
|
||||||
|
|
||||||
export class PreMember {
|
export class PreMember {
|
||||||
id = 0
|
id = 0
|
||||||
@ -30,10 +30,12 @@ const elementName = "PreMember"
|
|||||||
|
|
||||||
export type PreMemberWithoutId = Omit<PreMember, "id">
|
export type PreMemberWithoutId = Omit<PreMember, "id">
|
||||||
|
|
||||||
export const preMemberGet = get<PreMember>(elementName, translationPreMember)
|
const { listGet, get, set, add } = getServiceAccessors<PreMemberWithoutId, PreMember>(
|
||||||
|
elementName,
|
||||||
|
translationPreMember
|
||||||
|
)
|
||||||
|
|
||||||
export const preMemberListGet = listGet<PreMember>(elementName, translationPreMember)
|
export const preMemberListGet = listGet()
|
||||||
|
export const preMemberGet = get()
|
||||||
export const preMemberAdd = add<PreMemberWithoutId, PreMember>(elementName, translationPreMember)
|
export const preMemberAdd = add()
|
||||||
|
export const preMemberSet = set()
|
||||||
export const preMemberSet = set<PreMember>(elementName, translationPreMember)
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { get, listGet, add, set } from "./accessors"
|
import getServiceAccessors from "./accessors"
|
||||||
|
|
||||||
export class Volunteer {
|
export class Volunteer {
|
||||||
id = 0
|
id = 0
|
||||||
@ -60,10 +60,12 @@ export interface MemberLogin {
|
|||||||
|
|
||||||
export type VolunteerWithoutId = Omit<Volunteer, "id">
|
export type VolunteerWithoutId = Omit<Volunteer, "id">
|
||||||
|
|
||||||
export const volunteerGet = get<Volunteer>(elementName, translationMember)
|
const { listGet, get, set, add } = getServiceAccessors<VolunteerWithoutId, Volunteer>(
|
||||||
|
elementName,
|
||||||
|
translationMember
|
||||||
|
)
|
||||||
|
|
||||||
export const volunteerListGet = listGet<Volunteer>(elementName, translationMember)
|
export const volunteerListGet = listGet()
|
||||||
|
export const volunteerGet = get()
|
||||||
export const volunteerAdd = add<VolunteerWithoutId, Volunteer>(elementName, translationMember)
|
export const volunteerAdd = add()
|
||||||
|
export const volunteerSet = set()
|
||||||
export const volunteerSet = set<Volunteer>(elementName, translationMember)
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { get, listGet, add, set } from "./accessors"
|
import getServiceAccessors from "./accessors"
|
||||||
|
|
||||||
export class Wish {
|
export class Wish {
|
||||||
id = 0
|
id = 0
|
||||||
@ -27,10 +27,12 @@ const elementName = "Wish"
|
|||||||
|
|
||||||
export type WishWithoutId = Omit<Wish, "id">
|
export type WishWithoutId = Omit<Wish, "id">
|
||||||
|
|
||||||
export const wishGet = get<Wish>(elementName, translationWish)
|
const { listGet, get, set, add } = getServiceAccessors<WishWithoutId, Wish>(
|
||||||
|
elementName,
|
||||||
|
translationWish
|
||||||
|
)
|
||||||
|
|
||||||
export const wishListGet = listGet<Wish>(elementName, translationWish)
|
export const wishListGet = listGet()
|
||||||
|
export const wishGet = get()
|
||||||
export const wishAdd = add<WishWithoutId, Wish>(elementName, translationWish)
|
export const wishAdd = add()
|
||||||
|
export const wishSet = set()
|
||||||
export const wishSet = set<Wish>(elementName, translationWish)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user