Skip to content

Commit

Permalink
Merge pull request #32 from cf15-t5/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
lutfianRhdn authored Aug 23, 2023
2 parents 1ff8ef4 + 3c21b40 commit 999bc5e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/config/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
event_organizer_permission=[
'/events/*',
'!/events/verify',

'/tickets/attend',

]
permissions = {
Expand Down
10 changes: 10 additions & 0 deletions src/controllers/TicketController.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ def delete(id):
return Response.error(result['data'],result['code'])

return Response.success(result['data'],"success delete event")

@TicketApp.route('/attend', methods=['PUT'])
@isAuthenticated
def verify():

result = ticketService.attendTicket(request.json,g.user['user_id'])
if(result['status'] == 'failed'):
return Response.error(result['data'],result['code'])

return Response.success(result['data'],"success verify ticket")
1 change: 0 additions & 1 deletion src/models/Event.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Event(db.Model):
category_id = db.Column(db.Integer, db.ForeignKey('categories.category_id'))
category = db.relationship('Category', backref='events')
user = db.relationship('User', backref='events')

def __init__(self, title, description, price, date_of_event, number_of_ticket,user_id,poster_path,address,category_id ):
self.title= title
self.description = description
Expand Down
11 changes: 9 additions & 2 deletions src/repositories/TicketRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ def createNewTicket(self,data,user_id):
)
db.session.add(newTicket)
db.session.commit()
return newTicket\

return newTicket
def attendTicket(self,code):
ticket = Ticket.query.filter_by(ticket_code=code).first()
if(not ticket) :return False
ticket.is_attended = True
db.session.commit()
return ticket
def getTicketByCode(self,code):
return Ticket.query.filter_by(ticket_code=code).first()
8 changes: 7 additions & 1 deletion src/services/EventService.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ def getAllEvent(self,filter):
data = eventRepository.getAllEventFiltered(filter)
else:
data = eventRepository.getAllEvent()
return EventService.failedOrSuccessRequest('success', 200, queryResultToDict(data,['user','category']))
# add ticket count to each event
data_dict = queryResultToDict(data,['user','category','tickets'])
for event in data_dict:
event['ticket_count'] = len(event['tickets'])
event['attendances_count'] = len([ticket for ticket in event['tickets'] if ticket['is_attended']])
event.pop('tickets')
return EventService.failedOrSuccessRequest('success', 200,data_dict )
except Exception as e:
print(e)
return EventService.failedOrSuccessRequest('failed', 500, str(e))
Expand Down
30 changes: 29 additions & 1 deletion src/services/TicketService.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from src.repositories.EventRepository import EventRepository
from src.utils.convert import queryResultToDict
from src.services.Service import Service
from src.utils.validator.TicketValidator import CreateNewTicketValidator
from src.utils.validator.TicketValidator import CreateNewTicketValidator,AttendTicketValidator
from src.utils.errorHandler import errorHandler
from src.utils.sendMail import sendMail
from datetime import datetime

ticketRepository = TicketRepository()
userRepository = UserRepository()
Expand Down Expand Up @@ -38,6 +39,14 @@ def createNewTicket(self,data,user_id):
event = eventRepository.getEventById(data['event_id'])
if(not event):
return self.failedOrSuccessRequest('failed', 400, 'event not found')
if (len(event.tickets) >= event.number_of_ticket):
return self.failedOrSuccessRequest('failed', 400, 'ticket sold out')
today = datetime.today()
if (event.date_of_event.date() < today.date()):
return self.failedOrSuccessRequest('failed', 400, 'event has passed')
if([item for item in event.tickets if item.user_id ==user_id]):
return self.failedOrSuccessRequest('failed', 400, 'you already have ticket for this event')

if user.balance < event.price:
return self.failedOrSuccessRequest('failed', 400, 'balance not enough')
userRepository.updateBalance(id=user_id,nominal=event.price,operator='minus')
Expand All @@ -63,5 +72,24 @@ def getTicketByUserId(self,user_id):
try:
data = ticketRepository.getAllTicketByUserId(user_id)
return self.failedOrSuccessRequest('success', 200, queryResultToDict(data,['user','event']))
except Exception as e:
return self.failedOrSuccessRequest('failed', 500, str(e))
def attendTicket(self,data,user_id):
try:
validate= AttendTicketValidator(**data)
if(not validate):
return self.failedOrSuccessRequest('failed', 400, validate.errors())
ticket = ticketRepository.getTicketByCode(data['ticket_code'])
if(not ticket):
return self.failedOrSuccessRequest('failed', 400, 'ticket not found')
if(ticket.user_id != user_id):
return self.failedOrSuccessRequest('failed', 400, 'ticket not belong to you')
if(ticket.is_attended):
return self.failedOrSuccessRequest('failed', 400, 'ticket already attended')
ticket = ticketRepository.attendTicket(data['ticket_code'])
return self.failedOrSuccessRequest('success', 200, queryResultToDict([ticket])[0])

except ValueError as e:
return self.failedOrSuccessRequest('failed', 400, errorHandler(e.errors()))
except Exception as e:
return self.failedOrSuccessRequest('failed', 500, str(e))
6 changes: 4 additions & 2 deletions src/utils/validator/TicketValidator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pydantic import BaseModel
from pydantic import BaseModel, constr

class CreateNewTicketValidator(BaseModel):
event_id: int


class AttendTicketValidator(BaseModel):
ticket_code: constr(min_length=6, max_length=6)

0 comments on commit 999bc5e

Please sign in to comment.