Beispiel #1
0
	def get(self, account_id):
		if not session.get('user_id', ):
			return '', 403
		with db.connection as con:
			service = UsersService(con)
			is_seller = service.account_is_seller(account_id)
			account = service.get_user(account_id, is_seller)
			if not account:
				return '', 404
		return jsonify(account), 200
Beispiel #2
0
    def post(self, user_id=None):
        if not session.get('user_id', None):
            return '', 403
        if user_id is not None and user_id != session['user_id']:
            return '', 403
        account_id = session['user_id']

        with db.connection as con:
            service = UsersService(con)
            service_ads = AdsService(con)
            is_seller = service.account_is_seller(account_id)
            if not is_seller:
                return '', 403
            request_json = request.json
            response = dict()
            title = request_json['title']
            tags = request_json['tags']
            car = request_json['car']
            colors_id = car['colors']
            images = car['images']
            car.setdefault('num_owners', 1)

            response.update({
                'title': title,
                'tags': tags,
                'seller_id': account_id,
                'is_seller': is_seller
            })
            car_id = service_ads.create_car(car)
            service_ads.add_images(images, car_id)

            date = datetime.datetime.now().strftime('%Y-%m-%d')
            response.update({'date': date})

            service_ads.add_tag(tags)
            tags_id = service_ads.get_tags_id(tags=tags)

            colors = []
            service_ads.set_car_color(colors_id, car_id)
            service_color = ColorsService(con)
            for color_id in colors_id:
                color = service_color.get_color(id=color_id)
                colors += color
            car['colors'] = colors

            response.update({'car': car})

            ad_id = service_ads.create_ad(title=title,
                                          date=date,
                                          account_id=account_id,
                                          car_id=car_id)
            response.update({'id': ad_id})
            service_ads.set_ad_tag(ad_id, tags_id)
            return jsonify(response), 201
Beispiel #3
0
	def post(self):
		account_id = session.get('user_id')
		if not account_id:
			return '', 401

		request_json = request.json
		name = request_json['name']
		hex = request_json['hex']

		if not name or not hex:
			return '', 400
		with db.connection as con:
			user_service = UsersService(con)
			is_seller = user_service.account_is_seller(account_id)
			if not is_seller:
				return '', 403
			service = ColorsService(con)
			try:
				service.add_color(name, hex)
			except:
				color = service.get_color(name)
				return jsonify(color), 208
			color = service.get_color(name)
			return jsonify(color), 200
Beispiel #4
0
	def patch(self, account_id):
		def update_data(data_dict, key, req_json):
			if req_json.get(key):
				data_dict.update({key: req_json[key]})
			return data_dict

		if not session.get('user_id'):
			return '', 403

		if account_id != session['user_id']:
			return '', 401

		request_json = request.json
		with db.connection as con:
			service = UsersService(con)

			account_update = dict()
			update_data(account_update, 'first_name', request_json)
			update_data(account_update, 'last_name', request_json)
			if len(account_update):
				service.update_account(account_id, account_update)

			is_seller_update = request_json.get('is_seller')
			is_seller = service.account_is_seller(account_id)
			if is_seller_update is not None:
				if is_seller_update is True:
					seller_update = dict()
					zipcode_update = dict()
					update_data(seller_update, 'phone', request_json)
					update_data(seller_update, "zip_code", request_json)
					update_data(seller_update, "street", request_json)
					update_data(seller_update, "home", request_json)
					update_data(zipcode_update, "zip_code", request_json)
					update_data(zipcode_update, "city_id", request_json)

					CitiesService(con).create_zip_code(
						zip_code=zipcode_update['zip_code'],
						city_id=zipcode_update['city_id']
					)
					if is_seller is False:
						service.create_seller(
							account_id=account_id,
							zip_code=zipcode_update['zip_code'],
							home=seller_update['home'],
							phone=seller_update['phone'],
							street=seller_update['street']
						)
						user = service.get_user(account_id, is_seller_update)
						return jsonify(user), 200
					else:
						print(seller_update)
						service.update_seller(account_id, seller_update)
						return jsonify(UsersService(con).get_user(account_id, is_seller_update)), 200
				else:
					if is_seller is True:
						with db.connection as con:
							cur = con.execute(
								f'SELECT * '
								f'FROM ad '
								f'WHERE seller_id = {account_id} '
							)
							ads = [dict(row) for row in cur.fetchall()]
							for ad in ads:
								car_id = ad['car_id']
								con.execute(
									f'DELETE FROM carcolor '
									f'WHERE car_id = {car_id}'
								)
								con.execute(
									f'DELETE FROM car '
									f'WHERE id = {car_id}'
								)
								con.execute(
									f'DELETE FROM image '
									f'WHERE car_id = {car_id}'
								)
								con.execute(
									f'DELETE FROM ad '
									f'WHERE seller_id = {account_id} '
								)
							print(account_id)
							con.execute(
								f'DELETE FROM seller '
								f'WHERE account_id = {account_id}'
							)
							return jsonify(UsersService(con).get_user(account_id, is_seller_update)), 200
			is_seller_update = is_seller
			return jsonify(UsersService(con).get_user(account_id, is_seller_update)), 200