synthwave/src/library/event.rs
JMARyA 1dfb3d4964
Some checks failed
ci/woodpecker/push/deploy Pipeline failed
based
2024-12-18 18:59:00 +01:00

62 lines
1.8 KiB
Rust

use based::{auth::User, get_pg};
use serde::{Deserialize, Serialize};
use sqlx::prelude::FromRow;
/// Represents a user event in the database.
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct Event {
/// Unique identifier for this event.
pub id: uuid::Uuid,
/// Timestamp of when this event occurred.
pub time: chrono::DateTime<chrono::Utc>,
/// Type of event (e.g. play, stop).
pub kind: EventKind,
/// Username of the user who performed this action.
pub user: String,
/// ID of the track associated with this event.
pub track: uuid::Uuid,
}
/// Enum representing different types of events that can occur.
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::Type)]
#[sqlx(type_name = "event_kind", rename_all = "lowercase")]
pub enum EventKind {
/// A user started playing a track.
Play,
/// A user finished playing a track.
Played,
/// A user stopped playing a track.
Stop,
}
impl Event {
/// Creates a new event in the database with the given kind, user, and track.
///
/// Returns the newly created event object.
pub async fn create(kind: EventKind, user: &User, track: uuid::Uuid) -> Self {
sqlx::query_as("INSERT INTO events (kind, \"user\", track) VALUES ($1, $2, $3) RETURNING *")
.bind(kind)
.bind(&user.username)
.bind(track)
.fetch_one(get_pg!())
.await
.unwrap()
}
/// Retrieves the latest 300 events performed by a given user.
///
/// Returns a vector of event objects.
pub async fn get_latest_events_of(u: &User) -> Vec<Self> {
sqlx::query_as("SELECT * FROM events WHERE \"user\" = $1 ORDER BY time DESC LIMIT 300")
.bind(&u.username)
.fetch_all(get_pg!())
.await
.unwrap()
}
}