-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add subscriptions to database and the related flows
- Loading branch information
1 parent
c60e853
commit eafa78c
Showing
32 changed files
with
439 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
news/migrations/2023-09-23-202817_create_subscriptions/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE subscriptions; |
7 changes: 7 additions & 0 deletions
7
news/migrations/2023-09-23-202817_create_subscriptions/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE subscriptions ( | ||
feed_id UUID NOT NULL, | ||
user_id UUID NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
FOREIGN KEY (feed_id) REFERENCES feeds (id), | ||
PRIMARY KEY (feed_id, user_id) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod feeds; | ||
pub mod news; | ||
pub mod subscriptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use actix_web::{delete, get, web, HttpRequest, HttpResponse}; | ||
use actix_web::{post, HttpMessage}; | ||
use log::error; | ||
use serde::{Deserialize, Serialize}; | ||
use std::str::FromStr; | ||
use utils::{error::CommonError, http::middlewares::jwt_auth::JwtMiddleware}; | ||
use uuid::Uuid; | ||
use validator::Validate; | ||
|
||
use crate::models::subscription::Subscription; | ||
use crate::repositories::subscription_repository::SubscriptionRepository; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Validate)] | ||
pub struct CreateSubscriptionPayload { | ||
#[validate(required)] | ||
pub feed_id: Option<String>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct DeleteSubscriptionPayload { | ||
feed_id: Option<String>, | ||
} | ||
|
||
#[get("/subscriptions")] | ||
async fn get_subscriptions( | ||
r: HttpRequest, | ||
subscription_repo: web::Data<dyn SubscriptionRepository>, | ||
_: JwtMiddleware, | ||
) -> HttpResponse { | ||
let user_id = *r.extensions().get::<uuid::Uuid>().unwrap(); | ||
|
||
let result = subscription_repo.list_by_user(user_id); | ||
|
||
match result { | ||
Err(err) => { | ||
error!("failed getting subscriptions: {}", CommonError::from(err)); | ||
HttpResponse::InternalServerError().finish() | ||
} | ||
Ok(feeds) => HttpResponse::Ok().json(feeds), | ||
} | ||
} | ||
|
||
#[post("/subscriptions")] | ||
async fn create_subscription( | ||
r: HttpRequest, | ||
subscription_repo: web::Data<dyn SubscriptionRepository>, | ||
payload: Option<web::Json<CreateSubscriptionPayload>>, | ||
_: JwtMiddleware, | ||
) -> HttpResponse { | ||
let user_id = *r.extensions().get::<uuid::Uuid>().unwrap(); | ||
|
||
if payload.is_none() { | ||
return HttpResponse::BadRequest().body("empty body"); | ||
} | ||
|
||
let payload = payload.unwrap(); | ||
if let Err(err) = payload.validate() { | ||
return HttpResponse::BadRequest().json(err); | ||
} | ||
|
||
let CreateSubscriptionPayload { feed_id } = payload.into_inner(); | ||
|
||
if feed_id.is_none() { | ||
return HttpResponse::BadRequest().body("Missing 'feed_id' in body"); | ||
} | ||
|
||
let feed_id = Uuid::from_str(&feed_id.unwrap().to_string()).unwrap(); | ||
|
||
let result = subscription_repo.create(&Subscription { feed_id, user_id }); | ||
|
||
match result { | ||
Err(err) => { | ||
error!("failed creating subscription: {}", CommonError::from(err)); | ||
HttpResponse::InternalServerError().finish() | ||
} | ||
Ok(subscription) => HttpResponse::Ok().json(subscription), | ||
} | ||
} | ||
|
||
#[delete("/subscriptions")] | ||
async fn delete_subscription( | ||
r: HttpRequest, | ||
subscription_repo: web::Data<dyn SubscriptionRepository>, | ||
_: JwtMiddleware, | ||
payload: web::Query<DeleteSubscriptionPayload>, | ||
) -> HttpResponse { | ||
let user_id = *r.extensions().get::<uuid::Uuid>().unwrap(); | ||
|
||
if let Some(feed_id) = &payload.feed_id { | ||
let feed_id = Uuid::from_str(&feed_id.clone()).unwrap(); | ||
|
||
let result = subscription_repo.delete(feed_id, user_id); | ||
|
||
match result { | ||
Err(err) => { | ||
error!("failed deleting subscription: {}", CommonError::from(err)); | ||
HttpResponse::InternalServerError().finish() | ||
} | ||
Ok(subscription) => HttpResponse::Ok().json(subscription), | ||
} | ||
} else { | ||
HttpResponse::BadRequest().body("Missing 'feed_id' query parameter") | ||
} | ||
} |
Oops, something went wrong.