90 lines
2.2 KiB
TypeScript
90 lines
2.2 KiB
TypeScript
import api from './client';
|
|
import { User } from '@/lib/auth';
|
|
|
|
export interface Cat {
|
|
id: number;
|
|
image_url: string;
|
|
caption: string;
|
|
created_at: string;
|
|
user_id: number;
|
|
username: string;
|
|
user_points: number;
|
|
likes_count: number;
|
|
}
|
|
|
|
export interface AuthResponse {
|
|
token: string;
|
|
user: User;
|
|
}
|
|
|
|
export function register(username: string, password: string) {
|
|
return api.post<AuthResponse>('/auth/register', { username, password });
|
|
}
|
|
|
|
export function login(username: string, password: string) {
|
|
return api.post<AuthResponse>('/auth/login', { username, password });
|
|
}
|
|
|
|
export function getMe() {
|
|
return api.get<{ user: User }>('/auth/me');
|
|
}
|
|
|
|
export function getCats(page = 1) {
|
|
return api.get<{ cats: Cat[]; total: number; page: number; totalPages: number }>('/cats', { params: { page } });
|
|
}
|
|
|
|
export function getUserCats(userId: number) {
|
|
return api.get<{ cats: Cat[] }>(`/cats/user/${userId}`);
|
|
}
|
|
|
|
export function getCat(id: number) {
|
|
return api.get<{ cat: Cat }>(`/cats/${id}`);
|
|
}
|
|
|
|
export function uploadCat(formData: FormData) {
|
|
return api.post<{ cat: Cat }>('/cats', formData, {
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
});
|
|
}
|
|
|
|
export function deleteCat(id: number) {
|
|
return api.delete(`/cats/${id}`);
|
|
}
|
|
|
|
export function likeCat(catId: number) {
|
|
return api.post<{ liked: boolean; likesCount: number }>(`/likes/${catId}`);
|
|
}
|
|
|
|
export function unlikeCat(catId: number) {
|
|
return api.delete<{ liked: boolean; likesCount: number }>(`/likes/${catId}`);
|
|
}
|
|
|
|
export function getLikeStatus(catId: number) {
|
|
return api.get<{ liked: boolean }>(`/likes/${catId}/status`);
|
|
}
|
|
|
|
// Admin
|
|
export function getAdminUsers() {
|
|
return api.get<{ users: User[] }>('/admin/users');
|
|
}
|
|
|
|
export function getAdminCats() {
|
|
return api.get<{ cats: Cat[] }>('/admin/cats');
|
|
}
|
|
|
|
export function adminUpdateCaption(id: number, caption: string) {
|
|
return api.put(`/admin/cats/${id}/caption`, { caption });
|
|
}
|
|
|
|
export function adminSetPoints(id: number, points: number) {
|
|
return api.put(`/admin/users/${id}/points`, { points });
|
|
}
|
|
|
|
export function adminDeleteCat(id: number) {
|
|
return api.delete(`/admin/cats/${id}`);
|
|
}
|
|
|
|
export function adminDeleteUser(id: number) {
|
|
return api.delete(`/admin/users/${id}`);
|
|
}
|