Added project separation logic

This commit is contained in:
gulimabr
2025-12-01 11:01:13 -03:00
parent 6d02736cba
commit 07005788ed
17 changed files with 1337 additions and 115 deletions

View File

@@ -0,0 +1,191 @@
const API_BASE_URL = '/api'
export interface Project {
id: number
project_name: string
project_desc: string | null
created_at: string | null
}
export interface ProjectCreateRequest {
project_name: string
project_desc?: string
}
export interface ProjectUpdateRequest {
project_name?: string
project_desc?: string
}
class ProjectService {
/**
* Get all projects the current user is a member of.
*/
async getMyProjects(): Promise<Project[]> {
try {
const response = await fetch(`${API_BASE_URL}/projects`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const projects: Project[] = await response.json()
return projects
} catch (error) {
console.error('Failed to fetch projects:', error)
throw error
}
}
/**
* Get a specific project by ID.
*/
async getProject(projectId: number): Promise<Project> {
try {
const response = await fetch(`${API_BASE_URL}/projects/${projectId}`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const project: Project = await response.json()
return project
} catch (error) {
console.error('Failed to fetch project:', error)
throw error
}
}
/**
* Create a new project.
*/
async createProject(data: ProjectCreateRequest): Promise<Project> {
try {
const response = await fetch(`${API_BASE_URL}/projects`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const project: Project = await response.json()
return project
} catch (error) {
console.error('Failed to create project:', error)
throw error
}
}
/**
* Update an existing project.
*/
async updateProject(projectId: number, data: ProjectUpdateRequest): Promise<Project> {
try {
const response = await fetch(`${API_BASE_URL}/projects/${projectId}`, {
method: 'PUT',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const project: Project = await response.json()
return project
} catch (error) {
console.error('Failed to update project:', error)
throw error
}
}
/**
* Delete a project.
*/
async deleteProject(projectId: number): Promise<void> {
try {
const response = await fetch(`${API_BASE_URL}/projects/${projectId}`, {
method: 'DELETE',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
} catch (error) {
console.error('Failed to delete project:', error)
throw error
}
}
/**
* Add a member to a project.
*/
async addMember(projectId: number, userId: number): Promise<void> {
try {
const response = await fetch(`${API_BASE_URL}/projects/${projectId}/members`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ user_id: userId }),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
} catch (error) {
console.error('Failed to add member:', error)
throw error
}
}
/**
* Remove a member from a project.
*/
async removeMember(projectId: number, userId: number): Promise<void> {
try {
const response = await fetch(`${API_BASE_URL}/projects/${projectId}/members/${userId}`, {
method: 'DELETE',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
} catch (error) {
console.error('Failed to remove member:', error)
throw error
}
}
}
export const projectService = new ProjectService()