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"] == "组团信息" %> + + <% end %> + <% if micro_post["type"] == "失物招领" %> + + <% end %> <% end %>