Skip to content

Commit

Permalink
Merge pull request #28 from cf15-t5/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
lutfianRhdn authored Aug 22, 2023
2 parents 299e8bc + 1dc5aa5 commit 0f34dbe
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/config/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
'/auth/logout',
'/categories',
'/events/',
'/tickets/'
'/tickets/',
'/users/update-profile',
'/users/topup',
'/users/withdraw',

]

admin_permission=[
Expand All @@ -14,11 +18,13 @@
]

user_permission=[

]

event_organizer_permission=[
'/events/*',
'!/events/verify',


]
permissions = {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/AuthController.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def me():
@AuthApp.route('/verify', methods=['post'])
@isAuthenticated
def verify():
req = request.json
req = request
result = authService.verify(req)
if(result['status'] == 'failed'):
return Response.error(result['data'],result['code'])
Expand Down
18 changes: 16 additions & 2 deletions src/controllers/EventController.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Blueprint
from flask import request,g
from flask_cors import cross_origin
from src.services.EventService import EventService
from src.middlewares.AuthMiddleware import isAuthenticated
import src.utils.getResponse as Response
Expand All @@ -8,6 +9,7 @@
eventService = EventService()

@EventApp.route('/', methods=['GET'])
@cross_origin(supports_credentials=True)
def index():
_filter ={
"district" : request.args.get('district'),
Expand All @@ -21,6 +23,13 @@ def index():
result = eventService.getAllEvent(_filter)
return Response.success(result['data'],"success get all events")

@EventApp.route('/<id>', methods=['GET'])
def detail(id):
result = eventService.getEventById(id)
if(result['status'] == 'failed'):
return Response.error(result['data'],result['code'])

return Response.success(result['data'],"success get event by id")
@EventApp.route('/', methods=['POST'])
@isAuthenticated
def store():
Expand All @@ -39,7 +48,11 @@ def verify():
return Response.error(result['data'],result['code'])
return Response.success(result['data'],"success verify event")


@EventApp.route('/my', methods=['GET'])
@isAuthenticated
def getMyEvent():
result = eventService.getMyEvent(user_id=g.user['user_id'])
return Response.success(result['data'],"success get my event")
@EventApp.route('/<id>', methods=['PUT'])
@isAuthenticated
def update(id):
Expand All @@ -57,4 +70,5 @@ def delete(id):
if(result['status'] == 'failed'):
return Response.error(result['data'],result['code'])

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

32 changes: 29 additions & 3 deletions src/controllers/UserController.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from flask import Blueprint
from flask import request
from flask import Blueprint,request,g
from src.services.UserService import UserService as UserService
from src.middlewares.AuthMiddleware import isAuthenticated
import src.utils.getResponse as Response
Expand All @@ -11,4 +10,31 @@
@isAuthenticated
def index():
result = userService.getAllUser()
return Response.success(result['data'],"success get all user")
return Response.success(result['data'],"success get all user")

@UserApp.route('/update-profile', methods=['post'])
@isAuthenticated
def updateProfile():
req = request.json
result = userService.updateProfile(data=req,id=g.user['user_id'])
if(result['status'] == 'failed'):
return Response.error(result['data'],result['code'])
return Response.success(result['data'],"success update profile user")

@UserApp.route('/topup', methods=['post'])
@isAuthenticated
def topup():
req = request.json
result = userService.topup(data=req,id=g.user['user_id'])
if(result['status'] == 'failed'):
return Response.error(result['data'],result['code'])
return Response.success(result['data'],"success topup user")

@UserApp.route('/withdraw', methods=['post'])
@isAuthenticated
def withdraw():
req = request.json
result = userService.withdraw(data=req,id=g.user['user_id'])
if(result['status'] == 'failed'):
return Response.error(result['data'],result['code'])
return Response.success(result['data'],"success withdraw user")
18 changes: 18 additions & 0 deletions src/repositories/UserRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,23 @@ def verifyUser(self,user_id):
user = User.query.filter_by(user_id=user_id).first()
if(not user) :return False
user.status = 'ACTIVE'
db.session.commit()
return user
def updateProfile(self,id,data):
user = User.query.filter_by(user_id=id).first()
if(not user) :return False
user.name = data['name'] or user.name
user.email = data['email'] or user.email
user.password = bcrypt.hashpw(data['password'].encode('utf-8'), bcrypt.gensalt()) if data['password'] else user.password
db.session.commit()
return user
def updateBalance(self,id,nominal,operator):
user = User.query.filter_by(user_id=id).first()
if(not user) :return False
if(operator == 'plus'):
user.balance += nominal
if(operator == 'minus'):
user.balance -= nominal

db.session.commit()
return user
16 changes: 15 additions & 1 deletion src/services/EventService.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ def getAllEvent(self,filter):
except Exception as e:
print(e)
return EventService.failedOrSuccessRequest('failed', 500, str(e))

def getEventById(self,id):
try:
event = eventRepository.getEventById(id)
if not event:
return EventService.failedOrSuccessRequest('failed', 404, 'Event not found')
return EventService.failedOrSuccessRequest('success', 200, queryResultToDict([event],['user','category'])[0])
except Exception as e:
return EventService.failedOrSuccessRequest('failed', 500, str(e))

def createEvent(self,data,file,user_id):
try:

Expand Down Expand Up @@ -95,3 +103,9 @@ def verifyEvent(self,data):
return self.failedOrSuccessRequest('failed',400,errorHandler(e.errors()))
except Exception as e:
return self.failedOrSuccessRequest('failed',400,str(e))
def getMyEvent(self,user_id):
try:
data = eventRepository.getAllEventByUserId(user_id)
return EventService.failedOrSuccessRequest('success', 200, queryResultToDict(data,['user','category']))
except Exception as e:
return EventService.failedOrSuccessRequest('failed', 500, str(e))
14 changes: 13 additions & 1 deletion src/services/TicketService.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@

from src.repositories.TicketRepository import TicketRepository
from src.repositories.UserRepository import UserRepository
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.errorHandler import errorHandler
from src.utils.sendMail import sendMail

ticketRepository = TicketRepository()
userRepository = UserRepository()
eventRepository = EventRepository()

class TicketService(Service):
@staticmethod
Expand All @@ -29,7 +33,15 @@ def createNewTicket(self,data,user_id):
validate = CreateNewTicketValidator(**data)
if(not validate):
return self.failedOrSuccessRequest('failed', 400, validate.errors())


user = userRepository.getUserById(user_id)
event = eventRepository.getEventById(data['event_id'])
if(not event):
return self.failedOrSuccessRequest('failed', 400, 'event not found')
if user.balance < event.price:
return self.failedOrSuccessRequest('failed', 400, 'balance not enough')
userRepository.updateBalance(id=user_id,nominal=event.price,operator='minus')
userRepository.updateBalance(id=event.user_id,nominal=event.price,operator='plus')
data = ticketRepository.createNewTicket(data,user_id)
sendMail(
name=data.user.name,
Expand Down
37 changes: 36 additions & 1 deletion src/services/UserService.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

from src.repositories.UserRepository import UserRepository
from src.utils.errorHandler import errorHandler
from src.utils.validator.UserValidator import UpdateProfileValidator,UpdateBalanceValidator
from src.utils.convert import queryResultToDict
from src.services.Service import Service
userRepository = UserRepository()
Expand All @@ -20,4 +22,37 @@ def getAllUser(self):
except Exception as e:
return UserService.failedOrSuccessRequest('failed', 500, str(e))


def updateProfile(self,data,id):
try:
validate = UpdateProfileValidator(**data)
if not validate:
return self.failedOrSuccessRequest('failed', 400, 'Validation failed')
user = userRepository.updateProfile(id=id,data=data)
if not user:
return self.failedOrSuccessRequest('failed', 400, 'user not found')
return self.failedOrSuccessRequest('success', 200, queryResultToDict([user])[0])
except ValueError as e:
return self.failedOrSuccessRequest('failed', 500, errorHandler(e.errors()))
def topup(self,id,data):
try:
validate = UpdateBalanceValidator(**data)
if not validate:
return self.failedOrSuccessRequest('failed', 400, 'Validation failed')
user = userRepository.updateBalance(id=id,nominal=data['nominal'],operator='plus')
return self.failedOrSuccessRequest('success', 200, queryResultToDict([user])[0])
except ValueError as e:
return self.failedOrSuccessRequest('failed', 500, errorHandler(e.errors()))

def withdraw(self,id,data):
try:
validate = UpdateBalanceValidator(**data)
if not validate:
return self.failedOrSuccessRequest('failed', 400, 'Validation failed')
user = userRepository.getUserById(user_id=id)
if(user.balance < data['nominal']):
return self.failedOrSuccessRequest('failed', 400, 'balance not enough')

user = userRepository.updateBalance(id=id,nominal=data['nominal'],operator='minus')
return self.failedOrSuccessRequest('success', 200, queryResultToDict([user])[0])
except ValueError as e:
return self.failedOrSuccessRequest('failed', 500, errorHandler(e.errors()))
8 changes: 8 additions & 0 deletions src/utils/validator/UserValidator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pydantic import BaseModel, constr, EmailStr
class UpdateProfileValidator(BaseModel):
name: str
email: EmailStr
password: constr(min_length=8, max_length=16)

class UpdateBalanceValidator(BaseModel):
nominal: int

0 comments on commit 0f34dbe

Please sign in to comment.