---
to: "ai_generated_{{ name | snake_case }}.rs"
vars:
  name: "UserService"
  description: "A service for managing users with CRUD operations"
  language: "rust"
  framework: "axum"
---
// AI-Generated {{ name }} Service
// Description: {{ description }}
// Generated by ggen-ai

use {{ framework }}::{
    extract::{Path, State},
    http::StatusCode,
    response::Json,
    routing::{get, post, put, delete},
    Router,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct {{ name }} {
    pub id: u32,
    pub name: String,
    pub email: String,
    pub created_at: chrono::DateTime<chrono::Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Create{{ name }}Request {
    pub name: String,
    pub email: String,
}

#[derive(Debug, Clone)]
pub struct {{ name }}Service {
    // Add your service dependencies here
}

impl {{ name }}Service {
    pub fn new() -> Self {
        Self {}
    }

    pub async fn create_{{ name | snake_case }}(&self, req: Create{{ name }}Request) -> Result<{{ name }}, String> {
        // AI-generated implementation placeholder
        Ok({{ name }} {
            id: 1,
            name: req.name,
            email: req.email,
            created_at: chrono::Utc::now(),
        })
    }

    pub async fn get_{{ name | snake_case }}(&self, id: u32) -> Result<Option<{{ name }}>, String> {
        // AI-generated implementation placeholder
        Ok(Some({{ name }} {
            id,
            name: "Example User".to_string(),
            email: "user@example.com".to_string(),
            created_at: chrono::Utc::now(),
        }))
    }

    pub async fn update_{{ name | snake_case }}(&self, id: u32, req: Create{{ name }}Request) -> Result<{{ name }}, String> {
        // AI-generated implementation placeholder
        Ok({{ name }} {
            id,
            name: req.name,
            email: req.email,
            created_at: chrono::Utc::now(),
        })
    }

    pub async fn delete_{{ name | snake_case }}(&self, id: u32) -> Result<(), String> {
        // AI-generated implementation placeholder
        Ok(())
    }
}

pub fn create_{{ name | snake_case }}_router() -> Router {
    let service = Arc::new({{ name }}Service::new());

    Router::new()
        .route("/{{ name | snake_case }}s", post(create_{{ name | snake_case }}))
        .route("/{{ name | snake_case }}s/:id", get(get_{{ name | snake_case }}))
        .route("/{{ name | snake_case }}s/:id", put(update_{{ name | snake_case }}))
        .route("/{{ name | snake_case }}s/:id", delete(delete_{{ name | snake_case }}))
        .with_state(service)
}

async fn create_{{ name | snake_case }}(
    State(service): State<Arc<{{ name }}Service>>,
    Json(payload): Json<Create{{ name }}Request>,
) -> Result<Json<{{ name }}>, StatusCode> {
    match service.create_{{ name | snake_case }}(payload).await {
        Ok({{ name | snake_case }}) => Ok(Json({{ name | snake_case }})),
        Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
    }
}

async fn get_{{ name | snake_case }}(
    State(service): State<Arc<{{ name }}Service>>,
    Path(id): Path<u32>,
) -> Result<Json<{{ name }}>, StatusCode> {
    match service.get_{{ name | snake_case }}(id).await {
        Ok(Some({{ name | snake_case }})) => Ok(Json({{ name | snake_case }})),
        Ok(None) => Err(StatusCode::NOT_FOUND),
        Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
    }
}

async fn update_{{ name | snake_case }}(
    State(service): State<Arc<{{ name }}Service>>,
    Path(id): Path<u32>,
    Json(payload): Json<Create{{ name }}Request>,
) -> Result<Json<{{ name }}>, StatusCode> {
    match service.update_{{ name | snake_case }}(id, payload).await {
        Ok({{ name | snake_case }}) => Ok(Json({{ name | snake_case }})),
        Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
    }
}

async fn delete_{{ name | snake_case }}(
    State(service): State<Arc<{{ name }}Service>>,
    Path(id): Path<u32>,
) -> Result<StatusCode, StatusCode> {
    match service.delete_{{ name | snake_case }}(id).await {
        Ok(_) => Ok(StatusCode::NO_CONTENT),
        Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
    }
}

