database in service

This commit is contained in:
2021-01-15 12:53:46 +01:00
parent ceadf3338f
commit 24482da680
4 changed files with 97 additions and 21 deletions

19
tools/ws/dbpool.py Normal file
View File

@ -0,0 +1,19 @@
import mariadb
pool = None
def createConnectionPool():
global pool
pool = mariadb.ConnectionPool(
user = 'heroes',
password = 'test123',
host = '172.16.10.18',
database = 'heroes',
pool_name = 'wep-app',
pool_size = 5
)
def getConnection():
global pool
return pool.get_connection()

View File

@ -1,24 +1,24 @@
HEROES = [ from dbpool import getConnection
{ "id": 1, "name": "Wolfgang" }, from heroes_mock import HEROES
{ "id": 2, "name": "Andreas" },
{ "id": 3, "name": "Frank" },
{ "id": 4, "name": "Thomas" },
{ "id": 5, "name": "Barbara" },
{ "id": 6, "name": "Robert" },
{ "id": 7, "name": "Raphael" },
{ "id": 8, "name": "Lucia" },
{ "id": 9, "name": "Gregor" },
]
def get_heroes(): def get_heroes():
if HEROES: # if HEROES:
return HEROES # return HEROES
else: # else:
return 'No heroes available', 404 # return 'No heroes available', 404
try:
dbh = getConnection()
heroes = []
cur = dbh.cursor()
cur.execute("SELECT id, name FROM hero")
for (id, name) in cur:
heroes.append({"id": id, "name": name})
return heroes
except Exception:
return 500, str(Exception)
finally:
dbh.close()
def get_hero(id=None): def get_hero(id=None):
try: try:

47
tools/ws/heroes_mock.py Normal file
View File

@ -0,0 +1,47 @@
HEROES = [
{ "id": 1, "name": "Wolfgang" },
{ "id": 2, "name": "Andreas" },
{ "id": 3, "name": "Frank" },
{ "id": 4, "name": "Thomas" },
{ "id": 5, "name": "Barbara" },
{ "id": 6, "name": "Robert" },
{ "id": 7, "name": "Raphael" },
{ "id": 8, "name": "Lucia" },
{ "id": 9, "name": "Gregor" },
]
def get_heroes():
if HEROES:
return HEROES
else:
return 'No heroes available', 404
def get_hero(id=None):
try:
hero = next(x for x in HEROES if x["id"] == id)
return { "id": hero["id"], "name": hero["name"] }
except StopIteration:
return 'Hero not found', 404
def put_hero(id=None, hero=None):
try:
heroToUpdate = next(x for x in HEROES if x["id"] == id)
checkNewName = next((x for x in HEROES if x["name"] == hero["name"]), None)
if (checkNewName):
return 'Duplicate name', 403
heroToUpdate["name"] = hero["name"]
return 'Hero updated', 200
except StopIteration:
return 'Hero not found', 404
except Exception:
return 'Some error', 403
def post_hero(hero=None):
try:
newHeroId = len(HEROES)
hero["id"] = newHeroId
HEROES.append(hero)
return 'Hero inserted', 201
except Exception:
return 'Some error', 403

View File

@ -37,6 +37,8 @@ paths:
$ref: '#/definitions/Hero' $ref: '#/definitions/Hero'
404: 404:
description: Hero not found description: Hero not found
500:
description: Some server error
put: put:
operationId: heroes.put_hero operationId: heroes.put_hero
summary: Update a hero summary: Update a hero
@ -53,12 +55,14 @@ paths:
responses: responses:
200: 200:
description: Hero updated description: Hero updated
402:
description: Duplicate name
403: 403:
description: Some error description: Some error
409:
description: Duplicate name
404: 404:
description: Hero not found description: Hero not found
500:
description: Some server error
/hero: /hero:
post: post:
operationId: heroes.post_hero operationId: heroes.post_hero
@ -74,6 +78,10 @@ paths:
description: Hero inserted description: Hero inserted
403: 403:
description: Some error description: Some error
409:
description: Duplicate name
500:
description: Some server error
/heroes: /heroes:
get: get:
operationId: heroes.get_heroes operationId: heroes.get_heroes
@ -87,6 +95,8 @@ paths:
$ref: '#/definitions/Hero' $ref: '#/definitions/Hero'
404: 404:
description: No heroes available description: No heroes available
500:
description: Some server error
definitions: definitions: