From 482d2ff2e108bc5b99365474ace5ee4dab7ea7f9 Mon Sep 17 00:00:00 2001 From: Caio Almeida Date: Thu, 13 Jun 2024 19:29:09 -0300 Subject: [PATCH] integracao calendario --- backend/app/controllers/eventos_controller.rb | 70 +++++++++++++++++++ backend/app/helpers/eventos_helper.rb | 2 + backend/app/models/evento.rb | 3 + backend/app/models/user.rb | 2 +- backend/app/views/eventos/_evento.html.erb | 2 + .../app/views/eventos/_evento.json.jbuilder | 2 + backend/app/views/eventos/_form.html.erb | 17 +++++ backend/app/views/eventos/edit.html.erb | 10 +++ backend/app/views/eventos/index.html.erb | 14 ++++ backend/app/views/eventos/index.json.jbuilder | 1 + backend/app/views/eventos/new.html.erb | 9 +++ backend/app/views/eventos/show.html.erb | 10 +++ backend/app/views/eventos/show.json.jbuilder | 1 + backend/config/routes.rb | 1 + .../migrate/20240613205914_create_eventos.rb | 13 ++++ backend/db/schema.rb | 14 +++- backend/docs/PolluxEndpoints/CreateEvento.bru | 11 +++ .../controllers/eventos_controller_test.rb | 48 +++++++++++++ backend/test/fixtures/eventos.yml | 19 +++++ backend/test/models/evento_test.rb | 7 ++ backend/test/system/eventos_test.rb | 39 +++++++++++ view/src/constants/endpoints.js | 4 ++ view/src/queries/events.js | 52 ++++++++++++++ 23 files changed, 349 insertions(+), 2 deletions(-) create mode 100644 backend/app/controllers/eventos_controller.rb create mode 100644 backend/app/helpers/eventos_helper.rb create mode 100644 backend/app/models/evento.rb create mode 100644 backend/app/views/eventos/_evento.html.erb create mode 100644 backend/app/views/eventos/_evento.json.jbuilder create mode 100644 backend/app/views/eventos/_form.html.erb create mode 100644 backend/app/views/eventos/edit.html.erb create mode 100644 backend/app/views/eventos/index.html.erb create mode 100644 backend/app/views/eventos/index.json.jbuilder create mode 100644 backend/app/views/eventos/new.html.erb create mode 100644 backend/app/views/eventos/show.html.erb create mode 100644 backend/app/views/eventos/show.json.jbuilder create mode 100644 backend/db/migrate/20240613205914_create_eventos.rb create mode 100644 backend/docs/PolluxEndpoints/CreateEvento.bru create mode 100644 backend/test/controllers/eventos_controller_test.rb create mode 100644 backend/test/fixtures/eventos.yml create mode 100644 backend/test/models/evento_test.rb create mode 100644 backend/test/system/eventos_test.rb create mode 100644 view/src/queries/events.js diff --git a/backend/app/controllers/eventos_controller.rb b/backend/app/controllers/eventos_controller.rb new file mode 100644 index 0000000..f8097ae --- /dev/null +++ b/backend/app/controllers/eventos_controller.rb @@ -0,0 +1,70 @@ +class EventosController < ApplicationController + before_action :set_evento, only: %i[ show edit update destroy ] + + # GET /eventos or /eventos.json + def index + @eventos = Evento.all + end + + # GET /eventos/1 or /eventos/1.json + def show + end + + # GET /eventos/new + def new + @evento = Evento.new + end + + # GET /eventos/1/edit + def edit + end + + # POST /eventos or /eventos.json + def create + @evento = Evento.new(evento_params) + + respond_to do |format| + if @evento.save + format.html { redirect_to evento_url(@evento), notice: "Evento was successfully created." } + format.json { render :show, status: :created, location: @evento } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @evento.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /eventos/1 or /eventos/1.json + def update + respond_to do |format| + if @evento.update(evento_params) + format.html { redirect_to evento_url(@evento), notice: "Evento was successfully updated." } + format.json { render :show, status: :ok, location: @evento } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @evento.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /eventos/1 or /eventos/1.json + def destroy + @evento.destroy! + + respond_to do |format| + format.html { redirect_to eventos_url, notice: "Evento was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_evento + @evento = Evento.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def evento_params + params.require(:evento).permit(:nome, :data, :HoraInicio, :HoraTermino, :user_id) + end +end diff --git a/backend/app/helpers/eventos_helper.rb b/backend/app/helpers/eventos_helper.rb new file mode 100644 index 0000000..49fb04b --- /dev/null +++ b/backend/app/helpers/eventos_helper.rb @@ -0,0 +1,2 @@ +module EventosHelper +end diff --git a/backend/app/models/evento.rb b/backend/app/models/evento.rb new file mode 100644 index 0000000..782538c --- /dev/null +++ b/backend/app/models/evento.rb @@ -0,0 +1,3 @@ +class Evento < ApplicationRecord + belongs_to :user +end diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb index 760be2b..15c24ef 100644 --- a/backend/app/models/user.rb +++ b/backend/app/models/user.rb @@ -1,7 +1,7 @@ class User < ApplicationRecord belongs_to :cargo has_many :estoques - has_many :tarefas + has_many :eventos has_many :documentos has_many :reunioes_usuarios has_many :reuniaos, through: :reunioes_usuarios diff --git a/backend/app/views/eventos/_evento.html.erb b/backend/app/views/eventos/_evento.html.erb new file mode 100644 index 0000000..06ca1fc --- /dev/null +++ b/backend/app/views/eventos/_evento.html.erb @@ -0,0 +1,2 @@ +
+
diff --git a/backend/app/views/eventos/_evento.json.jbuilder b/backend/app/views/eventos/_evento.json.jbuilder new file mode 100644 index 0000000..502d58e --- /dev/null +++ b/backend/app/views/eventos/_evento.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! evento, :id, :created_at, :updated_at +json.url evento_url(evento, format: :json) diff --git a/backend/app/views/eventos/_form.html.erb b/backend/app/views/eventos/_form.html.erb new file mode 100644 index 0000000..66bad15 --- /dev/null +++ b/backend/app/views/eventos/_form.html.erb @@ -0,0 +1,17 @@ +<%= form_with(model: evento) do |form| %> + <% if evento.errors.any? %> +
+

