diff --git a/Gemfile b/Gemfile index d5275ba..08b8ba4 100644 --- a/Gemfile +++ b/Gemfile @@ -9,3 +9,5 @@ gem "sqlite3" gem "rake" gem "rackup", "~> 2.1" + +gem "sinatra-contrib", "~> 4.0" diff --git a/Gemfile.lock b/Gemfile.lock index 16d94f6..fe614cd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -26,6 +26,7 @@ GEM i18n (1.14.1) concurrent-ruby (~> 1.0) minitest (5.22.2) + multi_json (1.15.0) mustermann (3.0.0) ruby2_keywords (~> 0.0.1) mutex_m (0.2.0) @@ -52,6 +53,12 @@ GEM sinatra-activerecord (2.0.27) activerecord (>= 4.1) sinatra (>= 1.0) + sinatra-contrib (4.0.0) + multi_json (>= 0.0.2) + mustermann (~> 3.0) + rack-protection (= 4.0.0) + sinatra (= 4.0.0) + tilt (~> 2.0) sqlite3 (1.7.2-aarch64-linux) sqlite3 (1.7.2-arm-linux) sqlite3 (1.7.2-arm64-darwin) @@ -78,6 +85,7 @@ DEPENDENCIES rake sinatra sinatra-activerecord + sinatra-contrib (~> 4.0) sqlite3 BUNDLED WITH diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..e4864b9 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +# Rakefile +require "sinatra/activerecord/rake" + +namespace :db do + task :load_config do + require "./mood_app" + end +end diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..352ebae --- /dev/null +++ b/config.ru @@ -0,0 +1,2 @@ +require './mood_app' +run MoodApp diff --git a/db/migrate/20240211182231_create_moods.rb b/db/migrate/20240211182231_create_moods.rb new file mode 100644 index 0000000..bcd196d --- /dev/null +++ b/db/migrate/20240211182231_create_moods.rb @@ -0,0 +1,8 @@ +class CreateMoods < ActiveRecord::Migration[7.1] + def change + create_table :moods do |t| + t.string :mode, null: false + t.datetime :recorded_at, precision: nil, null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..c3aa6ed --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,19 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.1].define(version: 2024_02_11_182231) do + create_table "moods", force: :cascade do |t| + t.string "mode", null: false + t.datetime "recorded_at", precision: nil, null: false + end + +end diff --git a/mood_app.rb b/mood_app.rb new file mode 100644 index 0000000..d4ee812 --- /dev/null +++ b/mood_app.rb @@ -0,0 +1,34 @@ +require 'sinatra/base' +require 'sinatra/namespace' +require 'sinatra/activerecord' + +class MoodApp < Sinatra::Base + register Sinatra::ActiveRecordExtension + register Sinatra::Namespace + set :database, { adapter: 'sqlite3', database: 'mood.sqlite3' } + + get '/' do + @last_mood = Mood.order(recorded_at: :desc).first + erb :index + end + + namespace '/api/v1' do + before do + content_type 'application/json' + end + + post '/moods' do + json = request.body.read + puts json + data = JSON.parse json + Mood.create(mode: data['mode'], recorded_at: Time.now) + end + end + + run! if app_file == $0 +end + +class Mood < ActiveRecord::Base + validates_presence_of :mode + validates_presence_of :recorded_at +end diff --git a/public/creatif.png b/public/creatif.png new file mode 100644 index 0000000..37c656f Binary files /dev/null and b/public/creatif.png differ diff --git a/public/en-charge.png b/public/en-charge.png new file mode 100644 index 0000000..52c6a4a Binary files /dev/null and b/public/en-charge.png differ diff --git a/public/frigo-vide.png b/public/frigo-vide.png new file mode 100644 index 0000000..4e265b2 Binary files /dev/null and b/public/frigo-vide.png differ diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000..dcac01e --- /dev/null +++ b/views/index.erb @@ -0,0 +1,14 @@ + +
+ +