ggen_api/
models.rs

1//! API request and response models
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use uuid::Uuid;
6use validator::Validate;
7
8/// User registration request
9#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
10pub struct RegisterRequest {
11    #[validate(email)]
12    pub email: String,
13    #[validate(length(min = 6))]
14    pub password: String,
15    #[validate(length(min = 1))]
16    pub username: String,
17}
18
19/// Login request
20#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
21pub struct LoginRequest {
22    pub email: String,
23    #[validate(length(min = 6))]
24    pub password: String,
25}
26
27/// Login response with JWT token
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct LoginResponse {
30    pub token: String,
31    pub user_id: String,
32    pub username: String,
33    pub email: String,
34    pub tier: String,
35    pub expires_in_secs: u64,
36}
37
38/// Marketplace package search request
39#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
40pub struct SearchRequest {
41    pub query: String,
42    #[validate(range(min = 1, max = 100))]
43    pub limit: Option<u32>,
44    pub offset: Option<u32>,
45    pub category: Option<String>,
46    pub sort_by: Option<String>, // "relevance", "downloads", "recent", "rating"
47}
48
49/// Package search result
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct PackageResult {
52    pub id: String,
53    pub name: String,
54    pub version: String,
55    pub author: String,
56    pub description: String,
57    pub downloads: u64,
58    pub price: Option<f64>,
59    pub license: String,
60    pub rating: f32,
61}
62
63/// Package purchase request
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct PurchaseRequest {
66    pub package_id: String,
67    pub version: String,
68}
69
70/// Purchase response
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct PurchaseResponse {
73    pub transaction_id: String,
74    pub package_id: String,
75    pub price: f64,
76    pub status: String, // "pending", "completed", "failed"
77    pub payment_url: Option<String>,
78}
79
80/// API key creation request
81#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
82pub struct CreateApiKeyRequest {
83    #[validate(length(min = 1))]
84    pub name: String,
85    pub expires_in_days: Option<u32>,
86}
87
88/// API key response (only returned on creation)
89#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct ApiKeyResponse {
91    pub id: String,
92    pub key: String, // Hashed in database, only shown once
93    pub name: String,
94    pub created_at: DateTime<Utc>,
95    pub expires_at: Option<DateTime<Utc>>,
96}
97
98/// Usage tracking request
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct UsageRequest {
101    pub operation: String, // "api_call", "marketplace_search", "installation", etc.
102    pub cost_credits: u32,
103}
104
105/// Usage statistics response
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct UsageStatsResponse {
108    pub user_id: String,
109    pub tier: String,
110    pub period_start: DateTime<Utc>,
111    pub period_end: DateTime<Utc>,
112    pub api_calls_used: u64,
113    pub api_calls_limit: u64,
114    pub templates_used: u64,
115    pub templates_limit: u64,
116    pub total_credits_used: u64,
117    pub total_credits_available: u64,
118}
119
120/// Invoice response
121#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct InvoiceResponse {
123    pub invoice_id: String,
124    pub user_id: String,
125    pub amount: f64,
126    pub currency: String,
127    pub status: String, // "paid", "pending", "failed"
128    pub issued_at: DateTime<Utc>,
129    pub due_at: DateTime<Utc>,
130    pub items: Vec<InvoiceItem>,
131}
132
133/// Invoice line item
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct InvoiceItem {
136    pub description: String,
137    pub quantity: u32,
138    pub unit_price: f64,
139    pub total: f64,
140}
141
142/// Subscription upgrade/downgrade request
143#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
144pub struct UpgradeRequest {
145    pub new_tier: String, // "free", "pro", "enterprise"
146}
147
148/// Health check response
149#[derive(Debug, Clone, Serialize, Deserialize)]
150pub struct HealthResponse {
151    pub status: String,
152    pub timestamp: DateTime<Utc>,
153    pub version: String,
154}
155
156/// Generic paginated response wrapper
157#[derive(Debug, Clone, Serialize, Deserialize)]
158pub struct PaginatedResponse<T> {
159    pub data: Vec<T>,
160    pub total: u64,
161    pub limit: u32,
162    pub offset: u32,
163}
164
165/// Error response
166#[derive(Debug, Clone, Serialize, Deserialize)]
167pub struct ErrorResponse {
168    pub error: String,
169    pub status: u16,
170    pub timestamp: DateTime<Utc>,
171}