<%= pluralize(evento.errors.count, "error") %> prohibited this evento from being saved:

+ + +
+ <% end %> + +
+ <%= form.submit %> +
+<% end %> diff --git a/backend/app/views/eventos/edit.html.erb b/backend/app/views/eventos/edit.html.erb new file mode 100644 index 0000000..5d9ebf5 --- /dev/null +++ b/backend/app/views/eventos/edit.html.erb @@ -0,0 +1,10 @@ +

Editing evento

+ +<%= render "form", evento: @evento %> + +
+ +
+ <%= link_to "Show this evento", @evento %> | + <%= link_to "Back to eventos", eventos_path %> +
diff --git a/backend/app/views/eventos/index.html.erb b/backend/app/views/eventos/index.html.erb new file mode 100644 index 0000000..f47eb03 --- /dev/null +++ b/backend/app/views/eventos/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Eventos

+ +
+ <% @eventos.each do |evento| %> + <%= render evento %> +

+ <%= link_to "Show this evento", evento %> +

+ <% end %> +
+ +<%= link_to "New evento", new_evento_path %> diff --git a/backend/app/views/eventos/index.json.jbuilder b/backend/app/views/eventos/index.json.jbuilder new file mode 100644 index 0000000..9beb53d --- /dev/null +++ b/backend/app/views/eventos/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @eventos, partial: "eventos/evento", as: :evento diff --git a/backend/app/views/eventos/new.html.erb b/backend/app/views/eventos/new.html.erb new file mode 100644 index 0000000..afe563f --- /dev/null +++ b/backend/app/views/eventos/new.html.erb @@ -0,0 +1,9 @@ +

New evento

