From cf73b4b9c869e5e5b0936c57227af043bc9884cb Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 00:44:21 +0800 Subject: [PATCH 01/10] Update admin list view --- backend/app/views/campaigns/index.json.jbuilder | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/app/views/campaigns/index.json.jbuilder b/backend/app/views/campaigns/index.json.jbuilder index c0d95115..db953950 100644 --- a/backend/app/views/campaigns/index.json.jbuilder +++ b/backend/app/views/campaigns/index.json.jbuilder @@ -1,3 +1,13 @@ # frozen_string_literal: true -json.array! @campaigns, partial: 'campaigns/campaign', as: :campaign +json.array! @campaigns do |campaign| + json.id campaign.id + json.name campaign.name + json.promisedAmount campaign.promised_amount + json.start campaign.start + json.end campaign.end + json.primaryDonor do + json.id campaign.primary_donor.id + json.name campaign.primary_donor.name + end +end \ No newline at end of file From bb25731f45dcd5d7184e42c995b80ac4e2263832 Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 03:04:07 +0800 Subject: [PATCH 02/10] Add admin show controller and view --- .../app/controllers/campaigns_controller.rb | 4 +++- backend/app/models/campaign.rb | 17 +++++++++++++ backend/app/models/campaign_charity.rb | 14 +++++++++++ .../campaign_charities/_base.json.jbuilder | 7 ++++++ .../_campaign_charity.json.jbuilder | 6 +++++ .../_donation.json.jbuilder | 4 ++++ .../app/views/campaigns/_base.json.jbuilder | 22 +++++++++++++++++ .../views/campaigns/admin_show.json.jbuilder | 16 +++++++++++++ .../app/views/campaigns/index.json.jbuilder | 2 +- .../app/views/charities/_list.json.jbuilder | 4 ++++ .../views/charities/_minimal.json.jbuilder | 4 ++++ backend/app/views/coupons/_base.json.jbuilder | 6 +++++ backend/app/views/coupons/_list.json.jbuilder | 16 +++++++++++++ .../_breakdown.json.jbuilder | 11 +++++++++ .../_secondary_donation.json.jbuilder | 24 ++++++++++--------- backend/config/routes.rb | 6 ++++- 16 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 backend/app/views/campaign_charities/_base.json.jbuilder create mode 100644 backend/app/views/campaign_charities/_campaign_charity.json.jbuilder create mode 100644 backend/app/views/campaign_charities/_donation.json.jbuilder create mode 100644 backend/app/views/campaigns/_base.json.jbuilder create mode 100644 backend/app/views/campaigns/admin_show.json.jbuilder create mode 100644 backend/app/views/charities/_list.json.jbuilder create mode 100644 backend/app/views/charities/_minimal.json.jbuilder create mode 100644 backend/app/views/coupons/_base.json.jbuilder create mode 100644 backend/app/views/coupons/_list.json.jbuilder create mode 100644 backend/app/views/secondary_donations/_breakdown.json.jbuilder diff --git a/backend/app/controllers/campaigns_controller.rb b/backend/app/controllers/campaigns_controller.rb index 513fef35..90017b2a 100644 --- a/backend/app/controllers/campaigns_controller.rb +++ b/backend/app/controllers/campaigns_controller.rb @@ -2,7 +2,7 @@ class CampaignsController < ApplicationController before_action :authenticate_admin! - before_action :set_campaign, only: %i[show update destroy] + before_action :set_campaign, only: %i[show admin_show update destroy] wrap_parameters format: :json, include: %w[name description promisedAmount start end primaryDonorId @@ -14,6 +14,8 @@ def index def show; end + def admin_show; end + def create @campaign = Campaign.create!(campaign_params) diff --git a/backend/app/models/campaign.rb b/backend/app/models/campaign.rb index b4ebcf9c..40a95a52 100644 --- a/backend/app/models/campaign.rb +++ b/backend/app/models/campaign.rb @@ -26,12 +26,29 @@ class Campaign < ApplicationRecord message: 'is mot of a supported file type. Please upload a PNG, JPG or JPEG file.' }, size: { less_than: 1.megabytes, message: 'must be less than 1MB.' } + def donation_breakdown + primary_donor_amount = num_redeemed_coupons * coupon_denomination + secondary_donors_amount = secondary_donations.sum(&:amount) + total_amount = (primary_donor_amount + secondary_donors_amount).to_f + primary_donor_fraction = primary_donor_amount / total_amount + secondary_donors_fraction = secondary_donors_amount / total_amount + + { primary_donor_amount: primary_donor_amount, + primary_donor_fraction: primary_donor_fraction, + secondary_donors_amount: secondary_donors_amount, + secondary_donors_fraction: secondary_donors_fraction } + end + private def num_coupons promised_amount / coupon_denomination end + def num_redeemed_coupons + coupons.count(&:redeemed?) + end + def generate_coupons num_coupons.times do coupons.new(denomination: coupon_denomination, url_token: Coupon.generate_unique_url_token) diff --git a/backend/app/models/campaign_charity.rb b/backend/app/models/campaign_charity.rb index d13b34b7..b89cf60c 100644 --- a/backend/app/models/campaign_charity.rb +++ b/backend/app/models/campaign_charity.rb @@ -4,6 +4,20 @@ class CampaignCharity < ApplicationRecord belongs_to :campaign belongs_to :charity has_many :secondary_donations, dependent: :destroy + has_many :coupons, through: :secondary_donations validates :giving_sg_url, presence: true, allow_blank: false, format: { with: URI::DEFAULT_PARSER.make_regexp } + + def donation_breakdown + primary_donor_amount = coupons.count * campaign.coupon_denomination + secondary_donors_amount = secondary_donations.sum(&:amount) + total_amount = (primary_donor_amount + secondary_donors_amount).to_f + primary_donor_fraction = primary_donor_amount / total_amount + secondary_donors_fraction = secondary_donors_amount / total_amount + + { primary_donor_amount: primary_donor_amount, + primary_donor_fraction: primary_donor_fraction, + secondary_donors_amount: secondary_donors_amount, + secondary_donors_fraction: secondary_donors_fraction } + end end diff --git a/backend/app/views/campaign_charities/_base.json.jbuilder b/backend/app/views/campaign_charities/_base.json.jbuilder new file mode 100644 index 00000000..e230c56f --- /dev/null +++ b/backend/app/views/campaign_charities/_base.json.jbuilder @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +json.id campaign_charity.id +json.charity do + json.id campaign_charity.charity.id +end +json.givingSgUrl campaign_charity.giving_sg_url diff --git a/backend/app/views/campaign_charities/_campaign_charity.json.jbuilder b/backend/app/views/campaign_charities/_campaign_charity.json.jbuilder new file mode 100644 index 00000000..25c33940 --- /dev/null +++ b/backend/app/views/campaign_charities/_campaign_charity.json.jbuilder @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +json.partial! 'campaign_charities/base', campaign_charity: campaign_charity +json.charity do + json.partial! 'charities/list', charity: campaign_charity.charity +end diff --git a/backend/app/views/campaign_charities/_donation.json.jbuilder b/backend/app/views/campaign_charities/_donation.json.jbuilder new file mode 100644 index 00000000..b4de79b1 --- /dev/null +++ b/backend/app/views/campaign_charities/_donation.json.jbuilder @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +json.partial! 'campaign_charities/campaign_charity', campaign_charity: campaign_charity +json.partial! 'secondary_donations/breakdown', donation_breakdown: campaign_charity.donation_breakdown diff --git a/backend/app/views/campaigns/_base.json.jbuilder b/backend/app/views/campaigns/_base.json.jbuilder new file mode 100644 index 00000000..76c5d0a0 --- /dev/null +++ b/backend/app/views/campaigns/_base.json.jbuilder @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# json.id campaign_charity.id +# json.charity do +# json.id campaign_charity.charity.id +# end +# json.givingSgUrl campaign_charity.giving_sg_url + +json.id campaign.id +json.name campaign.name +json.description campaign.description +json.promisedAmount campaign.promised_amount +json.start campaign.start +json.end campaign.end +json.imageBase64 encoded_file_data_url(campaign.image) +json.charities campaign.campaign_charities do |campaign_charity| + json.partial! 'campaign_charities/base', campaign_charity: campaign_charity +end +json.primaryDonor do + json.partial! 'primary_donors/primary_donor', primary_donor: campaign.primary_donor +end +json.interestId campaign.interest_id diff --git a/backend/app/views/campaigns/admin_show.json.jbuilder b/backend/app/views/campaigns/admin_show.json.jbuilder new file mode 100644 index 00000000..efa46279 --- /dev/null +++ b/backend/app/views/campaigns/admin_show.json.jbuilder @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +json.partial! 'campaigns/base', campaign: @campaign + +json.donations do + json.partial! 'secondary_donations/breakdown', donation_breakdown: @campaign.donation_breakdown +end + +json.charities [] # ignore the charities from the base partial, since Jbuilder merges instead of overwriting +json.charities do + json.array! @campaign.campaign_charities, partial: 'campaign_charities/donation', as: :campaign_charity +end + +json.coupons do + json.array! @campaign.coupons, partial: 'coupons/list', as: :coupon +end diff --git a/backend/app/views/campaigns/index.json.jbuilder b/backend/app/views/campaigns/index.json.jbuilder index db953950..2531ab95 100644 --- a/backend/app/views/campaigns/index.json.jbuilder +++ b/backend/app/views/campaigns/index.json.jbuilder @@ -10,4 +10,4 @@ json.array! @campaigns do |campaign| json.id campaign.primary_donor.id json.name campaign.primary_donor.name end -end \ No newline at end of file +end diff --git a/backend/app/views/charities/_list.json.jbuilder b/backend/app/views/charities/_list.json.jbuilder new file mode 100644 index 00000000..9269bd9a --- /dev/null +++ b/backend/app/views/charities/_list.json.jbuilder @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +json.partial! 'charities/minimal', charity: charity +json.logoBase64 encoded_file_data_url(charity.logo) diff --git a/backend/app/views/charities/_minimal.json.jbuilder b/backend/app/views/charities/_minimal.json.jbuilder new file mode 100644 index 00000000..5c9687a8 --- /dev/null +++ b/backend/app/views/charities/_minimal.json.jbuilder @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +json.id charity.id +json.name charity.name diff --git a/backend/app/views/coupons/_base.json.jbuilder b/backend/app/views/coupons/_base.json.jbuilder new file mode 100644 index 00000000..8b4ff998 --- /dev/null +++ b/backend/app/views/coupons/_base.json.jbuilder @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +json.id coupon.id +json.urlToken coupon.url_token +json.denomination coupon.denomination +json.campaignId coupon.campaign_id diff --git a/backend/app/views/coupons/_list.json.jbuilder b/backend/app/views/coupons/_list.json.jbuilder new file mode 100644 index 00000000..50afea5f --- /dev/null +++ b/backend/app/views/coupons/_list.json.jbuilder @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +json.partial! 'coupons/base', coupon: coupon + +if coupon.secondary_donation.present? + json.secondaryDonation do + json.partial! 'secondary_donations/secondary_donation', secondary_donation: coupon.secondary_donation + end + + json.charity do + json.partial! 'charities/minimal', charity: coupon.secondary_donation.campaign_charity.charity + end +else + json.secondaryDonation nil + json.charity nil +end diff --git a/backend/app/views/secondary_donations/_breakdown.json.jbuilder b/backend/app/views/secondary_donations/_breakdown.json.jbuilder new file mode 100644 index 00000000..da8f013f --- /dev/null +++ b/backend/app/views/secondary_donations/_breakdown.json.jbuilder @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +json.primaryDonor do + json.amount donation_breakdown[:primary_donor_amount] + json.fraction donation_breakdown[:primary_donor_fraction] +end + +json.secondaryDonors do + json.amount donation_breakdown[:secondary_donors_amount] + json.fraction donation_breakdown[:secondary_donors_fraction] +end diff --git a/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder b/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder index 68cc962c..fa9e1ccc 100644 --- a/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder +++ b/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder @@ -1,16 +1,18 @@ # frozen_string_literal: true json.id secondary_donation.id +json.couponId secondary_donation.coupon_id json.amount secondary_donation.amount +json.campaignsCharityId secondary_donation.campaign_charity_id -if secondary_donation.coupon - json.coupon do - json.partial! 'coupons/coupon', coupon: secondary_donation.coupon - end -else - json.coupon nil -end - -json.charity do - json.partial! 'charities/charity', charity: secondary_donation.campaign_charity.charity -end +# if secondary_donation.coupon +# json.coupon do +# json.partial! 'coupons/coupon', coupon: secondary_donation.coupon +# end +# else +# json.coupon nil +# end +# +# json.charity do +# json.partial! 'charities/charity', charity: secondary_donation.campaign_charity.charity +# end diff --git a/backend/config/routes.rb b/backend/config/routes.rb index 002e1ec5..fd0989b1 100644 --- a/backend/config/routes.rb +++ b/backend/config/routes.rb @@ -9,7 +9,11 @@ sessions: 'auth/sessions' } - resources :campaigns + resources :campaigns do + member do + get 'admin_show' + end + end resources :charities From 2e65c0830a5fb936f650d8fc9d926e688cfddc8f Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 03:08:02 +0800 Subject: [PATCH 03/10] Remove commented out code --- backend/app/views/campaigns/_base.json.jbuilder | 9 +++------ .../_secondary_donation.json.jbuilder | 11 ----------- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/backend/app/views/campaigns/_base.json.jbuilder b/backend/app/views/campaigns/_base.json.jbuilder index 76c5d0a0..c8ce7ad1 100644 --- a/backend/app/views/campaigns/_base.json.jbuilder +++ b/backend/app/views/campaigns/_base.json.jbuilder @@ -1,11 +1,5 @@ # frozen_string_literal: true -# json.id campaign_charity.id -# json.charity do -# json.id campaign_charity.charity.id -# end -# json.givingSgUrl campaign_charity.giving_sg_url - json.id campaign.id json.name campaign.name json.description campaign.description @@ -13,10 +7,13 @@ json.promisedAmount campaign.promised_amount json.start campaign.start json.end campaign.end json.imageBase64 encoded_file_data_url(campaign.image) + json.charities campaign.campaign_charities do |campaign_charity| json.partial! 'campaign_charities/base', campaign_charity: campaign_charity end + json.primaryDonor do json.partial! 'primary_donors/primary_donor', primary_donor: campaign.primary_donor end + json.interestId campaign.interest_id diff --git a/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder b/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder index fa9e1ccc..7864e152 100644 --- a/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder +++ b/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder @@ -5,14 +5,3 @@ json.couponId secondary_donation.coupon_id json.amount secondary_donation.amount json.campaignsCharityId secondary_donation.campaign_charity_id -# if secondary_donation.coupon -# json.coupon do -# json.partial! 'coupons/coupon', coupon: secondary_donation.coupon -# end -# else -# json.coupon nil -# end -# -# json.charity do -# json.partial! 'charities/charity', charity: secondary_donation.campaign_charity.charity -# end From 95520092de4db4a4d7c5db45df80813c44b3797b Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 03:27:21 +0800 Subject: [PATCH 04/10] Update show view --- .../campaign_charities/_donation_public.json.jbuilder | 6 ++++++ backend/app/views/campaigns/show.json.jbuilder | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 backend/app/views/campaign_charities/_donation_public.json.jbuilder diff --git a/backend/app/views/campaign_charities/_donation_public.json.jbuilder b/backend/app/views/campaign_charities/_donation_public.json.jbuilder new file mode 100644 index 00000000..46061b4d --- /dev/null +++ b/backend/app/views/campaign_charities/_donation_public.json.jbuilder @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +json.partial! 'campaign_charities/campaign_charity', campaign_charity: campaign_charity +json.partial! 'secondary_donations/breakdown', donation_breakdown: campaign_charity.donation_breakdown + +json.attributes!.delete('givingSgUrl') diff --git a/backend/app/views/campaigns/show.json.jbuilder b/backend/app/views/campaigns/show.json.jbuilder index 56092f72..2a35de5f 100644 --- a/backend/app/views/campaigns/show.json.jbuilder +++ b/backend/app/views/campaigns/show.json.jbuilder @@ -1,3 +1,12 @@ # frozen_string_literal: true -json.partial! 'campaigns/campaign', campaign: @campaign +json.partial! 'campaigns/base', campaign: @campaign + +json.donations do + json.partial! 'secondary_donations/breakdown', donation_breakdown: @campaign.donation_breakdown +end + +json.charities [] # ignore the charities from the base partial, since Jbuilder merges instead of overwriting +json.charities do + json.array! @campaign.campaign_charities, partial: 'campaign_charities/donation_public', as: :campaign_charity +end From e35dca45a301dc46118dc0a0d6b17985e8812e5c Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 03:40:37 +0800 Subject: [PATCH 05/10] Add admin list --- .../app/controllers/campaigns_controller.rb | 4 ++++ backend/app/models/campaign.rb | 8 ++++---- .../views/campaigns/admin_index.json.jbuilder | 18 ++++++++++++++++++ .../_secondary_donation.json.jbuilder | 1 - backend/config/routes.rb | 4 ++++ 5 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 backend/app/views/campaigns/admin_index.json.jbuilder diff --git a/backend/app/controllers/campaigns_controller.rb b/backend/app/controllers/campaigns_controller.rb index 90017b2a..7d996b12 100644 --- a/backend/app/controllers/campaigns_controller.rb +++ b/backend/app/controllers/campaigns_controller.rb @@ -12,6 +12,10 @@ def index @campaigns = Campaign.all end + def admin_index + @campaigns = Campaign.all + end + def show; end def admin_show; end diff --git a/backend/app/models/campaign.rb b/backend/app/models/campaign.rb index 40a95a52..5a679eab 100644 --- a/backend/app/models/campaign.rb +++ b/backend/app/models/campaign.rb @@ -39,16 +39,16 @@ def donation_breakdown secondary_donors_fraction: secondary_donors_fraction } end + def num_redeemed_coupons + coupons.count(&:redeemed?) + end + private def num_coupons promised_amount / coupon_denomination end - def num_redeemed_coupons - coupons.count(&:redeemed?) - end - def generate_coupons num_coupons.times do coupons.new(denomination: coupon_denomination, url_token: Coupon.generate_unique_url_token) diff --git a/backend/app/views/campaigns/admin_index.json.jbuilder b/backend/app/views/campaigns/admin_index.json.jbuilder new file mode 100644 index 00000000..265e55ff --- /dev/null +++ b/backend/app/views/campaigns/admin_index.json.jbuilder @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +json.array! @campaigns do |campaign| + json.id campaign.id + json.name campaign.name + json.description campaign.description + json.imageBase64 encoded_file_data_url(campaign.image) + + json.charities do + json.array! campaign.charities, partial: 'charities/list', as: :charity + end + + json.donations do + json.partial! 'secondary_donations/breakdown', donation_breakdown: campaign.donation_breakdown + end + + json.couponsRedeemedCount campaign.num_redeemed_coupons +end diff --git a/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder b/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder index 7864e152..288427b6 100644 --- a/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder +++ b/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder @@ -4,4 +4,3 @@ json.id secondary_donation.id json.couponId secondary_donation.coupon_id json.amount secondary_donation.amount json.campaignsCharityId secondary_donation.campaign_charity_id - diff --git a/backend/config/routes.rb b/backend/config/routes.rb index fd0989b1..570dd885 100644 --- a/backend/config/routes.rb +++ b/backend/config/routes.rb @@ -10,6 +10,10 @@ } resources :campaigns do + collection do + get :admin_index + end + member do get 'admin_show' end From 9865513e0f44935c61987790c030d12888c734af Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 03:54:48 +0800 Subject: [PATCH 06/10] Fix swapped views --- .../views/campaigns/admin_index.json.jbuilder | 17 ++++++----------- backend/app/views/campaigns/index.json.jbuilder | 17 +++++++++++------ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/backend/app/views/campaigns/admin_index.json.jbuilder b/backend/app/views/campaigns/admin_index.json.jbuilder index 265e55ff..2531ab95 100644 --- a/backend/app/views/campaigns/admin_index.json.jbuilder +++ b/backend/app/views/campaigns/admin_index.json.jbuilder @@ -3,16 +3,11 @@ json.array! @campaigns do |campaign| json.id campaign.id json.name campaign.name - json.description campaign.description - json.imageBase64 encoded_file_data_url(campaign.image) - - json.charities do - json.array! campaign.charities, partial: 'charities/list', as: :charity - end - - json.donations do - json.partial! 'secondary_donations/breakdown', donation_breakdown: campaign.donation_breakdown + json.promisedAmount campaign.promised_amount + json.start campaign.start + json.end campaign.end + json.primaryDonor do + json.id campaign.primary_donor.id + json.name campaign.primary_donor.name end - - json.couponsRedeemedCount campaign.num_redeemed_coupons end diff --git a/backend/app/views/campaigns/index.json.jbuilder b/backend/app/views/campaigns/index.json.jbuilder index 2531ab95..265e55ff 100644 --- a/backend/app/views/campaigns/index.json.jbuilder +++ b/backend/app/views/campaigns/index.json.jbuilder @@ -3,11 +3,16 @@ json.array! @campaigns do |campaign| json.id campaign.id json.name campaign.name - json.promisedAmount campaign.promised_amount - json.start campaign.start - json.end campaign.end - json.primaryDonor do - json.id campaign.primary_donor.id - json.name campaign.primary_donor.name + json.description campaign.description + json.imageBase64 encoded_file_data_url(campaign.image) + + json.charities do + json.array! campaign.charities, partial: 'charities/list', as: :charity + end + + json.donations do + json.partial! 'secondary_donations/breakdown', donation_breakdown: campaign.donation_breakdown end + + json.couponsRedeemedCount campaign.num_redeemed_coupons end From d4dc76212c876aebc9a0634e4af02db9d3c9bb15 Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 03:54:54 +0800 Subject: [PATCH 07/10] Add response views --- backend/app/controllers/campaigns_controller.rb | 6 +++--- backend/app/models/coupon.rb | 2 +- backend/app/views/campaigns/response.json.jbuilder | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 backend/app/views/campaigns/response.json.jbuilder diff --git a/backend/app/controllers/campaigns_controller.rb b/backend/app/controllers/campaigns_controller.rb index 7d996b12..bb871b84 100644 --- a/backend/app/controllers/campaigns_controller.rb +++ b/backend/app/controllers/campaigns_controller.rb @@ -24,21 +24,21 @@ def create @campaign = Campaign.create!(campaign_params) add_success_message "Campaign \"#{@campaign.name}\" successfully created!" - render :show, status: :created, location: @campaign + render :response, status: :created, location: @campaign end def update @campaign.update!(campaign_params) add_success_message "Campaign \"#{@campaign.name}\" successfully updated!" - render :show, status: :ok, location: @campaign + render :response, status: :ok, location: @campaign end def destroy @campaign.destroy! add_success_message "Campaign \"#{@campaign.name}\" successfully deleted!" - render :show, status: :ok, location: @campaign + render :response, status: :ok, location: @campaign end private diff --git a/backend/app/models/coupon.rb b/backend/app/models/coupon.rb index 82b0fafe..b724f5cd 100644 --- a/backend/app/models/coupon.rb +++ b/backend/app/models/coupon.rb @@ -4,7 +4,7 @@ class Coupon < ApplicationRecord NUM_ALPHANUMERIC_CHARS_IN_TOKEN = 6 belongs_to :campaign - has_one :secondary_donation, required: false, dependent: nil + has_one :secondary_donation, required: false, dependent: :nullify validates :url_token, presence: true, uniqueness: true validates :denomination, presence: true, numericality: { only_integer: true, greater_than: 0 } diff --git a/backend/app/views/campaigns/response.json.jbuilder b/backend/app/views/campaigns/response.json.jbuilder new file mode 100644 index 00000000..f409ba0f --- /dev/null +++ b/backend/app/views/campaigns/response.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.partial! 'campaigns/base', campaign: @campaign From 07f1f98bec36e00fc3feed6352ff6a88f07f730d Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 15:18:55 +0800 Subject: [PATCH 08/10] Expose index and show controller methods to public --- backend/app/controllers/campaigns_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/controllers/campaigns_controller.rb b/backend/app/controllers/campaigns_controller.rb index bb871b84..9e837d4d 100644 --- a/backend/app/controllers/campaigns_controller.rb +++ b/backend/app/controllers/campaigns_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class CampaignsController < ApplicationController - before_action :authenticate_admin! + before_action :authenticate_admin!, except: %i[index show] before_action :set_campaign, only: %i[show admin_show update destroy] wrap_parameters format: :json, From c20e8dceb03101f4a108a758aa33c715d0c0050d Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 15:24:11 +0800 Subject: [PATCH 09/10] Add maximum 5 charities per campaign validation --- backend/app/models/campaign.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/models/campaign.rb b/backend/app/models/campaign.rb index 5a679eab..411cd415 100644 --- a/backend/app/models/campaign.rb +++ b/backend/app/models/campaign.rb @@ -18,7 +18,7 @@ class Campaign < ApplicationRecord validates :description, presence: true, allow_blank: false validates :start, presence: true validates :end, comparison: { greater_than: :start } - validates :campaign_charities, presence: true + validates :campaign_charities, length: { minimum: 1, maximum: 5 } validates :promised_amount, final: true validates :coupon_denomination, final: true validates :image, From 9938b86d533f7459870e0b720d76099dccb5cdc2 Mon Sep 17 00:00:00 2001 From: Hong Yao Date: Fri, 14 Oct 2022 15:26:59 +0800 Subject: [PATCH 10/10] Update JSON key --- .../views/secondary_donations/_secondary_donation.json.jbuilder | 2 +- frontend/types/donations.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder b/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder index 288427b6..d9070f15 100644 --- a/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder +++ b/backend/app/views/secondary_donations/_secondary_donation.json.jbuilder @@ -3,4 +3,4 @@ json.id secondary_donation.id json.couponId secondary_donation.coupon_id json.amount secondary_donation.amount -json.campaignsCharityId secondary_donation.campaign_charity_id +json.campaignCharityId secondary_donation.campaign_charity_id diff --git a/frontend/types/donations.ts b/frontend/types/donations.ts index 544d05de..91dabbc8 100644 --- a/frontend/types/donations.ts +++ b/frontend/types/donations.ts @@ -14,7 +14,7 @@ export type SecondaryDonationData = { id: number; couponId: Nullable; amount: number; - campaignsCharityId: number; + campaignCharityId: number; }; export type SecondaryDonationPostData = WithoutId;