Skip to content

Commit

Permalink
adds faculty and year of study as optional fields for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrltrent committed Feb 15, 2023
1 parent 2a3691e commit 29fbfd1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/services/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::auth::AuthenticatedUser;
use crate::conf;
use crate::masked_oid::{self, MaskedObjectId, MaskedSequentialId, MaskingKey};
use crate::to_unexpected;
use crate::types::{Post, PostGenre, School, Vote};
use crate::types::{Post, PostGenre, PosterFaculty, PosterYearOfStudy, School, Vote};

#[derive(Serialize, Deserialize)]
pub struct ReplyContext {
Expand All @@ -35,6 +35,8 @@ pub struct Detail {
pub reply_context: Option<MaskedObjectId>,
pub school_id: String,
pub genre: PostGenre,
pub year_of_study: Option<PosterYearOfStudy>,
pub faculty: Option<PosterFaculty>,
pub body_text: String,
pub header_text: String,
pub created_at: String,
Expand All @@ -56,6 +58,8 @@ pub struct CreateRequest {
pub header_text: String,
pub body_text: String,
pub genre: PostGenre,
pub year_of_study: Option<PosterYearOfStudy>,
pub faculty: Option<PosterFaculty>,
pub associated_with_user: bool,
}

Expand Down Expand Up @@ -162,6 +166,8 @@ pub async fn daily_hottest(
.reply_context
.map(|object_id| masking_key.mask(&object_id)),
genre: post.genre,
year_of_study: post.year_of_study,
faculty: post.faculty,
body_text: post.body_text,
header_text: post.header_text,
created_at: post
Expand Down Expand Up @@ -233,6 +239,8 @@ pub async fn list(
.reply_context
.map(|object_id| masking_key.mask(&object_id)),
genre: post.genre,
year_of_study: post.year_of_study,
faculty: post.faculty,
body_text: post.body_text,
header_text: post.header_text,
created_at: post
Expand Down Expand Up @@ -289,25 +297,26 @@ pub async fn create(
.ok_or(Failure::BadRequest("invalid school id"))?;

let mut insert_doc: Document;
match to_bson(&request.genre) {
Ok(genre) => {
insert_doc = doc! {
"owner": &user.id,
"reply_context": &reply_context_id,
"header_text": &request.header_text,
"body_text": &request.body_text,
"school_id": &request.school_id,
"genre": genre,
"votes_up": 0,
"votes_down": 0,
"absolute_score": 0,
"trending_score": get_trending_score_time(&DateTime::now()), // approximate, but will match `_id` exactly with the next vote
"created_at": DateTime::now(),
"associated_with_user": request.associated_with_user,
};
}
Err(_) => return Err(Failure::Unexpected),
}
// Convert enums to [Bson] in order to store.
let genre = to_bson(&request.genre).map_err(|_| Failure::Unexpected)?;
let faculty = to_bson(&request.faculty).map_err(|_| Failure::Unexpected)?;
let year_of_study = to_bson(&request.year_of_study).map_err(|_| Failure::Unexpected)?;
insert_doc = doc! {
"owner": &user.id,
"reply_context": &reply_context_id,
"header_text": &request.header_text,
"body_text": &request.body_text,
"school_id": &request.school_id,
"genre": genre,
"year_of_study": year_of_study,
"faculty": faculty,
"votes_up": 0,
"votes_down": 0,
"absolute_score": 0,
"trending_score": get_trending_score_time(&DateTime::now()), // approximate, but will match `_id` exactly with the next vote
"created_at": DateTime::now(),
"associated_with_user": request.associated_with_user,
};

let mut attempt = 0;
let insertion = loop {
Expand Down
34 changes: 34 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ pub struct Post {
pub body_text: String,
// Genre of the post.
pub genre: PostGenre,
// Year of study of the poster.
pub year_of_study: Option<PosterYearOfStudy>,
// Fcaulty of the poster.
pub faculty: Option<PosterFaculty>,
pub votes_up: i32,
pub votes_down: i32,
pub absolute_score: i32,
Expand Down Expand Up @@ -117,6 +121,36 @@ pub enum PostGenre {
HotTakes,
}

/// The various years of study the creator of a post can be.
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub enum PosterYearOfStudy {
One,
Two,
Three,
Four,
Five,
Graduate,
PhD,
Alumni,
}

/// The various faculties the creator of a post can be associated with.
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub enum PosterFaculty {
Business,
Medicine,
SocialScience,
History,
Engineering,
ComputerScience,
Psychology,
Communication,
Arts,
Education,
}

mod token {
use super::*;

Expand Down

0 comments on commit 29fbfd1

Please sign in to comment.