Skip to content

Commit

Permalink
search order and from date
Browse files Browse the repository at this point in the history
  • Loading branch information
Dromader2137 committed May 23, 2024
1 parent 52fb1ef commit c88e713
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,7 @@
- Get: 200 (PostList) sorted by likes ascending
#### /api/get/posts/trending/{limit}/{offset}/{from_date}
- Get: 200 (PostList) sorted by (likes / age in minutes)
```
Post {
post_id: i64
user_id: i64
date: i64
body: string (max 2048 chars)
likes: i64
}
```
```
PostList {
post_list: Vec<Post>
}
```
#### /api/get/posts/from-search/{search-phrase}/{limit}/{offset}
#### /api/get/posts/from-search/{search-phrase}/{limit}/{offset}/{from_date}
- Get: 200 (PostList)
```
Post {
Expand Down Expand Up @@ -129,6 +115,7 @@ PostCreateRequest {
tags: Vec<string (max 64 chars)>
}
```
- With cookies
- Effect: Adds a post to the db
- Return: 201 ({post_id:i64}) / 401 ("Wrong token" / "User is banned") / 404 ("User not found")
- Headers: 'Content-Type: application/json' 'Content-Type: text/plain'
Expand All @@ -140,6 +127,7 @@ CommentCreateRequest {
body: string (max 2048 chars)
}
```
- With cookies
- Effect: Adds a comment to the post
- Return: 201 ({comment_id:i64}) / 401 ("Wrong token" / "User is banned") / 404 ("User not found" / "Post not found")
- Headers: 'Content-Type: application/json' 'Content-Type: text/plain'
Expand All @@ -148,9 +136,9 @@ CommentCreateRequest {
```
LikeRequest {
post_id: i64
token: string
}
```
- With cookies
- Effects: Adds like to a post
- Return: 200 ("Like added") / 406 ("Like already exists")
- Headers: 'Content-Type: application/json' 'Content-Type: text/plain'
Expand Down Expand Up @@ -182,9 +170,10 @@ SignupRequest {
- Post:
```
UserDeleteRequest {
token: string
user_id: i64
}
```
- With cookies
- Effect: Deletes a user
- Return: 200 ("User deleted") / 401 ("Wrong token") / 404 ("User not found")
- Headers: 'Content-Type: application/json' 'Content-Type: text/plain'
Expand All @@ -193,9 +182,9 @@ UserDeleteRequest {
```
UserUpgradeRequest {
user_id: i64
token: string
}
```
- With cookies
- Effect: User with given id becomes an admin
- Note: Token must belong to an admin
- Return: 200 ("Upgrade succesful") / 401 ("User is not admin" / "Wrong token") / 404 ("User not found")
Expand All @@ -207,9 +196,9 @@ UserBanRequest {
user_id: i64
ban_length: i64,
ban_message: string,
token: string
}
```
- With cookies
- Effect: User with given id is banned
- Note: Token must belong to an admin
- Return: 200 ("Ban succesful") / 401 ("User is not admin" / "Wrong token") / 404 ("User not found")
Expand All @@ -219,9 +208,9 @@ UserBanRequest {
```
UserUnbanRequest {
user_id: i64,
token: string
}
```
- With cookies
- Effect: User with given id is unbanned
- Note: Token must belong to an admin
- Return: 200 ("Unban succesful") / 401 ("User is not admin" / "Wrong token") / 404 ("User not found")
Expand All @@ -231,9 +220,20 @@ UserUnbanRequest {
```
DisplayNameChangeRequest {
new_display_name: string (max 64 chars)
token: string
}
```
- With cookies
- Effect: User's display name changes
- Return: 200 ("Change succesful") / 401 ("Wrong token") / 404 ("User not found")
- Headers: 'Content-Type: application/json' 'Content-Type: text/plain'
#### /api/post/change/user-name
- Post:
```
UserNameChangeRequest {
new_user_name: string (max 64 chars)
}
```
- With cookies
- Effect: User's display name changes
- Return: 200 ("Change succesful") / 401 ("Wrong token") / 404 ("User not found")
- Headers: 'Content-Type: application/json' 'Content-Type: text/plain'
Expand All @@ -242,26 +242,26 @@ DisplayNameChangeRequest {
```
DescriptionChangeRequest {
new_description: string (max 64 chars)
token: string
}
```
- With cookies
- Effect: User's description changes
- Return: 200 ("Change succesful") / 401 ("Wrong token") / 404 ("User not found")
- Headers: 'Content-Type: application/json' 'Content-Type: text/plain'
#### /api/post/upload/image
- Post: Image (max 25MB)
- With cookies
- Return: 200 (image-id) / 400 ("Invalid image format" / "File type error" / ) / 401 ("Wrong token") / 500 ("File read error")
- Headers: 'Content-Type: multipart/form-data', 'auth: {user_token}'
#### /api/post/add-image-to-post
- Post:
```
AddImageToPostRequest {
token: string,
image_id: i64,
post_id: i64
}
```
- With cookies
- Effect: Image is added to post
- Return: 200 ("Image added to post") / 400 ("Image already added to this post") / 401 ("Wrong token" / "User not authorized") / 404 ("Image not found" / "Post not found")
- Headers: 'Content-Type: application/json' 'Content-Type: text/plain'
### Types
8 changes: 5 additions & 3 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub async fn get_posts_by_tag(tag: String, limit: i64, offset: i64) -> Result<im
))
}

pub async fn get_posts_from_search(phrase: String, limit: i64, offset: i64) -> Result<impl warp::Reply, warp::Rejection> {
pub async fn get_posts_from_search(phrase: String, limit: i64, offset: i64, date_from: i64) -> Result<impl warp::Reply, warp::Rejection> {
let connection = tokio_rusqlite::Connection::open("projekt-db")
.await
.unwrap();
Expand All @@ -148,14 +148,16 @@ pub async fn get_posts_from_search(phrase: String, limit: i64, offset: i64) -> R
WHERE posts.body LIKE ?
AND posts.user_id NOT IN
(SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
ORDER BY posts.likes DESC
LIMIT ? OFFSET ?
",
timestamp
);
let post_list = connection
.call(move |conn| {
let mut statement = conn.prepare(&query).unwrap();
let mut rows = statement.query(params![phrase_cpy, limit, offset]).unwrap();
let mut rows = statement.query(params![phrase_cpy, date_from, limit, offset]).unwrap();
let mut post_vec: Vec<Post> = Vec::new();
while let Ok(Some(row)) = rows.next() {
post_vec.push(Post {
Expand Down Expand Up @@ -1514,7 +1516,7 @@ pub async fn change_user_name(
.call(move |conn| {
let mut statement = conn.prepare(change_query).unwrap();
statement
.execute(params![request.new_display_name, id])
.execute(params![request.new_user_name, id])
.unwrap();
Ok(0)
})
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and_then(validate_token);

let get_posts_from_search = warp::get()
.and(warp::path!("api" / "get" / "posts" / "from-search" / String / i64 / i64))
.and(warp::path!("api" / "get" / "posts" / "from-search" / String / i64 / i64 / i64))
.and_then(get_posts_from_search);

let get_users_from_search = warp::get()
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub struct DisplayNameChangeRequest {

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct UserNameChangeRequest {
pub new_display_name: String,
pub new_user_name: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down

0 comments on commit c88e713

Please sign in to comment.