Skip to content

Commit fa62cd5

Browse files
committed
Merge branch 'charles/readme-for-ground-station-cli' of github.com:UWOrbital/OBC-firmware into charles/readme-for-ground-station-cli
2 parents 2b534ae + dfcf6fd commit fa62cd5

20 files changed

+913
-9
lines changed
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
from fastapi import APIRouter, Depends
2-
from sqlmodel import Session, select
1+
from fastapi import APIRouter
32

43
from gs.backend.api.v1.mcc.models.responses import MainCommandsResponse
5-
from gs.backend.data.database.engine import get_db_session
6-
from gs.backend.data.tables.main_tables import MainCommand
4+
from gs.backend.data.data_wrappers.mcc_wrappers.main_command_wrapper import get_all_main_commands
75

86
main_commands_router = APIRouter(tags=["MCC", "Main Commands"])
97

108

119
@main_commands_router.get("/")
12-
async def get_main_commands(db_session: Session = Depends(get_db_session)) -> MainCommandsResponse:
10+
async def get_main_commands() -> MainCommandsResponse:
1311
"""
14-
@brief Gets the main commands that are available for the MCC
12+
Gets the main commands that are available for the MCC
13+
14+
:return: list of all commands
1515
"""
16-
main_commands_query = select(MainCommand)
17-
items = list(db_session.exec(main_commands_query).all())
16+
items = get_all_main_commands()
1817
return MainCommandsResponse(data=items)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from datetime import datetime
2+
from decimal import Decimal
3+
from uuid import UUID
4+
5+
from sqlmodel import select
6+
7+
from gs.backend.data.database.engine import get_db_session
8+
from gs.backend.data.enums.aro_requests import ARORequestStatus
9+
from gs.backend.data.tables.transactional_tables import ARORequest
10+
11+
12+
def get_all_requests() -> list[ARORequest]:
13+
"""
14+
Get all the requests from aro
15+
"""
16+
with get_db_session() as session:
17+
requests = list(session.exec(select(ARORequest)).all())
18+
return requests
19+
20+
21+
def add_request(
22+
aro_id: UUID,
23+
long: Decimal,
24+
lat: Decimal,
25+
created_on: datetime,
26+
request_sent_obc: datetime,
27+
taken_date: datetime,
28+
transmission: datetime,
29+
status: ARORequestStatus,
30+
) -> ARORequest:
31+
"""
32+
Add a request
33+
34+
:param long: the longitude represented as a decimal of max 3 decimal places
35+
:param lat: the latitude represented as a decimal of max 3 decimal places
36+
:param created_on: datetime object representing the date this request was made. defaults to now
37+
:param taken_date: datetime object representing the date that this picture was taken on
38+
:param taken_date: datetime object representing the date that this picture was trasmitted
39+
:param status: the status of the request, can only be from the requets in ARORequestStatus
40+
"""
41+
with get_db_session() as session:
42+
request = ARORequest(
43+
aro_id=aro_id,
44+
latitude=lat,
45+
longitude=long,
46+
created_on=created_on,
47+
request_sent_to_obc_on=request_sent_obc,
48+
taken_date=taken_date,
49+
transmission=transmission,
50+
status=status,
51+
)
52+
53+
session.add(request)
54+
session.commit()
55+
session.refresh(request)
56+
return request
57+
58+
59+
def delete_request_by_id(request_id: str) -> list[ARORequest]:
60+
"""
61+
Delete a request based on id
62+
63+
:param request_id: unique identifier of the request
64+
"""
65+
with get_db_session() as session:
66+
request = session.get(ARORequest, request_id)
67+
if request:
68+
session.delete(request)
69+
session.commit()
70+
else:
71+
raise ValueError("Request not found, ID does not exist")
72+
73+
return get_all_requests()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from datetime import datetime
2+
from uuid import UUID
3+
4+
from sqlmodel import select
5+
6+
from gs.backend.data.database.engine import get_db_session
7+
from gs.backend.data.enums.aro_auth_token import AROAuthToken as AROEnums
8+
from gs.backend.data.tables.aro_user_tables import AROUserAuthToken
9+
10+
11+
def get_all_auth_tokens() -> list[AROUserAuthToken]:
12+
"""
13+
Get all the auth tokens
14+
"""
15+
with get_db_session() as session:
16+
auth_tokens = list(session.exec(select(AROUserAuthToken)).all())
17+
return auth_tokens
18+
19+
20+
def add_auth_token(token: str, user_data_id: UUID, expiry: datetime, auth_type: AROEnums) -> AROUserAuthToken:
21+
"""
22+
Add auth token to the db
23+
24+
:param token: the auth token we want to add
25+
:param user_data_id: UUID which identifies which user this auth token belongs to
26+
:param expiry: the date in which this token expires
27+
:param auth_type: the type of auth token this is, can only be from AROAuthToken
28+
"""
29+
with get_db_session() as session:
30+
auth_token = AROUserAuthToken(token=token, user_data_id=user_data_id, expiry=expiry, auth_type=auth_type)
31+
32+
session.add(auth_token)
33+
session.commit()
34+
session.refresh(auth_token)
35+
return auth_token
36+
37+
38+
def delete_auth_token_by_id(token_id: UUID) -> list[AROUserAuthToken]:
39+
"""
40+
Delete the auth token based on the token id
41+
42+
:param token_id: the unique identifier for a particular auth token
43+
"""
44+
45+
with get_db_session() as session:
46+
auth_token = session.get(AROUserAuthToken, token_id)
47+
48+
if auth_token:
49+
session.delete(auth_token)
50+
session.commit()
51+
else:
52+
print("Token does not exist")
53+
54+
return get_all_auth_tokens()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from uuid import UUID
2+
3+
from sqlmodel import select
4+
5+
from gs.backend.data.database.engine import get_db_session
6+
from gs.backend.data.tables.aro_user_tables import AROUsers
7+
8+
9+
# selects all objects of type AROUser from db and returns them in list
10+
def get_all_users() -> list[AROUsers]:
11+
"""
12+
Gets all user
13+
"""
14+
with get_db_session() as session:
15+
users = list(session.exec(select(AROUsers)).all())
16+
return users
17+
18+
19+
# adds user to database of type AROUser then fetches the user from database
20+
# so that the user now has an assigned ID
21+
def add_user(call_sign: str, email: str, f_name: str, l_name: str, phone_number: str) -> AROUsers:
22+
"""
23+
Add a new user to the AROUser table in database
24+
25+
:param call_sign: a 6 character string such as ABCDEF
26+
:param email: unique email which is bound to the user
27+
:param f_name: first name of user
28+
:param l_name: last name of user
29+
:param phone_numer: phone number of user
30+
"""
31+
with get_db_session() as session:
32+
# check if the user already exists with email as it is unique
33+
existing_user = session.exec(select(AROUsers).where(AROUsers.email == email)).first()
34+
35+
if existing_user:
36+
raise ValueError("User already exsits based on email")
37+
38+
user = AROUsers(
39+
call_sign=call_sign, email=email, first_name=f_name, last_name=l_name, phone_number=phone_number
40+
)
41+
42+
session.add(user)
43+
session.commit()
44+
session.refresh(user)
45+
return user
46+
47+
48+
# deletes the user with given id and returns the remaining users
49+
def delete_user_by_id(userid: UUID) -> list[AROUsers]:
50+
"""
51+
Use the user.id to delete a user from table
52+
53+
:param userid: identifier unique to the user
54+
"""
55+
with get_db_session() as session:
56+
user = session.get(AROUsers, userid)
57+
58+
if user:
59+
session.delete(user)
60+
session.commit()
61+
else:
62+
raise ValueError("User ID does not exist")
63+
64+
return get_all_users()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from uuid import UUID
2+
3+
from sqlmodel import select
4+
5+
from gs.backend.data.database.engine import get_db_session
6+
from gs.backend.data.tables.aro_user_tables import AROUserLogin
7+
8+
9+
def get_all_logins() -> list[AROUserLogin]:
10+
"""
11+
Gets all the logins
12+
"""
13+
with get_db_session() as session:
14+
user_logins = list(session.exec(select(AROUserLogin)).all())
15+
return user_logins
16+
17+
18+
def add_login(email: str, pwd: str, hash_algo: str, user_data_id: UUID, email_verification_token: str) -> AROUserLogin:
19+
"""
20+
Add a new user login
21+
22+
:param email: the email which the user used to sign up
23+
:param pwd: the password the user set
24+
:hash_algo: the hashing algorithm used to encrypt the password w the salt
25+
:user_data_id: the unique identifier which binds this login to the user which created it
26+
:email_verification_token: email verification token
27+
"""
28+
with get_db_session() as session:
29+
# check if the user exists already
30+
existing_login = session.exec(select(AROUserLogin).where(AROUserLogin.email == email)).first()
31+
32+
if existing_login:
33+
raise ValueError("User login already exists based on email")
34+
35+
user_login = AROUserLogin(
36+
email=email,
37+
password=pwd,
38+
hashing_algorithm_name=hash_algo,
39+
user_data_id=user_data_id,
40+
email_verification_token=email_verification_token,
41+
)
42+
43+
session.add(user_login)
44+
session.commit()
45+
session.refresh(user_login)
46+
return user_login
47+
48+
49+
def delete_login_by_id(loginid: UUID) -> list[AROUserLogin]:
50+
"""
51+
Use the .id to delete a user from table
52+
53+
:param loginid: unique identifier of the target login
54+
"""
55+
with get_db_session() as session:
56+
user_login = session.get(AROUserLogin, loginid)
57+
if user_login:
58+
session.delete(user_login)
59+
session.commit()
60+
else:
61+
raise ValueError("Login ID does not exist")
62+
63+
return get_all_logins()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import Any
2+
from uuid import UUID
3+
4+
from sqlmodel import select
5+
6+
from gs.backend.data.database.engine import get_db_session
7+
from gs.backend.data.tables.transactional_tables import Commands
8+
9+
10+
def get_all_commands() -> list[Commands]:
11+
"""
12+
Get all data wrapper for Commands
13+
14+
:return: a list of all commands
15+
"""
16+
with get_db_session() as session:
17+
commands = list(session.exec(select(Commands)).all())
18+
return commands
19+
20+
21+
def create_commands(command_data: dict[str, Any]) -> Commands:
22+
"""
23+
Post data wrapper for Commands
24+
25+
:param command_data: the JSON object of the command to be created
26+
:return: the newly created command
27+
"""
28+
with get_db_session() as session:
29+
command = Commands(**command_data)
30+
session.add(command)
31+
session.commit()
32+
session.refresh(command)
33+
return command
34+
35+
36+
def delete_commands_by_id(command_id: UUID) -> Commands:
37+
"""
38+
Delete data wrapper for Commands
39+
40+
:param command_id: UUID of command to be deleted
41+
:return: the deleted command
42+
"""
43+
with get_db_session() as session:
44+
command = session.get(Commands, command_id)
45+
if not command:
46+
raise ValueError("Command not found.")
47+
session.delete(command)
48+
session.commit()
49+
return command
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import Any
2+
from uuid import UUID
3+
4+
from sqlmodel import select
5+
6+
from gs.backend.data.database.engine import get_db_session
7+
from gs.backend.data.tables.transactional_tables import CommsSession
8+
9+
10+
def get_all_comms_sessions() -> list[CommsSession]:
11+
"""
12+
Get all data wrapper for CommsSession
13+
14+
:return: a list of all sessions
15+
"""
16+
with get_db_session() as session:
17+
sessions = list(session.exec(select(CommsSession)).all())
18+
return sessions
19+
20+
21+
def create_comms_session(session_data: dict[str, Any]) -> CommsSession:
22+
"""
23+
Post data wrapper for CommsSession
24+
25+
:param session_data: the JSON object of the comms_session to be created
26+
:return: the newly created comms_session
27+
"""
28+
with get_db_session() as session:
29+
comms_session = CommsSession(**session_data)
30+
session.add(comms_session)
31+
session.commit()
32+
session.refresh(comms_session)
33+
return comms_session
34+
35+
36+
def delete_telemetry_by_id(session_id: UUID) -> CommsSession:
37+
"""
38+
Delete data wrapper for CommsSession
39+
40+
:param session_id: UUID of session to be deleted
41+
:return: the deleted session
42+
"""
43+
with get_db_session() as session:
44+
comms_session = session.get(CommsSession, session_id)
45+
if not comms_session:
46+
raise ValueError("Comms session not found.")
47+
session.delete(comms_session)
48+
session.commit()
49+
return comms_session

0 commit comments

Comments
 (0)