blueimpact / kucipong

BSD 3-Clause "New" or "Revised" License
5 stars 2 forks source link

Add JSON APIs for upserting images for stores and coupons #170

Closed cdepillabout closed 7 years ago

cdepillabout commented 7 years ago

This PR adds an Image table to hold images uploaded to Kucipong. It changes the Store and Coupon tables to reference this Image table.

Since this PR changes tables, you'll have to run the following sql commands before you run the API:

drop table "coupon" cascade ;
drop table "store_login_token" cascade ;
drop table "store" cascade ;

Three new APIs have been added.

  1. POST Images. This API is for uploading new image files. It accepts multipart/form-data. This creates a new entry in the Image table.

    Assuming you have an image in the current directory called goat-cafe.jpg, you can upload it with curl using the following command:

    $ curl --verbose --request POST \
        --cookie 'storeEmail=3zydRKER%2Bho4wzyG2hxfYFcKQ4ga8T19FIwqRJYd84LG%2Bo9Tlt8FxplOV%2BuDP5WRPw%3D%3D' \
        -F 'image=@goat-cafe.jpg' \
        'http://localhost:8101/store/image'
    {"data":3}

    The return value is a Envelope with the Image key.

    There is currently no way to delete images that have been uploaded.

  2. Set an Image for a Store. This sets a Store's image column to an Image key that has been uploaded in (1).

    This is a JSON API and can be accessed with curl like the following:

    $ curl --verbose --request POST --header 'Accept: application/json' \
        --cookie 'storeEmail=3zydRKER%2Bho4wzyG2hxfYFcKQ4ga8T19FIwqRJYd84LG%2Bo9Tlt8FxplOV%2BuDP5WRPw%3D%3D' \
        --data '{"imageKey": 3}' \
        'http://localhost:8101/store/set/image'

    If the image doesn't exist or the Image's owner is not the current user, an error will be returned.

  3. Set an Image for a Coupon. This is just like (2), but for a Coupon instead of a Store

    $ curl --verbose --request POST --header 'Accept: application/json' \
        --cookie 'storeEmail=3zydRKER%2Bho4wzyG2hxfYFcKQ4ga8T19FIwqRJYd84LG%2Bo9Tlt8FxplOV%2BuDP5WRPw%3D%3D' \
        --data '{"imageKey": 2}' \
        'http://localhost:8101/store/coupon/1/set/image'

Fixes #161.

cdepillabout commented 7 years ago

Also, the existing Store POST and Coupon POST handlers will now take an imageKey parameter. This is used to set the imageKey for a Store or Coupon.

This includes the following three handlers:

/store/edit POST /store/coupon/\<id\>/edit POST /store/coupon/create POST

arowM commented 7 years ago

LGTM!