diff --git a/app/controllers/matchmaking_groups_controller.rb b/app/controllers/matchmaking_groups_controller.rb index f813251..f54bb2b 100644 --- a/app/controllers/matchmaking_groups_controller.rb +++ b/app/controllers/matchmaking_groups_controller.rb @@ -10,7 +10,13 @@ def new end def create - MatchmakingGroup.create(group_params.merge(slack_user_id: @current_user.slack_user_id)) + if MatchmakingGroup.exists?(name: group_params[:name]) + flash[:error] = "Group already exists" + else + MatchmakingGroup.create(group_params.merge(slack_user_id: @current_user.slack_user_id)) + flash[:notice] = "Group created" + end + redirect_to matchmaking_groups_path end @@ -21,7 +27,15 @@ def edit def update @group = MatchmakingGroup.find_by(id: params[:id]) - @group.update(group_params) + + if @group.name != group_params[:name] && MatchmakingGroup.exists?(name: group_params[:name]) + flash[:error] = "Other group already exists with that name" + else + @group = MatchmakingGroup.find_by(id: params[:id]) + @group.update(group_params) + + flash[:notice] = "Group updated" + end redirect_to matchmaking_groups_path end diff --git a/app/views/matchmaking_groups/index.html.erb b/app/views/matchmaking_groups/index.html.erb index bd913d6..8c267c2 100644 --- a/app/views/matchmaking_groups/index.html.erb +++ b/app/views/matchmaking_groups/index.html.erb @@ -1,6 +1,10 @@
- <% if notice.present? %> -

<%= notice %>

+ <% if flash[:notice].present? %> +

<%= flash[:notice] %>

+ <% end %> + + <% if flash[:error].present? %> +

<%= flash[:error] %>

<% end %>