Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
…power into dev
  • Loading branch information
TumiPare committed Aug 1, 2023
2 parents 8792ac1 + d1311d6 commit b1d10ad
Show file tree
Hide file tree
Showing 25 changed files with 700 additions and 447 deletions.
2 changes: 1 addition & 1 deletion .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ref: https://docs.codecov.com/docs/codecovyml-reference
coverage:
# Hold ourselves to a high bar
range: 50..100
range: 30..100
round: down
precision: 1
status:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

env:
CARGO_TERM_COLOR: always
MAPBOX_API_KEY: ${{ secrets.MAPBOX_API_KEY }}

jobs:
build:
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ to help assist users navigate their routes through these dark times.
- [User Manual](https://github.com/COS301-SE-2023/Where-is-the-power/files/11878231/User_Manual.pdf)
- [System Requirements Specifications](https://github.com/COS301-SE-2023/Where-is-the-power/wiki/Software-Requirements-Specification-(SRS))
- [Architectural Requirements Document](https://github.com/COS301-SE-2023/Where-is-the-power/wiki/Architectural-Requirements-Document)

- ## Demo 3
[**All documentation can be found on the Wiki!**](https://github.com/COS301-SE-2023/Where-is-the-power/wiki)

***However PDFs are listed below:***
- [Project Management](https://github.com/orgs/COS301-SE-2023/projects/7)
- [User Manual](https://github.com/COS301-SE-2023/Where-is-the-power/wiki/User_Manual.pdf)
- [Technical Installation Manual](https://github.com/COS301-SE-2023/Where-is-the-power/wiki/Technical%20Installation%20Manual.pdf)
- [Coding Standards/Quality](https://github.com/COS301-SE-2023/Where-is-the-power/wiki/Coding%20Standards.pdf)
- [SRS PDF](https://github.com/COS301-SE-2023/Where-is-the-power/wiki/Software-Requirements-Specification-(SRS).pdf)
- [Architectural Requirements Document](https://github.com/COS301-SE-2023/Where-is-the-power/wiki/Architectural-Requirements-Document.pdf)


# About the repo ℹ️
Expand Down
3 changes: 0 additions & 3 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@ mockall = "0.11.4"
mockall_double = "0.3.0"
rand = "0.8.5"
hex = "0.4.3"

[build-dependencies]
copy_dir = "0.1.2"
46 changes: 40 additions & 6 deletions api/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,48 @@ pub fn insertable(input: TokenStream) -> TokenStream {
}
};

let find = quote! {
async fn query(filter: bson::document::Document, db: &mongodb::Database) -> std::result::Result<mongodb::Cursor<Self::Output>, mongodb::error::Error> {
let query = quote! {
async fn query(
filter: bson::document::Document,
db: &mongodb::Database
) -> std::result::Result<mongodb::Cursor<Self>, mongodb::error::Error> {
db.collection::<#ident>(#collection_name).find(filter, None).await
}
};

let find = quote! {
async fn find(
filter: bson::document::Document,
db: &mongodb::Database,
options: std::option::Option<mongodb::options::FindOptions>
) -> std::result::Result<std::vec::Vec<Box<Self>>, mongodb::error::Error> {
let mut cursor = db.collection::<#ident>(#collection_name).find(filter, options).await?;
let mut result = Vec::new();
while cursor.advance().await? {
result.push(std::boxed::Box::new(cursor.deserialize_current()?));
}
Ok(result)
}
};

let find_one = quote! {
async fn find_one(
filter: bson::document::Document,
db: &mongodb::Database,
options: std::option::Option<mongodb::options::FindOptions>
) -> std::option::Option<std::boxed::Box<Self>> {
let mut cursor = db.collection::<#ident>(#collection_name).find(filter, options).await.ok()?;
cursor.advance().await.ok()?;
cursor.deserialize_current().ok().map(|x| std::boxed::Box::new(x))
}
};

let update = quote! {
async fn update(&mut self, update: mongodb::options::UpdateModifications, db: &mongodb::Database) -> std::result::Result<mongodb::results::UpdateResult, mongodb::error::Error> {
async fn update(
&mut self,
update: mongodb::options::UpdateModifications,
db: &mongodb::Database
) -> std::result::Result<mongodb::results::UpdateResult, mongodb::error::Error> {
let doc = mongodb::bson::doc! {
"_id": self.#id_field_ident.unwrap()
};
Expand All @@ -116,12 +150,12 @@ pub fn insertable(input: TokenStream) -> TokenStream {
quote! {
#[async_trait::async_trait]
impl Entity for #ident {
type Output = Self;

#insert
#delete
#find
#query
#update
#find
#find_one
}
}
.into()
Expand Down
3 changes: 2 additions & 1 deletion api/scripts/mapdatatest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
def sendRequest():
body = {
"bottomLeft": [-90,-180],
"topRight": [90,180]
"topRight": [90,180],
"time" : 1690834649
}
request = json.dumps(body)
headers = {
Expand Down
13 changes: 7 additions & 6 deletions api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use crate::loadshedding::MapDataDefaultResponse;
use rocket::{http::ContentType, response::Responder};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Debug, Serialize, ToSchema, Clone)]
#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
#[non_exhaustive]
pub enum ApiError<'a> {
AuthError(&'a str),
Expand All @@ -21,7 +21,7 @@ pub enum ApiResponse<'a, O: Serialize> {
Err(ApiError<'a>),
}

#[derive(Serialize, ToSchema)]
#[derive(Serialize, Deserialize, ToSchema)]
#[schema(example = json! {
UnifiedResponse::<()> {
success: false,
Expand All @@ -34,9 +34,10 @@ pub enum ApiResponse<'a, O: Serialize> {
ResponseMapData = UnifiedResponse<'a, MapDataDefaultResponse>
)]
pub struct UnifiedResponse<'b, O: Serialize> {
success: bool,
result: Option<O>,
error: Option<ApiError<'b>>,
pub success: bool,
pub result: Option<O>,
#[serde(borrow)]
pub error: Option<ApiError<'b>>,
}

impl<'r, 'a, O: Serialize> Responder<'r, 'static> for ApiResponse<'a, O> {
Expand Down
22 changes: 17 additions & 5 deletions api/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
use async_trait::async_trait;
use bson::Document;
use mongodb::{
options::UpdateModifications,
options::{FindOptions, UpdateModifications},
results::{DeleteResult, InsertOneResult, UpdateResult},
Cursor, Database,
};

#[async_trait]
pub trait Entity {
type Output;

async fn insert(&self, db: &Database) -> Result<InsertOneResult, mongodb::error::Error>;
async fn delete(self, db: &Database) -> Result<DeleteResult, mongodb::error::Error>;
async fn query(

async fn find(
filter: Document,
db: &Database,
) -> Result<Cursor<Self::Output>, mongodb::error::Error>;
options: Option<FindOptions>,
) -> Result<Vec<Box<Self>>, mongodb::error::Error>;

async fn find_one(
filter: Document,
db: &Database,
options: Option<FindOptions>,
) -> Option<Box<Self>>;

#[deprecated(note = "Use `Entity::find` instead.")]
async fn query(filter: Document, db: &Database) -> Result<Cursor<Self>, mongodb::error::Error>
where
Self: Sized;

async fn update(
&mut self,
update: UpdateModifications,
Expand Down
Loading

0 comments on commit b1d10ad

Please sign in to comment.