Skip to content

Commit

Permalink
Merge branch 'feature/fix_buy'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoya-mikami committed Dec 15, 2017
2 parents df66ad6 + 7202ac9 commit d758372
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
26 changes: 19 additions & 7 deletions app/assets/javascripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,26 @@ function confirm_clothes_purchase(item, coin, price, bookid, userid) {
}

function buy_clothes(bookid, userid){
var data = '{"buy_id":"' + bookid + '","user_id":"' + userid + '"}';

var data = {
"buy_id" : bookid,
"user_id" : userid,
}
console.log(data);
$.post(
"/buy_clothes",
data,
function(){location.href="/closets"},
"json"
)
$.ajax({
type: "post",
url: "/buy_clothes",
data: JSON.stringify(data),
contentType: "application/json",
dataType: "json",
success: function(response) {
location.reload();
console.log(response);
},
error: function() {
console.log("error");
}
});
}

function escape_html(string) {
Expand Down
38 changes: 25 additions & 13 deletions app/controllers/closets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ClosetsController < ApplicationController
before_action :logged_in_user, only: [:edit, :update]
protect_from_forgery :except => [:update]
protect_from_forgery :except => [:update, :buy]
#before_action :correct_user, only: [:edit, :update]

def edit
Expand Down Expand Up @@ -105,19 +105,29 @@ def update

def buy
#知らないuser, clotheがきたらrescue
@result ={
'user_id' => 0,
'clothe_id' => 0,
'result' => 0,
}
begin

begin
#受け取った、購入したい情報をパース
json_request = JSON.parse(request.body.read)
buy_id = json_request["buy_id"]
user_id = json_request["user_id"]
input_params = params
#json_request = JSON.parse(request.body.read)
buy_id = input_params["buy_id"]
user_id = input_params["user_id"]

user = User.find_by(id: user_id)
cloth = Clothe.find_by(id: buy_id)

@result['user_id'] = user_id
@result['clothe_id'] = buy_id

puts "successfully received"
rescue
puts "failed to receive json data"
flash[:danger] = "服の読み込みかユーザーの読み込みに失敗しました user_id : #{user_id} buy_id: #{buy_id}"
raise
end

Expand All @@ -134,19 +144,22 @@ def buy
end
rescue
puts "null pointer Exception."
flash[:danger] = "ユーザーか服が見つかりませんでした"
raise
end

if error
puts "purchase failed."
@result = {'result' => 0}
@result['result'] = 0
else
begin
if !UserHasClothe.find_by(clothes_id: cloth.id)
if ! UserHasClothe::user_has_clothe?(user.id, cloth.id)
user.update_attribute(:coin, user.coin - cloth.price) #支払い
UserHasClothe.create(user_id: user.id, clothes_id: cloth.id) #ユーザの持ってる服追加
flash[:danger] = "服が購入できました"
puts "服の新規購入"
else
flash[:danger] = "もう服を持っています"
puts "既に持ってるので着せ替えだけしました"
end

Expand All @@ -165,21 +178,20 @@ def buy
change_cloth_data.update_attribute(:clothe_id, buy_id) #ユーザの服情報更新
end

@result = {'result' => 1}
@result['result'] = 1
puts('successfully updated user data')
rescue
flash[:danger] = "服が購入できませんでした"
raise
end
end
#viewにjsonを送信
respond_to do |format|
format.html{redirect_to action: :edit}
format.json{render :json => @result}
end

rescue
puts "something failed. check error message"
end

#viewにjsonを送信
render :json => @result
end


Expand Down

0 comments on commit d758372

Please sign in to comment.