Fix project CRUD
This commit is contained in:
@@ -50,7 +50,9 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
// Load environment variables from .env file
|
// Load environment variables from .env file
|
||||||
dotenvy::dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
|
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt()
|
||||||
|
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env().add_directive("bloop_backend=debug".parse()?))
|
||||||
|
.init();
|
||||||
|
|
||||||
// Database connection
|
// Database connection
|
||||||
let database_url =
|
let database_url =
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ pub struct Project {
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct CreateProject {
|
pub struct CreateProject {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub owner_id: Uuid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use uuid::Uuid;
|
|||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
|
||||||
use crate::models::{ApiResponse, project::{Project, CreateProject, UpdateProject}};
|
use crate::models::{ApiResponse, project::{Project, CreateProject, UpdateProject}};
|
||||||
|
use crate::auth::AuthUser;
|
||||||
|
|
||||||
pub async fn get_projects(Extension(pool): Extension<PgPool>) -> Result<ResponseJson<ApiResponse<Vec<Project>>>, StatusCode> {
|
pub async fn get_projects(Extension(pool): Extension<PgPool>) -> Result<ResponseJson<ApiResponse<Vec<Project>>>, StatusCode> {
|
||||||
match sqlx::query_as!(
|
match sqlx::query_as!(
|
||||||
@@ -58,18 +59,21 @@ pub async fn get_project(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_project(
|
pub async fn create_project(
|
||||||
|
auth: AuthUser,
|
||||||
Extension(pool): Extension<PgPool>,
|
Extension(pool): Extension<PgPool>,
|
||||||
Json(payload): Json<CreateProject>
|
Json(payload): Json<CreateProject>
|
||||||
) -> Result<ResponseJson<ApiResponse<Project>>, StatusCode> {
|
) -> Result<ResponseJson<ApiResponse<Project>>, StatusCode> {
|
||||||
let id = Uuid::new_v4();
|
let id = Uuid::new_v4();
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
|
|
||||||
|
tracing::debug!("Creating project '{}' for user {}", payload.name, auth.user_id);
|
||||||
|
|
||||||
match sqlx::query_as!(
|
match sqlx::query_as!(
|
||||||
Project,
|
Project,
|
||||||
"INSERT INTO projects (id, name, owner_id, created_at, updated_at) VALUES ($1, $2, $3, $4, $5) RETURNING id, name, owner_id, created_at, updated_at",
|
"INSERT INTO projects (id, name, owner_id, created_at, updated_at) VALUES ($1, $2, $3, $4, $5) RETURNING id, name, owner_id, created_at, updated_at",
|
||||||
id,
|
id,
|
||||||
payload.name,
|
payload.name,
|
||||||
payload.owner_id,
|
auth.user_id,
|
||||||
now,
|
now,
|
||||||
now
|
now
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { Alert, AlertDescription } from '@/components/ui/alert'
|
|||||||
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||||
import { Project, CreateProject, UpdateProject } from 'shared/types'
|
import { Project, CreateProject, UpdateProject } from 'shared/types'
|
||||||
import { AlertCircle } from 'lucide-react'
|
import { AlertCircle } from 'lucide-react'
|
||||||
|
import { makeAuthenticatedRequest } from '@/lib/auth'
|
||||||
|
|
||||||
interface ProjectFormProps {
|
interface ProjectFormProps {
|
||||||
open: boolean
|
open: boolean
|
||||||
@@ -29,9 +30,8 @@ export function ProjectForm({ open, onClose, onSuccess, project }: ProjectFormPr
|
|||||||
try {
|
try {
|
||||||
if (isEditing) {
|
if (isEditing) {
|
||||||
const updateData: UpdateProject = { name }
|
const updateData: UpdateProject = { name }
|
||||||
const response = await fetch(`/api/projects/${project.id}`, {
|
const response = await makeAuthenticatedRequest(`/api/projects/${project.id}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(updateData),
|
body: JSON.stringify(updateData),
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -39,14 +39,11 @@ export function ProjectForm({ open, onClose, onSuccess, project }: ProjectFormPr
|
|||||||
throw new Error('Failed to update project')
|
throw new Error('Failed to update project')
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// For now, using a placeholder owner_id - this should come from auth
|
|
||||||
const createData: CreateProject = {
|
const createData: CreateProject = {
|
||||||
name,
|
name
|
||||||
owner_id: '00000000-0000-0000-0000-000000000000'
|
|
||||||
}
|
}
|
||||||
const response = await fetch('/api/projects', {
|
const response = await makeAuthenticatedRequest('/api/projects', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(createData),
|
body: JSON.stringify(createData),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ export interface Project {
|
|||||||
|
|
||||||
export interface CreateProject {
|
export interface CreateProject {
|
||||||
name: string
|
name: string
|
||||||
owner_id: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateProject {
|
export interface UpdateProject {
|
||||||
|
|||||||
Reference in New Issue
Block a user