Verified Commit f2d4b6ff authored by Sli's avatar Sli
Browse files

server: implement Balance endpoint

parent 74f690b3
......@@ -3,10 +3,17 @@
from concurrent import futures
import grpc
from google.protobuf.timestamp_pb2 import Timestamp
from server import db, models, com_pb2, com_pb2_grpc
def pb_now():
timestamp = Timestamp()
timestamp.GetCurrentTime()
return timestamp
class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
"""
Communication protocol to communicate with the cashless client
......@@ -43,10 +50,22 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
raise NotImplementedError("Method not implemented!")
def Balance(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")
"""
Return balance of a customer and create it if it doesn't exist
"""
if not request.customer_id:
return com_pb2.BalanceReply(
status=com_pb2.BalanceReply.MISSING_CUSTOMER, now=pb_now()
)
customer = db.query(models.Customer).get(request.customer_id)
if customer is None:
customer = models.Customer(id=request.customer_id, balance=0)
db.add(customer)
db.commit()
return com_pb2.BalanceReply(
status=com_pb2.BalanceReply.SUCCESS, now=pb_now(), balance=customer.balance
)
def History(self, request, context):
"""Missing associated documentation comment in .proto file"""
......@@ -64,7 +83,7 @@ class PaymentServicer(com_pb2_grpc.PaymentProtocolServicer):
com_pb2.CounterListReply.Counter(id=counter.id, name=counter.name)
)
return com_pb2.CounterListReply(
status=com_pb2.CounterListReply.SUCCESS, counters=resp
status=com_pb2.CounterListReply.SUCCESS, counters=resp, now=pb_now()
)
def Products(self, request, context):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment