Skip to content

Commit

Permalink
Merge pull request #5 from KaioFelps/dev
Browse files Browse the repository at this point in the history
Added article tags and refactored some code
  • Loading branch information
KaioFelps authored Jul 21, 2024
2 parents eefab7d + 9cd2845 commit 09406c1
Show file tree
Hide file tree
Showing 62 changed files with 1,571 additions and 519 deletions.
17 changes: 16 additions & 1 deletion entities/src/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ pub struct Model {
pub cover_url: String,
pub updated_at: Option<DateTime>,
pub approved: bool,
#[sea_orm(unique)]
pub slug: String,
pub tag_id: Option<i32>,
pub tag_value: Option<String>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::article_tag::Entity",
from = "Column::TagId",
to = "super::article_tag::Column::Id",
on_update = "NoAction",
on_delete = "SetNull"
)]
ArticleTag,
#[sea_orm(has_many = "super::comment::Entity")]
Comment,
#[sea_orm(
Expand All @@ -34,6 +43,12 @@ pub enum Relation {
User,
}

impl Related<super::article_tag::Entity> for Entity {
fn to() -> RelationDef {
Relation::ArticleTag.def()
}
}

impl Related<super::comment::Entity> for Entity {
fn to() -> RelationDef {
Relation::Comment.def()
Expand Down
26 changes: 26 additions & 0 deletions entities/src/article_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.10
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "article_tag")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub value: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::article::Entity")]
Article,
}

impl Related<super::article::Entity> for Entity {
fn to() -> RelationDef {
Relation::Article.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
1 change: 1 addition & 0 deletions entities/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod prelude;

pub mod article;
pub mod article_tag;
pub mod comment;
pub mod comment_report;
pub mod sea_orm_active_enums;
Expand Down
1 change: 1 addition & 0 deletions entities/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.10
pub use super::article::Entity as Article;
pub use super::article_tag::Entity as ArticleTag;
pub use super::comment::Entity as Comment;
pub use super::comment_report::Entity as CommentReport;
pub use super::team_role::Entity as TeamRole;
Expand Down
4 changes: 2 additions & 2 deletions entities/src/sea_orm_active_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ pub enum Role {
Principal,
#[sea_orm(string_value = "User")]
User,
#[sea_orm(string_value = "Writter")]
Writter,
#[sea_orm(string_value = "Writer")]
Writer,
}
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod m20240307_142155_change_comment_report_solved_field_to_be_solved_by;
mod m20240316_042435_drop_team_role_team_user_relation_table;
mod m20240316_042712_alter_team_user_table;
mod m20240604_054455_make_article_slug_a_unique_key;
mod m20240719_034959_create_article_tag_table_and_add_tag_to_article;

pub struct Migrator;

Expand Down Expand Up @@ -47,6 +48,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240316_042435_drop_team_role_team_user_relation_table::Migration),
Box::new(m20240316_042712_alter_team_user_table::Migration),
Box::new(m20240604_054455_make_article_slug_a_unique_key::Migration),
Box::new(m20240719_034959_create_article_tag_table_and_add_tag_to_article::Migration),
]
}
}
4 changes: 2 additions & 2 deletions migration/src/m20240114_032712_add_roles_to_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub enum Role {
Table,
#[iden = "User"]
User,
#[iden = "Writter"]
Writter,
#[iden = "Writer"]
Writer,
#[iden = "Editor"]
Editor,
#[iden = "Coord"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ArticleTag::Table)
.if_not_exists()
.col(
ColumnDef::new(ArticleTag::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(ArticleTag::Value).string().not_null().unique_key())
.to_owned(),
).await?;

manager.alter_table(
Table::alter()
.table(Article::Table)
.add_column(
ColumnDef::new(Article::TagId)
.integer()
)
.add_column(
ColumnDef::new(Article::TagValue).string()
)
.add_foreign_key(TableForeignKey::new().name("fk-article-article-tag")
.from_tbl(Article::Table)
.from_col(Article::TagId)
.to_tbl(ArticleTag::Table)
.to_col(ArticleTag::Id)
.on_delete(ForeignKeyAction::SetNull.to_owned()
))
.to_owned()
).await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.drop_foreign_key(
ForeignKeyDropStatement::new()
.table(Article::Table)
.name("fk-article-article-tag")
.to_owned()
).await?;

manager
.drop_table(Table::drop().table(ArticleTag::Table).to_owned())
.await?;

manager.alter_table(
Table::alter()
.table(Article::Table)
.drop_column(Article::TagValue)
.drop_column(Article::TagId)
.to_owned()
).await?;

Ok(())
}
}

#[derive(DeriveIden)]
enum ArticleTag {
Table,
Id,
Value,
}

#[derive(DeriveIden)]
enum Article {
Table,
TagId,
TagValue
}
24 changes: 23 additions & 1 deletion src/domain/domain_entities/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct Article {
title: String,
content: String,
approved: bool,
tag_id: Option<i32>,
tag_value: Option<String>,
created_at: DateTime,
updated_at: Option<DateTime>,
slug: Slug,
Expand All @@ -25,7 +27,9 @@ impl Article {
author_id: Uuid,
title: String,
content: String,
cover_url: String
cover_url: String,
tag_id: i32,
tag_value: String
) -> Self {
let id = Uuid::new_v4();

Expand All @@ -40,6 +44,8 @@ impl Article {
cover_url,
title,
content,
tag_id: Some(tag_id),
tag_value: Some(tag_value),
approved: false,
created_at,
updated_at,
Expand All @@ -56,6 +62,8 @@ impl Article {
approved: bool,
created_at: DateTime,
updated_at: Option<DateTime>,
tag_id: Option<i32>,
tag_value: Option<String>,
slug: Slug,
) -> Self {
Article {
Expand All @@ -65,6 +73,8 @@ impl Article {
title,
content,
approved,
tag_value,
tag_id,
created_at,
updated_at,
slug
Expand Down Expand Up @@ -115,6 +125,14 @@ impl Article {
self.slug.clone()
}

pub fn tag_id(&self) -> Option<i32> {
self.tag_id
}

pub fn tag_value(&self) -> Option<String> {
self.tag_value.clone()
}

// SETTERS

pub fn set_author_id(&mut self, author_id: Uuid) {
Expand Down Expand Up @@ -142,4 +160,8 @@ impl Article {
pub fn set_approved(&mut self, approved: bool) {
self.approved = approved;
}

pub fn set_tag_id(&mut self, tag_id: i32) { self.tag_id = Some(tag_id) }

pub fn set_tag_value(&mut self, tag_value: String) { self.tag_value = Some(tag_value) }
}
42 changes: 42 additions & 0 deletions src/domain/domain_entities/article_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pub struct DraftArticleTag {
value: String
}

impl DraftArticleTag {
pub fn new(value: String) -> Self {
Self {
value
}
}

pub fn value(&self) -> &String {
&self.value
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ArticleTag {
id: i32,
value: String
}

impl ArticleTag {
pub fn new_from_existing(id: i32, value: String) -> Self {
Self {
id,
value,
}
}

pub fn id(&self) -> i32 {
self.id
}

pub fn value(&self) -> &String {
&self.value
}

pub fn set_value(&mut self, value: String) -> () {
self.value = value;
}
}
3 changes: 2 additions & 1 deletion src/domain/domain_entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub mod comment;
pub mod comment_report;
pub mod comment_with_author;
pub mod team_user;
pub mod team_role;
pub mod team_role;
pub mod article_tag;
10 changes: 5 additions & 5 deletions src/domain/domain_entities/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ pub enum Role {
Editor,
Principal,
User,
Writter,
Writer,
}

impl FromStr for Role {
type Err = EnumCoercionError;

fn from_str(s: &str) -> Result<Self, EnumCoercionError> {
let s = s.to_uppercase();
let s = s.as_str();

match s {
"ADMIN" => Ok(Self::Admin),
"CEO" => Ok(Self::Ceo),
"COORD" => Ok(Self::Coord),
"EDITOR" => Ok(Self::Editor),
"PRINCIPAL" => Ok(Self::Principal),
"USER" => Ok(Self::User),
"WRITTER" => Ok(Self::Writter),
"WRITER" => Ok(Self::Writer),
_ => Err(EnumCoercionError::new("Role"))
}
}

type Err = EnumCoercionError;
}
5 changes: 4 additions & 1 deletion src/domain/factories/create_article_service_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use actix_web::HttpResponse;
use either::Either::{self, *};
use crate::errors::internal_error::InternalError;
use crate::infra::sea::repositories::sea_article_repository::SeaArticleRepository;
use crate::infra::sea::repositories::sea_article_tag_repository::SeaArticleTagRepository;
use crate::infra::sea::repositories::sea_user_repository::SeaUserRepository;
use crate::infra::sea::sea_service::SeaService;

pub async fn exec() -> Either<CreateArticleService<SeaArticleRepository, SeaUserRepository>, HttpResponse> {
pub async fn exec() -> Either<CreateArticleService<SeaArticleRepository, SeaArticleTagRepository, SeaUserRepository>, HttpResponse> {
let sea_service = SeaService::new().await;

if sea_service.is_err() {
Expand All @@ -16,10 +17,12 @@ pub async fn exec() -> Either<CreateArticleService<SeaArticleRepository, SeaUser
let sea_service = sea_service.unwrap();

let sea_article_repository: Box<SeaArticleRepository> = Box::new(SeaArticleRepository::new(sea_service.clone()).await);
let sea_article_tag_repository: Box<SeaArticleTagRepository> = Box::new(SeaArticleTagRepository::new(sea_service.clone()).await);
let sea_user_repository: Box<SeaUserRepository> = Box::new(SeaUserRepository::new(sea_service).await);

let create_article_service = CreateArticleService::new(
sea_article_repository,
sea_article_tag_repository,
sea_user_repository
);

Expand Down
Loading

0 comments on commit 09406c1

Please sign in to comment.