Fix project CRUD
This commit is contained in:
@@ -50,7 +50,9 @@ async fn main() -> anyhow::Result<()> {
|
||||
// Load environment variables from .env file
|
||||
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
|
||||
let database_url =
|
||||
|
||||
@@ -15,7 +15,6 @@ pub struct Project {
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct CreateProject {
|
||||
pub name: String,
|
||||
pub owner_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
||||
@@ -11,6 +11,7 @@ use uuid::Uuid;
|
||||
use chrono::Utc;
|
||||
|
||||
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> {
|
||||
match sqlx::query_as!(
|
||||
@@ -58,18 +59,21 @@ pub async fn get_project(
|
||||
}
|
||||
|
||||
pub async fn create_project(
|
||||
auth: AuthUser,
|
||||
Extension(pool): Extension<PgPool>,
|
||||
Json(payload): Json<CreateProject>
|
||||
) -> Result<ResponseJson<ApiResponse<Project>>, StatusCode> {
|
||||
let id = Uuid::new_v4();
|
||||
let now = Utc::now();
|
||||
|
||||
tracing::debug!("Creating project '{}' for user {}", payload.name, auth.user_id);
|
||||
|
||||
match sqlx::query_as!(
|
||||
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",
|
||||
id,
|
||||
payload.name,
|
||||
payload.owner_id,
|
||||
auth.user_id,
|
||||
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 { Project, CreateProject, UpdateProject } from 'shared/types'
|
||||
import { AlertCircle } from 'lucide-react'
|
||||
import { makeAuthenticatedRequest } from '@/lib/auth'
|
||||
|
||||
interface ProjectFormProps {
|
||||
open: boolean
|
||||
@@ -29,9 +30,8 @@ export function ProjectForm({ open, onClose, onSuccess, project }: ProjectFormPr
|
||||
try {
|
||||
if (isEditing) {
|
||||
const updateData: UpdateProject = { name }
|
||||
const response = await fetch(`/api/projects/${project.id}`, {
|
||||
const response = await makeAuthenticatedRequest(`/api/projects/${project.id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(updateData),
|
||||
})
|
||||
|
||||
@@ -39,14 +39,11 @@ export function ProjectForm({ open, onClose, onSuccess, project }: ProjectFormPr
|
||||
throw new Error('Failed to update project')
|
||||
}
|
||||
} else {
|
||||
// For now, using a placeholder owner_id - this should come from auth
|
||||
const createData: CreateProject = {
|
||||
name,
|
||||
owner_id: '00000000-0000-0000-0000-000000000000'
|
||||
name
|
||||
}
|
||||
const response = await fetch('/api/projects', {
|
||||
const response = await makeAuthenticatedRequest('/api/projects', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(createData),
|
||||
})
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ export interface Project {
|
||||
|
||||
export interface CreateProject {
|
||||
name: string
|
||||
owner_id: string
|
||||
}
|
||||
|
||||
export interface UpdateProject {
|
||||
|
||||
Reference in New Issue
Block a user