mitlist/fe/src/services/choreService.ts
mohamad f49e15c05c
Some checks failed
Deploy to Production, build images and push to Gitea Registry / build_and_push (pull_request) Failing after 1m24s
feat: Introduce FastAPI and Vue.js guidelines, enhance API structure, and add caching support
This commit adds new guidelines for FastAPI and Vue.js development, emphasizing best practices for component structure, API performance, and data handling. It also introduces caching mechanisms using Redis for improved performance and updates the API structure to streamline authentication and user management. Additionally, new endpoints for categories and time entries are implemented, enhancing the overall functionality of the application.
2025-06-09 21:02:51 +02:00

143 lines
5.1 KiB
TypeScript

import { api } from './api'
import type { Chore, ChoreCreate, ChoreUpdate, ChoreType, ChoreAssignment, ChoreAssignmentCreate, ChoreAssignmentUpdate, ChoreHistory } from '../types/chore'
import { groupService } from './groupService'
import { apiClient, API_ENDPOINTS } from '@/services/api'
import type { Group } from '@/types/group'
export const choreService = {
async getAllChores(): Promise<Chore[]> {
try {
const response = await api.get('/chores/all')
return response.data
} catch (error) {
console.error('Failed to get all chores via optimized endpoint, falling back to individual calls:', error)
return this.getAllChoresFallback()
}
},
async getAllChoresFallback(): Promise<Chore[]> {
let allChores: Chore[] = []
try {
const personalChores = await this.getPersonalChores()
allChores = allChores.concat(personalChores)
const userGroups: Group[] = await groupService.getUserGroups()
for (const group of userGroups) {
try {
const groupChores = await this.getChores(group.id)
allChores = allChores.concat(groupChores)
} catch (groupError) {
console.error(`Failed to get chores for group ${group.id} (${group.name}):`, groupError)
}
}
} catch (error) {
console.error('Failed to get all chores:', error)
throw error
}
return allChores
},
async getChores(groupId: number): Promise<Chore[]> {
const response = await api.get(`/api/v1/chores/groups/${groupId}/chores`)
return response.data
},
async createChore(chore: ChoreCreate): Promise<Chore> {
if (chore.type === 'personal') {
const response = await api.post('/api/v1/chores/personal', chore)
return response.data
} else if (chore.type === 'group' && chore.group_id) {
const response = await api.post(`/api/v1/chores/groups/${chore.group_id}/chores`, chore)
return response.data
} else {
throw new Error('Invalid chore type or missing group_id for group chore')
}
},
async updateChore(choreId: number, chore: ChoreUpdate): Promise<Chore> {
if (chore.type === 'personal') {
const response = await api.put(`/api/v1/chores/personal/${choreId}`, chore)
return response.data
} else if (chore.type === 'group' && chore.group_id) {
const response = await api.put(
`/api/v1/chores/groups/${chore.group_id}/chores/${choreId}`,
chore,
)
return response.data
} else {
throw new Error('Invalid chore type or missing group_id for group chore update')
}
},
async deleteChore(choreId: number, choreType: ChoreType, groupId?: number): Promise<void> {
if (choreType === 'personal') {
await api.delete(`/api/v1/chores/personal/${choreId}`)
} else if (choreType === 'group' && groupId) {
await api.delete(`/api/v1/chores/groups/${groupId}/chores/${choreId}`)
} else {
throw new Error('Invalid chore type or missing group_id for group chore deletion')
}
},
async getPersonalChores(): Promise<Chore[]> {
const response = await api.get('/api/v1/chores/personal')
return response.data
},
async createAssignment(assignment: ChoreAssignmentCreate): Promise<ChoreAssignment> {
const response = await api.post('/api/v1/chores/assignments', assignment)
return response.data
},
async getMyAssignments(includeCompleted: boolean = false): Promise<ChoreAssignment[]> {
const response = await api.get(`/api/v1/chores/assignments/my?include_completed=${includeCompleted}`)
return response.data
},
async getChoreAssignments(choreId: number): Promise<ChoreAssignment[]> {
const response = await api.get(`/api/v1/chores/chores/${choreId}/assignments`)
return response.data
},
async updateAssignment(assignmentId: number, update: ChoreAssignmentUpdate): Promise<ChoreAssignment> {
const response = await apiClient.put(`/api/v1/chores/assignments/${assignmentId}`, update)
return response.data
},
async deleteAssignment(assignmentId: number): Promise<void> {
await api.delete(`/api/v1/chores/assignments/${assignmentId}`)
},
async completeAssignment(assignmentId: number): Promise<ChoreAssignment> {
const response = await api.patch(`/api/v1/chores/assignments/${assignmentId}/complete`)
return response.data
},
async _original_updateGroupChore(
groupId: number,
choreId: number,
chore: ChoreUpdate,
): Promise<Chore> {
const response = await api.put(`/api/v1/chores/groups/${groupId}/chores/${choreId}`, chore)
return response.data
},
async _original_deleteGroupChore(groupId: number, choreId: number): Promise<void> {
await api.delete(`/api/v1/chores/groups/${groupId}/chores/${choreId}`)
},
async _updatePersonalChore(choreId: number, chore: ChoreUpdate): Promise<Chore> {
const response = await api.put(`/api/v1/chores/personal/${choreId}`, chore)
return response.data
},
async _deletePersonalChore(choreId: number): Promise<void> {
await api.delete(`/api/v1/chores/personal/${choreId}`)
},
async getChoreHistory(choreId: number): Promise<ChoreHistory[]> {
const response = await apiClient.get(API_ENDPOINTS.CHORES.HISTORY(choreId))
return response.data
},
}