Added creation of messages

This commit is contained in:
Gregory Ballantine 2024-07-03 11:20:06 -04:00
parent 8a7d5b93e3
commit c5d6293737
6 changed files with 66 additions and 1 deletions

View File

@ -2,6 +2,7 @@ source 'https://rubygems.org'
gem 'sinatra', '~> 3.0' gem 'sinatra', '~> 3.0'
gem 'sinatra-contrib', '~> 3.0' gem 'sinatra-contrib', '~> 3.0'
gem 'sinatra-flash', '~> 0.3.0'
gem 'puma', '~> 6.3' gem 'puma', '~> 6.3'
gem 'sequel', '~> 5.70' gem 'sequel', '~> 5.70'

View File

@ -3,6 +3,7 @@ Sequel.migration do
up do up do
create_table(:messages) do create_table(:messages) do
primary_key :id primary_key :id
String :slug, null: false, unique: true
String :body, null: false String :body, null: false
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP

33
src/routes/message.rb Normal file
View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
require 'securerandom'
# /message routes
class Destructo < Sinatra::Base
get '/message' do
redirect '/message/new'
end
get '/message/new' do
erb :'message/new', locals: {
title: 'Create new message'
}
end
post '/message/new' do
# generate a random string for the slug
slug = SecureRandom.urlsafe_base64(12)
# create object in DB
msg = Message.create(
slug: slug,
body: params[:message_body]
)
flash[:success] = "Your message was created successfully! Share this link: https://msg.destructo.com/#{slug}"
redirect '/'
end
end

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><%= title %> | Game Data</title> <title><%= title %> | Destructo</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css">
<link rel="stylesheet" href="/css/hallowvale.css"> <link rel="stylesheet" href="/css/hallowvale.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" charset="utf-8"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" charset="utf-8"></script>
@ -14,6 +14,9 @@
<!-- main navigation --> <!-- main navigation -->
<%= erb :'partials/navbar', :locals => locals %> <%= erb :'partials/navbar', :locals => locals %>
<!-- flash messages -->
<%= erb :'partials/flash' %>
<!-- main content --> <!-- main content -->
<div id="main-content" class="container"> <div id="main-content" class="container">
<%= yield %> <%= yield %>

18
views/message/new.erb Normal file
View File

@ -0,0 +1,18 @@
<div class="row">
<form class="u-full-width" action="/message/new" method="post">
<div class="row">
<div class="twelve columns">
<label>
Message body:
<textarea class="u-full-width" name="message_body"></textarea>
</label>
</div>
<div class="row">
<div class="twelve columns">
<input class="button-primary" type="submit" value="Create message">
</div>
</div>
</div>
</form>
</div>

9
views/partials/flash.erb Normal file
View File

@ -0,0 +1,9 @@
<% if flash[:success] %>
<div class="container">
<div class="row">
<div class="columns twelve">
<p><%= flash[:success] %></p>
</div>
</div>
</div>
<% end %>