From af26c41a7d3df0bc62b03e61bbc6ad93e2b63c02 Mon Sep 17 00:00:00 2001 From: lovelyzhang Date: Tue, 27 Dec 2016 16:49:34 +0800 Subject: [PATCH] zy:modify activity signup and quit --- app/controllers/main_controller.rb | 13 ++- app/controllers/micro_posts_controller.rb | 3 +- app/helpers/main_helper.rb | 92 +++++++++++------ app/views/main/main.html.erb | 33 +++--- db/development.sqlite3 | Bin 40960 -> 40960 bytes 多图片上传.html | 120 ---------------------- 高级软件工程TODOList.md | 2 + 7 files changed, 89 insertions(+), 174 deletions(-) delete mode 100644 多图片上传.html diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb index 3134bc4..55b213a 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -12,7 +12,7 @@ class MainController < ApplicationController page_num = 1 end if !post_type - post_type=[1,2,3] + post_type=[1, 2, 3] end @posts = MicroPost.where(post_type: post_type).paginate(:page => page_num, :per_page => 10).order(post_time: :desc) @@ -22,18 +22,17 @@ class MainController < ApplicationController end def activity + @user = current_user micro_post_id = params[:micropost_id] join = params[:join].to_s - micro_post = MicroPost.find_by(id: micro_post_id) + if join == "true" - micro_post.engage_people += 1 + num, names = new_engaged_people(micro_post_id, current_user.name) else - micro_post.engage_people -= 1 - end - if micro_post.save - render json: {total_num: micro_post.engage_people} + num, names = delete_engaged_people(micro_post_id, current_user.name) end + render json: {total_num: num, total_names: names} end end diff --git a/app/controllers/micro_posts_controller.rb b/app/controllers/micro_posts_controller.rb index b60544a..9edfd0d 100644 --- a/app/controllers/micro_posts_controller.rb +++ b/app/controllers/micro_posts_controller.rb @@ -27,7 +27,8 @@ class MicroPostsController < ApplicationController if post_type >= 1 and post_type <= 3 and !title.empty? and !content.empty? micropost = @user.micro_posts.new(title: title, content: content, pic: pic_path, - post_time: DateTime.now, post_type: post_type, engage_people: 0) + post_time: DateTime.now, post_type: post_type, + engage_people: 1, engaged_people_names: current_user.name) micropost.save end redirect_to microposts_path diff --git a/app/helpers/main_helper.rb b/app/helpers/main_helper.rb index 99f6f6b..f7708db 100644 --- a/app/helpers/main_helper.rb +++ b/app/helpers/main_helper.rb @@ -1,35 +1,5 @@ module MainHelper - def read_newest_post offset_num - micro_posts_array = [] - micro_posts = MicroPost.all.order(post_time: :desc).limit(20).offset(offset_num) - if !micro_posts.empty? - micro_posts.each do |micro_post| - x = Hash.new() - x["username"] = micro_post.user.name - x["userpic"] = micro_post.user.picurl - x["postid"] = micro_post.id - x["title"] = micro_post.title - x["content"] = micro_post.content - case micro_post.post_type - when 1 - x["type"] = "新鲜事" - when 2 - x["type"] = "组团信息" - when 3 - x["type"] = "失物招领" - else - x["type"] = "新鲜事" - end - x["time"] = get_strftime(micro_post.post_time) - x["pics"] = micro_post.pic.split(',') if micro_post.pic - x["peo_num"] = micro_post.engage_people - micro_posts_array << x - end - end - return micro_posts_array - end - def get_post posts micro_posts_array = [] if !posts.empty? @@ -60,7 +30,65 @@ module MainHelper end def get_engaged_people micropost_id - + # 获得参加活动的人数和姓名 + MicroPost.transaction do + micropost = MicroPost.lock.find_by(id: micropost_id) + engage_people = micropost.engage_people + engage_people_name = micropost.engaged_people_names + return engage_people, engage_people_name + end end -end + def save_engaged_people(micropost_id, num, names) + # 将参加活动的人数和姓名存入数据库中 + MicroPost.transaction do + micropost = MicroPost.lock.find_by(id: micropost_id) + micropost.engage_people = num + micropost.engaged_people_names = names + micropost.save + end + end + + def new_engaged_people(micropost_id, name) + # 新参加活动登记 + engage_people, engage_people_names = get_engaged_people micropost_id + if engage_people_names.nil? + engage_people += 1 + engage_people_names = name + else + engage_people_names = engage_people_names.split(',') + if engage_people_names.include?(name) != true + engage_people_names << name + engage_people += 1 + end + engage_people_names = engage_people_names.join(',') + end + save_engaged_people(micropost_id, engage_people, engage_people_names) + return engage_people, engage_people_names + end + + def delete_engaged_people(micropost_id, name) + # 退出活动登记 + engage_people, engage_people_names = get_engaged_people micropost_id + engage_people_names = engage_people_names.split(',') + if engage_people_names.delete(name) + engage_people_names = engage_people_names.join(',') + engage_people -= 1 + save_engaged_people(micropost_id, engage_people, engage_people_names) + end + return engage_people,engage_people_names + end + + def joinded_activity(micropost_id,name) + engage_people, engage_people_names = get_engaged_people micropost_id + engage_people_names = engage_people_names.split(',') + return engage_people_names.include?(name) + end + + + def micro_post_belong_to_user(micropost_id,user) + find_result = user.micro_posts.where(id: micropost_id) + return !find_result.empty? + end + + end diff --git a/app/views/main/main.html.erb b/app/views/main/main.html.erb index 49131fd..c786149 100644 --- a/app/views/main/main.html.erb +++ b/app/views/main/main.html.erb @@ -32,19 +32,24 @@ <% end %> - <% if micro_post["type"] == "组团信息" %> -
- -
-
-
- <% end %> - <% if micro_post["type"] == "失物招领" %> -
- -
+ <% if micro_post_belong_to_user(micro_post["postid"], @user) == false %> + <% if micro_post["type"] == "组团信息" %> +
+ <% if joinded_activity(micro_post["postid"], @user.name) %> + +
+ <% else %> + +
+ <% end %> +
+ <% end %> + <% if micro_post["type"] == "失物招领" %> +
+ +
+ <% end %> <% end %>
@@ -185,7 +190,7 @@ url: '/activity', success: function (data, textStatus, jqXHR) { activity.text("报名参加"); - micropost.find(".activity-detail").text(""); + micropost.find(".activity-detail").text("成功取消报名"); } }); } diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 36f869b42f178d8c43f9a907baced83abb065da3..5e1aafed24d9c152376e22f1bfa97c2096d3077e 100644 GIT binary patch delta 653 zcma)(&ubGw6vrnk29s{mN-rY9R=hUL%)FU5vvW(4)_=g81$UB-AccC9_L6Kt!PZnI zUTP6qTJ+iwLlqB)=U-S{Ilwg+Jt2Htnx`+mN!apW|Poaa}a%#|0{=F+=; z_vLaX+xfoiPEAkTB-#85nVc&=btei-`4zX_xjq%;>+!9N1yYK=a;99)3Wmsao#MKPdhG|2o zFR;#0Q|J7eq@cztfCiKgl4}Xh@%tyq_NV^qM$+3!4xXK~dxbvvOcrjZDDmtCm$2ch zoXat9Bpq}Q2HnFHivucKt%z1}b8%i9E)8&B3JHv#75_cp9U1kGZ=4Rf#0sr%&4+bu z#PIkaWN==qSZCc|7Sptumjaw@JsPxk`+FP7{@X!!d**Zj5<;sa6BR}UhM>d9q6I{j z8n-sYVf5qq@H^6Q>MN!Z*gvVJh(Ak#l63qR1V#it4=`YXbJo#_zO~d`og{?3`IGE# U=wiA{ZaDf3m7ub-y7~C>4|clbzyJUM delta 93 zcmZoTz|?SnX@V3JgYrZfCm^{oVM;zD%jU27JOca-3=Ay1ISl+xd_BC+cyl%@3dHbE w&greVgLXD diff --git a/多图片上传.html b/多图片上传.html deleted file mode 100644 index a9c79ac..0000000 --- a/多图片上传.html +++ /dev/null @@ -1,120 +0,0 @@ - - - - - -
-  - - -
当前选择上传0个图片
-
-
- - - \ No newline at end of file diff --git a/高级软件工程TODOList.md b/高级软件工程TODOList.md index 3aa400b..4089a81 100644 --- a/高级软件工程TODOList.md +++ b/高级软件工程TODOList.md @@ -12,6 +12,8 @@ # 朱京乔 ------------------------ 多图上传 朱京乔 +多图上传bug +传同样的照片 虽然链接上不显示 但是传了2次 # 尹吉宪 ------------------------