feat: controller + queries
This commit is contained in:
		
							parent
							
								
									834e991568
								
							
						
					
					
						commit
						17dc9649c5
					
				
					 3 changed files with 94 additions and 8 deletions
				
			
		|  | @ -4,6 +4,7 @@ import type { StudentsResponse } from "./students"; | ||||||
| import type { AssignmentsResponse } from "./assignments"; | import type { AssignmentsResponse } from "./assignments"; | ||||||
| import type { TeacherInvitationDTO } from "@dwengo-1/common/interfaces/teacher-invitation"; | import type { TeacherInvitationDTO } from "@dwengo-1/common/interfaces/teacher-invitation"; | ||||||
| import type { TeachersResponse } from "@/controllers/teachers.ts"; | import type { TeachersResponse } from "@/controllers/teachers.ts"; | ||||||
|  | import type {TeacherInvitationsResponse} from "@/controllers/teacher-invitations.ts"; | ||||||
| 
 | 
 | ||||||
| export interface ClassesResponse { | export interface ClassesResponse { | ||||||
|     classes: ClassDTO[] | string[]; |     classes: ClassDTO[] | string[]; | ||||||
|  | @ -13,14 +14,6 @@ export interface ClassResponse { | ||||||
|     class: ClassDTO; |     class: ClassDTO; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface TeacherInvitationsResponse { |  | ||||||
|     invites: TeacherInvitationDTO[]; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| export interface TeacherInvitationResponse { |  | ||||||
|     invite: TeacherInvitationDTO; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| export class ClassController extends BaseController { | export class ClassController extends BaseController { | ||||||
|     constructor() { |     constructor() { | ||||||
|         super("class"); |         super("class"); | ||||||
|  |  | ||||||
							
								
								
									
										28
									
								
								frontend/src/controllers/teacher-invitations.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								frontend/src/controllers/teacher-invitations.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,28 @@ | ||||||
|  | import {BaseController} from "@/controllers/base-controller.ts"; | ||||||
|  | import type {TeacherInvitationData, TeacherInvitationDTO} from "@dwengo-1/common/interfaces/teacher-invitation"; | ||||||
|  | 
 | ||||||
|  | export interface TeacherInvitationsResponse { | ||||||
|  |     invitations: TeacherInvitationDTO[] | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export interface TeacherInvitationResponse { | ||||||
|  |     invitation: TeacherInvitationDTO | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export class TeacherInvitationController extends BaseController { | ||||||
|  |     constructor() { | ||||||
|  |         super("teachers/invitations"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     async getAll(username: string, by: boolean): Promise<TeacherInvitationsResponse> { | ||||||
|  |         return this.get<TeacherInvitationsResponse>(`/${username}`, { by }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     async create(data: TeacherInvitationData): Promise<TeacherInvitationResponse> { | ||||||
|  |         return this.post<TeacherInvitationResponse>("/", data); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     async respond(data: TeacherInvitationData, accepted: boolean): Promise<TeacherInvitationResponse> { | ||||||
|  |         return this.delete<TeacherInvitationResponse>(`/${data.sender}/${data.receiver}/${data.class}`, { accepted }); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										65
									
								
								frontend/src/queries/teacher-invitations.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								frontend/src/queries/teacher-invitations.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,65 @@ | ||||||
|  | import { | ||||||
|  |     useMutation, | ||||||
|  |     useQuery, | ||||||
|  |     type UseMutationReturnType, | ||||||
|  |     type UseQueryReturnType, | ||||||
|  | } from "@tanstack/vue-query"; | ||||||
|  | import { computed, toValue } from "vue"; | ||||||
|  | import type { MaybeRefOrGetter } from "vue"; | ||||||
|  | import { | ||||||
|  |     TeacherInvitationController, | ||||||
|  |     type TeacherInvitationResponse, | ||||||
|  |     type TeacherInvitationsResponse | ||||||
|  | } from "@/controllers/teacher-invitations.ts"; | ||||||
|  | import type {TeacherInvitationData} from "@dwengo-1/common/dist/interfaces/teacher-invitation.ts"; | ||||||
|  | import type {TeacherDTO} from "@dwengo-1/common/dist/interfaces/teacher.ts"; | ||||||
|  | 
 | ||||||
|  | const controller = new TeacherInvitationController(); | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |     all the invitations the teacher send | ||||||
|  | **/ | ||||||
|  | export function useTeacherInvitationsByQuery(username: MaybeRefOrGetter<string | undefined> | ||||||
|  | ): UseQueryReturnType<TeacherInvitationsResponse, Error> { | ||||||
|  |     return useQuery({ | ||||||
|  |         queryFn: computed(() => controller.getAll(toValue(username), true)), | ||||||
|  |         enabled: () => Boolean(toValue(username)), | ||||||
|  |     }) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |     all the pending invitations send to this teacher | ||||||
|  |  */ | ||||||
|  | export function useTeacherInvitationsForQuery(username: MaybeRefOrGetter<string | undefined> | ||||||
|  | ): UseQueryReturnType<TeacherInvitationsResponse, Error> { | ||||||
|  |     return useQuery({ | ||||||
|  |         queryFn: computed(() => controller.getAll(toValue(username), false)), | ||||||
|  |         enabled: () => Boolean(toValue(username)), | ||||||
|  |     }) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function useCreateTeacherInvitationMutation(): UseMutationReturnType<TeacherInvitationResponse, Error, TeacherDTO, unknown>{ | ||||||
|  |     return useMutation({ | ||||||
|  |         mutationFn: async (data: TeacherInvitationData) => controller.create(data) | ||||||
|  |     }) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function useAcceptTeacherInvitationMutation(): UseMutationReturnType<TeacherInvitationResponse, Error, TeacherDTO, unknown> { | ||||||
|  |     return useMutation({ | ||||||
|  |         mutationFn: async (data: TeacherInvitationData) => controller.respond(data, true) | ||||||
|  |     }) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function useDeclineTeacherInvitationMutation(): UseMutationReturnType<TeacherInvitationResponse, Error, TeacherDTO, unknown> { | ||||||
|  |     return useMutation({ | ||||||
|  |         mutationFn: async (data: TeacherInvitationData) => controller.respond(data, false) | ||||||
|  |     }) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function useDeleteTeacherInvitationMutation(): UseMutationReturnType<TeacherInvitationResponse, Error, TeacherDTO, unknown> { | ||||||
|  |     return useMutation({ | ||||||
|  |         mutationFn: async (data: TeacherInvitationData) => controller.respond(data, false) | ||||||
|  |     }) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl