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