created api for post comment and user

parent e59e054a
import { EStatuses } from "../enums/EStatuses";
import IComment from "../interfaces/IComment";
import ICommentDto from "../interfaces/ICommentDto";
import IResponse from "../interfaces/IResponse";
import { instance } from "./instance";
class CommentApi {
public getCommentsByPost = async (post_id: string): Promise<IResponse<IComment[] | undefined>> => {
try {
const response = await instance.get(`/comments?post_id=${post_id}`)
return response.data
} catch(err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
message: error.message,
result: undefined
}
return response
}
}
public createComment = async (commentDto: ICommentDto): Promise<IResponse<IComment | undefined>> => {
try {
const response = await instance.post(`/comments`, commentDto)
return response.data
} catch(err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
message: error.message,
result: undefined
}
return response
}
}
public deleteCommentById = async (id: string): Promise<IResponse<IComment | undefined>> => {
try {
const response = await instance.delete(`/comments/${id}`)
return response.data
} catch(err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
}
export const commentsApi = new CommentApi()
\ No newline at end of file
import axios, { AxiosRequestConfig, InternalAxiosRequestConfig } from "axios";
export const instance = axios.create({
baseURL: import.meta.env.VITE_BASE_URL
})
instance.interceptors.request.use((req: InternalAxiosRequestConfig) => {
const token = localStorage.getItem('token')
if(token) {
req.headers.Authorization = token
}
return req
})
\ No newline at end of file
import { EStatuses } from '../enums/EStatuses'
import IPost from "../interfaces/IPost";
import IResponse from "../interfaces/IResponse";
import { instance } from "./instance";
class PostApi {
public getPosts = async (): Promise<IResponse<IPost[] | undefined>> => {
try {
const response = await instance.get('/posts')
return response.data
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
public getPostById = async(id: string): Promise<IResponse<IPost | undefined>> => {
try {
const response = await instance.get(`/posts/${id}`)
return response.data
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
public createPost = async(post: FormData): Promise<IResponse<IPost | undefined>> => {
try {
const response = await instance.post(`/posts`, post)
return response.data
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
public deletePostById = async(id: string): Promise<IResponse<IPost | undefined>> => {
try {
const response = await instance.delete(`/posts/${id}`)
return response.data
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
}
export const postApi = new PostApi()
\ No newline at end of file
import { EStatuses } from "../enums/EStatuses"
import IResponse from "../interfaces/IResponse"
import IUserCreateDto from "../interfaces/IUserCreateDto"
import IUserGetDto from "../interfaces/IUserGetDto"
import { instance } from "./instance"
class UserApi {
public login = async (user: IUserCreateDto): Promise<IResponse<IUserGetDto | undefined>> => {
try {
const response = await instance.post(`/users/login`, user)
return response.data
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
public register = async (user: IUserCreateDto): Promise<IResponse<IUserGetDto | undefined>> => {
try {
const response = await instance.post(`/users`, user)
return response.data
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
public checkToken = async (): Promise<IResponse<IUserGetDto | undefined>> => {
try {
const response = await instance.get(`/users/token`)
return response.data
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
}
export const userApi = new UserApi()
\ No newline at end of file
export const enum EStatuses {
OK = 1,
NOT_OK = 0
}
\ No newline at end of file
import IPost from "./IPost"
import IUser from "./IUser"
export default interface IComment {
_id: string
user: IUser
post: IPost
comment: string
datetime: Date
}
\ No newline at end of file
import IComment from "./IComment";
export default interface ICommentDto {
comment: IComment['comment']
}
\ No newline at end of file
export default interface IDataBase {
init: () => Promise<void>
close: () => Promise<void> | void
}
\ No newline at end of file
export default interface IPost {
_id: string
title: string
description: string
image: File | undefined | string
datetime: Date
}
\ No newline at end of file
import IPost from "./IPost"
export default interface IPostDto {
title: IPost['title']
description: IPost['description']
image: File | undefined | string
}
\ No newline at end of file
import { EStatuses } from '../enums/EStatuses'
export default interface IResponse<T> {
result: T
message: string
status: EStatuses
}
\ No newline at end of file
export default interface IUser {
_id: string
username: string;
password: string;
token: string;
}
import IUSer from "./IUser";
export default interface IUserCreateDto {
username: IUSer['username']
password: IUSer['password']
}
\ No newline at end of file
import IUSer from "./IUser";
export default interface IUserGetDto {
_id: IUSer['_id']
username: IUSer['username']
token: IUSer['token']
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment