Add function and HTTP handler to delete services

This commit is contained in:
jordi fita mas 2023-09-26 17:29:49 +02:00
parent 650d3bff7b
commit 067ca7a3ee
8 changed files with 128 additions and 7 deletions

22
deploy/remove_service.sql Normal file
View File

@ -0,0 +1,22 @@
-- Deploy camper:remove_service to pg
-- requires: roles
-- requires: schema_camper
-- requires: service
-- requires: service_i18n
begin;
set search_path to camper, public;
create or replace function remove_service(service_id integer) returns void as
$$
delete from service_i18n where service_id = $1;
delete from service where service_id = $1;
$$
language sql
;
revoke execute on function remove_service(integer) from public;
grant execute on function remove_service(integer) to admin;
commit;

View File

@ -85,8 +85,10 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
f.MustRender(w, r, user, company)
case http.MethodPut:
editService(w, r, user, company, conn, f)
case http.MethodDelete:
deleteService(w, r, user, conn, id)
default:
httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut)
httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut, http.MethodDelete)
}
default:
loc, ok := h.locales.Get(langTag)
@ -205,6 +207,15 @@ func editService(w http.ResponseWriter, r *http.Request, user *auth.User, compan
httplib.Redirect(w, r, "/admin/services", http.StatusSeeOther)
}
func deleteService(w http.ResponseWriter, r *http.Request, user *auth.User, conn *database.Conn, id int) {
if err := user.VerifyCSRFToken(r); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
conn.MustExec(r.Context(), "select remove_service($1)", id)
httplib.Redirect(w, r, "/admin/services", http.StatusSeeOther)
}
type serviceForm struct {
ID int
Icon *form.Select

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: camper\n"
"Report-Msgid-Bugs-To: jordi@tandem.blog\n"
"POT-Creation-Date: 2023-09-26 16:55+0200\n"
"POT-Creation-Date: 2023-09-26 17:28+0200\n"
"PO-Revision-Date: 2023-07-22 23:45+0200\n"
"Last-Translator: jordi fita mas <jordi@tandem.blog>\n"
"Language-Team: Catalan <ca@dodds.net>\n"
@ -762,7 +762,7 @@ msgstr "Automàtic"
#: pkg/app/user.go:249 pkg/campsite/types/l10n.go:82
#: pkg/campsite/types/admin.go:274 pkg/season/admin.go:203
#: pkg/services/l10n.go:73 pkg/services/admin.go:255
#: pkg/services/l10n.go:73 pkg/services/admin.go:266
msgid "Name can not be empty."
msgstr "No podeu deixar el nom en blanc."
@ -816,7 +816,7 @@ msgstr "No podeu deixar el color en blanc."
msgid "This color is not valid. It must be like #123abc."
msgstr "Aquest color no és vàlid. Hauria de ser similar a #123abc."
#: pkg/services/admin.go:254
#: pkg/services/admin.go:265
msgid "Selected icon is not valid."
msgstr "La icona escollida no és vàlida."

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: camper\n"
"Report-Msgid-Bugs-To: jordi@tandem.blog\n"
"POT-Creation-Date: 2023-09-26 16:55+0200\n"
"POT-Creation-Date: 2023-09-26 17:28+0200\n"
"PO-Revision-Date: 2023-07-22 23:46+0200\n"
"Last-Translator: jordi fita mas <jordi@tandem.blog>\n"
"Language-Team: Spanish <es@tp.org.es>\n"
@ -762,7 +762,7 @@ msgstr "Automático"
#: pkg/app/user.go:249 pkg/campsite/types/l10n.go:82
#: pkg/campsite/types/admin.go:274 pkg/season/admin.go:203
#: pkg/services/l10n.go:73 pkg/services/admin.go:255
#: pkg/services/l10n.go:73 pkg/services/admin.go:266
msgid "Name can not be empty."
msgstr "No podéis dejar el nombre en blanco."
@ -816,7 +816,7 @@ msgstr "No podéis dejar el color en blanco."
msgid "This color is not valid. It must be like #123abc."
msgstr "Este color no es válido. Tiene que ser parecido a #123abc."
#: pkg/services/admin.go:254
#: pkg/services/admin.go:265
msgid "Selected icon is not valid."
msgstr "El icono escogido no es válido."

View File

@ -0,0 +1,7 @@
-- Revert camper:remove_service from pg
begin;
drop function if exists camper.remove_service(integer);
commit;

View File

@ -78,4 +78,5 @@ add_service [roles schema_camper service] 2023-09-17T00:00:00Z jordi fita mas <j
edit_service [roles schema_camper service] 2023-09-17T00:01:16Z jordi fita mas <jordi@tandem.blog> # Add function to edit services
service_i18n [roles schema_camper service language] 2023-09-17T00:13:42Z jordi fita mas <jordi@tandem.blog> # Add relation for service translations
translate_service [roles schema_camper service_i18n] 2023-09-17T00:17:00Z jordi fita mas <jordi@tandem.blog> # Add function to translate a service
remove_service [roles schema_camper service service_i18n] 2023-09-26T15:21:00Z jordi fita mas <jordi@tandem.blog> # Add function to remove service
translation [schema_camper] 2023-09-25T16:27:50Z jordi fita mas <jordi@tandem.blog> # Add the type for a translation

73
test/remove_service.sql Normal file
View File

@ -0,0 +1,73 @@
-- Test remove_service
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;
begin;
select plan(12);
set search_path to camper, public;
select has_function('camper', 'remove_service', array['integer']);
select function_lang_is('camper', 'remove_service', array['integer'], 'sql');
select function_returns('camper', 'remove_service', array['integer'], 'void');
select isnt_definer('camper', 'remove_service', array['integer']);
select volatility_is('camper', 'remove_service', array['integer'], 'volatile');
select function_privs_are('camper', 'remove_service', array['integer'], 'guest', array[]::text[]);
select function_privs_are('camper', 'remove_service', array['integer'], 'employee', array[]::text[]);
select function_privs_are('camper', 'remove_service', array['integer'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'remove_service', array['integer'], 'authenticator', array[]::text[]);
set client_min_messages to warning;
truncate service_i18n cascade;
truncate service cascade;
truncate company cascade;
reset client_min_messages;
insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code, currency_code, default_lang_tag)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', 'ca')
;
insert into service (service_id, company_id, icon_name, name, description)
values (5, 1, 'information', 'Service A', '<p>A</p>')
, (6, 1, 'toilet', 'Service B', '<p>B</p>')
, (7, 1, 'wheelchair', 'Service C', '<p>C</p>')
;
insert into service_i18n (service_id, lang_tag, name, description)
values (5, 'ca', 'Servei A', '<p>A</p>')
, (5, 'es', 'Servicio A', '<p>A</p>')
, (6, 'ca', 'Servei B', '<p>B</p>')
, (6, 'es', 'Servicio B', '<p>B</p>')
, (7, 'ca', 'Servei C', '<p>C</p>')
, (7, 'es', 'Servicio C', '<p>C</p>')
;
select lives_ok(
$$ select remove_service(6) $$,
'Should be able to delete a service'
);
select bag_eq(
$$ select service_id, name from service $$,
$$ values (5, 'Service A')
, (7, 'Service C')
$$,
'Should have removed the serivec'
);
select bag_eq(
$$ select service_id, lang_tag, name from service_i18n $$,
$$ values (5, 'ca', 'Servei A')
, (5, 'es', 'Servicio A')
, (7, 'ca', 'Servei C')
, (7, 'es', 'Servicio C')
$$,
'Should have removed the services translations'
);
select *
from finish();
rollback;

View File

@ -0,0 +1,7 @@
-- Verify camper:remove_service on pg
begin;
select has_function_privilege('camper.remove_service(integer)', 'execute');
rollback;