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