Created
September 23, 2019 17:59
-
-
Save robsonsilv4/caab1c72d2463d628e22955f2720f372 to your computer and use it in GitHub Desktop.
Flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask_restful import Resource, reqparse | |
hoteis = [ | |
{ | |
'hotel_id': 1, | |
'nome': 'Hotel 1', | |
'estrelas': 4.3, | |
'diaria': 420.34, | |
'cidade': 'Fortaleza' | |
}, | |
{ | |
'hotel_id': 2, | |
'nome': 'Hotel 2', | |
'estrelas': 4.4, | |
'diaria': 380.90, | |
'cidade': 'Pacoti' | |
}, | |
{ | |
'hotel_id': 3, | |
'nome': 'Hotel 3', | |
'estrelas': 4.3, | |
'diaria': 320.20, | |
'cidade': 'Quixadá' | |
}, | |
] | |
class HotelModel: | |
def __init__(self, hotel_id, nome, estrelas, diaria, cidade): | |
self.hotel_id = hotel_id | |
self.nome = nome, | |
self.estrelas = estrelas | |
self.diaria = diaria, | |
self.cidade = cidade | |
def json(self): | |
return { | |
'hotel_id': self.hotel_id, | |
'nome': self.nome, | |
'estrelas': self.estrelas, | |
'diaria': self.diaria, | |
'cidade': self.cidade | |
} | |
class Hoteis(Resource): | |
def get(self): | |
return {'hoteis': hoteis} | |
class Hotel(Resource): | |
argumentos = reqparse.RequestParser() | |
argumentos.add_argument('nome') | |
argumentos.add_argument('estrelas') | |
argumentos.add_argument('diaria') | |
argumentos.add_argument('cidade') | |
def encontrar_hotel(self, hotel_id): | |
for hotel in hoteis: | |
if hotel['hotel_id'] == hotel_id: | |
return hotel | |
return None | |
def get(self, hotel_id): | |
hotel = self.encontrar_hotel(hotel_id) | |
if hotel: | |
return hotel | |
return {'message': 'Hotel não encontrado.'}, 404 | |
def post(self, hotel_id): | |
dados = self.argumentos.parse_args() | |
hotel_objeto = HotelModel(hotel_id, **dados) | |
print(hotel_objeto.json()) | |
novo_hotel = hotel_objeto.json() | |
hoteis.append(novo_hotel) | |
return novo_hotel, 201 | |
def put(self, hotel_id): | |
dados = self.argumentos.parse_args() | |
hotel_objeto = Hotel(hotel_id, **dados) | |
novo_hotel = hotel_objeto.json() | |
hotel = self.encontrar_hotel(hotel_id) | |
if hotel: | |
hotel.update(novo_hotel) | |
return novo_hotel, 200 | |
hoteis.append(novo_hotel) | |
return novo_hotel, 201 | |
def delete(self, hotel_id): | |
global hoteis | |
hoteis = [hotel for hotel in hoteis if hotel['hotel_id'] != hotel_id] | |
return hoteis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment