Simplify CRUD trait & Package repository
This commit is contained in:
@ -1,2 +1,3 @@
|
||||
pub mod user;
|
||||
pub mod base;
|
||||
pub mod package;
|
||||
pub mod user;
|
||||
|
@ -4,95 +4,94 @@ use sqlx::{Executor, MySql};
|
||||
|
||||
pub struct BaseAdapter;
|
||||
|
||||
struct DatabaseBase {
|
||||
id: u64,
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
created_at: DateTime<Utc>,
|
||||
updated_at: DateTime<Utc>,
|
||||
}
|
||||
impl From<DatabaseBase> for Base {
|
||||
fn from(value: DatabaseBase) -> Self {
|
||||
Self {
|
||||
id: Id(value.id),
|
||||
name: value.name.into(),
|
||||
description: value.description.into(),
|
||||
created_at: value.created_at,
|
||||
updated_at: value.updated_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> BaseRepository<E> for BaseAdapter where for<'a> &'a E: Executor<'a, Database = MySql> {}
|
||||
impl<E> crate::port::CRUD<E> for BaseAdapter
|
||||
where
|
||||
for<'a> &'a E: Executor<'a, Database = MySql>,
|
||||
{
|
||||
type Create = New;
|
||||
type Read = Id;
|
||||
type New = New;
|
||||
type Unique = u64;
|
||||
type Update = Field;
|
||||
type Delete = Id;
|
||||
type Existing = Base;
|
||||
type Id = Id;
|
||||
|
||||
async fn create(connection: &mut E, data: Self::Create) -> Result<Self::Id> {
|
||||
Ok(Id(sqlx::query!(
|
||||
"INSERT INTO PackageBases (name, description) VALUES (?, ?)",
|
||||
async fn create(connection: &mut E, data: Self::New) -> Result<Self::Existing> {
|
||||
let created_at = Utc::now();
|
||||
let id = sqlx::query!(
|
||||
"INSERT INTO PackageBases (name, description, created_at, updated_at) VALUES (?, ?, ?, ?)",
|
||||
data.name.0,
|
||||
data.description.0,
|
||||
created_at, created_at,
|
||||
)
|
||||
.execute(&*connection)
|
||||
.await?
|
||||
.last_insert_id()))
|
||||
.last_insert_id();
|
||||
|
||||
Ok(Self::Existing {
|
||||
id,
|
||||
name: data.name.into_inner().0,
|
||||
description: data.description.into_inner().0,
|
||||
created_at,
|
||||
updated_at: created_at,
|
||||
})
|
||||
}
|
||||
|
||||
async fn read(connection: &E, data: Self::Read) -> Result<Option<Self::Existing>> {
|
||||
Ok(sqlx::query_as!(
|
||||
DatabaseBase,
|
||||
"SELECT * FROM PackageBases WHERE id = ?",
|
||||
data.0
|
||||
async fn read(connection: &E, data: Self::Unique) -> Result<Option<Self::Existing>> {
|
||||
Ok(
|
||||
sqlx::query_as!(Base, "SELECT * FROM PackageBases WHERE id = ?", data)
|
||||
.fetch_optional(connection)
|
||||
.await?,
|
||||
)
|
||||
.fetch_optional(connection)
|
||||
.await?
|
||||
.map(Into::into))
|
||||
}
|
||||
|
||||
async fn update(connection: &mut E, id: Self::Id, data: Self::Update) -> Result {
|
||||
match data {
|
||||
Field::Name(valid) => {
|
||||
async fn update(
|
||||
connection: &mut E,
|
||||
existing: &mut Self::Existing,
|
||||
data: Self::Update,
|
||||
) -> Result {
|
||||
match &data {
|
||||
Field::Name(name) => {
|
||||
sqlx::query!(
|
||||
"UPDATE PackageBases SET name = ? WHERE id = ?",
|
||||
valid.0,
|
||||
id.0
|
||||
name.0,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::Description(valid) => {
|
||||
Field::Description(description) => {
|
||||
sqlx::query!(
|
||||
"UPDATE PackageBases SET description = ? WHERE id = ?",
|
||||
valid.0,
|
||||
id.0
|
||||
description.0,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::CreatedAt(date_time) => sqlx::query!(
|
||||
"UPDATE PackageBases SET created_at = ? WHERE id = ?",
|
||||
date_time,
|
||||
id.0
|
||||
existing.id
|
||||
),
|
||||
Field::UpdatedAt(date_time) => sqlx::query!(
|
||||
"UPDATE PackageBases SET updated_at = ? WHERE id = ?",
|
||||
date_time,
|
||||
id.0
|
||||
existing.id
|
||||
),
|
||||
}
|
||||
.execute(&*connection)
|
||||
.await?;
|
||||
|
||||
match data {
|
||||
Field::Name(valid) => existing.name = valid.into_inner().0,
|
||||
Field::Description(valid) => existing.description = valid.into_inner().0,
|
||||
Field::CreatedAt(date_time) => existing.created_at = date_time,
|
||||
Field::UpdatedAt(date_time) => existing.updated_at = date_time,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(connection: &mut E, data: Self::Delete) -> Result {
|
||||
sqlx::query!("DELETE FROM PackageBases WHERE id = ?", data.0)
|
||||
async fn delete(connection: &mut E, data: Self::Unique) -> Result {
|
||||
sqlx::query!("DELETE FROM PackageBases WHERE id = ?", data)
|
||||
.execute(&*connection)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
150
3/coursework/src/database/src/adapter/mysql/package.rs
Normal file
150
3/coursework/src/database/src/adapter/mysql/package.rs
Normal file
@ -0,0 +1,150 @@
|
||||
pub use crate::port::package::*;
|
||||
|
||||
use sqlx::{Executor, MySql};
|
||||
|
||||
pub struct PackageAdapter;
|
||||
|
||||
impl<E> PackageRepository<E> for PackageAdapter where for<'a> &'a E: Executor<'a, Database = MySql> {}
|
||||
impl<E> crate::port::CRUD<E> for PackageAdapter
|
||||
where
|
||||
for<'a> &'a E: Executor<'a, Database = MySql>,
|
||||
{
|
||||
type New = New;
|
||||
type Update = Field;
|
||||
type Unique = Unique;
|
||||
type Existing = Package;
|
||||
|
||||
async fn create(connection: &mut E, data: Self::New) -> Result<Self::Existing> {
|
||||
let created_at = Utc::now();
|
||||
let id = sqlx::query!(
|
||||
"INSERT INTO Packages \
|
||||
(package_base, name, version, description, url, flagged_at, created_at, updated_at) \
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
data.package_base.id,
|
||||
data.name.0,
|
||||
data.version.0,
|
||||
data.description.0,
|
||||
data.url.0,
|
||||
data.flagged_at,
|
||||
created_at,
|
||||
created_at,
|
||||
)
|
||||
.execute(&*connection)
|
||||
.await?
|
||||
.last_insert_id();
|
||||
|
||||
Ok(Self::Existing {
|
||||
id,
|
||||
package_base: data.package_base.id,
|
||||
name: data.name.into_inner().0,
|
||||
version: data.version.into_inner().0,
|
||||
description: data.description.into_inner().0,
|
||||
url: data.url.into_inner().0,
|
||||
flagged_at: data.flagged_at,
|
||||
created_at,
|
||||
updated_at: created_at,
|
||||
})
|
||||
}
|
||||
|
||||
async fn read(connection: &E, data: Self::Unique) -> Result<Option<Self::Existing>> {
|
||||
Ok(match data {
|
||||
Unique::Id(id) => {
|
||||
sqlx::query_as!(Package, "SELECT * FROM Packages WHERE id = ?", id)
|
||||
.fetch_optional(connection)
|
||||
.await
|
||||
}
|
||||
Unique::Name(name) => {
|
||||
sqlx::query_as!(Package, "SELECT * FROM Packages WHERE name = ?", name.0)
|
||||
.fetch_optional(connection)
|
||||
.await
|
||||
}
|
||||
}?)
|
||||
}
|
||||
|
||||
async fn update(
|
||||
connection: &mut E,
|
||||
existing: &mut Self::Existing,
|
||||
data: Self::Update,
|
||||
) -> Result {
|
||||
match &data {
|
||||
Field::Name(name) => {
|
||||
sqlx::query!(
|
||||
"UPDATE Packages SET name = ? WHERE id = ?",
|
||||
name.0,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::PackageBase(package_base) => {
|
||||
sqlx::query!(
|
||||
"UPDATE Packages SET package_base = ? WHERE id = ?",
|
||||
package_base.id,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::Version(version) => {
|
||||
sqlx::query!(
|
||||
"UPDATE Packages SET version = ? WHERE id = ?",
|
||||
version.0,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::Description(description) => {
|
||||
sqlx::query!(
|
||||
"UPDATE Packages SET description = ? WHERE id = ?",
|
||||
description.0,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::URL(url) => {
|
||||
sqlx::query!(
|
||||
"UPDATE Packages SET url = ? WHERE id = ?",
|
||||
url.0,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::FlaggedAt(date_time) => sqlx::query!(
|
||||
"UPDATE Packages SET flagged_at = ? WHERE id = ?",
|
||||
date_time,
|
||||
existing.id
|
||||
),
|
||||
Field::CreatedAt(date_time) => sqlx::query!(
|
||||
"UPDATE Packages SET created_at = ? WHERE id = ?",
|
||||
date_time,
|
||||
existing.id
|
||||
),
|
||||
Field::UpdatedAt(date_time) => sqlx::query!(
|
||||
"UPDATE Packages SET updated_at = ? WHERE id = ?",
|
||||
date_time,
|
||||
existing.id
|
||||
),
|
||||
}
|
||||
.execute(&*connection)
|
||||
.await?;
|
||||
|
||||
match data {
|
||||
Field::Name(valid) => existing.name = valid.into_inner().0,
|
||||
Field::PackageBase(base) => existing.package_base = base.id,
|
||||
Field::Version(valid) => existing.version = valid.into_inner().0,
|
||||
Field::Description(valid) => existing.description = valid.into_inner().0,
|
||||
Field::URL(valid) => existing.url = valid.into_inner().0,
|
||||
Field::FlaggedAt(date_time) => existing.flagged_at = date_time,
|
||||
Field::CreatedAt(date_time) => existing.created_at = date_time,
|
||||
Field::UpdatedAt(date_time) => existing.updated_at = date_time,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(connection: &mut E, data: Self::Unique) -> Result {
|
||||
match data {
|
||||
Unique::Id(id) => sqlx::query!("DELETE FROM Packages WHERE id = ?", id),
|
||||
Unique::Name(name) => {
|
||||
sqlx::query!("DELETE FROM Packages WHERE name = ?", name.0)
|
||||
}
|
||||
}
|
||||
.execute(&*connection)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -4,113 +4,125 @@ use sqlx::{Executor, MySql};
|
||||
|
||||
pub struct UserAdapter;
|
||||
|
||||
struct DatabaseUser {
|
||||
id: u64,
|
||||
name: String,
|
||||
email: String,
|
||||
password: String,
|
||||
last_used: Option<DateTime<Utc>>,
|
||||
created_at: DateTime<Utc>,
|
||||
updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl From<DatabaseUser> for User {
|
||||
fn from(value: DatabaseUser) -> Self {
|
||||
Self {
|
||||
id: Id(value.id),
|
||||
name: value.name.into(),
|
||||
email: value.email.into(),
|
||||
password: value.password.into(),
|
||||
last_used: value.last_used,
|
||||
created_at: value.created_at,
|
||||
updated_at: value.updated_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> UserRepository<E> for UserAdapter where for<'a> &'a E: Executor<'a, Database = MySql> {}
|
||||
impl<E> crate::port::CRUD<E> for UserAdapter
|
||||
where
|
||||
for<'a> &'a E: Executor<'a, Database = MySql>,
|
||||
{
|
||||
type Create = New;
|
||||
type Read = Unique;
|
||||
type New = New;
|
||||
type Update = Field;
|
||||
type Delete = Unique;
|
||||
type Unique = Unique;
|
||||
type Existing = User;
|
||||
type Id = Id;
|
||||
|
||||
async fn create(connection: &mut E, data: Self::Create) -> Result<Self::Id> {
|
||||
Ok(Id(sqlx::query!(
|
||||
"INSERT INTO Users (name, email, password, last_used) VALUES (?, ?, ?, ?)",
|
||||
async fn create(connection: &mut E, data: Self::New) -> Result<Self::Existing> {
|
||||
let created_at = Utc::now();
|
||||
let id = sqlx::query!(
|
||||
"INSERT INTO Users (name, email, password, last_used, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
data.name.0,
|
||||
data.email.0,
|
||||
data.password.0,
|
||||
data.last_used,
|
||||
created_at,
|
||||
created_at,
|
||||
)
|
||||
.execute(&*connection)
|
||||
.await?
|
||||
.last_insert_id()))
|
||||
.last_insert_id();
|
||||
|
||||
Ok(Self::Existing {
|
||||
id,
|
||||
name: data.name.into_inner().0,
|
||||
email: data.email.into_inner().0,
|
||||
password: data.password.into_inner().0,
|
||||
last_used: data.last_used,
|
||||
created_at,
|
||||
updated_at: created_at,
|
||||
})
|
||||
}
|
||||
|
||||
async fn read(connection: &E, data: Self::Read) -> Result<Option<Self::Existing>> {
|
||||
async fn read(connection: &E, data: Self::Unique) -> Result<Option<Self::Existing>> {
|
||||
Ok(match data {
|
||||
Unique::Id(id) => {
|
||||
sqlx::query_as!(DatabaseUser, "SELECT * FROM Users WHERE id = ?", id.0)
|
||||
sqlx::query_as!(User, "SELECT * FROM Users WHERE id = ?", id)
|
||||
.fetch_optional(connection)
|
||||
.await
|
||||
}
|
||||
Unique::Name(name) => {
|
||||
sqlx::query_as!(DatabaseUser, "SELECT * FROM Users WHERE name = ?", name.0)
|
||||
sqlx::query_as!(User, "SELECT * FROM Users WHERE name = ?", name.0)
|
||||
.fetch_optional(connection)
|
||||
.await
|
||||
}
|
||||
Unique::Email(email) => {
|
||||
sqlx::query_as!(DatabaseUser, "SELECT * FROM Users WHERE email = ?", email.0)
|
||||
sqlx::query_as!(User, "SELECT * FROM Users WHERE email = ?", email.0)
|
||||
.fetch_optional(connection)
|
||||
.await
|
||||
}
|
||||
}?
|
||||
.map(Into::into))
|
||||
}?)
|
||||
}
|
||||
|
||||
async fn update(connection: &mut E, id: Self::Id, data: Self::Update) -> Result {
|
||||
match data {
|
||||
Field::Name(valid) => {
|
||||
sqlx::query!("UPDATE Users SET name = ? WHERE id = ?", valid.0, id.0)
|
||||
async fn update(
|
||||
connection: &mut E,
|
||||
existing: &mut Self::Existing,
|
||||
data: Self::Update,
|
||||
) -> Result {
|
||||
match &data {
|
||||
Field::Name(name) => {
|
||||
sqlx::query!(
|
||||
"UPDATE Users SET name = ? WHERE id = ?",
|
||||
name.0,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::Email(valid) => {
|
||||
sqlx::query!("UPDATE Users SET email = ? WHERE id = ?", valid.0, id.0)
|
||||
Field::Email(email) => {
|
||||
sqlx::query!(
|
||||
"UPDATE Users SET email = ? WHERE id = ?",
|
||||
email.0,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::Password(valid) => {
|
||||
sqlx::query!("UPDATE Users SET password = ? WHERE id = ?", valid.0, id.0)
|
||||
Field::Password(password) => {
|
||||
sqlx::query!(
|
||||
"UPDATE Users SET password = ? WHERE id = ?",
|
||||
password.0,
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::LastUsed(date_time) => {
|
||||
sqlx::query!(
|
||||
"UPDATE Users SET last_used = ? WHERE id = ?",
|
||||
date_time,
|
||||
id.0
|
||||
existing.id
|
||||
)
|
||||
}
|
||||
Field::CreatedAt(date_time) => sqlx::query!(
|
||||
"UPDATE Users SET created_at = ? WHERE id = ?",
|
||||
date_time,
|
||||
id.0
|
||||
existing.id
|
||||
),
|
||||
Field::UpdatedAt(date_time) => sqlx::query!(
|
||||
"UPDATE Users SET updated_at = ? WHERE id = ?",
|
||||
date_time,
|
||||
id.0
|
||||
existing.id
|
||||
),
|
||||
}
|
||||
.execute(&*connection)
|
||||
.await?;
|
||||
|
||||
match data {
|
||||
Field::Name(valid) => existing.name = valid.into_inner().0,
|
||||
Field::Email(valid) => existing.email = valid.into_inner().0,
|
||||
Field::Password(valid) => existing.password = valid.into_inner().0,
|
||||
Field::LastUsed(date_time) => existing.last_used = date_time,
|
||||
Field::CreatedAt(date_time) => existing.created_at = date_time,
|
||||
Field::UpdatedAt(date_time) => existing.updated_at = date_time,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(connection: &mut E, data: Self::Delete) -> Result {
|
||||
async fn delete(connection: &mut E, data: Self::Unique) -> Result {
|
||||
match data {
|
||||
Unique::Id(id) => sqlx::query!("DELETE FROM Users WHERE id = ?", id.0),
|
||||
Unique::Id(id) => sqlx::query!("DELETE FROM Users WHERE id = ?", id),
|
||||
Unique::Name(name) => {
|
||||
sqlx::query!("DELETE FROM Users WHERE name = ?", name.0)
|
||||
}
|
||||
@ -120,6 +132,7 @@ where
|
||||
}
|
||||
.execute(&*connection)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ pub trait IntoValid: Validate {
|
||||
}
|
||||
impl<T: garde::Validate> IntoValid for T {}
|
||||
|
||||
pub mod adapter;
|
||||
pub mod atomic;
|
||||
pub mod connect;
|
||||
pub mod port;
|
||||
pub mod adapter;
|
||||
|
@ -1,18 +0,0 @@
|
||||
use garde::{Valid, Validate};
|
||||
|
||||
#[derive(Validate)]
|
||||
enum Data {
|
||||
Struct {
|
||||
#[garde(range(min=-10, max=10))]
|
||||
field: i32,
|
||||
},
|
||||
Tuple(#[garde(ascii)] String),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let data = Data::Struct { field: 100 };
|
||||
let data = Data::Tuple("🧱".into());
|
||||
if let Err(e) = data.validate() {
|
||||
println!("invalid data: {e}");
|
||||
}
|
||||
}
|
@ -2,20 +2,21 @@ pub type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait CRUD<C> {
|
||||
type Create;
|
||||
type Read;
|
||||
type New;
|
||||
type Unique;
|
||||
type Update;
|
||||
type Delete;
|
||||
type Existing;
|
||||
type Id;
|
||||
|
||||
async fn create(connection: &mut C, data: Self::Create) -> Result<Self::Id>;
|
||||
async fn read(connection: &C, data: Self::Read) -> Result<Option<Self::Existing>>;
|
||||
async fn update(connection: &mut C, id: Self::Id, data: Self::Update) -> Result;
|
||||
async fn delete(connection: &mut C, data: Self::Delete) -> Result;
|
||||
async fn create(connection: &mut C, data: Self::New) -> Result<Self::Existing>;
|
||||
async fn read(connection: &C, data: Self::Unique) -> Result<Option<Self::Existing>>;
|
||||
async fn update(
|
||||
connection: &mut C,
|
||||
existing: &mut Self::Existing,
|
||||
data: Self::Update,
|
||||
) -> Result;
|
||||
async fn delete(connection: &mut C, data: Self::Unique) -> Result;
|
||||
}
|
||||
|
||||
pub mod user;
|
||||
pub mod base;
|
||||
// pub mod package;
|
||||
// pub mod session;
|
||||
pub mod package;
|
||||
pub mod user;
|
||||
|
@ -6,45 +6,21 @@ use garde::{Valid, Validate};
|
||||
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait BaseRepository<C>:
|
||||
super::CRUD<
|
||||
C,
|
||||
Create = New,
|
||||
Read = Id,
|
||||
Update = Field,
|
||||
Delete = Id,
|
||||
Existing = Base,
|
||||
Id = Id,
|
||||
>
|
||||
CRUD<C, New = New, Unique = u64, Update = Field, Existing = Base>
|
||||
{
|
||||
async fn update_base(connection: &mut C, base: &mut Base, data: Self::Update) -> Result {
|
||||
Self::update(connection, base.id, data.clone()).await?;
|
||||
match data {
|
||||
Field::Name(valid) => base.name = valid.into_inner(),
|
||||
Field::Description(valid) => base.description = valid.into_inner(),
|
||||
Field::CreatedAt(date_time) => base.created_at = date_time,
|
||||
Field::UpdatedAt(date_time) => base.updated_at = date_time,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deref, Into, Clone, Copy)]
|
||||
pub struct Id(pub(crate) u64);
|
||||
// #[derive(Deref, Into, Clone, Copy)]
|
||||
// pub struct Id(pub(crate) u64);
|
||||
|
||||
#[derive(Validate, Deref, From, Clone)]
|
||||
#[derive(Validate, Deref, From, Into)]
|
||||
#[garde(transparent)]
|
||||
pub struct Name(#[garde(alphanumeric, length(min = 2, max = 127))] pub String);
|
||||
pub struct Name(#[garde(length(chars, max = 127))] pub String);
|
||||
|
||||
#[derive(Validate, Deref, From, Clone)]
|
||||
#[derive(Validate, Deref, From, Into)]
|
||||
#[garde(transparent)]
|
||||
pub struct Description(#[garde(length(max = 510))] pub Option<String>);
|
||||
pub struct Description(#[garde(length(chars, max = 510))] pub Option<String>);
|
||||
|
||||
pub struct New {
|
||||
pub name: Name,
|
||||
pub description: Description,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Field {
|
||||
Name(Valid<Name>),
|
||||
Description(Valid<Description>),
|
||||
@ -52,28 +28,33 @@ pub enum Field {
|
||||
UpdatedAt(DateTime<Utc>),
|
||||
}
|
||||
|
||||
pub struct New {
|
||||
pub name: Valid<Name>,
|
||||
pub description: Valid<Description>,
|
||||
}
|
||||
|
||||
pub struct Base {
|
||||
pub(crate) id: Id,
|
||||
pub(crate) name: Name,
|
||||
pub(crate) description: Description,
|
||||
pub(crate) id: u64,
|
||||
pub(crate) name: String,
|
||||
pub(crate) description: Option<String>,
|
||||
pub(crate) created_at: DateTime<Utc>,
|
||||
pub(crate) updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Base {
|
||||
pub fn id(&self) -> Id {
|
||||
pub const fn id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
pub fn name(&self) -> &Name {
|
||||
pub const fn name(&self) -> &String {
|
||||
&self.name
|
||||
}
|
||||
pub fn description(&self) -> &Description {
|
||||
&self.description
|
||||
pub const fn description(&self) -> Option<&String> {
|
||||
self.description.as_ref()
|
||||
}
|
||||
pub fn created_at(&self) -> DateTime<Utc> {
|
||||
pub const fn created_at(&self) -> DateTime<Utc> {
|
||||
self.created_at
|
||||
}
|
||||
pub fn updated_at(&self) -> DateTime<Utc> {
|
||||
pub const fn updated_at(&self) -> DateTime<Utc> {
|
||||
self.updated_at
|
||||
}
|
||||
}
|
||||
|
@ -1,168 +1,94 @@
|
||||
use super::{Result, package_base::Base};
|
||||
pub use super::{CRUD, Result, base::Base};
|
||||
|
||||
pub use chrono::{DateTime, Utc};
|
||||
use derive_more::{Deref, From, Into};
|
||||
use garde::{Valid, Validate};
|
||||
use sqlx::{Executor, MySql};
|
||||
|
||||
// pub enum GetBy
|
||||
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait PackageRepository<C> {
|
||||
async fn get_by_id(connection: &C, id: Id) -> Result<Option<Package>>;
|
||||
async fn get_by_name(connection: &C, name: &Valid<Name>) -> Result<Option<Package>>;
|
||||
|
||||
async fn change_name(connection: &mut C, package: &mut Package, name: Valid<Name>) -> Result;
|
||||
async fn change_base(connection: &mut C, package: &mut Package, base: &Base) -> Result;
|
||||
async fn change_version(connection: &mut C, package: &mut Package, version: Valid<Version>) -> Result;
|
||||
|
||||
async fn create(connection: &mut C, data: Valid<PackageData>) -> Result<Package>;
|
||||
pub trait PackageRepository<C>:
|
||||
CRUD<C, New = New, Update = Field, Unique = Unique, Existing = Package>
|
||||
{
|
||||
}
|
||||
|
||||
#[derive(Deref, Into, Clone, Copy)]
|
||||
pub struct Id(u32);
|
||||
|
||||
pub type BaseId = super::package_base::Id;
|
||||
|
||||
#[derive(Validate, Deref)]
|
||||
#[derive(Validate, Deref, From, Into)]
|
||||
#[garde(transparent)]
|
||||
pub struct Name(#[garde(alphanumeric, length(min = 2, max = 127))] pub String);
|
||||
pub struct Name(#[garde(length(chars, max = 127))] pub String);
|
||||
|
||||
#[derive(Validate, Deref)]
|
||||
#[derive(Validate, Deref, From, Into)]
|
||||
#[garde(transparent)]
|
||||
pub struct Version(#[garde(alphanumeric, length(min = 1, max = 127))] pub String);
|
||||
pub struct Version(#[garde(length(chars, max = 127))] pub String);
|
||||
|
||||
#[derive(Validate, Deref)]
|
||||
#[derive(Validate, Deref, From, Into)]
|
||||
#[garde(transparent)]
|
||||
pub struct Description(#[garde(ascii, length(max = 255))] pub Option<String>);
|
||||
pub struct Description(#[garde(length(chars, max = 255))] pub Option<String>);
|
||||
|
||||
#[derive(Validate)]
|
||||
#[derive(Validate, Deref, From, Into)]
|
||||
#[garde(transparent)]
|
||||
pub struct URL(#[garde(url, length(max = 510))] pub Option<String>);
|
||||
pub struct URL(#[garde(length(chars, max = 510))] pub Option<String>);
|
||||
|
||||
#[derive(Validate)]
|
||||
pub struct PackageData {
|
||||
#[garde(dive)]
|
||||
pub name: Name,
|
||||
#[garde(dive)]
|
||||
pub description: Description,
|
||||
pub enum Unique {
|
||||
Id(u64),
|
||||
Name(Valid<Name>),
|
||||
}
|
||||
|
||||
pub enum Field {
|
||||
PackageBase(Base),
|
||||
Name(Valid<Name>),
|
||||
Version(Valid<Version>),
|
||||
Description(Valid<Description>),
|
||||
URL(Valid<URL>),
|
||||
FlaggedAt(Option<DateTime<Utc>>),
|
||||
CreatedAt(DateTime<Utc>),
|
||||
UpdatedAt(DateTime<Utc>),
|
||||
}
|
||||
|
||||
pub struct New {
|
||||
pub package_base: Base,
|
||||
pub name: Valid<Name>,
|
||||
pub version: Valid<Version>,
|
||||
pub description: Valid<Description>,
|
||||
pub url: Valid<URL>,
|
||||
pub flagged_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Deref)]
|
||||
pub struct Package {
|
||||
id: Id,
|
||||
#[deref]
|
||||
data: PackageData,
|
||||
pub(crate) id: u64,
|
||||
pub(crate) package_base: u64,
|
||||
pub(crate) name: String,
|
||||
pub(crate) version: String,
|
||||
pub(crate) description: Option<String>,
|
||||
pub(crate) url: Option<String>,
|
||||
pub(crate) flagged_at: Option<DateTime<Utc>>,
|
||||
pub(crate) created_at: DateTime<Utc>,
|
||||
pub(crate) updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Package {
|
||||
pub const fn id(&self) -> Id {
|
||||
pub const fn id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
pub const fn package_base(&self) -> u64 {
|
||||
self.package_base
|
||||
}
|
||||
pub const fn name(&self) -> &String {
|
||||
&self.name
|
||||
}
|
||||
pub const fn version(&self) -> &String {
|
||||
&self.version
|
||||
}
|
||||
pub const fn description(&self) -> Option<&String> {
|
||||
self.description.as_ref()
|
||||
}
|
||||
pub const fn url(&self) -> Option<&String> {
|
||||
self.url.as_ref()
|
||||
}
|
||||
pub const fn flagged_at(&self) -> Option<DateTime<Utc>> {
|
||||
self.flagged_at
|
||||
}
|
||||
pub const fn created_at(&self) -> DateTime<Utc> {
|
||||
self.created_at
|
||||
}
|
||||
pub const fn updated_at(&self) -> DateTime<Utc> {
|
||||
self.updated_at
|
||||
}
|
||||
}
|
||||
|
||||
// pub struct UserAdapter;
|
||||
//
|
||||
// struct QueryUser {
|
||||
// id: u32,
|
||||
// name: String,
|
||||
// email: String,
|
||||
// password: String,
|
||||
// }
|
||||
// impl From<QueryUser> for Package {
|
||||
// fn from(value: QueryUser) -> Self {
|
||||
// Self {
|
||||
// id: Id(value.id),
|
||||
// data: PackageData {
|
||||
// name: Name(value.name),
|
||||
// description: Description(value.email),
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// impl<E> PackageRepository<E> for UserAdapter
|
||||
// where
|
||||
// for<'a> &'a E: Executor<'a, Database = MySql>,
|
||||
// {
|
||||
// async fn get_by_id(connection: &E, id: Id) -> Result<Option<Package>> {
|
||||
// Ok(sqlx::query_as!(
|
||||
// QueryUser,
|
||||
// "SELECT id, name, email, password FROM Users WHERE id = ?",
|
||||
// id.0
|
||||
// )
|
||||
// .fetch_optional(connection)
|
||||
// .await?
|
||||
// .map(Into::into))
|
||||
// }
|
||||
// async fn get_by_name(connection: &E, name: &Valid<Name>) -> Result<Option<Package>> {
|
||||
// Ok(sqlx::query_as!(
|
||||
// QueryUser,
|
||||
// "SELECT id, name, email, password FROM Users WHERE name = ?",
|
||||
// name.0
|
||||
// )
|
||||
// .fetch_optional(connection)
|
||||
// .await?
|
||||
// .map(Into::into))
|
||||
// }
|
||||
// async fn get_by_email(connection: &E, email: &Valid<Description>) -> Result<Option<Package>> {
|
||||
// Ok(sqlx::query_as!(
|
||||
// QueryUser,
|
||||
// "SELECT id, name, email, password FROM Users WHERE email = ?",
|
||||
// email.0
|
||||
// )
|
||||
// .fetch_optional(connection)
|
||||
// .await?
|
||||
// .map(Into::into))
|
||||
// }
|
||||
//
|
||||
// async fn change_name(connection: &mut E, user: &mut Package, name: Valid<Name>) -> Result {
|
||||
// sqlx::query!("UPDATE Users SET name = ? WHERE id = ?", name.0, user.id.0)
|
||||
// .execute(&*connection)
|
||||
// .await?;
|
||||
// Ok(())
|
||||
// }
|
||||
// async fn change_email(
|
||||
// connection: &mut E,
|
||||
// user: &mut Package,
|
||||
// email: Valid<Description>,
|
||||
// ) -> Result {
|
||||
// sqlx::query!(
|
||||
// "UPDATE Users SET email = ? WHERE id = ?",
|
||||
// email.0,
|
||||
// user.id.0
|
||||
// )
|
||||
// .execute(&*connection)
|
||||
// .await?;
|
||||
// Ok(())
|
||||
// }
|
||||
// async fn change_password(
|
||||
// connection: &mut E,
|
||||
// user: &mut Package,
|
||||
// password: Valid<Password>,
|
||||
// ) -> Result {
|
||||
// sqlx::query!(
|
||||
// "UPDATE Users SET password = ? WHERE id = ?",
|
||||
// password.0,
|
||||
// user.id.0
|
||||
// )
|
||||
// .execute(&*connection)
|
||||
// .await?;
|
||||
// Ok(())
|
||||
// }
|
||||
//
|
||||
// async fn create(connection: &mut E, data: Valid<PackageData>) -> Result<Package> {
|
||||
// let id = sqlx::query!(
|
||||
// "INSERT INTO Users (name, email, password) VALUES (?, ?, ?)",
|
||||
// data.name.0,
|
||||
// data.description.0,
|
||||
// data.password.0
|
||||
// )
|
||||
// .execute(&*connection)
|
||||
// .await?
|
||||
// .last_insert_id() as u32;
|
||||
//
|
||||
// Ok(Package {
|
||||
// id: Id(id),
|
||||
// data: data.into_inner(),
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
@ -6,61 +6,28 @@ use garde::{Valid, Validate};
|
||||
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait UserRepository<C>:
|
||||
super::CRUD<
|
||||
C,
|
||||
Create = New,
|
||||
Read = Unique,
|
||||
Update = Field,
|
||||
Delete = Unique,
|
||||
Existing = User,
|
||||
Id = Id,
|
||||
>
|
||||
CRUD<C, New = New, Update = Field, Unique = Unique, Existing = User>
|
||||
{
|
||||
async fn update_user(connection: &mut C, user: &mut User, data: Self::Update) -> Result {
|
||||
Self::update(connection, user.id, data.clone()).await?;
|
||||
match data {
|
||||
Field::Name(valid) => user.name = valid.into_inner(),
|
||||
Field::Email(valid) => user.email = valid.into_inner(),
|
||||
Field::Password(valid) => user.password = valid.into_inner(),
|
||||
Field::LastUsed(date_time) => user.last_used = date_time,
|
||||
Field::CreatedAt(date_time) => user.created_at = date_time,
|
||||
Field::UpdatedAt(date_time) => user.updated_at = date_time,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deref, Into, Clone, Copy)]
|
||||
pub struct Id(pub(crate) u64);
|
||||
|
||||
// TODO: is this the right layer for requirements (email) validatoin?
|
||||
|
||||
#[derive(Validate, Deref, From, Clone)]
|
||||
#[derive(Validate, Deref, From, Into)]
|
||||
#[garde(transparent)]
|
||||
pub struct Name(#[garde(alphanumeric, length(min = 2, max = 31))] pub String);
|
||||
pub struct Name(#[garde(length(chars, max = 31))] pub String);
|
||||
|
||||
#[derive(Validate, Deref, From, Clone)]
|
||||
#[derive(Validate, Deref, From, Into)]
|
||||
#[garde(transparent)]
|
||||
pub struct Email(#[garde(email, length(max = 255))] pub String);
|
||||
pub struct Email(#[garde(length(chars, max = 255))] pub String);
|
||||
|
||||
#[derive(Validate, Deref, From, Clone)]
|
||||
#[derive(Validate, Deref, From, Into)]
|
||||
#[garde(transparent)]
|
||||
pub struct Password(#[garde(ascii, length(max = 255))] pub String);
|
||||
|
||||
pub struct New {
|
||||
pub name: Valid<Name>,
|
||||
pub email: Valid<Email>,
|
||||
pub password: Valid<Password>,
|
||||
pub last_used: Option<DateTime<Utc>>,
|
||||
}
|
||||
pub struct Password(#[garde(length(chars, max = 255))] pub String);
|
||||
|
||||
pub enum Unique {
|
||||
Id(Id),
|
||||
Id(u64),
|
||||
Name(Valid<Name>),
|
||||
Email(Valid<Email>),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Field {
|
||||
Name(Valid<Name>),
|
||||
Email(Valid<Email>),
|
||||
@ -70,27 +37,34 @@ pub enum Field {
|
||||
UpdatedAt(DateTime<Utc>),
|
||||
}
|
||||
|
||||
pub struct New {
|
||||
pub name: Valid<Name>,
|
||||
pub email: Valid<Email>,
|
||||
pub password: Valid<Password>,
|
||||
pub last_used: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
pub struct User {
|
||||
pub(crate) id: Id,
|
||||
pub(crate) name: Name,
|
||||
pub(crate) email: Email,
|
||||
pub(crate) password: Password,
|
||||
pub(crate) id: u64,
|
||||
pub(crate) name: String,
|
||||
pub(crate) email: String,
|
||||
pub(crate) password: String,
|
||||
pub(crate) last_used: Option<DateTime<Utc>>,
|
||||
pub(crate) created_at: DateTime<Utc>,
|
||||
pub(crate) updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub const fn id(&self) -> Id {
|
||||
pub const fn id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
pub const fn name(&self) -> &Name {
|
||||
pub const fn name(&self) -> &String {
|
||||
&self.name
|
||||
}
|
||||
pub const fn email(&self) -> &Email {
|
||||
pub const fn email(&self) -> &String {
|
||||
&self.email
|
||||
}
|
||||
pub const fn password(&self) -> &Password {
|
||||
pub const fn password(&self) -> &String {
|
||||
&self.password
|
||||
}
|
||||
pub const fn last_used(&self) -> Option<DateTime<Utc>> {
|
||||
|
Reference in New Issue
Block a user