Skip to content

Commit

Permalink
added adding recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ai-Kiwi committed Sep 26, 2024
1 parent 64b9d3a commit a5d477f
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .server/database.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ CREATE TABLE posts (
title TEXT,
description TEXT,
image_count INT NOT NULL,
post_date BIGINT NOT NULL
post_date BIGINT NOT NULL,
recipe TEXT
-- rating REAL NOT NULL DEFAULT 0,
-- rating_count INT NOT NULL DEFAULT 0
);
Expand Down
1 change: 1 addition & 0 deletions .server/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ pub async fn get_page_change_log() -> Html<&'static str> {
- updated dependencies for app <br>
- added support link on login screen <br>
- changed post popup to say for post not message, fixed copy id being user id instead of post id <br>
- added feature to add recipes for posts <br>
<br>
<b> 2.1.0 </b> <br>
Expand Down
21 changes: 20 additions & 1 deletion .server/src/user_posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct Posts {
pub description: String,
pub image_count: i32,
pub post_date: i64,
pub recipe: Option<String>
//pub rating: f32,
//pub rating_count: i32,
}
Expand All @@ -103,6 +104,13 @@ pub async fn get_post_data(pagination: Query<GetPostPaginator>, State(app_state)
data_returning.insert("title".to_string(), Value::String(post_data.title));
data_returning.insert("description".to_string(), Value::String(post_data.description));
data_returning.insert("postDate".to_string(), Value::Number(post_data.post_date.into()));
match post_data.recipe {
//pretty sure something else is meant to be used instead of match here lol
Some(recipe) => {data_returning.insert("recipe".to_string(), Value::String(recipe)); ()},
None => (),
};



let row: (i64,) = match sqlx::query_as(
"SELECT COUNT(*) FROM post_ratings WHERE parent_post_id = $1"
Expand Down Expand Up @@ -388,6 +396,7 @@ pub struct PostUpload {
title : String,
description : String,
images : Vec<String>,
recipe : Option<String>,
//share_mode : String
}

Expand All @@ -398,6 +407,7 @@ pub async fn post_create_upload(State(app_state): State<AppState<'_>>, headers:
let post_description: &String = &body.description;
let images: &Vec<String> = &body.images;
let image_count: usize = images.len();
let recipe: &Option<String> = &body.recipe;

let token = test_token_header(&headers, &app_state).await;
let user_id = match token {
Expand Down Expand Up @@ -429,6 +439,14 @@ pub async fn post_create_upload(State(app_state): State<AppState<'_>>, headers:
return (StatusCode::BAD_REQUEST, "description is to short".to_string());
}

match recipe {
Some(recipe) => if recipe.len() > 10000 {
return (StatusCode::BAD_REQUEST, "recipe size to large".to_string());

}
None => (),
}

if image_count > 8 {
return (StatusCode::BAD_REQUEST, "To many images".to_string());
}
Expand Down Expand Up @@ -470,13 +488,14 @@ pub async fn post_create_upload(State(app_state): State<AppState<'_>>, headers:

//add post to database
let result = sqlx::query(
"INSERT INTO posts (post_id, poster_user_id, title, description, image_count, post_date) VALUES ($1, $2, $3, $4, $5, $6)")
"INSERT INTO posts (post_id, poster_user_id, title, description, image_count, post_date, recipe) VALUES ($1, $2, $3, $4, $5, $6, $7)")
.bind(&post_id)
.bind(&user_id)
.bind(post_title)
.bind(post_description)
.bind(image_count as i32)
.bind(time_now_ms as i64)
.bind(recipe)
.execute(database_pool).await;

let mut image_upto = 0;
Expand Down
92 changes: 91 additions & 1 deletion lib/createPost/createPost.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:PlateRates/libs/alertSystem.dart';
import 'package:PlateRates/libs/imageUtils.dart';
import 'package:PlateRates/libs/loadScreen.dart';
import 'package:PlateRates/libs/usefullWidgets.dart';
import 'package:PlateRates/posts/userPostRecipe.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -37,6 +38,7 @@ class _CreatePostState extends State<CreatePostPage> {
final PageController _pageController = PageController();
int currentPage = 0;
bool uploadingPost = false;
String? recipe;

_CreatePostState({required this.imagesData});

Expand Down Expand Up @@ -223,8 +225,95 @@ class _CreatePostState extends State<CreatePostPage> {
// ),
//),
const SizedBox(height: 16.0),
//recipe
Visibility(
visible: recipe == null,
child: Padding(
//take photo button
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Container(
width: double.infinity,
height: 50.0,
child: ElevatedButton(
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
)),
onPressed: () {
setState(() {
recipe = r'[{"insert":"Recipes input will go here\n"}]';
});
},
child: const Text(
'Attach recipe',
style: TextStyle(fontSize: 18.0),
),
),
),
),
),
Visibility(
visible: recipe != null,
child: Padding(
//take photo button
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
Expanded(
child: SizedBox(
height: 50,
child: ElevatedButton(
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
)),
onPressed: () async {
final updated_recipe = await Navigator.push(
context,
MaterialPageRoute(
maintainState: true,
builder: (context) => RecipeEditor(
recipeData: recipe!,
)),
);
setState(() {
recipe = updated_recipe;
});
},
child: const Text(
'Edit recipe',
style: TextStyle(fontSize: 18.0),
),
),
),
),
const SizedBox(width: 8),
SizedBox(
width: 50.0,
height: 50.0,
child: ElevatedButton(
style: OutlinedButton.styleFrom(
backgroundColor: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
)),
onPressed: () async {
setState(() {
recipe = null;
});
},
child: const Center(
child: Icon(
Icons.delete,
color: Colors.white,
))),
),
],
)),
),
const SizedBox(height: 16.0),
Padding(
//take photo button
//upload post button
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Container(
width: double.infinity,
Expand Down Expand Up @@ -270,6 +359,7 @@ class _CreatePostState extends State<CreatePostPage> {
"description": _description,
"share_mode": postCodeNames[shareModeSelected],
"images": imagesUploading,
"recipe": recipe,
//"image": base64Encode(
// imageUtils.uintListToBytes(imagesData)),
}),
Expand Down
39 changes: 38 additions & 1 deletion lib/posts/userPost.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:PlateRates/libs/userAvatar.dart';
import 'package:PlateRates/notifications/appNotificationHandler.dart';
import 'package:PlateRates/posts/fullPagePost.dart';
import 'package:PlateRates/login/userLogin.dart';
import 'package:PlateRates/posts/userPostRecipe.dart';
import 'package:PlateRates/userProfile/userProfile.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -65,6 +66,7 @@ class _PostItemState extends State<PostItem> {
int? postDate;
int ratingsAmount = 0;
bool? hasRated = true;
String? recipe;
var posterAvatar;
String posterUserId = "";
var imagesData = {};
Expand Down Expand Up @@ -101,6 +103,7 @@ class _PostItemState extends State<PostItem> {
setState(() {
imagesData[0] = base64Decode(firstImageData['imageData']);
title = jsonData["title"];
recipe = jsonData["recipe"];
description = jsonData["description"];
rating = double.parse('${jsonData["rating"]}');
//imageData = base64Decode(jsonData['imageData']);
Expand Down Expand Up @@ -362,7 +365,41 @@ class _PostItemState extends State<PostItem> {
),
),
),
const SizedBox(height: 8.0),
const SizedBox(height: 4.0),
Visibility(
visible: recipe != null,
child: Padding(
//login button
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
SizedBox(
width: 100,
height: 20.0,
child: ElevatedButton(
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
)),
onPressed: () async {
await Navigator.push(
context,
MaterialPageRoute(
maintainState: true,
builder: (context) => RecipeViewer(
recipeData: recipe!,
)),
);
},
child: const Text(
'Open recipe',
style: TextStyle(fontSize: 12.0),
),
)),
],
)),
),
const SizedBox(height: 4.0),
Padding(
//description
padding: const EdgeInsets.symmetric(horizontal: 16.0),
Expand Down
Loading

0 comments on commit a5d477f

Please sign in to comment.