+ +<%= render "form", evento: @evento %> + +
+ +
+ <%= link_to "Back to eventos", eventos_path %> +
diff --git a/backend/app/views/eventos/show.html.erb b/backend/app/views/eventos/show.html.erb new file mode 100644 index 0000000..a9211d0 --- /dev/null +++ b/backend/app/views/eventos/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @evento %> + +
+ <%= link_to "Edit this evento", edit_evento_path(@evento) %> | + <%= link_to "Back to eventos", eventos_path %> + + <%= button_to "Destroy this evento", @evento, method: :delete %> +
diff --git a/backend/app/views/eventos/show.json.jbuilder b/backend/app/views/eventos/show.json.jbuilder new file mode 100644 index 0000000..ac52af6 --- /dev/null +++ b/backend/app/views/eventos/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "eventos/evento", evento: @evento diff --git a/backend/config/routes.rb b/backend/config/routes.rb index 5076602..3152932 100644 --- a/backend/config/routes.rb +++ b/backend/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :eventos resources :reuniaos resources :storages resources :documentos diff --git a/backend/db/migrate/20240613205914_create_eventos.rb b/backend/db/migrate/20240613205914_create_eventos.rb new file mode 100644 index 0000000..f8963a2 --- /dev/null +++ b/backend/db/migrate/20240613205914_create_eventos.rb @@ -0,0 +1,13 @@ +class CreateEventos < ActiveRecord::Migration[7.1] + def change + create_table :eventos do |t| + t.string :nome + t.string :data + t.string :HoraInicio + t.string :HoraTermino + t.references :user, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index fe996e4..884c3c2 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_06_13_170605) do +ActiveRecord::Schema[7.1].define(version: 2024_06_13_205914) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -40,6 +40,17 @@ t.index ["user_id"], name: "index_documentos_on_user_id" end + create_table "eventos", force: :cascade do |t| + t.string "nome" + t.string "data" + t.string "HoraInicio" + t.string "HoraTermino" + t.bigint "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["user_id"], name: "index_eventos_on_user_id" + end + create_table "reuniaos", force: :cascade do |t| t.string "nome" t.string "link" @@ -92,6 +103,7 @@ add_foreign_key "acaos", "users" add_foreign_key "documentos", "users" + add_foreign_key "eventos", "users" add_foreign_key "reuniaos", "users" add_foreign_key "reunioes_usuarios", "reuniaos" add_foreign_key "reunioes_usuarios", "users" diff --git a/backend/docs/PolluxEndpoints/CreateEvento.bru b/backend/docs/PolluxEndpoints/CreateEvento.bru new file mode 100644 index 0000000..ef6f322 --- /dev/null +++ b/backend/docs/PolluxEndpoints/CreateEvento.bru @@ -0,0 +1,11 @@ +meta { + name: CreateEvento + type: http + seq: 2 +} + +post { + url: http://localhost/3000/eventos + body: none + auth: none +} diff --git a/backend/test/controllers/eventos_controller_test.rb b/backend/test/controllers/eventos_controller_test.rb new file mode 100644 index 0000000..9666200 --- /dev/null +++ b/backend/test/controllers/eventos_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class EventosControllerTest < ActionDispatch::IntegrationTest + setup do + @evento = eventos(:one) + end + + test "should get index" do + get eventos_url + assert_response :success + end + + test "should get new" do + get new_evento_url + assert_response :success + end + + test "should create evento" do + assert_difference("Evento.count") do + post eventos_url, params: { evento: { } } + end + + assert_redirected_to evento_url(Evento.last) + end + + test "should show evento" do + get evento_url(@evento) + assert_response :success + end + + test "should get edit" do + get edit_evento_url(@evento) + assert_response :success + end + + test "should update evento" do + patch evento_url(@evento), params: { evento: { } } + assert_redirected_to evento_url(@evento) + end + + test "should destroy evento" do + assert_difference("Evento.count", -1) do + delete evento_url(@evento) + end + + assert_redirected_to eventos_url + end +end diff --git a/backend/test/fixtures/eventos.yml b/backend/test/fixtures/eventos.yml new file mode 100644 index 0000000..663158c --- /dev/null +++ b/backend/test/fixtures/eventos.yml @@ -0,0 +1,19 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + nome: MyString + string: MyString + data: MyString + string: MyString + HoraInicio: MyString + HoraTermino: MyString + user: one + +two: + nome: MyString + string: MyString + data: MyString + string: MyString + HoraInicio: MyString + HoraTermino: MyString + user: two diff --git a/backend/test/models/evento_test.rb b/backend/test/models/evento_test.rb new file mode 100644 index 0000000..056816b --- /dev/null +++ b/backend/test/models/evento_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class EventoTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/backend/test/system/eventos_test.rb b/backend/test/system/eventos_test.rb new file mode 100644 index 0000000..a2b05cd --- /dev/null +++ b/backend/test/system/eventos_test.rb @@ -0,0 +1,39 @@ +require "application_system_test_case" + +class EventosTest < ApplicationSystemTestCase + setup do + @evento = eventos(:one) + end + + test "visiting the index" do + visit eventos_url + assert_selector "h1", text: "Eventos" + end + + test "should create evento" do + visit eventos_url + click_on "New evento" + + click_on "Create Evento" + + assert_text "Evento was successfully created" + click_on "Back" + end + + test "should update Evento" do + visit evento_url(@evento) + click_on "Edit this evento", match: :first + + click_on "Update Evento" + + assert_text "Evento was successfully updated" + click_on "Back" + end + + test "should destroy Evento" do + visit evento_url(@evento) + click_on "Destroy this evento", match: :first + + assert_text "Evento was successfully destroyed" + end +end diff --git a/view/src/constants/endpoints.js b/view/src/constants/endpoints.js index edeb0b8..2c09d36 100644 --- a/view/src/constants/endpoints.js +++ b/view/src/constants/endpoints.js @@ -12,5 +12,9 @@ export default { document: { base: '/documents', single: (id) => `/documents/${id}`, + }, + event: { + base: '/eventos', + single: (id) => `/eventos/${id}`, } } \ No newline at end of file diff --git a/view/src/queries/events.js b/view/src/queries/events.js new file mode 100644 index 0000000..033ebd3 --- /dev/null +++ b/view/src/queries/events.js @@ -0,0 +1,52 @@ +import server from "../config/axiosInstance"; +import endpoints from "../constants/endpoints"; + +export const getEvents = async () => { + try { + const event = await server.get(endpoints.event.base); + return event; + } catch (error) { + console.log(error); + throw error; + } +}; + +export const getSingleEvent = async (eventId) => { + try { + const event = await server.get(endpoints.event.single(eventId)); + return event; + } catch (error) { + console.log(error); + throw error; + } +}; + +export const createEvent = async (data) => { + try { + const event = await server.post(endpoints.event.base, data); + return event; + } catch (error) { + console.log(error); + throw error; + } + }; + + export const editEvent = async (eventId, data) => { + try { + const event = await server.put(endpoints.event.single(eventId), data); + return event; + } catch (error) { + console.log(error); + throw error; + } + }; + + export const deleteEvent = async (eventId) => { + try { + const event = await server.delete(endpoints.event.single(eventId)); + return event; + } catch (error) { + console.log(error); + throw error; + } + }; \ No newline at end of file