def post(self, name): if CompanyModel.find_by_name(name): return {'message': "A store with name '{}' already exists.".format(name)}, 400 company = CompanyModel(name) try: company.save_to_db() except: return {"message": "An error occurred creating the store."}, 500 return company.json(), 201
def post(self): data = self.parser.parse_args() companyName = data['companyName'] if CompanyModel.find_by_name(companyName): return { 'message': "An item with name '{}' already exists.".format(companyName) }, 400 company = CompanyModel(companyName, **data) try: company.save_to_db() except: return { "message": "An error occurred while inserting the item." }, 500 return company.json(), 201
def test_crud(self): with self.app_context(): competition = CompanyModel('AVIANCA') self.assertIsNone( CompanyModel.find_by_name('AVIANCA').first(), "Found an competition with name 'AVIANCA' before save_to_db") competition.save_to_db() self.assertIsNotNone( CompanyModel.find_by_name('AVIANCA').first(), "Did not find a competition with name 'AVIANCA' after save_to_db" ) competition.delete_from_db() self.assertIsNone( CompanyModel.find_by_name('AVIANCA').first(), "Found an competition with name 'AVIANCA' after delete_from_db" )
def post(self, us_employer_id): if CompanyModel.find_by_us_employer_id(us_employer_id): return { "message": "A company already exists with the same employer ID, use a different one!" }, 400 data = Company.parser.parse_args() is_valid, error_message = CompanyModel.check_if_data_has_valid_format( us_employer_id, **data) if is_valid: new_company = CompanyModel(us_employer_id, **data) try: format_company_to_json(new_company) new_company.save_to_db() return format_company_to_json(new_company), 201 except: return { "message": "An error occurred while creating the user!" }, 500 else: return {"message": error_message}, 400