فهرست منبع

feat(api): распределены все запросы по модулям

horanchikk 5 ماه پیش
والد
کامیت
c3577d7b84

+ 2 - 2
components/Base/SideBar/index.vue

@@ -115,10 +115,10 @@
 
 <script setup lang="ts">
 import { useSwipe } from '@vueuse/core'
-import { ApiModules } from '~/repository/api'
 import { useSideBar } from '~/store/useSideBar'
 import { useUser } from '~/store/useUser'
 
+const { $api } = useNuxtApp()
 const store = useSideBar()
 const sideBarEl = ref(null)
 const translateTo = ref(null)
@@ -174,6 +174,6 @@ const info = ref()
 
 onMounted(
   async () =>
-    (info.value = await ApiModules.user.getInfo(user.$state.data.access_token)),
+    (info.value = await $api.user.getInfo(user.$state.data.access_token)),
 )
 </script>

+ 2 - 4
pages/auth.vue

@@ -131,10 +131,9 @@ definePageMeta({
 
 const emit = defineEmits(['isClosed'])
 
-const { $config: { public: { ACCOUNT_LOGIN, ACCOUNT_PASSWD } } } = useNuxtApp()
+const { $config: { public: { ACCOUNT_LOGIN, ACCOUNT_PASSWD } }, $api } = useNuxtApp()
 const router = useRouter()
 const user = useUser()
-const api = useApi()
 
 const infoAuthForm = ref(false)
 const isLoading = ref(false)
@@ -151,8 +150,7 @@ async function auth() {
   isLoading.value = true
   errorMsg.show = false
 
-  await api
-    .post('/user/login', authData)
+  await $api.user.login(authData)
     .then((res) => {
       user.setAuthData(res.access_token, res.user_id)
       router.push('/setup/branch')

+ 5 - 12
pages/blog/index.vue

@@ -6,7 +6,7 @@
       </button>
     </NuxtLink> -->
     <div
-      v-if="postList === null"
+      v-if="!postList"
       class="w-full h-full"
     >
       <div
@@ -32,24 +32,17 @@
 </template>
 
 <script setup lang="ts">
-interface PostList {
-  title: string
-  avatar: string
-  author: string
-  date: string
-  raw_content: string
-  attachments: []
-}
+import type { TPosts } from '~/types/posts'
 
 definePageMeta({
   name: 'Блог',
   middleware: ['user-only'],
 })
 
-const api = useApi()
-const postList = ref<{ items: PostList[] } | null>(null)
+const { $api } = useNuxtApp()
+const postList = ref<TPosts>()
 
 onMounted(async () => {
-  postList.value = await api.get('/blogs/')
+  postList.value = await $api.blog.getPosts()
 })
 </script>

+ 3 - 4
pages/gallery/[id].vue

@@ -25,15 +25,14 @@
 </template>
 
 <script setup lang="ts">
-import { ApiModules } from '~/repository/api'
-
+const { $api } = useNuxtApp()
 const { params: { id } } = useRoute('gallery-id')
-const photos = ref<null | object>(null)
+const photos = ref<object>()
 
 definePageMeta({
   name: 'Фотографии',
   middleware: ['user-only'],
 })
 
-onMounted(async () => photos.value = await ApiModules.gallery.getPhotos(id))
+onMounted(async () => photos.value = await $api.gallery.getPhotos(id))
 </script>

+ 3 - 4
pages/gallery/index.vue

@@ -33,13 +33,12 @@
 </template>
 
 <script setup lang="ts">
-import { ApiModules } from '~/repository/api'
-
 definePageMeta({
   name: 'Галерея',
   middleware: ['user-only'],
 })
 
-const albums = ref<null | object>(null)
-onMounted(async () => albums.value = await ApiModules.gallery.getAlbums())
+const albums = ref<object>()
+const { $api } = useNuxtApp()
+onMounted(async () => albums.value = await $api.gallery.getAlbums())
 </script>

+ 4 - 10
pages/news/[id]/index.vue

@@ -62,13 +62,7 @@
 
 <script setup lang="ts">
 import { useWindowScroll, useSwipe } from '@vueuse/core'
-
-interface News {
-  content: string
-  date: string
-  id: number
-  title: string
-}
+import type { News } from '~/types/news'
 
 definePageMeta({
   middleware: ['user-only'],
@@ -89,8 +83,8 @@ const lineHeight = ref(32)
 const titleStyles = ref('')
 
 const route = useRoute()
-const api = useApi()
-const news = ref<null | News>()
+const { $api } = useNuxtApp()
+const news = ref<News>()
 
 const { y } = useWindowScroll({ behavior: 'smooth' })
 const { direction, lengthX } = useSwipe(page, {
@@ -115,6 +109,6 @@ watch(y, (val) => {
 })
 
 onMounted(async () => {
-  news.value = await api.get<News>(`/news/id${route.params.id}?md=false`)
+  news.value = await $api.news.getPost(route.params.id)
 })
 </script>

+ 5 - 32
pages/news/index.vue

@@ -1,7 +1,7 @@
 <template>
   <div class="w-full overflow-y-scroll flex flex-col p-2 gap-5">
     <div
-      v-if="newsList === null"
+      v-if="!newsList"
       class="w-full h-full"
     >
       <div
@@ -26,44 +26,17 @@
 </template>
 
 <script setup lang="ts">
-import { useDebug } from '~/store/useDebug'
-import { useHeader } from '~/store/useHeader'
-
-interface NewsList {
-  id: number
-  date: string
-  title: string
-  description: string
-  preview: string
-  type: string
-}
+import type { NewsList } from '~/types/news'
 
 definePageMeta({
   name: 'Новости',
   middleware: ['user-only'],
 })
 
-const api = useApi()
-const debug = useDebug()
-const header = useHeader()
-const newsList = ref<NewsList | null>(null)
-
-header.setAdditionalMenu([
-  {
-    name: 'Debug',
-    icon: 'bug-report',
-    action: () => debug.show(),
-  },
-  {
-    name: 'Trigger error',
-    icon: 'bug-report',
-    action: () => {
-      throw new Error('Test Error <3')
-    },
-  },
-])
+const { $api } = useNuxtApp()
+const newsList = ref<NewsList>()
 
 onMounted(async () => {
-  newsList.value = await api.get('/news/')
+  newsList.value = await $api.news.getPosts()
 })
 </script>

+ 2 - 4
pages/setup/branch.vue

@@ -86,8 +86,8 @@ definePageMeta({
   middleware: ['user-only'],
 })
 
+const { $api } = useNuxtApp()
 const selectedBranch = ref()
-const api = useApi()
 const router = useRouter()
 const user = useUser()
 const branches = ref()
@@ -103,9 +103,7 @@ function nextPage() {
 }
 
 onMounted(async () => {
-  branches.value = await api.get('/branches', {
-    squeeze: true,
-  })
+  branches.value = await $api.branch.getBranches()
 })
 </script>
 

+ 6 - 7
pages/timetable/auditories.vue

@@ -49,7 +49,10 @@ definePageMeta({
   middleware: ['user-only'],
 })
 
-const api = useApi()
+const { $api } = useNuxtApp()
+const date = new Date()
+const hours = date.getHours().toString().padStart(2, '0')
+const minutes = date.getMinutes().toString().padStart(2, '0')
 const auditories = ref<Array<string> | null>(null)
 const auditoriesFree = ref<Array<string> | null>(null)
 
@@ -66,11 +69,7 @@ const busyAuditories = computed(() => {
 })
 
 onMounted(async () => {
-  const date = new Date()
-  const hours = date.getHours().toString().padStart(2, '0')
-  const minutes = date.getMinutes().toString().padStart(2, '0')
-
-  auditories.value = await api.get(`/timetable/classrooms/`)
-  auditoriesFree.value = await api.get(`/timetable/classrooms/free?time=${hours}:${minutes}`)
+  auditories.value = await $api.timetable.getClassrooms()
+  auditoriesFree.value = await $api.timetable.getFreeClassrooms(`${hours}:${minutes}`)
 })
 </script>

+ 5 - 3
pages/timetable/auditory/[name].vue

@@ -55,17 +55,19 @@
 </template>
 
 <script setup lang="ts">
+import type { TDays } from '~/types/timetable'
+
 definePageMeta({
   name: 'Расписание аудитории',
   middleware: ['user-only'],
 })
 
-const api = useApi()
+const { $api } = useNuxtApp()
 const { params: { name } } = useRoute()
 
-const data = ref(null)
+const data = ref<TDays>()
 
 onMounted(async () => {
-  data.value = await api.get(`/timetable/classrooms/room/${name}`)
+  data.value = await $api.timetable.getClassroom(name.toString())
 })
 </script>

+ 4 - 4
pages/timetable/group.vue

@@ -40,20 +40,20 @@ definePageMeta({
 })
 
 const user = useUser()
-const api = useApi()
+const { $api } = useNuxtApp()
 const router = useRouter()
-const data = ref(null)
+const data = ref()
 
 if (!(Object.hasOwn(user.data, 'is_student') && Object.hasOwn(user.data, 'branch_id'))) {
   router.push('/setup/branch')
 }
 
-function setGroup(group: string) {
+function setGroup(group: object) {
   user.data.group_id = group.id
   router.push('/timetable')
 }
 
 onMounted(async () => {
-  data.value = await api.get(`/timetable/students/courses/${user.data.branch_id}`)
+  data.value = await $api.timetable.getCourses(user.data.branch_id)
 })
 </script>

+ 2 - 2
pages/timetable/teacher.vue

@@ -27,8 +27,8 @@ definePageMeta({
   middleware: ['user-only'],
 })
 
+const { $api } = useNuxtApp()
 const user = useUser()
-const api = useApi()
 const router = useRouter()
 const data = ref(null)
 
@@ -37,6 +37,6 @@ if (!(Object.hasOwn(user.data, 'is_student') && Object.hasOwn(user.data, 'branch
 }
 
 onMounted(async () => {
-  data.value = await api.get(`/teachers/${user.data.branch_id}`)
+  data.value = await $api.timetable.getTeachers(user.data.branch_id)
 })
 </script>

+ 10 - 0
repository/modules/blog.ts

@@ -0,0 +1,10 @@
+import { API } from '../api'
+import type { TPosts } from '@/types/posts'
+
+class BlogModule extends API {
+  async getPosts() {
+    return await this.get<TPosts>('/blogs/')
+  }
+}
+
+export default BlogModule

+ 14 - 0
repository/modules/news.ts

@@ -0,0 +1,14 @@
+import { API } from '../api'
+import type { NewsList, News } from '~/types/news'
+
+class NewsModule extends API {
+  async getPosts() {
+    return await this.get<NewsList>('/news/')
+  }
+
+  async getPost(id: number) {
+    return await this.get<News>(`/news/id${id}?md=false`)
+  }
+}
+
+export default NewsModule

+ 12 - 0
repository/modules/ota.ts

@@ -0,0 +1,12 @@
+import { API } from '../api'
+import type { Tota } from '~/types/ota'
+
+class OTAModule extends API {
+  async getVersion(isDev: boolean = false) {
+    return await this.get<Tota>(
+      `/updates/check${isDev ? '?prerelease=true' : ''}`,
+    )
+  }
+}
+
+export default OTAModule

+ 10 - 0
repository/modules/timetable.ts

@@ -23,6 +23,16 @@ class TimetableModule extends API {
   async getClassroom(name: string) {
     return await this.get<TDays>(`/timetable/classrooms/room/${name}`)
   }
+
+  async getTeacherTimetable(branch_id: number, teacher_id: number) {
+    return await this.get(
+      `/teachers/${branch_id}/id${teacher_id}`,
+    )
+  }
+
+  async getTeachers(branch_id: number) {
+    return await this.get(`/teachers/${branch_id}`)
+  }
 }
 
 export default TimetableModule

+ 15 - 0
types/news.ts

@@ -0,0 +1,15 @@
+export interface NewsList {
+  id: number
+  date: string
+  title: string
+  description: string
+  preview: string
+  type: string
+}
+
+export interface News {
+  content: string
+  date: string
+  id: number
+  title: string
+}

+ 7 - 0
types/ota.ts

@@ -0,0 +1,7 @@
+export interface Tota {
+  version: string
+  description: string
+  tag_info: string
+  size: number
+  apkfile: string
+}

+ 12 - 0
types/posts.ts

@@ -0,0 +1,12 @@
+interface PostList {
+  title: string
+  avatar: string
+  author: string
+  date: string
+  raw_content: string
+  attachments: []
+}
+
+export interface TPosts {
+  items: PostList[]
+}