refactor: group query gerefactord
This commit is contained in:
		
							parent
							
								
									0784db3680
								
							
						
					
					
						commit
						9c58614382
					
				
					 2 changed files with 26 additions and 35 deletions
				
			
		|  | @ -12,43 +12,35 @@ export interface GroupResponse { | |||
| } | ||||
| 
 | ||||
| export class GroupController extends BaseController { | ||||
|     constructor() { | ||||
|         super(''); | ||||
|     constructor(classid: string, assignmentNumber: number) { | ||||
|         super(`class/${classid}/assignments/${assignmentNumber}/groups`); | ||||
|     } | ||||
| 
 | ||||
|     update(classid: string, assignmentNumber: number) { | ||||
|         this.basePath = `class/${classid}/assignments/${assignmentNumber}/groups`; | ||||
|     async getAll(full = true): Promise<GroupsResponse> { | ||||
|         return this.get<GroupsResponse>(`/`, { full }); | ||||
|     } | ||||
| 
 | ||||
|     protected getBasePath(classid: string, assignmentNumber: number) { | ||||
|         return `class/${classid}/assignments/${assignmentNumber}/groups`; | ||||
|     async getByNumber(num: number): Promise<GroupResponse> { | ||||
|         return this.get<GroupResponse>(`/${num}`); | ||||
|     } | ||||
| 
 | ||||
|     async getAll(classid: string, assignmentNumber: number, full = true): Promise<GroupsResponse> { | ||||
|         return this.get<GroupsResponse>(`${this.getBasePath(classid, assignmentNumber)}/`, { full }); | ||||
|     async createGroup(data: GroupDTO): Promise<GroupResponse> { | ||||
|         return this.post<GroupResponse>(`/`, data); | ||||
|     } | ||||
| 
 | ||||
|     async getByNumber(classid: string, assignmentNumber: number, num: number | string): Promise<GroupResponse> { | ||||
|         return this.get<GroupResponse>(`${this.getBasePath(classid, assignmentNumber)}/${num}`); | ||||
|     async deleteGroup(num: number): Promise<GroupResponse> { | ||||
|         return this.delete<GroupResponse>(`/${num}`); | ||||
|     } | ||||
| 
 | ||||
|     async createGroup(classid: string, assignmentNumber: number, data: GroupDTO): Promise<GroupResponse> { | ||||
|         return this.post<GroupResponse>(`${this.getBasePath(classid, assignmentNumber)}/`, data); | ||||
|     async updateGroup(num: number, data: Partial<GroupDTO>): Promise<GroupResponse> { | ||||
|         return this.put<GroupResponse>(`/${num}`, data); | ||||
|     } | ||||
| 
 | ||||
|     async deleteGroup(classid: string, assignmentNumber: number, num: number): Promise<GroupResponse> { | ||||
|         return this.delete<GroupResponse>(`${this.getBasePath(classid, assignmentNumber)}/${num}`); | ||||
|     async getSubmissions(num: number, full = true): Promise<SubmissionsResponse> { | ||||
|         return this.get<SubmissionsResponse>(`/${num}/submissions`, { full }); | ||||
|     } | ||||
| 
 | ||||
|     async updateGroup(classid: string, assignmentNumber: number, num: number, data: Partial<GroupDTO>): Promise<GroupResponse> { | ||||
|         return this.put<GroupResponse>(`${this.getBasePath(classid, assignmentNumber)}/${num}`, data); | ||||
|     } | ||||
| 
 | ||||
|     async getSubmissions(classid: string, assignmentNumber: number, groupNumber: number, full = true): Promise<SubmissionsResponse> { | ||||
|         return this.get<SubmissionsResponse>(`${this.getBasePath(classid, assignmentNumber)}/${groupNumber}/submissions`, { full }); | ||||
|     } | ||||
| 
 | ||||
|     async getQuestions(classid: string, assignmentNumber: number, groupNumber: number, full = true): Promise<QuestionsResponse> { | ||||
|         return this.get<QuestionsResponse>(`${this.getBasePath(classid, assignmentNumber)}/${groupNumber}/questions`, { full }); | ||||
|     async getQuestions(num: number, full = true): Promise<QuestionsResponse> { | ||||
|         return this.get<QuestionsResponse>(`/${num}/questions`, { full }); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -6,8 +6,6 @@ import type { GroupDTO } from "@dwengo-1/common/interfaces/group"; | |||
| import { useMutation, useQuery, useQueryClient, type UseMutationReturnType, type UseQueryReturnType } from "@tanstack/vue-query"; | ||||
| import { computed, toValue, type MaybeRefOrGetter } from "vue"; | ||||
| 
 | ||||
| const groupController = new GroupController(); | ||||
| 
 | ||||
| function groupsQueryKey(classid: string, assignmentNumber: number, full: boolean) { | ||||
|     return [ "groups", classid, assignmentNumber, full ]; | ||||
| } | ||||
|  | @ -40,12 +38,13 @@ function toValues( | |||
| export function useGroupsQuery( | ||||
|     classid: MaybeRefOrGetter<string | undefined>,  | ||||
|     assignmentNumber: MaybeRefOrGetter<number | undefined>,  | ||||
|     full: MaybeRefOrGetter<boolean> = true, | ||||
| ): UseQueryReturnType<GroupsResponse, Error> { | ||||
|     const { cid, an, f } = toValues(classid, assignmentNumber, 1, true); | ||||
|     const { cid, an, f } = toValues(classid, assignmentNumber, 1, full); | ||||
| 
 | ||||
|     return useQuery({ | ||||
|         queryKey: computed(() => (groupsQueryKey(cid!, an!, f))), | ||||
|         queryFn: async () => groupController.getAll(cid!, an!), | ||||
|         queryFn: async () => new GroupController(cid!, an!).getAll(f), | ||||
|         enabled: () => checkEnabled(cid, an, 1), | ||||
|     }); | ||||
| } | ||||
|  | @ -59,7 +58,7 @@ export function useGroupQuery( | |||
| 
 | ||||
|     return useQuery({ | ||||
|         queryKey: computed(() => groupQueryKey(cid!, an!, gn!)), | ||||
|         queryFn: async () => groupController.getByNumber(cid!, an!, gn!), | ||||
|         queryFn: async () => new GroupController(cid!, an!).getByNumber(gn!), | ||||
|         enabled: () => checkEnabled(cid, an, gn), | ||||
|     }); | ||||
| } | ||||
|  | @ -74,7 +73,7 @@ export function useCreateGroupMutation( | |||
|     const { cid, an } = toValues(classid, assignmentNumber, 1, true); | ||||
| 
 | ||||
|     return useMutation({ | ||||
|         mutationFn: async (data) => groupController.createGroup(cid!, an!, data), | ||||
|         mutationFn: async (data) => new GroupController(cid!, an!).createGroup(data), | ||||
|         onSuccess: async () => { | ||||
|             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid!, an!, true) }); | ||||
|             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid!, an!, false) }); | ||||
|  | @ -90,7 +89,7 @@ export function useDeleteGroupMutation( | |||
|     const { cid, an, gn } = toValues(classid, assignmentNumber, 1, true); | ||||
| 
 | ||||
|     return useMutation({ | ||||
|         mutationFn: async (id) => groupController.deleteGroup(cid!, an!, id), | ||||
|         mutationFn: async (id) => new GroupController(cid!, an!).deleteGroup(id), | ||||
|         onSuccess: async () => { | ||||
|             await queryClient.invalidateQueries({ queryKey: groupQueryKey(cid!, an!, gn!) }); | ||||
| 
 | ||||
|  | @ -114,8 +113,8 @@ export function useUpdateGroupMutation( | |||
|     const { cid, an, gn } = toValues(classid, assignmentNumber, 1, true); | ||||
| 
 | ||||
|     return useMutation({ | ||||
|         mutationFn: async (data) => groupController.updateGroup(cid!, an!, gn!, data), | ||||
|         onSuccess: async (data) => { | ||||
|         mutationFn: async (data) => new GroupController(cid!, an!).updateGroup(gn!, data), | ||||
|         onSuccess: async () => { | ||||
|             await queryClient.invalidateQueries({ queryKey: groupQueryKey(cid!, an!, gn!) }); | ||||
| 
 | ||||
|             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid!, an!, true) }); | ||||
|  | @ -134,7 +133,7 @@ export function useGroupSubmissionsQuery( | |||
| 
 | ||||
|     return useQuery({ | ||||
|         queryKey: computed(() => groupSubmissionsQueryKey(cid!, an!, gn!, f)), | ||||
|         queryFn: async () => groupController.getSubmissions(cid!, an!, gn!, f), | ||||
|         queryFn: async () => new GroupController(cid!, an!).getSubmissions(gn!, f), | ||||
|         enabled: () => checkEnabled(cid, an, gn), | ||||
|     }); | ||||
| } | ||||
|  | @ -149,7 +148,7 @@ export function useGroupQuestionsQuery( | |||
| 
 | ||||
|     return useQuery({ | ||||
|         queryKey: computed(() => groupQuestionsQueryKey(cid!, an!, gn!, f)), | ||||
|         queryFn: async () => groupController.getSubmissions(cid!, an!, gn!, f), | ||||
|         queryFn: async () => new GroupController(cid!, an!).getSubmissions(gn!, f), | ||||
|         enabled: () => checkEnabled(cid, an, gn), | ||||
|     }); | ||||
| } | ||||
|  |  | |||
		Reference in a new issue