Skip to content

Commit

Permalink
Merge pull request #289 from traPtitech/refactor/validation
Browse files Browse the repository at this point in the history
♻️ Repository にあったバリデーションを usecase で行う
  • Loading branch information
anko9801 authored Sep 7, 2024
2 parents ca982c7 + d17c5da commit 6340d93
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
8 changes: 0 additions & 8 deletions src/features/request/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ export const useRequestRepository = () => {

const createRequestRepository = () => ({
fetchRequests: async (querySeed: RequestQuerySeed): Promise<Request[]> => {
const rule = /^2[0-9]{3}-[0-9]{1,2}-[0-9]{1,2}$/
if (
(querySeed.since && !rule.test(querySeed.since)) ||
(querySeed.until && !rule.test(querySeed.until))
) {
throw new Error('日付はyyyy-MM-ddの形式で入力してください')
}

const { data } = await apis.getRequests(
querySeed.sort,
querySeed.currentStatus !== '' ? querySeed.currentStatus : undefined,
Expand Down
13 changes: 10 additions & 3 deletions src/features/request/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ import { createTagIfNotExistUsecase } from '/@/features/tag/usecase'

export const useFetchRequestsUsecase = async () => {
const repository = useRequestRepository()
const { requests, isRequestFetched, filterParams } = storeToRefs(
useRequestStore()
)
const { requests, isRequestFetched, filterParams } =
storeToRefs(useRequestStore())

const rule = /^2[0-9]{3}-[0-9]{1,2}-[0-9]{1,2}$/
if (
(filterParams.value.since && !rule.test(filterParams.value.since)) ||
(filterParams.value.until && !rule.test(filterParams.value.until))
) {
throw new Error('日付はyyyy-MM-ddの形式で入力してください')
}

try {
requests.value = await repository.fetchRequests(filterParams.value)
Expand Down
8 changes: 0 additions & 8 deletions src/features/transaction/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ const createTransactionRepository = () => ({
fetchTransactions: async (
querySeed: TransactionQuerySeed
): Promise<Transaction[]> => {
const rule = /^2[0-9]{3}-[0-9]{1,2}-[0-9]{1,2}$/
if (
(querySeed.since && !rule.test(querySeed.since)) ||
(querySeed.until && !rule.test(querySeed.until))
) {
throw new Error('日付はyyyy-MM-ddの形式で入力してください')
}

const { data } = await apis.getTransactions(
querySeed.sort,
querySeed.target,
Expand Down
8 changes: 8 additions & 0 deletions src/features/transaction/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export const useFetchTransactionsUsecase = async () => {
useTransactionStore()
)

const rule = /^2[0-9]{3}-[0-9]{1,2}-[0-9]{1,2}$/
if (
(filterParams.value.since && !rule.test(filterParams.value.since)) ||
(filterParams.value.until && !rule.test(filterParams.value.until))
) {
throw new Error('日付はyyyy-MM-ddの形式で入力してください')
}

try {
transactions.value = await repository.fetchTransactions(filterParams.value)
isTransactionFetched.value = true
Expand Down

0 comments on commit 6340d93

Please sign in to comment.