add app CRUD and public store API endpoints
This commit is contained in:
@@ -4,6 +4,7 @@ package database
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -360,3 +361,512 @@ func (db *DB) LogAudit(ctx context.Context, developerID, action, ipAddress, user
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`, developerID, action, details, ipAddress, userAgent)
|
||||
}
|
||||
|
||||
// App represents an app in the database
|
||||
type App struct {
|
||||
ID string `json:"id"`
|
||||
DeveloperID string `json:"developer_id"`
|
||||
PackageID string `json:"package_id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// AppVersion represents an app version in the database
|
||||
type AppVersion struct {
|
||||
ID string `json:"id"`
|
||||
AppID string `json:"app_id"`
|
||||
VersionCode int `json:"version_code"`
|
||||
VersionName string `json:"version_name"`
|
||||
PackageURL string `json:"package_url,omitempty"`
|
||||
PackageSize int64 `json:"package_size,omitempty"`
|
||||
Signature string `json:"signature,omitempty"`
|
||||
Permissions []string `json:"permissions"`
|
||||
MinMosisVersion string `json:"min_mosis_version,omitempty"`
|
||||
ReleaseNotes string `json:"release_notes,omitempty"`
|
||||
Status string `json:"status"`
|
||||
ReviewNotes string `json:"review_notes,omitempty"`
|
||||
PublishedAt *time.Time `json:"published_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// CreateApp creates a new app
|
||||
func (db *DB) CreateApp(ctx context.Context, app *App) (*App, error) {
|
||||
app.ID = uuid.New().String()
|
||||
app.Tags = []string{}
|
||||
app.CreatedAt = time.Now()
|
||||
app.UpdatedAt = app.CreatedAt
|
||||
|
||||
_, err := db.ExecContext(ctx, `
|
||||
INSERT INTO apps (id, developer_id, package_id, name, description, category, tags, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, '[]', ?, datetime('now'), datetime('now'))
|
||||
`, app.ID, app.DeveloperID, app.PackageID, app.Name, app.Description, app.Category, app.Status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return app, nil
|
||||
}
|
||||
|
||||
// GetApp retrieves an app by ID
|
||||
func (db *DB) GetApp(ctx context.Context, id string) (*App, error) {
|
||||
row := db.QueryRowContext(ctx, `
|
||||
SELECT id, developer_id, package_id, name, description, category, tags, status, created_at, updated_at
|
||||
FROM apps WHERE id = ?
|
||||
`, id)
|
||||
|
||||
return scanApp(row)
|
||||
}
|
||||
|
||||
// GetAppByPackageID retrieves an app by package ID
|
||||
func (db *DB) GetAppByPackageID(ctx context.Context, packageID string) (*App, error) {
|
||||
row := db.QueryRowContext(ctx, `
|
||||
SELECT id, developer_id, package_id, name, description, category, tags, status, created_at, updated_at
|
||||
FROM apps WHERE package_id = ?
|
||||
`, packageID)
|
||||
|
||||
return scanApp(row)
|
||||
}
|
||||
|
||||
func scanApp(row *sql.Row) (*App, error) {
|
||||
var app App
|
||||
var desc, cat, tagsJSON sql.NullString
|
||||
var createdAt, updatedAt string
|
||||
|
||||
err := row.Scan(&app.ID, &app.DeveloperID, &app.PackageID, &app.Name, &desc, &cat, &tagsJSON, &app.Status, &createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
app.Description = desc.String
|
||||
app.Category = cat.String
|
||||
app.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", createdAt)
|
||||
app.UpdatedAt, _ = time.Parse("2006-01-02 15:04:05", updatedAt)
|
||||
|
||||
// Parse tags JSON
|
||||
app.Tags = []string{}
|
||||
if tagsJSON.Valid && tagsJSON.String != "" {
|
||||
json.Unmarshal([]byte(tagsJSON.String), &app.Tags)
|
||||
}
|
||||
|
||||
return &app, nil
|
||||
}
|
||||
|
||||
// ListApps lists apps for a developer
|
||||
func (db *DB) ListApps(ctx context.Context, developerID, status string, page, limit int) ([]*App, int, error) {
|
||||
offset := (page - 1) * limit
|
||||
|
||||
// Build query
|
||||
query := "SELECT id, developer_id, package_id, name, description, category, tags, status, created_at, updated_at FROM apps WHERE developer_id = ?"
|
||||
countQuery := "SELECT COUNT(*) FROM apps WHERE developer_id = ?"
|
||||
args := []interface{}{developerID}
|
||||
countArgs := []interface{}{developerID}
|
||||
|
||||
if status != "" {
|
||||
query += " AND status = ?"
|
||||
countQuery += " AND status = ?"
|
||||
args = append(args, status)
|
||||
countArgs = append(countArgs, status)
|
||||
}
|
||||
|
||||
query += " ORDER BY updated_at DESC LIMIT ? OFFSET ?"
|
||||
args = append(args, limit, offset)
|
||||
|
||||
// Get total count
|
||||
var total int
|
||||
db.QueryRowContext(ctx, countQuery, countArgs...).Scan(&total)
|
||||
|
||||
// Get apps
|
||||
rows, err := db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var apps []*App
|
||||
for rows.Next() {
|
||||
var app App
|
||||
var desc, cat, tagsJSON sql.NullString
|
||||
var createdAt, updatedAt string
|
||||
|
||||
err := rows.Scan(&app.ID, &app.DeveloperID, &app.PackageID, &app.Name, &desc, &cat, &tagsJSON, &app.Status, &createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
app.Description = desc.String
|
||||
app.Category = cat.String
|
||||
app.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", createdAt)
|
||||
app.UpdatedAt, _ = time.Parse("2006-01-02 15:04:05", updatedAt)
|
||||
app.Tags = []string{}
|
||||
if tagsJSON.Valid && tagsJSON.String != "" {
|
||||
json.Unmarshal([]byte(tagsJSON.String), &app.Tags)
|
||||
}
|
||||
|
||||
apps = append(apps, &app)
|
||||
}
|
||||
|
||||
return apps, total, nil
|
||||
}
|
||||
|
||||
// UpdateApp updates an app
|
||||
func (db *DB) UpdateApp(ctx context.Context, app *App) error {
|
||||
tagsJSON, _ := json.Marshal(app.Tags)
|
||||
_, err := db.ExecContext(ctx, `
|
||||
UPDATE apps SET name = ?, description = ?, category = ?, tags = ?, updated_at = datetime('now')
|
||||
WHERE id = ?
|
||||
`, app.Name, app.Description, app.Category, string(tagsJSON), app.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateAppStatus updates an app's status
|
||||
func (db *DB) UpdateAppStatus(ctx context.Context, id, status string) error {
|
||||
_, err := db.ExecContext(ctx, `
|
||||
UPDATE apps SET status = ?, updated_at = datetime('now') WHERE id = ?
|
||||
`, status, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteApp deletes an app and its versions
|
||||
func (db *DB) DeleteApp(ctx context.Context, id string) error {
|
||||
_, err := db.ExecContext(ctx, `DELETE FROM apps WHERE id = ?`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// AppHasPublishedVersions checks if an app has any published versions
|
||||
func (db *DB) AppHasPublishedVersions(ctx context.Context, appID string) (bool, error) {
|
||||
var count int
|
||||
err := db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*) FROM app_versions WHERE app_id = ? AND status = 'published'
|
||||
`, appID).Scan(&count)
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// CreateVersion creates a new app version
|
||||
func (db *DB) CreateVersion(ctx context.Context, version *AppVersion) (*AppVersion, error) {
|
||||
version.ID = uuid.New().String()
|
||||
version.Permissions = []string{}
|
||||
version.CreatedAt = time.Now()
|
||||
|
||||
_, err := db.ExecContext(ctx, `
|
||||
INSERT INTO app_versions (id, app_id, version_code, version_name, package_url, package_size, signature, permissions, min_mosis_version, release_notes, status, created_at)
|
||||
VALUES (?, ?, ?, ?, '', 0, '', '[]', '', ?, 'draft', datetime('now'))
|
||||
`, version.ID, version.AppID, version.VersionCode, version.VersionName, version.ReleaseNotes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return version, nil
|
||||
}
|
||||
|
||||
// GetVersion retrieves a version by ID
|
||||
func (db *DB) GetVersion(ctx context.Context, id string) (*AppVersion, error) {
|
||||
row := db.QueryRowContext(ctx, `
|
||||
SELECT id, app_id, version_code, version_name, package_url, package_size, signature, permissions, min_mosis_version, release_notes, status, review_notes, published_at, created_at
|
||||
FROM app_versions WHERE id = ?
|
||||
`, id)
|
||||
|
||||
return scanVersion(row)
|
||||
}
|
||||
|
||||
func scanVersion(row *sql.Row) (*AppVersion, error) {
|
||||
var v AppVersion
|
||||
var packageURL, signature, permsJSON, minVersion, releaseNotes, reviewNotes sql.NullString
|
||||
var publishedAt, createdAt sql.NullString
|
||||
var packageSize sql.NullInt64
|
||||
|
||||
err := row.Scan(&v.ID, &v.AppID, &v.VersionCode, &v.VersionName, &packageURL, &packageSize, &signature, &permsJSON, &minVersion, &releaseNotes, &v.Status, &reviewNotes, &publishedAt, &createdAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v.PackageURL = packageURL.String
|
||||
v.PackageSize = packageSize.Int64
|
||||
v.Signature = signature.String
|
||||
v.MinMosisVersion = minVersion.String
|
||||
v.ReleaseNotes = releaseNotes.String
|
||||
v.ReviewNotes = reviewNotes.String
|
||||
|
||||
v.Permissions = []string{}
|
||||
if permsJSON.Valid && permsJSON.String != "" {
|
||||
json.Unmarshal([]byte(permsJSON.String), &v.Permissions)
|
||||
}
|
||||
|
||||
if createdAt.Valid {
|
||||
v.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", createdAt.String)
|
||||
}
|
||||
if publishedAt.Valid {
|
||||
t, _ := time.Parse("2006-01-02 15:04:05", publishedAt.String)
|
||||
v.PublishedAt = &t
|
||||
}
|
||||
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
// ListVersions lists versions for an app
|
||||
func (db *DB) ListVersions(ctx context.Context, appID, status string, page, limit int) ([]*AppVersion, int, error) {
|
||||
offset := (page - 1) * limit
|
||||
|
||||
query := "SELECT id, app_id, version_code, version_name, package_url, package_size, signature, permissions, min_mosis_version, release_notes, status, review_notes, published_at, created_at FROM app_versions WHERE app_id = ?"
|
||||
countQuery := "SELECT COUNT(*) FROM app_versions WHERE app_id = ?"
|
||||
args := []interface{}{appID}
|
||||
countArgs := []interface{}{appID}
|
||||
|
||||
if status != "" {
|
||||
query += " AND status = ?"
|
||||
countQuery += " AND status = ?"
|
||||
args = append(args, status)
|
||||
countArgs = append(countArgs, status)
|
||||
}
|
||||
|
||||
query += " ORDER BY version_code DESC LIMIT ? OFFSET ?"
|
||||
args = append(args, limit, offset)
|
||||
|
||||
var total int
|
||||
db.QueryRowContext(ctx, countQuery, countArgs...).Scan(&total)
|
||||
|
||||
rows, err := db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var versions []*AppVersion
|
||||
for rows.Next() {
|
||||
var v AppVersion
|
||||
var packageURL, signature, permsJSON, minVersion, releaseNotes, reviewNotes sql.NullString
|
||||
var publishedAt, createdAt sql.NullString
|
||||
var packageSize sql.NullInt64
|
||||
|
||||
err := rows.Scan(&v.ID, &v.AppID, &v.VersionCode, &v.VersionName, &packageURL, &packageSize, &signature, &permsJSON, &minVersion, &releaseNotes, &v.Status, &reviewNotes, &publishedAt, &createdAt)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
v.PackageURL = packageURL.String
|
||||
v.PackageSize = packageSize.Int64
|
||||
v.Signature = signature.String
|
||||
v.MinMosisVersion = minVersion.String
|
||||
v.ReleaseNotes = releaseNotes.String
|
||||
v.ReviewNotes = reviewNotes.String
|
||||
|
||||
v.Permissions = []string{}
|
||||
if permsJSON.Valid && permsJSON.String != "" {
|
||||
json.Unmarshal([]byte(permsJSON.String), &v.Permissions)
|
||||
}
|
||||
|
||||
if createdAt.Valid {
|
||||
v.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", createdAt.String)
|
||||
}
|
||||
if publishedAt.Valid {
|
||||
t, _ := time.Parse("2006-01-02 15:04:05", publishedAt.String)
|
||||
v.PublishedAt = &t
|
||||
}
|
||||
|
||||
versions = append(versions, &v)
|
||||
}
|
||||
|
||||
return versions, total, nil
|
||||
}
|
||||
|
||||
// VersionCodeExists checks if a version code exists for an app
|
||||
func (db *DB) VersionCodeExists(ctx context.Context, appID string, code int) (bool, error) {
|
||||
var count int
|
||||
err := db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*) FROM app_versions WHERE app_id = ? AND version_code = ?
|
||||
`, appID, code).Scan(&count)
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// UpdateVersionStatus updates a version's status
|
||||
func (db *DB) UpdateVersionStatus(ctx context.Context, id, status string) error {
|
||||
_, err := db.ExecContext(ctx, `
|
||||
UPDATE app_versions SET status = ? WHERE id = ?
|
||||
`, status, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// PublishVersion publishes a version
|
||||
func (db *DB) PublishVersion(ctx context.Context, id string) error {
|
||||
_, err := db.ExecContext(ctx, `
|
||||
UPDATE app_versions SET status = 'published', published_at = datetime('now') WHERE id = ?
|
||||
`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// PublicApp represents a published app for the store
|
||||
type PublicApp struct {
|
||||
PackageID string `json:"package_id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Tags []string `json:"tags"`
|
||||
AuthorName string `json:"author_name"`
|
||||
LatestVersion string `json:"latest_version"`
|
||||
DownloadCount int64 `json:"download_count"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// ListPublishedApps lists published apps for the store
|
||||
func (db *DB) ListPublishedApps(ctx context.Context, query, category, sort string, page, limit int) ([]*PublicApp, int, error) {
|
||||
offset := (page - 1) * limit
|
||||
|
||||
// Base query
|
||||
baseQuery := `
|
||||
SELECT a.package_id, a.name, a.description, a.category, a.tags, d.name as author_name,
|
||||
COALESCE(v.version_name, '') as latest_version, 0 as download_count,
|
||||
a.created_at, a.updated_at
|
||||
FROM apps a
|
||||
JOIN developers d ON d.id = a.developer_id
|
||||
LEFT JOIN app_versions v ON v.app_id = a.id AND v.status = 'published'
|
||||
WHERE a.status = 'published'
|
||||
`
|
||||
countQuery := `SELECT COUNT(*) FROM apps WHERE status = 'published'`
|
||||
var args []interface{}
|
||||
var countArgs []interface{}
|
||||
|
||||
// Search filter
|
||||
if query != "" {
|
||||
baseQuery += " AND (a.name LIKE ? OR a.description LIKE ? OR a.package_id LIKE ?)"
|
||||
countQuery += " AND (name LIKE ? OR description LIKE ? OR package_id LIKE ?)"
|
||||
searchTerm := "%" + query + "%"
|
||||
args = append(args, searchTerm, searchTerm, searchTerm)
|
||||
countArgs = append(countArgs, searchTerm, searchTerm, searchTerm)
|
||||
}
|
||||
|
||||
// Category filter
|
||||
if category != "" {
|
||||
baseQuery += " AND a.category = ?"
|
||||
countQuery += " AND category = ?"
|
||||
args = append(args, category)
|
||||
countArgs = append(countArgs, category)
|
||||
}
|
||||
|
||||
// Group by to handle multiple versions (pick latest)
|
||||
baseQuery += " GROUP BY a.id"
|
||||
|
||||
// Sorting
|
||||
switch sort {
|
||||
case "name":
|
||||
baseQuery += " ORDER BY a.name ASC"
|
||||
case "recent":
|
||||
baseQuery += " ORDER BY a.updated_at DESC"
|
||||
default: // popular
|
||||
baseQuery += " ORDER BY a.updated_at DESC" // TODO: implement download count sorting
|
||||
}
|
||||
|
||||
baseQuery += " LIMIT ? OFFSET ?"
|
||||
args = append(args, limit, offset)
|
||||
|
||||
// Get total count
|
||||
var total int
|
||||
db.QueryRowContext(ctx, countQuery, countArgs...).Scan(&total)
|
||||
|
||||
// Get apps
|
||||
rows, err := db.QueryContext(ctx, baseQuery, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var apps []*PublicApp
|
||||
for rows.Next() {
|
||||
var app PublicApp
|
||||
var desc, cat, tagsJSON, latestVersion sql.NullString
|
||||
var createdAt, updatedAt string
|
||||
|
||||
err := rows.Scan(&app.PackageID, &app.Name, &desc, &cat, &tagsJSON, &app.AuthorName,
|
||||
&latestVersion, &app.DownloadCount, &createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
app.Description = desc.String
|
||||
app.Category = cat.String
|
||||
app.LatestVersion = latestVersion.String
|
||||
app.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", createdAt)
|
||||
app.UpdatedAt, _ = time.Parse("2006-01-02 15:04:05", updatedAt)
|
||||
|
||||
app.Tags = []string{}
|
||||
if tagsJSON.Valid && tagsJSON.String != "" {
|
||||
json.Unmarshal([]byte(tagsJSON.String), &app.Tags)
|
||||
}
|
||||
|
||||
apps = append(apps, &app)
|
||||
}
|
||||
|
||||
return apps, total, nil
|
||||
}
|
||||
|
||||
// GetPublishedApp retrieves a published app by package ID for the store
|
||||
func (db *DB) GetPublishedApp(ctx context.Context, packageID string) (*PublicApp, error) {
|
||||
row := db.QueryRowContext(ctx, `
|
||||
SELECT a.package_id, a.name, a.description, a.category, a.tags, d.name as author_name,
|
||||
COALESCE(v.version_name, '') as latest_version, 0 as download_count,
|
||||
a.created_at, a.updated_at
|
||||
FROM apps a
|
||||
JOIN developers d ON d.id = a.developer_id
|
||||
LEFT JOIN (
|
||||
SELECT app_id, version_name FROM app_versions
|
||||
WHERE status = 'published'
|
||||
ORDER BY version_code DESC LIMIT 1
|
||||
) v ON v.app_id = a.id
|
||||
WHERE a.package_id = ? AND a.status = 'published'
|
||||
`, packageID)
|
||||
|
||||
var app PublicApp
|
||||
var desc, cat, tagsJSON, latestVersion sql.NullString
|
||||
var createdAt, updatedAt string
|
||||
|
||||
err := row.Scan(&app.PackageID, &app.Name, &desc, &cat, &tagsJSON, &app.AuthorName,
|
||||
&latestVersion, &app.DownloadCount, &createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
app.Description = desc.String
|
||||
app.Category = cat.String
|
||||
app.LatestVersion = latestVersion.String
|
||||
app.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", createdAt)
|
||||
app.UpdatedAt, _ = time.Parse("2006-01-02 15:04:05", updatedAt)
|
||||
|
||||
app.Tags = []string{}
|
||||
if tagsJSON.Valid && tagsJSON.String != "" {
|
||||
json.Unmarshal([]byte(tagsJSON.String), &app.Tags)
|
||||
}
|
||||
|
||||
return &app, nil
|
||||
}
|
||||
|
||||
// GetLatestPublishedVersion retrieves the latest published version for an app by package ID
|
||||
func (db *DB) GetLatestPublishedVersion(ctx context.Context, packageID string) (*AppVersion, error) {
|
||||
row := db.QueryRowContext(ctx, `
|
||||
SELECT v.id, v.app_id, v.version_code, v.version_name, v.package_url, v.package_size, v.signature,
|
||||
v.permissions, v.min_mosis_version, v.release_notes, v.status, v.review_notes, v.published_at, v.created_at
|
||||
FROM app_versions v
|
||||
JOIN apps a ON a.id = v.app_id
|
||||
WHERE a.package_id = ? AND v.status = 'published'
|
||||
ORDER BY v.version_code DESC
|
||||
LIMIT 1
|
||||
`, packageID)
|
||||
|
||||
return scanVersion(row)
|
||||
}
|
||||
|
||||
// GetPublishedVersionByCode retrieves a specific published version by package ID and version code
|
||||
func (db *DB) GetPublishedVersionByCode(ctx context.Context, packageID string, versionCode int) (*AppVersion, error) {
|
||||
row := db.QueryRowContext(ctx, `
|
||||
SELECT v.id, v.app_id, v.version_code, v.version_name, v.package_url, v.package_size, v.signature,
|
||||
v.permissions, v.min_mosis_version, v.release_notes, v.status, v.review_notes, v.published_at, v.created_at
|
||||
FROM app_versions v
|
||||
JOIN apps a ON a.id = v.app_id
|
||||
WHERE a.package_id = ? AND v.version_code = ? AND v.status = 'published'
|
||||
`, packageID, versionCode)
|
||||
|
||||
return scanVersion(row)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user