Rezervacije

This commit is contained in:
2024-08-06 14:16:40 +02:00
parent 8d5a410c60
commit 6d5509856f
58 changed files with 1052 additions and 3 deletions

View File

@@ -1,2 +1,9 @@
class ApplicationController < ActionController::Base
private
def set_company
company_id = session.fetch(:company_id, Company.first&.id)
session[:company_id] = company_id
@company = Company.find(session[:company_id])
end
end

View File

@@ -0,0 +1,72 @@
class CustomersController < ApplicationController
before_action :set_company
before_action :set_customer, only: %i[ show edit update destroy ]
# GET /customers or /customers.json
def index
@customers = Customer.all
end
# GET /customers/1 or /customers/1.json
def show
end
# GET /customers/new
def new
@customer = Customer.new
end
# GET /customers/1/edit
def edit
end
# POST /customers or /customers.json
def create
@customer = Customer.new(customer_params)
@customer.company = @company
respond_to do |format|
if @customer.save
format.html { redirect_to customer_url(@customer), notice: "Customer was successfully created." }
format.json { render :show, status: :created, location: @customer }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /customers/1 or /customers/1.json
def update
respond_to do |format|
if @customer.update(customer_params)
format.html { redirect_to customer_url(@customer), notice: "Customer was successfully updated." }
format.json { render :show, status: :ok, location: @customer }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /customers/1 or /customers/1.json
def destroy
@customer.destroy!
respond_to do |format|
format.html { redirect_to customers_url, notice: "Customer was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_customer
@customer = Customer.find(params[:id])
end
# Only allow a list of trusted parameters through.
def customer_params
params.require(:customer).permit(:name, :phone, :notes, :email, :birthyear)
end
end

View File

@@ -0,0 +1,72 @@
class PlacesController < ApplicationController
before_action :set_company
before_action :set_place, only: %i[ show edit update destroy ]
# GET /places or /places.json
def index
@places = Place.all
end
# GET /places/1 or /places/1.json
def show
end
# GET /places/new
def new
@place = Place.new
end
# GET /places/1/edit
def edit
end
# POST /places or /places.json
def create
@place = Place.new(place_params)
@place.company = @company
respond_to do |format|
if @place.save
format.html { redirect_to place_url(@place), notice: "Place was successfully created." }
format.json { render :show, status: :created, location: @place }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @place.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /places/1 or /places/1.json
def update
respond_to do |format|
if @place.update(place_params)
format.html { redirect_to place_url(@place), notice: "Place was successfully updated." }
format.json { render :show, status: :ok, location: @place }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @place.errors, status: :unprocessable_entity }
end
end
end
# DELETE /places/1 or /places/1.json
def destroy
@place.destroy!
respond_to do |format|
format.html { redirect_to places_url, notice: "Place was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_place
@place = Place.find(params[:id])
end
# Only allow a list of trusted parameters through.
def place_params
params.require(:place).permit(:name, :company_id)
end
end

View File

@@ -0,0 +1,73 @@
class ReservationsController < ApplicationController
before_action :set_company
before_action :set_reservation, only: %i[ show edit update destroy ]
# GET /reservations or /reservations.json
def index
@reservations = Reservation.all
end
# GET /reservations/1 or /reservations/1.json
def show
end
# GET /reservations/new
def new
@reservation = Reservation.new
@reservation.place = @company.places.first
end
# GET /reservations/1/edit
def edit
end
# POST /reservations or /reservations.json
def create
@reservation = Reservation.new(reservation_params)
@reservation.company = @company
respond_to do |format|
if @reservation.save
format.html { redirect_to reservation_url(@reservation), notice: "Reservation was successfully created." }
format.json { render :show, status: :created, location: @reservation }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reservations/1 or /reservations/1.json
def update
respond_to do |format|
if @reservation.update(reservation_params)
format.html { redirect_to reservation_url(@reservation), notice: "Reservation was successfully updated." }
format.json { render :show, status: :ok, location: @reservation }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reservations/1 or /reservations/1.json
def destroy
@reservation.destroy!
respond_to do |format|
format.html { redirect_to reservations_url, notice: "Reservation was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reservation
@reservation = Reservation.find(params[:id])
end
# Only allow a list of trusted parameters through.
def reservation_params
params.require(:reservation).permit(:company_id, :customer_id, :place_id, :title, :description, :start_time, :end_time)
end
end

View File

@@ -0,0 +1,2 @@
module CustomersHelper
end

View File

@@ -0,0 +1,2 @@
module PlacesHelper
end

View File

@@ -0,0 +1,2 @@
module ReservationsHelper
end

View File

@@ -1,2 +1,5 @@
class Company < ApplicationRecord
has_many :customers, dependent: :destroy
has_many :reservations, dependent: :destroy
has_many :places, dependent: :destroy
end

7
app/models/customer.rb Normal file
View File

@@ -0,0 +1,7 @@
class Customer < ApplicationRecord
belongs_to :company
validates :name, presence: true, uniqueness: { scope: :company_id }
validates :phone, presence: true
validates :company_id, presence: true
end

3
app/models/place.rb Normal file
View File

@@ -0,0 +1,3 @@
class Place < ApplicationRecord
belongs_to :company
end

View File

@@ -0,0 +1,9 @@
class Reservation < ApplicationRecord
belongs_to :company
belongs_to :customer
belongs_to :place
validates :company_id, presence: true
validates :customer_id, presence: true
validates :place_id, presence: true
end

View File

@@ -0,0 +1,27 @@
<div id="<%= dom_id customer %>">
<p class="my-5">
<strong class="block font-medium mb-1">Name:</strong>
<%= customer.name %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Phone:</strong>
<%= customer.phone %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Notes:</strong>
<%= customer.notes %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Email:</strong>
<%= customer.email %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Birthyear:</strong>
<%= customer.birthyear %>
</p>
</div>

View File

@@ -0,0 +1,2 @@
json.extract! customer, :id, :name, :phone, :notes, :email, :birthyear, :created_at, :updated_at
json.url customer_url(customer, format: :json)

View File

@@ -0,0 +1,42 @@
<%= form_with(model: customer, class: "contents") do |form| %>
<% if customer.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(customer.errors.count, "error") %> prohibited this customer from being saved:</h2>
<ul>
<% customer.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :name %>
<%= form.text_field :name, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :phone %>
<%= form.text_field :phone, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :notes %>
<%= form.text_area :notes, rows: 4, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :email %>
<%= form.text_field :email, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :birthyear %>
<%= form.number_field :birthyear, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>

View File

@@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing customer</h1>
<%= render "form", customer: @customer %>
<%= link_to "Show this customer", @customer, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to customers", customers_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,21 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<% content_for :title, "Customers" %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Customers</h1>
<%= link_to "New customer", new_customer_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="customers" class="min-w-full">
<% @customers.each do |customer| %>
<%= render customer %>
<p>
<%= link_to "Show this customer", customer, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</p>
<% end %>
</div>
</div>

View File

@@ -0,0 +1 @@
json.array! @customers, partial: "customers/customer", as: :customer

View File

@@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New customer</h1>
<%= render "form", customer: @customer %>
<%= link_to "Back to customers", customers_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @customer %>
<%= link_to "Edit this customer", edit_customer_path(@customer), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to customers", customers_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to "Destroy this customer", @customer, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
</div>
</div>

View File

@@ -0,0 +1 @@
json.partial! "customers/customer", customer: @customer

View File

@@ -0,0 +1,22 @@
<%= form_with(model: place, class: "contents") do |form| %>
<% if place.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(place.errors.count, "error") %> prohibited this place from being saved:</h2>
<ul>
<% place.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :name %>
<%= form.text_field :name, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>

View File

@@ -0,0 +1,12 @@
<div id="<%= dom_id place %>">
<p class="my-5">
<strong class="block font-medium mb-1">Name:</strong>
<%= place.name %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Company:</strong>
<%= place.company_id %>
</p>
</div>

View File

@@ -0,0 +1,2 @@
json.extract! place, :id, :name, :company_id, :created_at, :updated_at
json.url place_url(place, format: :json)

View File

@@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing place</h1>
<%= render "form", place: @place %>
<%= link_to "Show this place", @place, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to places", places_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,21 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<% content_for :title, "Places" %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Places</h1>
<%= link_to "New place", new_place_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="places" class="min-w-full">
<% @places.each do |place| %>
<%= render place %>
<p>
<%= link_to "Show this place", place, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</p>
<% end %>
</div>
</div>

View File

@@ -0,0 +1 @@
json.array! @places, partial: "places/place", as: :place

View File

@@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New place</h1>
<%= render "form", place: @place %>
<%= link_to "Back to places", places_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @place %>
<%= link_to "Edit this place", edit_place_path(@place), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to places", places_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to "Destroy this place", @place, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
</div>
</div>

View File

@@ -0,0 +1 @@
json.partial! "places/place", place: @place

View File

@@ -0,0 +1,47 @@
<%= form_with(model: reservation, class: "contents") do |form| %>
<% if reservation.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(reservation.errors.count, "error") %> prohibited this reservation from being saved:</h2>
<ul>
<% reservation.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :customer_id %>
<%= form.collection_select :customer_id, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :place_id %>
<%= form.collection_select :place_id, @company.places, :id, :name, prompt: "Select a Place", class:"block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :title %>
<%= form.text_field :title, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :description %>
<%= form.text_area :description, rows: 4, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :start_time %>`
<%= form.datetime_field :start_time, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :end_time %>
<%= form.datetime_field :end_time, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>

View File

@@ -0,0 +1,37 @@
<div id="<%= dom_id reservation %>">
<p class="my-5">
<strong class="block font-medium mb-1">Company:</strong>
<%= reservation.company_id %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Customer:</strong>
<%= reservation.customer_id %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Place:</strong>
<%= reservation.place_id %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Title:</strong>
<%= reservation.title %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Description:</strong>
<%= reservation.description %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Start time:</strong>
<%= reservation.start_time %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">End time:</strong>
<%= reservation.end_time %>
</p>
</div>

View File

@@ -0,0 +1,2 @@
json.extract! reservation, :id, :company_id, :customer_id, :place_id, :title, :description, :start_time, :end_time, :created_at, :updated_at
json.url reservation_url(reservation, format: :json)

View File

@@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing reservation</h1>
<%= render "form", reservation: @reservation %>
<%= link_to "Show this reservation", @reservation, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to reservations", reservations_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,21 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<% content_for :title, "Reservations" %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Reservations</h1>
<%= link_to "New reservation", new_reservation_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="reservations" class="min-w-full">
<% @reservations.each do |reservation| %>
<%= render reservation %>
<p>
<%= link_to "Show this reservation", reservation, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</p>
<% end %>
</div>
</div>

View File

@@ -0,0 +1 @@
json.array! @reservations, partial: "reservations/reservation", as: :reservation

View File

@@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New reservation</h1>
<%= render "form", reservation: @reservation %>
<%= link_to "Back to reservations", reservations_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @reservation %>
<%= link_to "Edit this reservation", edit_reservation_path(@reservation), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to reservations", reservations_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to "Destroy this reservation", @reservation, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
</div>
</div>

View File

@@ -0,0 +1 @@
json.partial! "reservations/reservation", reservation: @reservation