Skip to content

Commit 804120c

Browse files
committed
Fixes #14 - Add Activity model
1 parent 8346198 commit 804120c

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from flask import Flask
2+
3+
from time_tracker_api.database import CRUDDao
4+
5+
6+
class ActivityDao(CRUDDao):
7+
pass
8+
9+
10+
def create_dao(app: Flask) -> ActivityDao:
11+
from time_tracker_api.sql_repository import db
12+
from time_tracker_api.sql_repository import SQLCRUDDao, AuditedSQLModel, SQLModel
13+
14+
class ActivitySQLModel(db.Model, SQLModel, AuditedSQLModel):
15+
__tablename__ = 'activity'
16+
id = db.Column(db.Integer, primary_key=True)
17+
name = db.Column(db.String(50), unique=True, nullable=False)
18+
description = db.Column(db.String(250), unique=False, nullable=False)
19+
20+
def __repr__(self):
21+
return '<Activity %r>' % self.name
22+
23+
def __str___(self):
24+
return "the activity \"%s\"" % self.name
25+
26+
class ActivitySQLDao(SQLCRUDDao):
27+
def __init__(self):
28+
SQLCRUDDao.__init__(self, ActivitySQLModel)
29+
30+
return ActivitySQLDao()

time_tracker_api/activities/activities_namespace.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from faker import Faker
22
from flask_restplus import fields, Resource, Namespace
33

4+
from time_tracker_api import flask_app
45
from time_tracker_api.api import audit_fields
6+
from time_tracker_api.activities.activities_model import create_dao
57

68
faker = Faker()
79

@@ -40,39 +42,46 @@
4042
activity_response_fields
4143
)
4244

45+
activity_dao = create_dao(flask_app)
46+
4347

4448
@ns.route('')
4549
class Activities(Resource):
4650
@ns.doc('list_activities')
4751
@ns.marshal_list_with(activity, code=200)
4852
def get(self):
49-
return []
53+
"""List all activities"""
54+
return activity_dao.get_all(), 200
5055

5156
@ns.doc('create_activity')
57+
@ns.response(400, 'Bad request')
5258
@ns.expect(activity_input)
5359
@ns.marshal_with(activity, code=201)
54-
@ns.response(400, 'Invalid format of the attributes of the activity.')
5560
def post(self):
56-
return ns.payload, 201
61+
"""Create an activity"""
62+
return activity_dao.create(ns.payload), 201
5763

5864

5965
@ns.route('/<string:id>')
6066
@ns.response(404, 'Activity not found')
61-
@ns.param('id', 'The unique identifier of the activity')
67+
@ns.param('id', 'The activity identifier')
6268
class Activity(Resource):
6369
@ns.doc('get_activity')
64-
@ns.marshal_with(activity)
70+
@ns.marshal_with(activity, code=200)
6571
def get(self, id):
66-
return {}
67-
68-
@ns.doc('delete_activity')
69-
@ns.response(204, 'The activity was deleted successfully (No content is returned)')
70-
def delete(self, id):
71-
return None, 204
72+
"""Get an activity"""
73+
return activity_dao.get(id)
7274

73-
@ns.doc('put_activity')
74-
@ns.response(400, 'Invalid format of the attributes of the activity.')
75+
@ns.doc('update_activity')
7576
@ns.expect(activity_input)
7677
@ns.marshal_with(activity)
7778
def put(self, id):
78-
return ns.payload
79+
"""Update an activity"""
80+
return activity_dao.update(id, ns.payload)
81+
82+
@ns.doc('delete_activity')
83+
@ns.response(204, 'Activity deleted successfully')
84+
def delete(self, id):
85+
"""Delete an activity"""
86+
activity_dao.delete(id)
87+
return None, 204

0 commit comments

Comments
 (0)